3 * Standard C library function wrappers.
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,
10 * Some drivers will want to override/replace this file with something
11 * specialized, but that'll be rare.
13 * Eventually, I want to move roll the glheader.h file into this.
15 * \todo Functions still needed:
22 * Mesa 3-D graphics library
24 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
33 * The above copyright notice and this permission notice shall be included
34 * in all copies or substantial portions of the Software.
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.
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);
66 /**********************************************************************/
71 * Allocate aligned memory.
73 * \param bytes number of bytes to allocate.
74 * \param alignment alignment (must be greater than zero).
76 * Allocates extra memory to accommodate rounding up the address for
77 * alignment and to record the real malloc address.
79 * \sa _mesa_align_free().
82 _mesa_align_malloc(size_t bytes, unsigned long alignment)
84 #if defined(HAVE_POSIX_MEMALIGN)
86 int err = posix_memalign(& mem, alignment, bytes);
90 #elif defined(_WIN32) && defined(_MSC_VER)
91 return _aligned_malloc(bytes, alignment);
95 ASSERT( alignment > 0 );
97 ptr = (uintptr_t)malloc(bytes + alignment + sizeof(void *));
101 buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1);
102 *(uintptr_t *)(buf - sizeof(void *)) = ptr;
105 /* mark the non-aligned area */
106 while ( ptr < buf - sizeof(void *) ) {
107 *(unsigned long *)ptr = 0xcdcdcdcd;
108 ptr += sizeof(unsigned long);
113 #endif /* defined(HAVE_POSIX_MEMALIGN) */
117 * Same as _mesa_align_malloc(), but using calloc(1, ) instead of
121 _mesa_align_calloc(size_t bytes, unsigned long alignment)
123 #if defined(HAVE_POSIX_MEMALIGN)
126 mem = _mesa_align_malloc(bytes, alignment);
128 (void) memset(mem, 0, bytes);
132 #elif defined(_WIN32) && defined(_MSC_VER)
135 mem = _aligned_malloc(bytes, alignment);
137 (void) memset(mem, 0, bytes);
144 ASSERT( alignment > 0 );
146 ptr = (uintptr_t)calloc(1, bytes + alignment + sizeof(void *));
150 buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1);
151 *(uintptr_t *)(buf - sizeof(void *)) = ptr;
154 /* mark the non-aligned area */
155 while ( ptr < buf - sizeof(void *) ) {
156 *(unsigned long *)ptr = 0xcdcdcdcd;
157 ptr += sizeof(unsigned long);
162 #endif /* defined(HAVE_POSIX_MEMALIGN) */
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.
175 _mesa_align_free(void *ptr)
177 #if defined(HAVE_POSIX_MEMALIGN)
179 #elif defined(_WIN32) && defined(_MSC_VER)
183 void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
184 void *realAddr = *cubbyHole;
187 #endif /* defined(HAVE_POSIX_MEMALIGN) */
191 * Reallocate memory, with alignment.
194 _mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize,
195 unsigned long alignment)
197 #if defined(_WIN32) && defined(_MSC_VER)
199 return _aligned_realloc(oldBuffer, newSize, alignment);
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);
207 _mesa_align_free(oldBuffer);
215 /**********************************************************************/
222 * Find the first bit set in a word.
227 register int bit = 0;
229 if ((i & 0xffff) == 0) {
233 if ((i & 0xff) == 0) {
237 if ((i & 0xf) == 0) {
241 while ((i & 1) == 0) {
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
258 ffsll(long long int val)
262 assert(sizeof(val) == 8);
264 bit = ffs((int) val);
268 bit = ffs((int) (val >> 32));
274 #endif /* __GNUC__ */
277 #if !defined(__GNUC__) ||\
278 ((__GNUC__ * 100 + __GNUC_MINOR__) < 304) /* Not gcc 3.4 or later */
280 * Return number of bits set in given GLuint.
283 _mesa_bitcount(unsigned int n)
286 for (bits = 0; n > 0; n = n >> 1) {
293 * Return number of bits set in given 64-bit uint.
296 _mesa_bitcount_64(uint64_t n)
299 for (bits = 0; n > 0; n = n >> 1) {
307 /* Using C99 rounding functions for roundToEven() implementation is
308 * difficult, because round(), rint, and nearbyint() are affected by
309 * fesetenv(), which the application may have done for its own
310 * purposes. Mesa's IROUND macro is close to what we want, but it
311 * rounds away from 0 on n + 0.5.
314 _mesa_round_to_even(float val)
316 int rounded = IROUND(val);
318 if (val - floor(val) == 0.5) {
319 if (rounded % 2 != 0)
320 rounded += val > 0 ? -1 : 1;
328 * Convert a 4-byte float to a 2-byte half float.
330 * Not all float32 values can be represented exactly as a float16 value. We
331 * round such intermediate float32 values to the nearest float16. When the
332 * float32 lies exactly between to float16 values, we round to the one with
335 * This rounding behavior has several benefits:
336 * - It has no sign bias.
338 * - It reproduces the behavior of real hardware: opcode F32TO16 in Intel's
341 * - By reproducing the behavior of the GPU (at least on Intel hardware),
342 * compile-time evaluation of constant packHalf2x16 GLSL expressions will
343 * result in the same value as if the expression were executed on the GPU.
346 _mesa_float_to_half(float val)
348 const fi_type fi = {val};
349 const int flt_m = fi.i & 0x7fffff;
350 const int flt_e = (fi.i >> 23) & 0xff;
351 const int flt_s = (fi.i >> 31) & 0x1;
358 /* handle special cases */
359 if ((flt_e == 0) && (flt_m == 0)) {
361 /* m = 0; - already set */
364 else if ((flt_e == 0) && (flt_m != 0)) {
365 /* denorm -- denorm float maps to 0 half */
366 /* m = 0; - already set */
369 else if ((flt_e == 0xff) && (flt_m == 0)) {
371 /* m = 0; - already set */
374 else if ((flt_e == 0xff) && (flt_m != 0)) {
381 const int new_exp = flt_e - 127;
383 /* The float32 lies in the range (0.0, min_normal16) and is rounded
384 * to a nearby float16 value. The result will be either zero, subnormal,
388 m = _mesa_round_to_even((1 << 24) * fabsf(fi.f));
390 else if (new_exp > 15) {
391 /* map this value to infinity */
392 /* m = 0; - already set */
396 /* The float32 lies in the range
397 * [min_normal16, max_normal16 + max_step16)
398 * and is rounded to a nearby float16 value. The result will be
399 * either normal or infinite.
402 m = _mesa_round_to_even(flt_m / (float) (1 << 13));
406 assert(0 <= m && m <= 1024);
408 /* The float32 was rounded upwards into the range of the next exponent,
409 * so bump the exponent. This correctly handles the case where f32
410 * should be rounded up to float16 infinity.
416 result = (s << 15) | (e << 10) | m;
422 * Convert a 2-byte half float to a 4-byte float.
423 * Based on code from:
424 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
427 _mesa_half_to_float(GLhalfARB val)
429 /* XXX could also use a 64K-entry lookup table */
430 const int m = val & 0x3ff;
431 const int e = (val >> 10) & 0x1f;
432 const int s = (val >> 15) & 0x1;
433 int flt_m, flt_e, flt_s;
440 /* handle special cases */
441 if ((e == 0) && (m == 0)) {
446 else if ((e == 0) && (m != 0)) {
447 /* denorm -- denorm half will fit in non-denorm single */
448 const float half_denorm = 1.0f / 16384.0f; /* 2^-14 */
449 float mantissa = ((float) (m)) / 1024.0f;
450 float sign = s ? -1.0f : 1.0f;
451 return sign * mantissa * half_denorm;
453 else if ((e == 31) && (m == 0)) {
458 else if ((e == 31) && (m != 0)) {
469 fi.i = (flt_s << 31) | (flt_e << 23) | flt_m;
477 /**********************************************************************/
478 /** \name Sort & Search */
482 * Wrapper for bsearch().
485 _mesa_bsearch( const void *key, const void *base, size_t nmemb, size_t size,
486 int (*compar)(const void *, const void *) )
488 return bsearch(key, base, nmemb, size, compar);
494 /**********************************************************************/
495 /** \name Environment vars */
499 * Wrapper for getenv().
502 _mesa_getenv( const char *var )
510 /**********************************************************************/
515 * Implemented using malloc() and strcpy.
516 * Note that NULL is handled accordingly.
519 _mesa_strdup( const char *s )
522 size_t l = strlen(s);
523 char *s2 = malloc(l + 1);
533 /** Wrapper around strtof() */
535 _mesa_strtof( const char *s, char **end )
537 #if defined(_GNU_SOURCE) && !defined(__CYGWIN__) && !defined(__FreeBSD__) && \
538 !defined(ANDROID) && !defined(__HAIKU__) && !defined(__UCLIBC__) && \
540 static locale_t loc = NULL;
542 loc = newlocale(LC_CTYPE_MASK, "C", NULL);
544 return strtof_l(s, end, loc);
545 #elif defined(_ISOC99_SOURCE) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
546 return strtof(s, end);
548 return (float)strtod(s, end);
552 /** Compute simple checksum/hash for a string */
554 _mesa_str_checksum(const char *str)
556 /* This could probably be much better */
560 for (c = str; *c; c++, i++)
561 sum += *c * (i % 100);
569 /** Needed due to #ifdef's, above. */
571 _mesa_vsnprintf(char *str, size_t size, const char *fmt, va_list args)
573 return vsnprintf( str, size, fmt, args);
576 /** Wrapper around vsnprintf() */
578 _mesa_snprintf( char *str, size_t size, const char *fmt, ... )
582 va_start( args, fmt );
583 r = vsnprintf( str, size, fmt, args );