glx: omit loader_loader() for macOS
[mesa.git] / src / util / 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 <stdarg.h>
41 #include <string.h>
42 #include "util/compiler.h"
43 #include "util/bitscan.h"
44 #include "util/u_memory.h"
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50
51 /*
52 * For GL_ARB_vertex_buffer_object we need to treat vertex array pointers
53 * as offsets into buffer stores. Since the vertex array pointer and
54 * buffer store pointer are both pointers and we need to add them, we use
55 * this macro.
56 * Both pointers/offsets are expressed in bytes.
57 */
58 #define ADD_POINTERS(A, B) ( (uint8_t *) (A) + (uintptr_t) (B) )
59
60
61 /**
62 * Sometimes we treat floats as ints. On x86 systems, moving a float
63 * as an int (thereby using integer registers instead of FP registers) is
64 * a performance win. Typically, this can be done with ordinary casts.
65 * But with gcc's -fstrict-aliasing flag (which defaults to on in gcc 3.0)
66 * these casts generate warnings.
67 * The following union typedef is used to solve that.
68 */
69 typedef union { float f; int i; unsigned u; } fi_type;
70
71
72
73 /*@}*/
74
75
76 /***
77 *** LOG2: Log base 2 of float
78 ***/
79 static inline float LOG2(float x)
80 {
81 #if 0
82 /* This is pretty fast, but not accurate enough (only 2 fractional bits).
83 * Based on code from http://www.stereopsis.com/log2.html
84 */
85 const float y = x * x * x * x;
86 const unsigned ix = *((unsigned *) &y);
87 const unsigned exp = (ix >> 23) & 0xFF;
88 const int log2 = ((int) exp) - 127;
89 return (float) log2 * (1.0 / 4.0); /* 4, because of x^4 above */
90 #endif
91 /* Pretty fast, and accurate.
92 * Based on code from http://www.flipcode.com/totd/
93 */
94 fi_type num;
95 int log_2;
96 num.f = x;
97 log_2 = ((num.i >> 23) & 255) - 128;
98 num.i &= ~(255 << 23);
99 num.i += 127 << 23;
100 num.f = ((-1.0f/3) * num.f + 2) * num.f - 2.0f/3;
101 return num.f + log_2;
102 }
103
104
105
106 /**
107 * finite macro.
108 */
109 #if defined(_MSC_VER)
110 # define finite _finite
111 #endif
112
113
114 /***
115 *** IS_INF_OR_NAN: test if float is infinite or NaN
116 ***/
117 #if defined(isfinite)
118 #define IS_INF_OR_NAN(x) (!isfinite(x))
119 #elif defined(finite)
120 #define IS_INF_OR_NAN(x) (!finite(x))
121 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
122 #define IS_INF_OR_NAN(x) (!isfinite(x))
123 #else
124 #define IS_INF_OR_NAN(x) (!finite(x))
125 #endif
126
127
128 /**
129 * Convert float to int by rounding to nearest integer, away from zero.
130 */
131 static inline int IROUND(float f)
132 {
133 return (int) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F));
134 }
135
136 /**
137 * Convert double to int by rounding to nearest integer, away from zero.
138 */
139 static inline int IROUNDD(double d)
140 {
141 return (int) ((d >= 0.0) ? (d + 0.5) : (d - 0.5));
142 }
143
144 /**
145 * Convert float to int64 by rounding to nearest integer.
146 */
147 static inline int64_t IROUND64(float f)
148 {
149 return (int64_t) ((f >= 0.0F) ? (f + 0.5F) : (f - 0.5F));
150 }
151
152
153 /**
154 * Convert positive float to int by rounding to nearest integer.
155 */
156 static inline int IROUND_POS(float f)
157 {
158 assert(f >= 0.0F);
159 return (int) (f + 0.5F);
160 }
161
162 /** Return (as an integer) floor of float */
163 static inline int IFLOOR(float f)
164 {
165 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
166 /*
167 * IEEE floor for computers that round to nearest or even.
168 * 'f' must be between -4194304 and 4194303.
169 * This floor operation is done by "(iround(f + .5) + iround(f - .5)) >> 1",
170 * but uses some IEEE specific tricks for better speed.
171 * Contributed by Josh Vanderhoof
172 */
173 int ai, bi;
174 double af, bf;
175 af = (3 << 22) + 0.5 + (double)f;
176 bf = (3 << 22) + 0.5 - (double)f;
177 /* GCC generates an extra fstp/fld without this. */
178 __asm__ ("fstps %0" : "=m" (ai) : "t" (af) : "st");
179 __asm__ ("fstps %0" : "=m" (bi) : "t" (bf) : "st");
180 return (ai - bi) >> 1;
181 #else
182 int ai, bi;
183 double af, bf;
184 fi_type u;
185 af = (3 << 22) + 0.5 + (double)f;
186 bf = (3 << 22) + 0.5 - (double)f;
187 u.f = (float) af; ai = u.i;
188 u.f = (float) bf; bi = u.i;
189 return (ai - bi) >> 1;
190 #endif
191 }
192
193
194 /**
195 * Is x a power of two?
196 */
197 static inline int
198 _mesa_is_pow_two(int x)
199 {
200 return !(x & (x - 1));
201 }
202
203 /**
204 * Round given integer to next higer power of two
205 * If X is zero result is undefined.
206 *
207 * Source for the fallback implementation is
208 * Sean Eron Anderson's webpage "Bit Twiddling Hacks"
209 * http://graphics.stanford.edu/~seander/bithacks.html
210 *
211 * When using builtin function have to do some work
212 * for case when passed values 1 to prevent hiting
213 * undefined result from __builtin_clz. Undefined
214 * results would be different depending on optimization
215 * level used for build.
216 */
217 static inline int32_t
218 _mesa_next_pow_two_32(uint32_t x)
219 {
220 #ifdef HAVE___BUILTIN_CLZ
221 uint32_t y = (x != 1);
222 return (1 + y) << ((__builtin_clz(x - y) ^ 31) );
223 #else
224 x--;
225 x |= x >> 1;
226 x |= x >> 2;
227 x |= x >> 4;
228 x |= x >> 8;
229 x |= x >> 16;
230 x++;
231 return x;
232 #endif
233 }
234
235 static inline int64_t
236 _mesa_next_pow_two_64(uint64_t x)
237 {
238 #ifdef HAVE___BUILTIN_CLZLL
239 uint64_t y = (x != 1);
240 STATIC_ASSERT(sizeof(x) == sizeof(long long));
241 return (1 + y) << ((__builtin_clzll(x - y) ^ 63));
242 #else
243 x--;
244 x |= x >> 1;
245 x |= x >> 2;
246 x |= x >> 4;
247 x |= x >> 8;
248 x |= x >> 16;
249 x |= x >> 32;
250 x++;
251 return x;
252 #endif
253 }
254
255
256 /*
257 * Returns the floor form of binary logarithm for a 32-bit integer.
258 */
259 static inline unsigned
260 _mesa_logbase2(unsigned n)
261 {
262 #ifdef HAVE___BUILTIN_CLZ
263 return (31 - __builtin_clz(n | 1));
264 #else
265 unsigned pos = 0;
266 if (n >= 1<<16) { n >>= 16; pos += 16; }
267 if (n >= 1<< 8) { n >>= 8; pos += 8; }
268 if (n >= 1<< 4) { n >>= 4; pos += 4; }
269 if (n >= 1<< 2) { n >>= 2; pos += 2; }
270 if (n >= 1<< 1) { pos += 1; }
271 return pos;
272 #endif
273 }
274
275
276 /**********************************************************************
277 * Functions
278 */
279
280 extern void *
281 _mesa_align_malloc( size_t bytes, unsigned long alignment );
282
283 extern void *
284 _mesa_align_calloc( size_t bytes, unsigned long alignment );
285
286 extern void
287 _mesa_align_free( void *ptr );
288
289 extern void *
290 _mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize,
291 unsigned long alignment);
292
293 extern int
294 _mesa_snprintf( char *str, size_t size, const char *fmt, ... ) PRINTFLIKE(3, 4);
295
296 extern int
297 _mesa_vsnprintf(char *str, size_t size, const char *fmt, va_list arg);
298
299
300 #if defined(_WIN32) && !defined(HAVE_STRTOK_R)
301 #define strtok_r strtok_s
302 #endif
303
304 #ifdef __cplusplus
305 }
306 #endif
307
308
309 #endif /* IMPORTS_H */