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.
42 #include "util/compiler.h"
43 #include "util/bitscan.h"
44 #include "util/u_memory.h"
52 * For GL_ARB_vertex_buffer_object we need to treat vertex array pointers
53 * as offsets into buffer stores. Since the vertex array pointer and
54 * buffer store pointer are both pointers and we need to add them, we use
56 * Both pointers/offsets are expressed in bytes.
58 #define ADD_POINTERS(A, B) ( (uint8_t *) (A) + (uintptr_t) (B) )
62 * Sometimes we treat floats as ints. On x86 systems, moving a float
63 * as an int (thereby using integer registers instead of FP registers) is
64 * a performance win. Typically, this can be done with ordinary casts.
65 * But with gcc's -fstrict-aliasing flag (which defaults to on in gcc 3.0)
66 * these casts generate warnings.
67 * The following union typedef is used to solve that.
69 typedef union { float f
; int i
; unsigned u
; } fi_type
;
77 *** LOG2: Log base 2 of float
79 static inline float LOG2(float x
)
82 /* This is pretty fast, but not accurate enough (only 2 fractional bits).
83 * Based on code from http://www.stereopsis.com/log2.html
85 const float y
= x
* x
* x
* x
;
86 const unsigned ix
= *((unsigned *) &y
);
87 const unsigned exp
= (ix
>> 23) & 0xFF;
88 const int log2
= ((int) exp
) - 127;
89 return (float) log2
* (1.0 / 4.0); /* 4, because of x^4 above */
91 /* Pretty fast, and accurate.
92 * Based on code from http://www.flipcode.com/totd/
97 log_2
= ((num
.i
>> 23) & 255) - 128;
98 num
.i
&= ~(255 << 23);
100 num
.f
= ((-1.0f
/3) * num
.f
+ 2) * num
.f
- 2.0f
/3;
101 return num
.f
+ log_2
;
109 #if defined(_MSC_VER)
110 # define finite _finite
115 *** IS_INF_OR_NAN: test if float is infinite or NaN
117 #if defined(isfinite)
118 #define IS_INF_OR_NAN(x) (!isfinite(x))
119 #elif defined(finite)
120 #define IS_INF_OR_NAN(x) (!finite(x))
121 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
122 #define IS_INF_OR_NAN(x) (!isfinite(x))
124 #define IS_INF_OR_NAN(x) (!finite(x))
129 * Convert float to int by rounding to nearest integer, away from zero.
131 static inline int IROUND(float f
)
133 return (int) ((f
>= 0.0F
) ? (f
+ 0.5F
) : (f
- 0.5F
));
137 * Convert double to int by rounding to nearest integer, away from zero.
139 static inline int IROUNDD(double d
)
141 return (int) ((d
>= 0.0) ? (d
+ 0.5) : (d
- 0.5));
145 * Convert float to int64 by rounding to nearest integer.
147 static inline int64_t IROUND64(float f
)
149 return (int64_t) ((f
>= 0.0F
) ? (f
+ 0.5F
) : (f
- 0.5F
));
154 * Convert positive float to int by rounding to nearest integer.
156 static inline int IROUND_POS(float f
)
159 return (int) (f
+ 0.5F
);
162 /** Return (as an integer) floor of float */
163 static inline int IFLOOR(float f
)
165 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
167 * IEEE floor for computers that round to nearest or even.
168 * 'f' must be between -4194304 and 4194303.
169 * This floor operation is done by "(iround(f + .5) + iround(f - .5)) >> 1",
170 * but uses some IEEE specific tricks for better speed.
171 * Contributed by Josh Vanderhoof
175 af
= (3 << 22) + 0.5 + (double)f
;
176 bf
= (3 << 22) + 0.5 - (double)f
;
177 /* GCC generates an extra fstp/fld without this. */
178 __asm__ ("fstps %0" : "=m" (ai
) : "t" (af
) : "st");
179 __asm__ ("fstps %0" : "=m" (bi
) : "t" (bf
) : "st");
180 return (ai
- bi
) >> 1;
185 af
= (3 << 22) + 0.5 + (double)f
;
186 bf
= (3 << 22) + 0.5 - (double)f
;
187 u
.f
= (float) af
; ai
= u
.i
;
188 u
.f
= (float) bf
; bi
= u
.i
;
189 return (ai
- bi
) >> 1;
195 * Is x a power of two?
198 _mesa_is_pow_two(int x
)
200 return !(x
& (x
- 1));
204 * Round given integer to next higer power of two
205 * If X is zero result is undefined.
207 * Source for the fallback implementation is
208 * Sean Eron Anderson's webpage "Bit Twiddling Hacks"
209 * http://graphics.stanford.edu/~seander/bithacks.html
211 * When using builtin function have to do some work
212 * for case when passed values 1 to prevent hiting
213 * undefined result from __builtin_clz. Undefined
214 * results would be different depending on optimization
215 * level used for build.
217 static inline int32_t
218 _mesa_next_pow_two_32(uint32_t x
)
220 #ifdef HAVE___BUILTIN_CLZ
221 uint32_t y
= (x
!= 1);
222 return (1 + y
) << ((__builtin_clz(x
- y
) ^ 31) );
235 static inline int64_t
236 _mesa_next_pow_two_64(uint64_t x
)
238 #ifdef HAVE___BUILTIN_CLZLL
239 uint64_t y
= (x
!= 1);
240 STATIC_ASSERT(sizeof(x
) == sizeof(long long));
241 return (1 + y
) << ((__builtin_clzll(x
- y
) ^ 63));
257 * Returns the floor form of binary logarithm for a 32-bit integer.
259 static inline unsigned
260 _mesa_logbase2(unsigned n
)
262 #ifdef HAVE___BUILTIN_CLZ
263 return (31 - __builtin_clz(n
| 1));
266 if (n
>= 1<<16) { n
>>= 16; pos
+= 16; }
267 if (n
>= 1<< 8) { n
>>= 8; pos
+= 8; }
268 if (n
>= 1<< 4) { n
>>= 4; pos
+= 4; }
269 if (n
>= 1<< 2) { n
>>= 2; pos
+= 2; }
270 if (n
>= 1<< 1) { pos
+= 1; }
276 /**********************************************************************
281 _mesa_align_malloc( size_t bytes
, unsigned long alignment
);
284 _mesa_align_calloc( size_t bytes
, unsigned long alignment
);
287 _mesa_align_free( void *ptr
);
290 _mesa_align_realloc(void *oldBuffer
, size_t oldSize
, size_t newSize
,
291 unsigned long alignment
);
294 _mesa_snprintf( char *str
, size_t size
, const char *fmt
, ... ) PRINTFLIKE(3, 4);
297 _mesa_vsnprintf(char *str
, size_t size
, const char *fmt
, va_list arg
);
300 #if defined(_WIN32) && !defined(HAVE_STRTOK_R)
301 #define strtok_r strtok_s
309 #endif /* IMPORTS_H */