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