mesa: Add a _mesa_fls() function to find the last bit set in a word.
[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 * \name Work-arounds for platforms that lack C99 math functions
101 */
102 /*@{*/
103 #if (!defined(_XOPEN_SOURCE) || (_XOPEN_SOURCE < 600)) && !defined(_ISOC99_SOURCE) \
104 && (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)) \
105 && (!defined(_MSC_VER) || (_MSC_VER < 1400))
106 #define acosf(f) ((float) acos(f))
107 #define asinf(f) ((float) asin(f))
108 #define atan2f(x,y) ((float) atan2(x,y))
109 #define atanf(f) ((float) atan(f))
110 #define ceilf(f) ((float) ceil(f))
111 #define cosf(f) ((float) cos(f))
112 #define coshf(f) ((float) cosh(f))
113 #define expf(f) ((float) exp(f))
114 #define exp2f(f) ((float) exp2(f))
115 #define floorf(f) ((float) floor(f))
116 #define logf(f) ((float) log(f))
117
118 #ifdef ANDROID
119 #define log2f(f) (logf(f) * (float) (1.0 / M_LN2))
120 #else
121 #define log2f(f) ((float) log2(f))
122 #endif
123
124 #define powf(x,y) ((float) pow(x,y))
125 #define sinf(f) ((float) sin(f))
126 #define sinhf(f) ((float) sinh(f))
127 #define sqrtf(f) ((float) sqrt(f))
128 #define tanf(f) ((float) tan(f))
129 #define tanhf(f) ((float) tanh(f))
130 #define acoshf(f) ((float) acosh(f))
131 #define asinhf(f) ((float) asinh(f))
132 #define atanhf(f) ((float) atanh(f))
133 #endif
134
135 #if defined(_MSC_VER)
136 static inline float truncf(float x) { return x < 0.0f ? ceilf(x) : floorf(x); }
137 static inline float exp2f(float x) { return powf(2.0f, x); }
138 static inline float log2f(float x) { return logf(x) * 1.442695041f; }
139 static inline float asinhf(float x) { return logf(x + sqrtf(x * x + 1.0f)); }
140 static inline float acoshf(float x) { return logf(x + sqrtf(x * x - 1.0f)); }
141 static inline float atanhf(float x) { return (logf(1.0f + x) - logf(1.0f - x)) / 2.0f; }
142 static inline int isblank(int ch) { return ch == ' ' || ch == '\t'; }
143 #define strtoll(p, e, b) _strtoi64(p, e, b)
144 #endif
145 /*@}*/
146
147
148
149 /** single-precision inverse square root */
150 static inline float
151 INV_SQRTF(float x)
152 {
153 /* XXX we could try Quake's fast inverse square root function here */
154 return 1.0F / sqrtf(x);
155 }
156
157
158 /***
159 *** LOG2: Log base 2 of float
160 ***/
161 static inline GLfloat LOG2(GLfloat x)
162 {
163 #ifdef USE_IEEE
164 #if 0
165 /* This is pretty fast, but not accurate enough (only 2 fractional bits).
166 * Based on code from http://www.stereopsis.com/log2.html
167 */
168 const GLfloat y = x * x * x * x;
169 const GLuint ix = *((GLuint *) &y);
170 const GLuint exp = (ix >> 23) & 0xFF;
171 const GLint log2 = ((GLint) exp) - 127;
172 return (GLfloat) log2 * (1.0 / 4.0); /* 4, because of x^4 above */
173 #endif
174 /* Pretty fast, and accurate.
175 * Based on code from http://www.flipcode.com/totd/
176 */
177 fi_type num;
178 GLint log_2;
179 num.f = x;
180 log_2 = ((num.i >> 23) & 255) - 128;
181 num.i &= ~(255 << 23);
182 num.i += 127 << 23;
183 num.f = ((-1.0f/3) * num.f + 2) * num.f - 2.0f/3;
184 return num.f + log_2;
185 #else
186 /*
187 * NOTE: log_base_2(x) = log(x) / log(2)
188 * NOTE: 1.442695 = 1/log(2).
189 */
190 return (GLfloat) (log(x) * 1.442695F);
191 #endif
192 }
193
194
195
196 /***
197 *** IS_INF_OR_NAN: test if float is infinite or NaN
198 ***/
199 #ifdef USE_IEEE
200 static inline int IS_INF_OR_NAN( float x )
201 {
202 fi_type tmp;
203 tmp.f = x;
204 return !(int)((unsigned int)((tmp.i & 0x7fffffff)-0x7f800000) >> 31);
205 }
206 #elif defined(isfinite)
207 #define IS_INF_OR_NAN(x) (!isfinite(x))
208 #elif defined(finite)
209 #define IS_INF_OR_NAN(x) (!finite(x))
210 #elif defined(__VMS)
211 #define IS_INF_OR_NAN(x) (!finite(x))
212 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
213 #define IS_INF_OR_NAN(x) (!isfinite(x))
214 #else
215 #define IS_INF_OR_NAN(x) (!finite(x))
216 #endif
217
218
219 /***
220 *** CEILF: ceiling of float
221 *** FLOORF: floor of float
222 *** FABSF: absolute value of float
223 *** LOGF: the natural logarithm (base e) of the value
224 *** EXPF: raise e to the value
225 *** LDEXPF: multiply value by an integral power of two
226 *** FREXPF: extract mantissa and exponent from value
227 ***/
228 #if defined(__gnu_linux__)
229 /* C99 functions */
230 #define CEILF(x) ceilf(x)
231 #define FLOORF(x) floorf(x)
232 #define FABSF(x) fabsf(x)
233 #define LOGF(x) logf(x)
234 #define EXPF(x) expf(x)
235 #define LDEXPF(x,y) ldexpf(x,y)
236 #define FREXPF(x,y) frexpf(x,y)
237 #else
238 #define CEILF(x) ((GLfloat) ceil(x))
239 #define FLOORF(x) ((GLfloat) floor(x))
240 #define FABSF(x) ((GLfloat) fabs(x))
241 #define LOGF(x) ((GLfloat) log(x))
242 #define EXPF(x) ((GLfloat) exp(x))
243 #define LDEXPF(x,y) ((GLfloat) ldexp(x,y))
244 #define FREXPF(x,y) ((GLfloat) frexp(x,y))
245 #endif
246
247
248 /**
249 * Convert float to int by rounding to nearest integer, away from zero.
250 */
251 static inline int IROUND(float f)
252 {
253 return (int) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F));
254 }
255
256
257 /**
258 * Convert float to int64 by rounding to nearest integer.
259 */
260 static inline GLint64 IROUND64(float f)
261 {
262 return (GLint64) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F));
263 }
264
265
266 /**
267 * Convert positive float to int by rounding to nearest integer.
268 */
269 static inline int IROUND_POS(float f)
270 {
271 assert(f >= 0.0F);
272 return (int) (f + 0.5F);
273 }
274
275
276 /**
277 * Convert float to int using a fast method. The rounding mode may vary.
278 * XXX We could use an x86-64/SSE2 version here.
279 */
280 static inline int F_TO_I(float f)
281 {
282 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
283 int r;
284 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
285 return r;
286 #elif defined(USE_X86_ASM) && defined(_MSC_VER)
287 int r;
288 _asm {
289 fld f
290 fistp r
291 }
292 return r;
293 #else
294 return IROUND(f);
295 #endif
296 }
297
298
299 /** Return (as an integer) floor of float */
300 static inline int IFLOOR(float f)
301 {
302 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
303 /*
304 * IEEE floor for computers that round to nearest or even.
305 * 'f' must be between -4194304 and 4194303.
306 * This floor operation is done by "(iround(f + .5) + iround(f - .5)) >> 1",
307 * but uses some IEEE specific tricks for better speed.
308 * Contributed by Josh Vanderhoof
309 */
310 int ai, bi;
311 double af, bf;
312 af = (3 << 22) + 0.5 + (double)f;
313 bf = (3 << 22) + 0.5 - (double)f;
314 /* GCC generates an extra fstp/fld without this. */
315 __asm__ ("fstps %0" : "=m" (ai) : "t" (af) : "st");
316 __asm__ ("fstps %0" : "=m" (bi) : "t" (bf) : "st");
317 return (ai - bi) >> 1;
318 #elif defined(USE_IEEE)
319 int ai, bi;
320 double af, bf;
321 fi_type u;
322 af = (3 << 22) + 0.5 + (double)f;
323 bf = (3 << 22) + 0.5 - (double)f;
324 u.f = (float) af; ai = u.i;
325 u.f = (float) bf; bi = u.i;
326 return (ai - bi) >> 1;
327 #else
328 int i = IROUND(f);
329 return (i > f) ? i - 1 : i;
330 #endif
331 }
332
333
334 /** Return (as an integer) ceiling of float */
335 static inline int ICEIL(float f)
336 {
337 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
338 /*
339 * IEEE ceil for computers that round to nearest or even.
340 * 'f' must be between -4194304 and 4194303.
341 * This ceil operation is done by "(iround(f + .5) + iround(f - .5) + 1) >> 1",
342 * but uses some IEEE specific tricks for better speed.
343 * Contributed by Josh Vanderhoof
344 */
345 int ai, bi;
346 double af, bf;
347 af = (3 << 22) + 0.5 + (double)f;
348 bf = (3 << 22) + 0.5 - (double)f;
349 /* GCC generates an extra fstp/fld without this. */
350 __asm__ ("fstps %0" : "=m" (ai) : "t" (af) : "st");
351 __asm__ ("fstps %0" : "=m" (bi) : "t" (bf) : "st");
352 return (ai - bi + 1) >> 1;
353 #elif defined(USE_IEEE)
354 int ai, bi;
355 double af, bf;
356 fi_type u;
357 af = (3 << 22) + 0.5 + (double)f;
358 bf = (3 << 22) + 0.5 - (double)f;
359 u.f = (float) af; ai = u.i;
360 u.f = (float) bf; bi = u.i;
361 return (ai - bi + 1) >> 1;
362 #else
363 int i = IROUND(f);
364 return (i < f) ? i + 1 : i;
365 #endif
366 }
367
368
369 /**
370 * Is x a power of two?
371 */
372 static inline int
373 _mesa_is_pow_two(int x)
374 {
375 return !(x & (x - 1));
376 }
377
378 /**
379 * Round given integer to next higer power of two
380 * If X is zero result is undefined.
381 *
382 * Source for the fallback implementation is
383 * Sean Eron Anderson's webpage "Bit Twiddling Hacks"
384 * http://graphics.stanford.edu/~seander/bithacks.html
385 *
386 * When using builtin function have to do some work
387 * for case when passed values 1 to prevent hiting
388 * undefined result from __builtin_clz. Undefined
389 * results would be different depending on optimization
390 * level used for build.
391 */
392 static inline int32_t
393 _mesa_next_pow_two_32(uint32_t x)
394 {
395 #if defined(__GNUC__) && \
396 ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */
397 uint32_t y = (x != 1);
398 return (1 + y) << ((__builtin_clz(x - y) ^ 31) );
399 #else
400 x--;
401 x |= x >> 1;
402 x |= x >> 2;
403 x |= x >> 4;
404 x |= x >> 8;
405 x |= x >> 16;
406 x++;
407 return x;
408 #endif
409 }
410
411 static inline int64_t
412 _mesa_next_pow_two_64(uint64_t x)
413 {
414 #if defined(__GNUC__) && \
415 ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */
416 uint64_t y = (x != 1);
417 if (sizeof(x) == sizeof(long))
418 return (1 + y) << ((__builtin_clzl(x - y) ^ 63));
419 else
420 return (1 + y) << ((__builtin_clzll(x - y) ^ 63));
421 #else
422 x--;
423 x |= x >> 1;
424 x |= x >> 2;
425 x |= x >> 4;
426 x |= x >> 8;
427 x |= x >> 16;
428 x |= x >> 32;
429 x++;
430 return x;
431 #endif
432 }
433
434
435 /*
436 * Returns the floor form of binary logarithm for a 32-bit integer.
437 */
438 static inline GLuint
439 _mesa_logbase2(GLuint n)
440 {
441 #if defined(__GNUC__) && \
442 ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */
443 return (31 - __builtin_clz(n | 1));
444 #else
445 GLuint pos = 0;
446 if (n >= 1<<16) { n >>= 16; pos += 16; }
447 if (n >= 1<< 8) { n >>= 8; pos += 8; }
448 if (n >= 1<< 4) { n >>= 4; pos += 4; }
449 if (n >= 1<< 2) { n >>= 2; pos += 2; }
450 if (n >= 1<< 1) { pos += 1; }
451 return pos;
452 #endif
453 }
454
455
456 /**
457 * Return 1 if this is a little endian machine, 0 if big endian.
458 */
459 static inline GLboolean
460 _mesa_little_endian(void)
461 {
462 const GLuint ui = 1; /* intentionally not static */
463 return *((const GLubyte *) &ui);
464 }
465
466
467
468 /**********************************************************************
469 * Functions
470 */
471
472 extern void *
473 _mesa_align_malloc( size_t bytes, unsigned long alignment );
474
475 extern void *
476 _mesa_align_calloc( size_t bytes, unsigned long alignment );
477
478 extern void
479 _mesa_align_free( void *ptr );
480
481 extern void *
482 _mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize,
483 unsigned long alignment);
484
485 extern void *
486 _mesa_exec_malloc( GLuint size );
487
488 extern void
489 _mesa_exec_free( void *addr );
490
491 extern void *
492 _mesa_realloc( void *oldBuffer, size_t oldSize, size_t newSize );
493
494
495 #ifndef FFS_DEFINED
496 #define FFS_DEFINED 1
497 #ifdef __GNUC__
498
499 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(ANDROID) || defined(__APPLE__)
500 #define ffs __builtin_ffs
501 #define ffsll __builtin_ffsll
502 #endif
503
504 #else
505
506 extern int ffs(int i);
507 extern int ffsll(long long int i);
508
509 #endif /*__ GNUC__ */
510 #endif /* FFS_DEFINED */
511
512
513 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304) /* gcc 3.4 or later */
514 #define _mesa_bitcount(i) __builtin_popcount(i)
515 #define _mesa_bitcount_64(i) __builtin_popcountll(i)
516 #else
517 extern unsigned int
518 _mesa_bitcount(unsigned int n);
519 extern unsigned int
520 _mesa_bitcount_64(uint64_t n);
521 #endif
522
523 /**
524 * Find the last (most significant) bit set in a word.
525 *
526 * Essentially ffs() in the reverse direction.
527 */
528 static inline unsigned int
529 _mesa_fls(unsigned int n)
530 {
531 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 304)
532 return n == 0 ? 0 : 32 - __builtin_clz(n);
533 #else
534 unsigned int v = 1;
535
536 if (n == 0)
537 return 0;
538
539 while (n >>= 1)
540 v++;
541
542 return v;
543 #endif
544 }
545
546 extern GLhalfARB
547 _mesa_float_to_half(float f);
548
549 extern float
550 _mesa_half_to_float(GLhalfARB h);
551
552
553 extern void *
554 _mesa_bsearch( const void *key, const void *base, size_t nmemb, size_t size,
555 int (*compar)(const void *, const void *) );
556
557 extern char *
558 _mesa_getenv( const char *var );
559
560 extern char *
561 _mesa_strdup( const char *s );
562
563 extern float
564 _mesa_strtof( const char *s, char **end );
565
566 extern unsigned int
567 _mesa_str_checksum(const char *str);
568
569 extern int
570 _mesa_snprintf( char *str, size_t size, const char *fmt, ... ) PRINTFLIKE(3, 4);
571
572 extern int
573 _mesa_vsnprintf(char *str, size_t size, const char *fmt, va_list arg);
574
575
576 #if defined(_MSC_VER) && !defined(snprintf)
577 #define snprintf _snprintf
578 #endif
579
580
581 #ifdef __cplusplus
582 }
583 #endif
584
585
586 #endif /* IMPORTS_H */