03a6c0ef358e7779170fd9c7bab0535ac1373a5b
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;
194 * Round given integer to next higer power of two
195 * If X is zero result is undefined.
197 * Source for the fallback implementation is
198 * Sean Eron Anderson's webpage "Bit Twiddling Hacks"
199 * http://graphics.stanford.edu/~seander/bithacks.html
201 * When using builtin function have to do some work
202 * for case when passed values 1 to prevent hiting
203 * undefined result from __builtin_clz. Undefined
204 * results would be different depending on optimization
205 * level used for build.
207 static inline int32_t
208 _mesa_next_pow_two_32(uint32_t x
)
210 #ifdef HAVE___BUILTIN_CLZ
211 uint32_t y
= (x
!= 1);
212 return (1 + y
) << ((__builtin_clz(x
- y
) ^ 31) );
225 static inline int64_t
226 _mesa_next_pow_two_64(uint64_t x
)
228 #ifdef HAVE___BUILTIN_CLZLL
229 uint64_t y
= (x
!= 1);
230 STATIC_ASSERT(sizeof(x
) == sizeof(long long));
231 return (1 + y
) << ((__builtin_clzll(x
- y
) ^ 63));
247 * Returns the floor form of binary logarithm for a 32-bit integer.
249 static inline unsigned
250 _mesa_logbase2(unsigned n
)
252 #ifdef HAVE___BUILTIN_CLZ
253 return (31 - __builtin_clz(n
| 1));
256 if (n
>= 1<<16) { n
>>= 16; pos
+= 16; }
257 if (n
>= 1<< 8) { n
>>= 8; pos
+= 8; }
258 if (n
>= 1<< 4) { n
>>= 4; pos
+= 4; }
259 if (n
>= 1<< 2) { n
>>= 2; pos
+= 2; }
260 if (n
>= 1<< 1) { pos
+= 1; }
266 /**********************************************************************
271 _mesa_align_malloc( size_t bytes
, unsigned long alignment
);
274 _mesa_align_calloc( size_t bytes
, unsigned long alignment
);
277 _mesa_align_free( void *ptr
);
280 _mesa_align_realloc(void *oldBuffer
, size_t oldSize
, size_t newSize
,
281 unsigned long alignment
);
284 _mesa_snprintf( char *str
, size_t size
, const char *fmt
, ... ) PRINTFLIKE(3, 4);
287 _mesa_vsnprintf(char *str
, size_t size
, const char *fmt
, va_list arg
);
290 #if defined(_WIN32) && !defined(HAVE_STRTOK_R)
291 #define strtok_r strtok_s
299 #endif /* IMPORTS_H */