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