03a6c0ef358e7779170fd9c7bab0535ac1373a5b
[mesa.git] / src / util / imports.h
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 <stdlib.h>
40 #include <stdarg.h>
41 #include <string.h>
42 #include "util/compiler.h"
43 #include "util/bitscan.h"
44 #include "util/u_memory.h"
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50
51 /*
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
55 * this macro.
56 * Both pointers/offsets are expressed in bytes.
57 */
58 #define ADD_POINTERS(A, B) ( (uint8_t *) (A) + (uintptr_t) (B) )
59
60
61 /**
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.
68 */
69 typedef union { float f; int i; unsigned u; } fi_type;
70
71
72
73 /*@}*/
74
75
76 /***
77 *** LOG2: Log base 2 of float
78 ***/
79 static inline float LOG2(float x)
80 {
81 #if 0
82 /* This is pretty fast, but not accurate enough (only 2 fractional bits).
83 * Based on code from http://www.stereopsis.com/log2.html
84 */
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 */
90 #endif
91 /* Pretty fast, and accurate.
92 * Based on code from http://www.flipcode.com/totd/
93 */
94 fi_type num;
95 int log_2;
96 num.f = x;
97 log_2 = ((num.i >> 23) & 255) - 128;
98 num.i &= ~(255 << 23);
99 num.i += 127 << 23;
100 num.f = ((-1.0f/3) * num.f + 2) * num.f - 2.0f/3;
101 return num.f + log_2;
102 }
103
104
105
106 /**
107 * finite macro.
108 */
109 #if defined(_MSC_VER)
110 # define finite _finite
111 #endif
112
113
114 /***
115 *** IS_INF_OR_NAN: test if float is infinite or NaN
116 ***/
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))
123 #else
124 #define IS_INF_OR_NAN(x) (!finite(x))
125 #endif
126
127
128 /**
129 * Convert float to int by rounding to nearest integer, away from zero.
130 */
131 static inline int IROUND(float f)
132 {
133 return (int) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F));
134 }
135
136 /**
137 * Convert double to int by rounding to nearest integer, away from zero.
138 */
139 static inline int IROUNDD(double d)
140 {
141 return (int) ((d >= 0.0) ? (d + 0.5) : (d - 0.5));
142 }
143
144 /**
145 * Convert float to int64 by rounding to nearest integer.
146 */
147 static inline int64_t IROUND64(float f)
148 {
149 return (int64_t) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F));
150 }
151
152
153 /**
154 * Convert positive float to int by rounding to nearest integer.
155 */
156 static inline int IROUND_POS(float f)
157 {
158 assert(f >= 0.0F);
159 return (int) (f + 0.5F);
160 }
161
162 /** Return (as an integer) floor of float */
163 static inline int IFLOOR(float f)
164 {
165 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
166 /*
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
172 */
173 int ai, bi;
174 double af, bf;
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;
181 #else
182 int ai, bi;
183 double af, bf;
184 fi_type u;
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;
190 #endif
191 }
192
193 /**
194 * Round given integer to next higer power of two
195 * If X is zero result is undefined.
196 *
197 * Source for the fallback implementation is
198 * Sean Eron Anderson's webpage "Bit Twiddling Hacks"
199 * http://graphics.stanford.edu/~seander/bithacks.html
200 *
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.
206 */
207 static inline int32_t
208 _mesa_next_pow_two_32(uint32_t x)
209 {
210 #ifdef HAVE___BUILTIN_CLZ
211 uint32_t y = (x != 1);
212 return (1 + y) << ((__builtin_clz(x - y) ^ 31) );
213 #else
214 x--;
215 x |= x >> 1;
216 x |= x >> 2;
217 x |= x >> 4;
218 x |= x >> 8;
219 x |= x >> 16;
220 x++;
221 return x;
222 #endif
223 }
224
225 static inline int64_t
226 _mesa_next_pow_two_64(uint64_t x)
227 {
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));
232 #else
233 x--;
234 x |= x >> 1;
235 x |= x >> 2;
236 x |= x >> 4;
237 x |= x >> 8;
238 x |= x >> 16;
239 x |= x >> 32;
240 x++;
241 return x;
242 #endif
243 }
244
245
246 /*
247 * Returns the floor form of binary logarithm for a 32-bit integer.
248 */
249 static inline unsigned
250 _mesa_logbase2(unsigned n)
251 {
252 #ifdef HAVE___BUILTIN_CLZ
253 return (31 - __builtin_clz(n | 1));
254 #else
255 unsigned pos = 0;
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; }
261 return pos;
262 #endif
263 }
264
265
266 /**********************************************************************
267 * Functions
268 */
269
270 extern void *
271 _mesa_align_malloc( size_t bytes, unsigned long alignment );
272
273 extern void *
274 _mesa_align_calloc( size_t bytes, unsigned long alignment );
275
276 extern void
277 _mesa_align_free( void *ptr );
278
279 extern void *
280 _mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize,
281 unsigned long alignment);
282
283 extern int
284 _mesa_snprintf( char *str, size_t size, const char *fmt, ... ) PRINTFLIKE(3, 4);
285
286 extern int
287 _mesa_vsnprintf(char *str, size_t size, const char *fmt, va_list arg);
288
289
290 #if defined(_WIN32) && !defined(HAVE_STRTOK_R)
291 #define strtok_r strtok_s
292 #endif
293
294 #ifdef __cplusplus
295 }
296 #endif
297
298
299 #endif /* IMPORTS_H */