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