mesa: clean up F_TO_I, IFLOOR, ICEIL functions
[mesa.git] / src / mesa / main / imports.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.5
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /**
27 * \file imports.h
28 * Standard C library function wrappers.
29 *
30 * This file provides wrappers for all the standard C library functions
31 * like malloc(), free(), printf(), getenv(), etc.
32 */
33
34
35 #ifndef IMPORTS_H
36 #define IMPORTS_H
37
38
39 #include "compiler.h"
40 #include "glheader.h"
41 #include "errors.h"
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47
48 /**********************************************************************/
49 /** Memory macros */
50 /*@{*/
51
52 /** Allocate \p BYTES bytes */
53 #define MALLOC(BYTES) malloc(BYTES)
54 /** Allocate and zero \p BYTES bytes */
55 #define CALLOC(BYTES) calloc(1, BYTES)
56 /** Allocate a structure of type \p T */
57 #define MALLOC_STRUCT(T) (struct T *) malloc(sizeof(struct T))
58 /** Allocate and zero a structure of type \p T */
59 #define CALLOC_STRUCT(T) (struct T *) calloc(1, sizeof(struct T))
60 /** Free memory */
61 #define FREE(PTR) free(PTR)
62
63 /*@}*/
64
65
66 /*
67 * For GL_ARB_vertex_buffer_object we need to treat vertex array pointers
68 * as offsets into buffer stores. Since the vertex array pointer and
69 * buffer store pointer are both pointers and we need to add them, we use
70 * this macro.
71 * Both pointers/offsets are expressed in bytes.
72 */
73 #define ADD_POINTERS(A, B) ( (GLubyte *) (A) + (uintptr_t) (B) )
74
75
76 /**
77 * Sometimes we treat GLfloats as GLints. On x86 systems, moving a float
78 * as a int (thereby using integer registers instead of FP registers) is
79 * a performance win. Typically, this can be done with ordinary casts.
80 * But with gcc's -fstrict-aliasing flag (which defaults to on in gcc 3.0)
81 * these casts generate warnings.
82 * The following union typedef is used to solve that.
83 */
84 typedef union { GLfloat f; GLint i; } fi_type;
85
86
87
88 /**********************************************************************
89 * Math macros
90 */
91
92 #define MAX_GLUSHORT 0xffff
93 #define MAX_GLUINT 0xffffffff
94
95 /* Degrees to radians conversion: */
96 #define DEG2RAD (M_PI/180.0)
97
98
99 /***
100 *** SQRTF: single-precision square root
101 ***/
102 #define SQRTF(X) (float) sqrt((float) (X))
103
104
105 /***
106 *** INV_SQRTF: single-precision inverse square root
107 ***/
108 #define INV_SQRTF(X) (1.0F / SQRTF(X))
109
110
111 /**
112 * \name Work-arounds for platforms that lack C99 math functions
113 */
114 /*@{*/
115 #if (!defined(_XOPEN_SOURCE) || (_XOPEN_SOURCE < 600)) && !defined(_ISOC99_SOURCE) \
116 && (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)) \
117 && (!defined(_MSC_VER) || (_MSC_VER < 1400))
118 #define acosf(f) ((float) acos(f))
119 #define asinf(f) ((float) asin(f))
120 #define atan2f(x,y) ((float) atan2(x,y))
121 #define atanf(f) ((float) atan(f))
122 #define ceilf(f) ((float) ceil(f))
123 #define cosf(f) ((float) cos(f))
124 #define coshf(f) ((float) cosh(f))
125 #define expf(f) ((float) exp(f))
126 #define exp2f(f) ((float) exp2(f))
127 #define floorf(f) ((float) floor(f))
128 #define logf(f) ((float) log(f))
129
130 #ifdef ANDROID
131 #define log2f(f) (logf(f) * (float) (1.0 / M_LN2))
132 #else
133 #define log2f(f) ((float) log2(f))
134 #endif
135
136 #define powf(x,y) ((float) pow(x,y))
137 #define sinf(f) ((float) sin(f))
138 #define sinhf(f) ((float) sinh(f))
139 #define sqrtf(f) ((float) sqrt(f))
140 #define tanf(f) ((float) tan(f))
141 #define tanhf(f) ((float) tanh(f))
142 #define acoshf(f) ((float) acosh(f))
143 #define asinhf(f) ((float) asinh(f))
144 #define atanhf(f) ((float) atanh(f))
145 #endif
146
147 #if defined(_MSC_VER)
148 static inline float truncf(float x) { return x < 0.0f ? ceilf(x) : floorf(x); }
149 static inline float exp2f(float x) { return powf(2.0f, x); }
150 static inline float log2f(float x) { return logf(x) * 1.442695041f; }
151 static inline float asinhf(float x) { return logf(x + sqrtf(x * x + 1.0f)); }
152 static inline float acoshf(float x) { return logf(x + sqrtf(x * x - 1.0f)); }
153 static inline float atanhf(float x) { return (logf(1.0f + x) - logf(1.0f - x)) / 2.0f; }
154 static inline int isblank(int ch) { return ch == ' ' || ch == '\t'; }
155 #define strtoll(p, e, b) _strtoi64(p, e, b)
156 #endif
157 /*@}*/
158
159 #if defined(__SUNPRO_C)
160 #define sqrtf(f) ((float) sqrt(f))
161 #endif
162
163
164 /***
165 *** LOG2: Log base 2 of float
166 ***/
167 #ifdef USE_IEEE
168 #if 0
169 /* This is pretty fast, but not accurate enough (only 2 fractional bits).
170 * Based on code from http://www.stereopsis.com/log2.html
171 */
172 static inline GLfloat LOG2(GLfloat x)
173 {
174 const GLfloat y = x * x * x * x;
175 const GLuint ix = *((GLuint *) &y);
176 const GLuint exp = (ix >> 23) & 0xFF;
177 const GLint log2 = ((GLint) exp) - 127;
178 return (GLfloat) log2 * (1.0 / 4.0); /* 4, because of x^4 above */
179 }
180 #endif
181 /* Pretty fast, and accurate.
182 * Based on code from http://www.flipcode.com/totd/
183 */
184 static inline GLfloat LOG2(GLfloat val)
185 {
186 fi_type num;
187 GLint log_2;
188 num.f = val;
189 log_2 = ((num.i >> 23) & 255) - 128;
190 num.i &= ~(255 << 23);
191 num.i += 127 << 23;
192 num.f = ((-1.0f/3) * num.f + 2) * num.f - 2.0f/3;
193 return num.f + log_2;
194 }
195 #else
196 /*
197 * NOTE: log_base_2(x) = log(x) / log(2)
198 * NOTE: 1.442695 = 1/log(2).
199 */
200 #define LOG2(x) ((GLfloat) (log(x) * 1.442695F))
201 #endif
202
203
204 /***
205 *** IS_INF_OR_NAN: test if float is infinite or NaN
206 ***/
207 #ifdef USE_IEEE
208 static inline int IS_INF_OR_NAN( float x )
209 {
210 fi_type tmp;
211 tmp.f = x;
212 return !(int)((unsigned int)((tmp.i & 0x7fffffff)-0x7f800000) >> 31);
213 }
214 #elif defined(isfinite)
215 #define IS_INF_OR_NAN(x) (!isfinite(x))
216 #elif defined(finite)
217 #define IS_INF_OR_NAN(x) (!finite(x))
218 #elif defined(__VMS)
219 #define IS_INF_OR_NAN(x) (!finite(x))
220 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
221 #define IS_INF_OR_NAN(x) (!isfinite(x))
222 #else
223 #define IS_INF_OR_NAN(x) (!finite(x))
224 #endif
225
226
227 /***
228 *** IS_NEGATIVE: test if float is negative
229 ***/
230 #if defined(USE_IEEE)
231 static inline int GET_FLOAT_BITS( float x )
232 {
233 fi_type fi;
234 fi.f = x;
235 return fi.i;
236 }
237 #define IS_NEGATIVE(x) (GET_FLOAT_BITS(x) < 0)
238 #else
239 #define IS_NEGATIVE(x) (x < 0.0F)
240 #endif
241
242
243 /***
244 *** DIFFERENT_SIGNS: test if two floats have opposite signs
245 ***/
246 #if defined(USE_IEEE)
247 #define DIFFERENT_SIGNS(x,y) ((GET_FLOAT_BITS(x) ^ GET_FLOAT_BITS(y)) & (1<<31))
248 #else
249 /* Could just use (x*y<0) except for the flatshading requirements.
250 * Maybe there's a better way?
251 */
252 #define DIFFERENT_SIGNS(x,y) ((x) * (y) <= 0.0F && (x) - (y) != 0.0F)
253 #endif
254
255
256 /***
257 *** CEILF: ceiling of float
258 *** FLOORF: floor of float
259 *** FABSF: absolute value of float
260 *** LOGF: the natural logarithm (base e) of the value
261 *** EXPF: raise e to the value
262 *** LDEXPF: multiply value by an integral power of two
263 *** FREXPF: extract mantissa and exponent from value
264 ***/
265 #if defined(__gnu_linux__)
266 /* C99 functions */
267 #define CEILF(x) ceilf(x)
268 #define FLOORF(x) floorf(x)
269 #define FABSF(x) fabsf(x)
270 #define LOGF(x) logf(x)
271 #define EXPF(x) expf(x)
272 #define LDEXPF(x,y) ldexpf(x,y)
273 #define FREXPF(x,y) frexpf(x,y)
274 #else
275 #define CEILF(x) ((GLfloat) ceil(x))
276 #define FLOORF(x) ((GLfloat) floor(x))
277 #define FABSF(x) ((GLfloat) fabs(x))
278 #define LOGF(x) ((GLfloat) log(x))
279 #define EXPF(x) ((GLfloat) exp(x))
280 #define LDEXPF(x,y) ((GLfloat) ldexp(x,y))
281 #define FREXPF(x,y) ((GLfloat) frexp(x,y))
282 #endif
283
284
285 /**
286 * Convert float to int by rounding to nearest integer, away from zero.
287 */
288 static inline int IROUND(float f)
289 {
290 return (int) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F));
291 }
292
293
294 /**
295 * Convert float to int64 by rounding to nearest integer.
296 */
297 static inline GLint64 IROUND64(float f)
298 {
299 return (GLint64) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F));
300 }
301
302
303 /**
304 * Convert positive float to int by rounding to nearest integer.
305 */
306 static inline int IROUND_POS(float f)
307 {
308 assert(f >= 0.0F);
309 return (int) (f + 0.5F);
310 }
311
312
313 /**
314 * Convert float to int using a fast method. The rounding mode may vary.
315 * XXX We could use an x86-64/SSE2 version here.
316 */
317 static inline int F_TO_I(float f)
318 {
319 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
320 int r;
321 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
322 return r;
323 #elif defined(USE_X86_ASM) && defined(_MSC_VER)
324 int r;
325 _asm {
326 fld f
327 fistp r
328 }
329 return r;
330 #else
331 return IROUND(f);
332 #endif
333 }
334
335
336 /** Return (as an integer) floor of float */
337 static inline int IFLOOR(float f)
338 {
339 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
340 /*
341 * IEEE floor for computers that round to nearest or even.
342 * 'f' must be between -4194304 and 4194303.
343 * This floor operation is done by "(iround(f + .5) + iround(f - .5)) >> 1",
344 * but uses some IEEE specific tricks for better speed.
345 * Contributed by Josh Vanderhoof
346 */
347 int ai, bi;
348 double af, bf;
349 af = (3 << 22) + 0.5 + (double)f;
350 bf = (3 << 22) + 0.5 - (double)f;
351 /* GCC generates an extra fstp/fld without this. */
352 __asm__ ("fstps %0" : "=m" (ai) : "t" (af) : "st");
353 __asm__ ("fstps %0" : "=m" (bi) : "t" (bf) : "st");
354 return (ai - bi) >> 1;
355 #elif defined(USE_IEEE)
356 int ai, bi;
357 double af, bf;
358 fi_type u;
359 af = (3 << 22) + 0.5 + (double)f;
360 bf = (3 << 22) + 0.5 - (double)f;
361 u.f = (float) af; ai = u.i;
362 u.f = (float) bf; bi = u.i;
363 return (ai - bi) >> 1;
364 #else
365 int i = IROUND(f);
366 return (i > f) ? i - 1 : i;
367 #endif
368 }
369
370
371 /** Return (as an integer) ceiling of float */
372 static inline int ICEIL(float f)
373 {
374 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
375 /*
376 * IEEE ceil for computers that round to nearest or even.
377 * 'f' must be between -4194304 and 4194303.
378 * This ceil operation is done by "(iround(f + .5) + iround(f - .5) + 1) >> 1",
379 * but uses some IEEE specific tricks for better speed.
380 * Contributed by Josh Vanderhoof
381 */
382 int ai, bi;
383 double af, bf;
384 af = (3 << 22) + 0.5 + (double)f;
385 bf = (3 << 22) + 0.5 - (double)f;
386 /* GCC generates an extra fstp/fld without this. */
387 __asm__ ("fstps %0" : "=m" (ai) : "t" (af) : "st");
388 __asm__ ("fstps %0" : "=m" (bi) : "t" (bf) : "st");
389 return (ai - bi + 1) >> 1;
390 #elif defined(USE_IEEE)
391 int ai, bi;
392 double af, bf;
393 fi_type u;
394 af = (3 << 22) + 0.5 + (double)f;
395 bf = (3 << 22) + 0.5 - (double)f;
396 u.f = (float) af; ai = u.i;
397 u.f = (float) bf; bi = u.i;
398 return (ai - bi + 1) >> 1;
399 #else
400 int i = IROUND(f);
401 return (i < f) ? i + 1 : i;
402 #endif
403 }
404
405
406 /**
407 * Is x a power of two?
408 */
409 static inline int
410 _mesa_is_pow_two(int x)
411 {
412 return !(x & (x - 1));
413 }
414
415 /**
416 * Round given integer to next higer power of two
417 * If X is zero result is undefined.
418 *
419 * Source for the fallback implementation is
420 * Sean Eron Anderson's webpage "Bit Twiddling Hacks"
421 * http://graphics.stanford.edu/~seander/bithacks.html
422 *
423 * When using builtin function have to do some work
424 * for case when passed values 1 to prevent hiting
425 * undefined result from __builtin_clz. Undefined
426 * results would be different depending on optimization
427 * level used for build.
428 */
429 static inline int32_t
430 _mesa_next_pow_two_32(uint32_t x)
431 {
432 #if defined(__GNUC__) && \
433 ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */
434 uint32_t y = (x != 1);
435 return (1 + y) << ((__builtin_clz(x - y) ^ 31) );
436 #else
437 x--;
438 x |= x >> 1;
439 x |= x >> 2;
440 x |= x >> 4;
441 x |= x >> 8;
442 x |= x >> 16;
443 x++;
444 return x;
445 #endif
446 }
447
448 static inline int64_t
449 _mesa_next_pow_two_64(uint64_t x)
450 {
451 #if defined(__GNUC__) && \
452 ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */
453 uint64_t y = (x != 1);
454 if (sizeof(x) == sizeof(long))
455 return (1 + y) << ((__builtin_clzl(x - y) ^ 63));
456 else
457 return (1 + y) << ((__builtin_clzll(x - y) ^ 63));
458 #else
459 x--;
460 x |= x >> 1;
461 x |= x >> 2;
462 x |= x >> 4;
463 x |= x >> 8;
464 x |= x >> 16;
465 x |= x >> 32;
466 x++;
467 return x;
468 #endif
469 }
470
471
472 /*
473 * Returns the floor form of binary logarithm for a 32-bit integer.
474 */
475 static inline GLuint
476 _mesa_logbase2(GLuint n)
477 {
478 #if defined(__GNUC__) && \
479 ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */
480 return (31 - __builtin_clz(n | 1));
481 #else
482 GLuint pos = 0;
483 if (n >= 1<<16) { n >>= 16; pos += 16; }
484 if (n >= 1<< 8) { n >>= 8; pos += 8; }
485 if (n >= 1<< 4) { n >>= 4; pos += 4; }
486 if (n >= 1<< 2) { n >>= 2; pos += 2; }
487 if (n >= 1<< 1) { pos += 1; }
488 return pos;
489 #endif
490 }
491
492
493 /**
494 * Return 1 if this is a little endian machine, 0 if big endian.
495 */
496 static inline GLboolean
497 _mesa_little_endian(void)
498 {
499 const GLuint ui = 1; /* intentionally not static */
500 return *((const GLubyte *) &ui);
501 }
502
503
504
505 /**********************************************************************
506 * Functions
507 */
508
509 extern void *
510 _mesa_align_malloc( size_t bytes, unsigned long alignment );
511
512 extern void *
513 _mesa_align_calloc( size_t bytes, unsigned long alignment );
514
515 extern void
516 _mesa_align_free( void *ptr );
517
518 extern void *
519 _mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize,
520 unsigned long alignment);
521
522 extern void *
523 _mesa_exec_malloc( GLuint size );
524
525 extern void
526 _mesa_exec_free( void *addr );
527
528 extern void *
529 _mesa_realloc( void *oldBuffer, size_t oldSize, size_t newSize );
530
531
532 #ifndef FFS_DEFINED
533 #define FFS_DEFINED 1
534 #ifdef __GNUC__
535
536 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(ANDROID) || defined(__APPLE__)
537 #define ffs __builtin_ffs
538 #define ffsll __builtin_ffsll
539 #endif
540
541 #else
542
543 extern int ffs(int i);
544 extern int ffsll(long long int i);
545
546 #endif /*__ GNUC__ */
547 #endif /* FFS_DEFINED */
548
549
550 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */
551 #define _mesa_bitcount(i) __builtin_popcount(i)
552 #define _mesa_bitcount_64(i) __builtin_popcountll(i)
553 #else
554 extern unsigned int
555 _mesa_bitcount(unsigned int n);
556 extern unsigned int
557 _mesa_bitcount_64(uint64_t n);
558 #endif
559
560
561 extern GLhalfARB
562 _mesa_float_to_half(float f);
563
564 extern float
565 _mesa_half_to_float(GLhalfARB h);
566
567
568 extern void *
569 _mesa_bsearch( const void *key, const void *base, size_t nmemb, size_t size,
570 int (*compar)(const void *, const void *) );
571
572 extern char *
573 _mesa_getenv( const char *var );
574
575 extern char *
576 _mesa_strdup( const char *s );
577
578 extern float
579 _mesa_strtof( const char *s, char **end );
580
581 extern unsigned int
582 _mesa_str_checksum(const char *str);
583
584 extern int
585 _mesa_snprintf( char *str, size_t size, const char *fmt, ... ) PRINTFLIKE(3, 4);
586
587 extern int
588 _mesa_vsnprintf(char *str, size_t size, const char *fmt, va_list arg);
589
590
591 #if defined(_MSC_VER) && !defined(snprintf)
592 #define snprintf _snprintf
593 #endif
594
595
596 #ifdef __cplusplus
597 }
598 #endif
599
600
601 #endif /* IMPORTS_H */