mesa: don't include math.h in compiler.h
[mesa.git] / src / mesa / main / imports.c
1 /**
2 * \file imports.c
3 * Standard C library function wrappers.
4 *
5 * Imports are services which the device driver or window system or
6 * operating system provides to the core renderer. The core renderer (Mesa)
7 * will call these functions in order to do memory allocation, simple I/O,
8 * etc.
9 *
10 * Some drivers will want to override/replace this file with something
11 * specialized, but that'll be rare.
12 *
13 * Eventually, I want to move roll the glheader.h file into this.
14 *
15 * \todo Functions still needed:
16 * - scanf
17 * - qsort
18 * - rand and RAND_MAX
19 */
20
21 /*
22 * Mesa 3-D graphics library
23 *
24 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
25 *
26 * Permission is hereby granted, free of charge, to any person obtaining a
27 * copy of this software and associated documentation files (the "Software"),
28 * to deal in the Software without restriction, including without limitation
29 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
30 * and/or sell copies of the Software, and to permit persons to whom the
31 * Software is furnished to do so, subject to the following conditions:
32 *
33 * The above copyright notice and this permission notice shall be included
34 * in all copies or substantial portions of the Software.
35 *
36 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
37 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
39 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
40 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
41 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
42 * OTHER DEALINGS IN THE SOFTWARE.
43 */
44
45 #include <stdarg.h>
46 #include "c99_math.h"
47 #include "imports.h"
48 #include "context.h"
49 #include "mtypes.h"
50 #include "version.h"
51
52 #ifdef _GNU_SOURCE
53 #include <locale.h>
54 #ifdef __APPLE__
55 #include <xlocale.h>
56 #endif
57 #endif
58
59
60 #ifdef _WIN32
61 #define vsnprintf _vsnprintf
62 #elif defined(__IBMC__) || defined(__IBMCPP__)
63 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list arg);
64 #endif
65
66 /**********************************************************************/
67 /** \name Memory */
68 /*@{*/
69
70 /**
71 * Allocate aligned memory.
72 *
73 * \param bytes number of bytes to allocate.
74 * \param alignment alignment (must be greater than zero).
75 *
76 * Allocates extra memory to accommodate rounding up the address for
77 * alignment and to record the real malloc address.
78 *
79 * \sa _mesa_align_free().
80 */
81 void *
82 _mesa_align_malloc(size_t bytes, unsigned long alignment)
83 {
84 #if defined(HAVE_POSIX_MEMALIGN)
85 void *mem;
86 int err = posix_memalign(& mem, alignment, bytes);
87 if (err)
88 return NULL;
89 return mem;
90 #elif defined(_WIN32) && defined(_MSC_VER)
91 return _aligned_malloc(bytes, alignment);
92 #else
93 uintptr_t ptr, buf;
94
95 assert( alignment > 0 );
96
97 ptr = (uintptr_t)malloc(bytes + alignment + sizeof(void *));
98 if (!ptr)
99 return NULL;
100
101 buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1);
102 *(uintptr_t *)(buf - sizeof(void *)) = ptr;
103
104 #ifdef DEBUG
105 /* mark the non-aligned area */
106 while ( ptr < buf - sizeof(void *) ) {
107 *(unsigned long *)ptr = 0xcdcdcdcd;
108 ptr += sizeof(unsigned long);
109 }
110 #endif
111
112 return (void *) buf;
113 #endif /* defined(HAVE_POSIX_MEMALIGN) */
114 }
115
116 /**
117 * Same as _mesa_align_malloc(), but using calloc(1, ) instead of
118 * malloc()
119 */
120 void *
121 _mesa_align_calloc(size_t bytes, unsigned long alignment)
122 {
123 #if defined(HAVE_POSIX_MEMALIGN)
124 void *mem;
125
126 mem = _mesa_align_malloc(bytes, alignment);
127 if (mem != NULL) {
128 (void) memset(mem, 0, bytes);
129 }
130
131 return mem;
132 #elif defined(_WIN32) && defined(_MSC_VER)
133 void *mem;
134
135 mem = _aligned_malloc(bytes, alignment);
136 if (mem != NULL) {
137 (void) memset(mem, 0, bytes);
138 }
139
140 return mem;
141 #else
142 uintptr_t ptr, buf;
143
144 assert( alignment > 0 );
145
146 ptr = (uintptr_t)calloc(1, bytes + alignment + sizeof(void *));
147 if (!ptr)
148 return NULL;
149
150 buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1);
151 *(uintptr_t *)(buf - sizeof(void *)) = ptr;
152
153 #ifdef DEBUG
154 /* mark the non-aligned area */
155 while ( ptr < buf - sizeof(void *) ) {
156 *(unsigned long *)ptr = 0xcdcdcdcd;
157 ptr += sizeof(unsigned long);
158 }
159 #endif
160
161 return (void *)buf;
162 #endif /* defined(HAVE_POSIX_MEMALIGN) */
163 }
164
165 /**
166 * Free memory which was allocated with either _mesa_align_malloc()
167 * or _mesa_align_calloc().
168 * \param ptr pointer to the memory to be freed.
169 * The actual address to free is stored in the word immediately before the
170 * address the client sees.
171 * Note that it is legal to pass NULL pointer to this function and will be
172 * handled accordingly.
173 */
174 void
175 _mesa_align_free(void *ptr)
176 {
177 #if defined(HAVE_POSIX_MEMALIGN)
178 free(ptr);
179 #elif defined(_WIN32) && defined(_MSC_VER)
180 _aligned_free(ptr);
181 #else
182 if (ptr) {
183 void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
184 void *realAddr = *cubbyHole;
185 free(realAddr);
186 }
187 #endif /* defined(HAVE_POSIX_MEMALIGN) */
188 }
189
190 /**
191 * Reallocate memory, with alignment.
192 */
193 void *
194 _mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize,
195 unsigned long alignment)
196 {
197 #if defined(_WIN32) && defined(_MSC_VER)
198 (void) oldSize;
199 return _aligned_realloc(oldBuffer, newSize, alignment);
200 #else
201 const size_t copySize = (oldSize < newSize) ? oldSize : newSize;
202 void *newBuf = _mesa_align_malloc(newSize, alignment);
203 if (newBuf && oldBuffer && copySize > 0) {
204 memcpy(newBuf, oldBuffer, copySize);
205 }
206
207 _mesa_align_free(oldBuffer);
208 return newBuf;
209 #endif
210 }
211
212 /*@}*/
213
214
215 /**********************************************************************/
216 /** \name Math */
217 /*@{*/
218
219
220 #ifndef HAVE___BUILTIN_FFS
221 /**
222 * Find the first bit set in a word.
223 */
224 int
225 ffs(int i)
226 {
227 register int bit = 0;
228 if (i != 0) {
229 if ((i & 0xffff) == 0) {
230 bit += 16;
231 i >>= 16;
232 }
233 if ((i & 0xff) == 0) {
234 bit += 8;
235 i >>= 8;
236 }
237 if ((i & 0xf) == 0) {
238 bit += 4;
239 i >>= 4;
240 }
241 while ((i & 1) == 0) {
242 bit++;
243 i >>= 1;
244 }
245 bit++;
246 }
247 return bit;
248 }
249 #endif
250
251 #ifndef HAVE___BUILTIN_FFSLL
252 /**
253 * Find position of first bit set in given value.
254 * XXX Warning: this function can only be used on 64-bit systems!
255 * \return position of least-significant bit set, starting at 1, return zero
256 * if no bits set.
257 */
258 int
259 ffsll(long long int val)
260 {
261 int bit;
262
263 assert(sizeof(val) == 8);
264
265 bit = ffs((int) val);
266 if (bit != 0)
267 return bit;
268
269 bit = ffs((int) (val >> 32));
270 if (bit != 0)
271 return 32 + bit;
272
273 return 0;
274 }
275 #endif
276
277
278 #ifndef HAVE___BUILTIN_POPCOUNT
279 /**
280 * Return number of bits set in given GLuint.
281 */
282 unsigned int
283 _mesa_bitcount(unsigned int n)
284 {
285 unsigned int bits;
286 for (bits = 0; n > 0; n = n >> 1) {
287 bits += (n & 1);
288 }
289 return bits;
290 }
291 #endif
292
293 #ifndef HAVE___BUILTIN_POPCOUNTLL
294 /**
295 * Return number of bits set in given 64-bit uint.
296 */
297 unsigned int
298 _mesa_bitcount_64(uint64_t n)
299 {
300 unsigned int bits;
301 for (bits = 0; n > 0; n = n >> 1) {
302 bits += (n & 1);
303 }
304 return bits;
305 }
306 #endif
307
308
309 /* Using C99 rounding functions for roundToEven() implementation is
310 * difficult, because round(), rint, and nearbyint() are affected by
311 * fesetenv(), which the application may have done for its own
312 * purposes. Mesa's IROUND macro is close to what we want, but it
313 * rounds away from 0 on n + 0.5.
314 */
315 int
316 _mesa_round_to_even(float val)
317 {
318 int rounded = IROUND(val);
319
320 if (val - floor(val) == 0.5) {
321 if (rounded % 2 != 0)
322 rounded += val > 0 ? -1 : 1;
323 }
324
325 return rounded;
326 }
327
328
329 /**
330 * Convert a 4-byte float to a 2-byte half float.
331 *
332 * Not all float32 values can be represented exactly as a float16 value. We
333 * round such intermediate float32 values to the nearest float16. When the
334 * float32 lies exactly between to float16 values, we round to the one with
335 * an even mantissa.
336 *
337 * This rounding behavior has several benefits:
338 * - It has no sign bias.
339 *
340 * - It reproduces the behavior of real hardware: opcode F32TO16 in Intel's
341 * GPU ISA.
342 *
343 * - By reproducing the behavior of the GPU (at least on Intel hardware),
344 * compile-time evaluation of constant packHalf2x16 GLSL expressions will
345 * result in the same value as if the expression were executed on the GPU.
346 */
347 GLhalfARB
348 _mesa_float_to_half(float val)
349 {
350 const fi_type fi = {val};
351 const int flt_m = fi.i & 0x7fffff;
352 const int flt_e = (fi.i >> 23) & 0xff;
353 const int flt_s = (fi.i >> 31) & 0x1;
354 int s, e, m = 0;
355 GLhalfARB result;
356
357 /* sign bit */
358 s = flt_s;
359
360 /* handle special cases */
361 if ((flt_e == 0) && (flt_m == 0)) {
362 /* zero */
363 /* m = 0; - already set */
364 e = 0;
365 }
366 else if ((flt_e == 0) && (flt_m != 0)) {
367 /* denorm -- denorm float maps to 0 half */
368 /* m = 0; - already set */
369 e = 0;
370 }
371 else if ((flt_e == 0xff) && (flt_m == 0)) {
372 /* infinity */
373 /* m = 0; - already set */
374 e = 31;
375 }
376 else if ((flt_e == 0xff) && (flt_m != 0)) {
377 /* NaN */
378 m = 1;
379 e = 31;
380 }
381 else {
382 /* regular number */
383 const int new_exp = flt_e - 127;
384 if (new_exp < -14) {
385 /* The float32 lies in the range (0.0, min_normal16) and is rounded
386 * to a nearby float16 value. The result will be either zero, subnormal,
387 * or normal.
388 */
389 e = 0;
390 m = _mesa_round_to_even((1 << 24) * fabsf(fi.f));
391 }
392 else if (new_exp > 15) {
393 /* map this value to infinity */
394 /* m = 0; - already set */
395 e = 31;
396 }
397 else {
398 /* The float32 lies in the range
399 * [min_normal16, max_normal16 + max_step16)
400 * and is rounded to a nearby float16 value. The result will be
401 * either normal or infinite.
402 */
403 e = new_exp + 15;
404 m = _mesa_round_to_even(flt_m / (float) (1 << 13));
405 }
406 }
407
408 assert(0 <= m && m <= 1024);
409 if (m == 1024) {
410 /* The float32 was rounded upwards into the range of the next exponent,
411 * so bump the exponent. This correctly handles the case where f32
412 * should be rounded up to float16 infinity.
413 */
414 ++e;
415 m = 0;
416 }
417
418 result = (s << 15) | (e << 10) | m;
419 return result;
420 }
421
422
423 /**
424 * Convert a 2-byte half float to a 4-byte float.
425 * Based on code from:
426 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
427 */
428 float
429 _mesa_half_to_float(GLhalfARB val)
430 {
431 /* XXX could also use a 64K-entry lookup table */
432 const int m = val & 0x3ff;
433 const int e = (val >> 10) & 0x1f;
434 const int s = (val >> 15) & 0x1;
435 int flt_m, flt_e, flt_s;
436 fi_type fi;
437 float result;
438
439 /* sign bit */
440 flt_s = s;
441
442 /* handle special cases */
443 if ((e == 0) && (m == 0)) {
444 /* zero */
445 flt_m = 0;
446 flt_e = 0;
447 }
448 else if ((e == 0) && (m != 0)) {
449 /* denorm -- denorm half will fit in non-denorm single */
450 const float half_denorm = 1.0f / 16384.0f; /* 2^-14 */
451 float mantissa = ((float) (m)) / 1024.0f;
452 float sign = s ? -1.0f : 1.0f;
453 return sign * mantissa * half_denorm;
454 }
455 else if ((e == 31) && (m == 0)) {
456 /* infinity */
457 flt_e = 0xff;
458 flt_m = 0;
459 }
460 else if ((e == 31) && (m != 0)) {
461 /* NaN */
462 flt_e = 0xff;
463 flt_m = 1;
464 }
465 else {
466 /* regular */
467 flt_e = e + 112;
468 flt_m = m << 13;
469 }
470
471 fi.i = (flt_s << 31) | (flt_e << 23) | flt_m;
472 result = fi.f;
473 return result;
474 }
475
476 /*@}*/
477
478
479 /**********************************************************************/
480 /** \name String */
481 /*@{*/
482
483 /**
484 * Implemented using malloc() and strcpy.
485 * Note that NULL is handled accordingly.
486 */
487 char *
488 _mesa_strdup( const char *s )
489 {
490 if (s) {
491 size_t l = strlen(s);
492 char *s2 = malloc(l + 1);
493 if (s2)
494 strcpy(s2, s);
495 return s2;
496 }
497 else {
498 return NULL;
499 }
500 }
501
502 /** Compute simple checksum/hash for a string */
503 unsigned int
504 _mesa_str_checksum(const char *str)
505 {
506 /* This could probably be much better */
507 unsigned int sum, i;
508 const char *c;
509 sum = i = 1;
510 for (c = str; *c; c++, i++)
511 sum += *c * (i % 100);
512 return sum + i;
513 }
514
515
516 /*@}*/
517
518
519 /** Needed due to #ifdef's, above. */
520 int
521 _mesa_vsnprintf(char *str, size_t size, const char *fmt, va_list args)
522 {
523 return vsnprintf( str, size, fmt, args);
524 }
525
526 /** Wrapper around vsnprintf() */
527 int
528 _mesa_snprintf( char *str, size_t size, const char *fmt, ... )
529 {
530 int r;
531 va_list args;
532 va_start( args, fmt );
533 r = vsnprintf( str, size, fmt, args );
534 va_end( args );
535 return r;
536 }
537
538