d61279ac4e5a8f5a2e99e5e13274b9d4cf30c42a
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.
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 a 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
;
86 #if _MSC_VER < 1800 /* Not req'd on VS2013 and above */
87 #define strtoll(p, e, b) _strtoi64(p, e, b)
88 #endif /* _MSC_VER < 1800 */
89 #define strcasecmp(s1, s2) _stricmp(s1, s2)
95 *** LOG2: Log base 2 of float
97 static inline GLfloat
LOG2(GLfloat x
)
100 /* This is pretty fast, but not accurate enough (only 2 fractional bits).
101 * Based on code from http://www.stereopsis.com/log2.html
103 const GLfloat y
= x
* x
* x
* x
;
104 const GLuint ix
= *((GLuint
*) &y
);
105 const GLuint exp
= (ix
>> 23) & 0xFF;
106 const GLint log2
= ((GLint
) exp
) - 127;
107 return (GLfloat
) log2
* (1.0 / 4.0); /* 4, because of x^4 above */
109 /* Pretty fast, and accurate.
110 * Based on code from http://www.flipcode.com/totd/
115 log_2
= ((num
.i
>> 23) & 255) - 128;
116 num
.i
&= ~(255 << 23);
118 num
.f
= ((-1.0f
/3) * num
.f
+ 2) * num
.f
- 2.0f
/3;
119 return num
.f
+ log_2
;
127 #if defined(_MSC_VER)
128 # define finite _finite
133 *** IS_INF_OR_NAN: test if float is infinite or NaN
135 #if defined(isfinite)
136 #define IS_INF_OR_NAN(x) (!isfinite(x))
137 #elif defined(finite)
138 #define IS_INF_OR_NAN(x) (!finite(x))
139 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
140 #define IS_INF_OR_NAN(x) (!isfinite(x))
142 #define IS_INF_OR_NAN(x) (!finite(x))
147 * Convert float to int by rounding to nearest integer, away from zero.
149 static inline int IROUND(float f
)
151 return (int) ((f
>= 0.0F
) ? (f
+ 0.5F
) : (f
- 0.5F
));
156 * Convert float to int64 by rounding to nearest integer.
158 static inline GLint64
IROUND64(float f
)
160 return (GLint64
) ((f
>= 0.0F
) ? (f
+ 0.5F
) : (f
- 0.5F
));
165 * Convert positive float to int by rounding to nearest integer.
167 static inline int IROUND_POS(float f
)
170 return (int) (f
+ 0.5F
);
173 /** Return (as an integer) floor of float */
174 static inline int IFLOOR(float f
)
176 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
178 * IEEE floor for computers that round to nearest or even.
179 * 'f' must be between -4194304 and 4194303.
180 * This floor operation is done by "(iround(f + .5) + iround(f - .5)) >> 1",
181 * but uses some IEEE specific tricks for better speed.
182 * Contributed by Josh Vanderhoof
186 af
= (3 << 22) + 0.5 + (double)f
;
187 bf
= (3 << 22) + 0.5 - (double)f
;
188 /* GCC generates an extra fstp/fld without this. */
189 __asm__ ("fstps %0" : "=m" (ai
) : "t" (af
) : "st");
190 __asm__ ("fstps %0" : "=m" (bi
) : "t" (bf
) : "st");
191 return (ai
- bi
) >> 1;
196 af
= (3 << 22) + 0.5 + (double)f
;
197 bf
= (3 << 22) + 0.5 - (double)f
;
198 u
.f
= (float) af
; ai
= u
.i
;
199 u
.f
= (float) bf
; bi
= u
.i
;
200 return (ai
- bi
) >> 1;
206 * Is x a power of two?
209 _mesa_is_pow_two(int x
)
211 return !(x
& (x
- 1));
215 * Round given integer to next higer power of two
216 * If X is zero result is undefined.
218 * Source for the fallback implementation is
219 * Sean Eron Anderson's webpage "Bit Twiddling Hacks"
220 * http://graphics.stanford.edu/~seander/bithacks.html
222 * When using builtin function have to do some work
223 * for case when passed values 1 to prevent hiting
224 * undefined result from __builtin_clz. Undefined
225 * results would be different depending on optimization
226 * level used for build.
228 static inline int32_t
229 _mesa_next_pow_two_32(uint32_t x
)
231 #ifdef HAVE___BUILTIN_CLZ
232 uint32_t y
= (x
!= 1);
233 return (1 + y
) << ((__builtin_clz(x
- y
) ^ 31) );
246 static inline int64_t
247 _mesa_next_pow_two_64(uint64_t x
)
249 #ifdef HAVE___BUILTIN_CLZLL
250 uint64_t y
= (x
!= 1);
251 STATIC_ASSERT(sizeof(x
) == sizeof(long long));
252 return (1 + y
) << ((__builtin_clzll(x
- y
) ^ 63));
268 * Returns the floor form of binary logarithm for a 32-bit integer.
271 _mesa_logbase2(GLuint n
)
273 #ifdef HAVE___BUILTIN_CLZ
274 return (31 - __builtin_clz(n
| 1));
277 if (n
>= 1<<16) { n
>>= 16; pos
+= 16; }
278 if (n
>= 1<< 8) { n
>>= 8; pos
+= 8; }
279 if (n
>= 1<< 4) { n
>>= 4; pos
+= 4; }
280 if (n
>= 1<< 2) { n
>>= 2; pos
+= 2; }
281 if (n
>= 1<< 1) { pos
+= 1; }
288 * Return 1 if this is a little endian machine, 0 if big endian.
290 static inline GLboolean
291 _mesa_little_endian(void)
293 const GLuint ui
= 1; /* intentionally not static */
294 return *((const GLubyte
*) &ui
);
299 /**********************************************************************
304 _mesa_align_malloc( size_t bytes
, unsigned long alignment
);
307 _mesa_align_calloc( size_t bytes
, unsigned long alignment
);
310 _mesa_align_free( void *ptr
);
313 _mesa_align_realloc(void *oldBuffer
, size_t oldSize
, size_t newSize
,
314 unsigned long alignment
);
317 _mesa_exec_malloc( GLuint size
);
320 _mesa_exec_free( void *addr
);
324 #define FFS_DEFINED 1
325 #ifdef HAVE___BUILTIN_FFS
326 #define ffs __builtin_ffs
328 extern int ffs(int i
);
331 #ifdef HAVE___BUILTIN_FFSLL
332 #define ffsll __builtin_ffsll
334 extern int ffsll(long long int i
);
336 #endif /* FFS_DEFINED */
339 #ifdef HAVE___BUILTIN_POPCOUNT
340 #define _mesa_bitcount(i) __builtin_popcount(i)
343 _mesa_bitcount(unsigned int n
);
346 #ifdef HAVE___BUILTIN_POPCOUNTLL
347 #define _mesa_bitcount_64(i) __builtin_popcountll(i)
350 _mesa_bitcount_64(uint64_t n
);
354 * Find the last (most significant) bit set in a word.
356 * Essentially ffs() in the reverse direction.
358 static inline unsigned int
359 _mesa_fls(unsigned int n
)
361 #ifdef HAVE___BUILTIN_CLZ
362 return n
== 0 ? 0 : 32 - __builtin_clz(n
);
377 * Find the last (most significant) bit set in a uint64_t value.
379 * Essentially ffsll() in the reverse direction.
381 static inline unsigned int
382 _mesa_flsll(uint64_t n
)
384 #ifdef HAVE___BUILTIN_CLZLL
385 return n
== 0 ? 0 : 64 - __builtin_clzll(n
);
401 _mesa_float_to_half(float f
);
404 _mesa_half_to_float(GLhalfARB h
);
407 _mesa_half_is_negative(GLhalfARB h
)
413 _mesa_str_checksum(const char *str
);
416 _mesa_snprintf( char *str
, size_t size
, const char *fmt
, ... ) PRINTFLIKE(3, 4);
419 _mesa_vsnprintf(char *str
, size_t size
, const char *fmt
, va_list arg
);
422 #if defined(_MSC_VER) && !defined(snprintf)
423 #define snprintf _snprintf
432 #endif /* IMPORTS_H */