2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
28 * Standard C library function wrappers.
30 * This file provides wrappers for all the standard C library functions
31 * like malloc(), free(), printf(), getenv(), etc.
44 #include "util/bitscan.h"
51 /**********************************************************************/
55 /** Allocate a structure of type \p T */
56 #define MALLOC_STRUCT(T) (struct T *) malloc(sizeof(struct T))
57 /** Allocate and zero a structure of type \p T */
58 #define CALLOC_STRUCT(T) (struct T *) calloc(1, sizeof(struct T))
64 * For GL_ARB_vertex_buffer_object we need to treat vertex array pointers
65 * as offsets into buffer stores. Since the vertex array pointer and
66 * buffer store pointer are both pointers and we need to add them, we use
68 * Both pointers/offsets are expressed in bytes.
70 #define ADD_POINTERS(A, B) ( (GLubyte *) (A) + (uintptr_t) (B) )
74 * Sometimes we treat GLfloats as GLints. On x86 systems, moving a float
75 * as an int (thereby using integer registers instead of FP registers) is
76 * a performance win. Typically, this can be done with ordinary casts.
77 * But with gcc's -fstrict-aliasing flag (which defaults to on in gcc 3.0)
78 * these casts generate warnings.
79 * The following union typedef is used to solve that.
81 typedef union { GLfloat f
; GLint i
; GLuint u
; } fi_type
;
89 *** LOG2: Log base 2 of float
91 static inline GLfloat
LOG2(GLfloat x
)
94 /* This is pretty fast, but not accurate enough (only 2 fractional bits).
95 * Based on code from http://www.stereopsis.com/log2.html
97 const GLfloat y
= x
* x
* x
* x
;
98 const GLuint ix
= *((GLuint
*) &y
);
99 const GLuint exp
= (ix
>> 23) & 0xFF;
100 const GLint log2
= ((GLint
) exp
) - 127;
101 return (GLfloat
) log2
* (1.0 / 4.0); /* 4, because of x^4 above */
103 /* Pretty fast, and accurate.
104 * Based on code from http://www.flipcode.com/totd/
109 log_2
= ((num
.i
>> 23) & 255) - 128;
110 num
.i
&= ~(255 << 23);
112 num
.f
= ((-1.0f
/3) * num
.f
+ 2) * num
.f
- 2.0f
/3;
113 return num
.f
+ log_2
;
121 #if defined(_MSC_VER)
122 # define finite _finite
127 *** IS_INF_OR_NAN: test if float is infinite or NaN
129 #if defined(isfinite)
130 #define IS_INF_OR_NAN(x) (!isfinite(x))
131 #elif defined(finite)
132 #define IS_INF_OR_NAN(x) (!finite(x))
133 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
134 #define IS_INF_OR_NAN(x) (!isfinite(x))
136 #define IS_INF_OR_NAN(x) (!finite(x))
141 * Convert float to int by rounding to nearest integer, away from zero.
143 static inline int IROUND(float f
)
145 return (int) ((f
>= 0.0F
) ? (f
+ 0.5F
) : (f
- 0.5F
));
149 * Convert double to int by rounding to nearest integer, away from zero.
151 static inline int IROUNDD(double d
)
153 return (int) ((d
>= 0.0) ? (d
+ 0.5) : (d
- 0.5));
157 * Convert float to int64 by rounding to nearest integer.
159 static inline GLint64
IROUND64(float f
)
161 return (GLint64
) ((f
>= 0.0F
) ? (f
+ 0.5F
) : (f
- 0.5F
));
166 * Convert positive float to int by rounding to nearest integer.
168 static inline int IROUND_POS(float f
)
171 return (int) (f
+ 0.5F
);
174 /** Return (as an integer) floor of float */
175 static inline int IFLOOR(float f
)
177 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
179 * IEEE floor for computers that round to nearest or even.
180 * 'f' must be between -4194304 and 4194303.
181 * This floor operation is done by "(iround(f + .5) + iround(f - .5)) >> 1",
182 * but uses some IEEE specific tricks for better speed.
183 * Contributed by Josh Vanderhoof
187 af
= (3 << 22) + 0.5 + (double)f
;
188 bf
= (3 << 22) + 0.5 - (double)f
;
189 /* GCC generates an extra fstp/fld without this. */
190 __asm__ ("fstps %0" : "=m" (ai
) : "t" (af
) : "st");
191 __asm__ ("fstps %0" : "=m" (bi
) : "t" (bf
) : "st");
192 return (ai
- bi
) >> 1;
197 af
= (3 << 22) + 0.5 + (double)f
;
198 bf
= (3 << 22) + 0.5 - (double)f
;
199 u
.f
= (float) af
; ai
= u
.i
;
200 u
.f
= (float) bf
; bi
= u
.i
;
201 return (ai
- bi
) >> 1;
207 * Is x a power of two?
210 _mesa_is_pow_two(int x
)
212 return !(x
& (x
- 1));
216 * Round given integer to next higer power of two
217 * If X is zero result is undefined.
219 * Source for the fallback implementation is
220 * Sean Eron Anderson's webpage "Bit Twiddling Hacks"
221 * http://graphics.stanford.edu/~seander/bithacks.html
223 * When using builtin function have to do some work
224 * for case when passed values 1 to prevent hiting
225 * undefined result from __builtin_clz. Undefined
226 * results would be different depending on optimization
227 * level used for build.
229 static inline int32_t
230 _mesa_next_pow_two_32(uint32_t x
)
232 #ifdef HAVE___BUILTIN_CLZ
233 uint32_t y
= (x
!= 1);
234 return (1 + y
) << ((__builtin_clz(x
- y
) ^ 31) );
247 static inline int64_t
248 _mesa_next_pow_two_64(uint64_t x
)
250 #ifdef HAVE___BUILTIN_CLZLL
251 uint64_t y
= (x
!= 1);
252 STATIC_ASSERT(sizeof(x
) == sizeof(long long));
253 return (1 + y
) << ((__builtin_clzll(x
- y
) ^ 63));
269 * Returns the floor form of binary logarithm for a 32-bit integer.
272 _mesa_logbase2(GLuint n
)
274 #ifdef HAVE___BUILTIN_CLZ
275 return (31 - __builtin_clz(n
| 1));
278 if (n
>= 1<<16) { n
>>= 16; pos
+= 16; }
279 if (n
>= 1<< 8) { n
>>= 8; pos
+= 8; }
280 if (n
>= 1<< 4) { n
>>= 4; pos
+= 4; }
281 if (n
>= 1<< 2) { n
>>= 2; pos
+= 2; }
282 if (n
>= 1<< 1) { pos
+= 1; }
288 /**********************************************************************
293 _mesa_align_malloc( size_t bytes
, unsigned long alignment
);
296 _mesa_align_calloc( size_t bytes
, unsigned long alignment
);
299 _mesa_align_free( void *ptr
);
302 _mesa_align_realloc(void *oldBuffer
, size_t oldSize
, size_t newSize
,
303 unsigned long alignment
);
306 _mesa_snprintf( char *str
, size_t size
, const char *fmt
, ... ) PRINTFLIKE(3, 4);
309 _mesa_vsnprintf(char *str
, size_t size
, const char *fmt
, va_list arg
);
312 #if defined(_WIN32) && !defined(HAVE_STRTOK_R)
313 #define strtok_r strtok_s
321 #endif /* IMPORTS_H */