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