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