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