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