mesa: remove INV_SQRTF() macro
[mesa.git] / src / mesa / main / 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 "compiler.h"
40 #include "glheader.h"
41 #include "errors.h"
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47
48 /**********************************************************************/
49 /** Memory macros */
50 /*@{*/
51
52 /** Allocate a structure of type \p T */
53 #define MALLOC_STRUCT(T) (struct T *) malloc(sizeof(struct T))
54 /** Allocate and zero a structure of type \p T */
55 #define CALLOC_STRUCT(T) (struct T *) calloc(1, sizeof(struct T))
56
57 /*@}*/
58
59
60 /*
61 * For GL_ARB_vertex_buffer_object we need to treat vertex array pointers
62 * as offsets into buffer stores. Since the vertex array pointer and
63 * buffer store pointer are both pointers and we need to add them, we use
64 * this macro.
65 * Both pointers/offsets are expressed in bytes.
66 */
67 #define ADD_POINTERS(A, B) ( (GLubyte *) (A) + (uintptr_t) (B) )
68
69
70 /**
71 * Sometimes we treat GLfloats as GLints. On x86 systems, moving a float
72 * as a int (thereby using integer registers instead of FP registers) is
73 * a performance win. Typically, this can be done with ordinary casts.
74 * But with gcc's -fstrict-aliasing flag (which defaults to on in gcc 3.0)
75 * these casts generate warnings.
76 * The following union typedef is used to solve that.
77 */
78 typedef union { GLfloat f; GLint i; GLuint u; } fi_type;
79
80
81
82 /**********************************************************************
83 * Math macros
84 */
85
86 #define MAX_GLUSHORT 0xffff
87 #define MAX_GLUINT 0xffffffff
88
89 /* Degrees to radians conversion: */
90 #define DEG2RAD (M_PI/180.0)
91
92
93 /**
94 * \name Work-arounds for platforms that lack C99 math functions
95 */
96 /*@{*/
97 #if (!defined(_XOPEN_SOURCE) || (_XOPEN_SOURCE < 600)) && !defined(_ISOC99_SOURCE) \
98 && (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)) \
99 && (!defined(_MSC_VER) || (_MSC_VER < 1400))
100
101 #define sqrtf(f) ((float) sqrt(f))
102 #endif
103
104 #if defined(_MSC_VER)
105 #if _MSC_VER < 1800 /* Not req'd on VS2013 and above */
106 static inline int isblank(int ch) { return ch == ' ' || ch == '\t'; }
107 #define strtoll(p, e, b) _strtoi64(p, e, b)
108 #endif /* _MSC_VER < 1800 */
109 #define strcasecmp(s1, s2) _stricmp(s1, s2)
110 #endif
111 /*@}*/
112
113
114 /*
115 * signbit() is a macro on Linux. Not available on Windows.
116 */
117 #ifndef signbit
118 #define signbit(x) ((x) < 0.0f)
119 #endif
120
121
122 /***
123 *** LOG2: Log base 2 of float
124 ***/
125 static inline GLfloat LOG2(GLfloat x)
126 {
127 #if 0
128 /* This is pretty fast, but not accurate enough (only 2 fractional bits).
129 * Based on code from http://www.stereopsis.com/log2.html
130 */
131 const GLfloat y = x * x * x * x;
132 const GLuint ix = *((GLuint *) &y);
133 const GLuint exp = (ix >> 23) & 0xFF;
134 const GLint log2 = ((GLint) exp) - 127;
135 return (GLfloat) log2 * (1.0 / 4.0); /* 4, because of x^4 above */
136 #endif
137 /* Pretty fast, and accurate.
138 * Based on code from http://www.flipcode.com/totd/
139 */
140 fi_type num;
141 GLint log_2;
142 num.f = x;
143 log_2 = ((num.i >> 23) & 255) - 128;
144 num.i &= ~(255 << 23);
145 num.i += 127 << 23;
146 num.f = ((-1.0f/3) * num.f + 2) * num.f - 2.0f/3;
147 return num.f + log_2;
148 }
149
150
151
152 /***
153 *** IS_INF_OR_NAN: test if float is infinite or NaN
154 ***/
155 #if defined(isfinite)
156 #define IS_INF_OR_NAN(x) (!isfinite(x))
157 #elif defined(finite)
158 #define IS_INF_OR_NAN(x) (!finite(x))
159 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
160 #define IS_INF_OR_NAN(x) (!isfinite(x))
161 #else
162 #define IS_INF_OR_NAN(x) (!finite(x))
163 #endif
164
165
166 /**
167 * Convert float to int by rounding to nearest integer, away from zero.
168 */
169 static inline int IROUND(float f)
170 {
171 return (int) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F));
172 }
173
174
175 /**
176 * Convert float to int64 by rounding to nearest integer.
177 */
178 static inline GLint64 IROUND64(float f)
179 {
180 return (GLint64) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F));
181 }
182
183
184 /**
185 * Convert positive float to int by rounding to nearest integer.
186 */
187 static inline int IROUND_POS(float f)
188 {
189 assert(f >= 0.0F);
190 return (int) (f + 0.5F);
191 }
192
193 #ifdef __x86_64__
194 # include <xmmintrin.h>
195 #endif
196
197 /**
198 * Convert float to int using a fast method. The rounding mode may vary.
199 */
200 static inline int F_TO_I(float f)
201 {
202 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
203 int r;
204 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
205 return r;
206 #elif defined(USE_X86_ASM) && defined(_MSC_VER)
207 int r;
208 _asm {
209 fld f
210 fistp r
211 }
212 return r;
213 #elif defined(__x86_64__)
214 return _mm_cvt_ss2si(_mm_load_ss(&f));
215 #else
216 return IROUND(f);
217 #endif
218 }
219
220
221 /** Return (as an integer) floor of float */
222 static inline int IFLOOR(float f)
223 {
224 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
225 /*
226 * IEEE floor for computers that round to nearest or even.
227 * 'f' must be between -4194304 and 4194303.
228 * This floor operation is done by "(iround(f + .5) + iround(f - .5)) >> 1",
229 * but uses some IEEE specific tricks for better speed.
230 * Contributed by Josh Vanderhoof
231 */
232 int ai, bi;
233 double af, bf;
234 af = (3 << 22) + 0.5 + (double)f;
235 bf = (3 << 22) + 0.5 - (double)f;
236 /* GCC generates an extra fstp/fld without this. */
237 __asm__ ("fstps %0" : "=m" (ai) : "t" (af) : "st");
238 __asm__ ("fstps %0" : "=m" (bi) : "t" (bf) : "st");
239 return (ai - bi) >> 1;
240 #else
241 int ai, bi;
242 double af, bf;
243 fi_type u;
244 af = (3 << 22) + 0.5 + (double)f;
245 bf = (3 << 22) + 0.5 - (double)f;
246 u.f = (float) af; ai = u.i;
247 u.f = (float) bf; bi = u.i;
248 return (ai - bi) >> 1;
249 #endif
250 }
251
252
253 /** Return (as an integer) ceiling of float */
254 static inline int ICEIL(float f)
255 {
256 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
257 /*
258 * IEEE ceil for computers that round to nearest or even.
259 * 'f' must be between -4194304 and 4194303.
260 * This ceil operation is done by "(iround(f + .5) + iround(f - .5) + 1) >> 1",
261 * but uses some IEEE specific tricks for better speed.
262 * Contributed by Josh Vanderhoof
263 */
264 int ai, bi;
265 double af, bf;
266 af = (3 << 22) + 0.5 + (double)f;
267 bf = (3 << 22) + 0.5 - (double)f;
268 /* GCC generates an extra fstp/fld without this. */
269 __asm__ ("fstps %0" : "=m" (ai) : "t" (af) : "st");
270 __asm__ ("fstps %0" : "=m" (bi) : "t" (bf) : "st");
271 return (ai - bi + 1) >> 1;
272 #else
273 int ai, bi;
274 double af, bf;
275 fi_type u;
276 af = (3 << 22) + 0.5 + (double)f;
277 bf = (3 << 22) + 0.5 - (double)f;
278 u.f = (float) af; ai = u.i;
279 u.f = (float) bf; bi = u.i;
280 return (ai - bi + 1) >> 1;
281 #endif
282 }
283
284
285 /**
286 * Is x a power of two?
287 */
288 static inline int
289 _mesa_is_pow_two(int x)
290 {
291 return !(x & (x - 1));
292 }
293
294 /**
295 * Round given integer to next higer power of two
296 * If X is zero result is undefined.
297 *
298 * Source for the fallback implementation is
299 * Sean Eron Anderson's webpage "Bit Twiddling Hacks"
300 * http://graphics.stanford.edu/~seander/bithacks.html
301 *
302 * When using builtin function have to do some work
303 * for case when passed values 1 to prevent hiting
304 * undefined result from __builtin_clz. Undefined
305 * results would be different depending on optimization
306 * level used for build.
307 */
308 static inline int32_t
309 _mesa_next_pow_two_32(uint32_t x)
310 {
311 #ifdef HAVE___BUILTIN_CLZ
312 uint32_t y = (x != 1);
313 return (1 + y) << ((__builtin_clz(x - y) ^ 31) );
314 #else
315 x--;
316 x |= x >> 1;
317 x |= x >> 2;
318 x |= x >> 4;
319 x |= x >> 8;
320 x |= x >> 16;
321 x++;
322 return x;
323 #endif
324 }
325
326 static inline int64_t
327 _mesa_next_pow_two_64(uint64_t x)
328 {
329 #ifdef HAVE___BUILTIN_CLZLL
330 uint64_t y = (x != 1);
331 STATIC_ASSERT(sizeof(x) == sizeof(long long));
332 return (1 + y) << ((__builtin_clzll(x - y) ^ 63));
333 #else
334 x--;
335 x |= x >> 1;
336 x |= x >> 2;
337 x |= x >> 4;
338 x |= x >> 8;
339 x |= x >> 16;
340 x |= x >> 32;
341 x++;
342 return x;
343 #endif
344 }
345
346
347 /*
348 * Returns the floor form of binary logarithm for a 32-bit integer.
349 */
350 static inline GLuint
351 _mesa_logbase2(GLuint n)
352 {
353 #ifdef HAVE___BUILTIN_CLZ
354 return (31 - __builtin_clz(n | 1));
355 #else
356 GLuint pos = 0;
357 if (n >= 1<<16) { n >>= 16; pos += 16; }
358 if (n >= 1<< 8) { n >>= 8; pos += 8; }
359 if (n >= 1<< 4) { n >>= 4; pos += 4; }
360 if (n >= 1<< 2) { n >>= 2; pos += 2; }
361 if (n >= 1<< 1) { pos += 1; }
362 return pos;
363 #endif
364 }
365
366
367 /**
368 * Return 1 if this is a little endian machine, 0 if big endian.
369 */
370 static inline GLboolean
371 _mesa_little_endian(void)
372 {
373 const GLuint ui = 1; /* intentionally not static */
374 return *((const GLubyte *) &ui);
375 }
376
377
378
379 /**********************************************************************
380 * Functions
381 */
382
383 extern void *
384 _mesa_align_malloc( size_t bytes, unsigned long alignment );
385
386 extern void *
387 _mesa_align_calloc( size_t bytes, unsigned long alignment );
388
389 extern void
390 _mesa_align_free( void *ptr );
391
392 extern void *
393 _mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize,
394 unsigned long alignment);
395
396 extern void *
397 _mesa_exec_malloc( GLuint size );
398
399 extern void
400 _mesa_exec_free( void *addr );
401
402
403 #ifndef FFS_DEFINED
404 #define FFS_DEFINED 1
405 #ifdef HAVE___BUILTIN_FFS
406 #define ffs __builtin_ffs
407 #else
408 extern int ffs(int i);
409 #endif
410
411 #ifdef HAVE___BUILTIN_FFSLL
412 #define ffsll __builtin_ffsll
413 #else
414 extern int ffsll(long long int i);
415 #endif
416 #endif /* FFS_DEFINED */
417
418
419 #ifdef HAVE___BUILTIN_POPCOUNT
420 #define _mesa_bitcount(i) __builtin_popcount(i)
421 #else
422 extern unsigned int
423 _mesa_bitcount(unsigned int n);
424 #endif
425
426 #ifdef HAVE___BUILTIN_POPCOUNTLL
427 #define _mesa_bitcount_64(i) __builtin_popcountll(i)
428 #else
429 extern unsigned int
430 _mesa_bitcount_64(uint64_t n);
431 #endif
432
433 /**
434 * Find the last (most significant) bit set in a word.
435 *
436 * Essentially ffs() in the reverse direction.
437 */
438 static inline unsigned int
439 _mesa_fls(unsigned int n)
440 {
441 #ifdef HAVE___BUILTIN_CLZ
442 return n == 0 ? 0 : 32 - __builtin_clz(n);
443 #else
444 unsigned int v = 1;
445
446 if (n == 0)
447 return 0;
448
449 while (n >>= 1)
450 v++;
451
452 return v;
453 #endif
454 }
455
456 extern int
457 _mesa_round_to_even(float val);
458
459 extern GLhalfARB
460 _mesa_float_to_half(float f);
461
462 extern float
463 _mesa_half_to_float(GLhalfARB h);
464
465 static inline bool
466 _mesa_half_is_negative(GLhalfARB h)
467 {
468 return h & 0x8000;
469 }
470
471 extern char *
472 _mesa_strdup( const char *s );
473
474 extern unsigned int
475 _mesa_str_checksum(const char *str);
476
477 extern int
478 _mesa_snprintf( char *str, size_t size, const char *fmt, ... ) PRINTFLIKE(3, 4);
479
480 extern int
481 _mesa_vsnprintf(char *str, size_t size, const char *fmt, va_list arg);
482
483
484 #if defined(_MSC_VER) && !defined(snprintf)
485 #define snprintf _snprintf
486 #endif
487
488
489 #ifdef __cplusplus
490 }
491 #endif
492
493
494 #endif /* IMPORTS_H */