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