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