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