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
25 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
27 * Permission is hereby granted, free of charge, to any person obtaining a
28 * copy of this software and associated documentation files (the "Software"),
29 * to deal in the Software without restriction, including without limitation
30 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
31 * and/or sell copies of the Software, and to permit persons to whom the
32 * Software is furnished to do so, subject to the following conditions:
34 * The above copyright notice and this permission notice shall be included
35 * in all copies or substantial portions of the Software.
37 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
38 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
40 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
41 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
42 * CONNECTION WITH THE SOFTWARE OR THE USE OR 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.
173 _mesa_align_free(void *ptr
)
175 #if defined(HAVE_POSIX_MEMALIGN)
177 #elif defined(_WIN32) && defined(_MSC_VER)
180 void **cubbyHole
= (void **) ((char *) ptr
- sizeof(void *));
181 void *realAddr
= *cubbyHole
;
183 #endif /* defined(HAVE_POSIX_MEMALIGN) */
187 * Reallocate memory, with alignment.
190 _mesa_align_realloc(void *oldBuffer
, size_t oldSize
, size_t newSize
,
191 unsigned long alignment
)
193 #if defined(_WIN32) && defined(_MSC_VER)
195 return _aligned_realloc(oldBuffer
, newSize
, alignment
);
197 const size_t copySize
= (oldSize
< newSize
) ? oldSize
: newSize
;
198 void *newBuf
= _mesa_align_malloc(newSize
, alignment
);
199 if (newBuf
&& oldBuffer
&& copySize
> 0) {
200 memcpy(newBuf
, oldBuffer
, copySize
);
203 _mesa_align_free(oldBuffer
);
210 /** Reallocate memory */
212 _mesa_realloc(void *oldBuffer
, size_t oldSize
, size_t newSize
)
214 const size_t copySize
= (oldSize
< newSize
) ? oldSize
: newSize
;
215 void *newBuffer
= malloc(newSize
);
216 if (newBuffer
&& oldBuffer
&& copySize
> 0)
217 memcpy(newBuffer
, oldBuffer
, copySize
);
225 /**********************************************************************/
232 * Find the first bit set in a word.
237 register int bit
= 0;
239 if ((i
& 0xffff) == 0) {
243 if ((i
& 0xff) == 0) {
247 if ((i
& 0xf) == 0) {
251 while ((i
& 1) == 0) {
262 * Find position of first bit set in given value.
263 * XXX Warning: this function can only be used on 64-bit systems!
264 * \return position of least-significant bit set, starting at 1, return zero
268 ffsll(long long int val
)
272 assert(sizeof(val
) == 8);
274 bit
= ffs((int) val
);
278 bit
= ffs((int) (val
>> 32));
284 #endif /* __GNUC__ */
287 #if !defined(__GNUC__) ||\
288 ((__GNUC__ * 100 + __GNUC_MINOR__) < 304) /* Not gcc 3.4 or later */
290 * Return number of bits set in given GLuint.
293 _mesa_bitcount(unsigned int n
)
296 for (bits
= 0; n
> 0; n
= n
>> 1) {
303 * Return number of bits set in given 64-bit uint.
306 _mesa_bitcount_64(uint64_t n
)
309 for (bits
= 0; n
> 0; n
= n
>> 1) {
317 /* Using C99 rounding functions for roundToEven() implementation is
318 * difficult, because round(), rint, and nearbyint() are affected by
319 * fesetenv(), which the application may have done for its own
320 * purposes. Mesa's IROUND macro is close to what we want, but it
321 * rounds away from 0 on n + 0.5.
324 _mesa_round_to_even(float val
)
326 int rounded
= IROUND(val
);
328 if (val
- floor(val
) == 0.5) {
329 if (rounded
% 2 != 0)
330 rounded
+= val
> 0 ? -1 : 1;
338 * Convert a 4-byte float to a 2-byte half float.
339 * Based on code from:
340 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
343 _mesa_float_to_half(float val
)
345 const fi_type fi
= {val
};
346 const int flt_m
= fi
.i
& 0x7fffff;
347 const int flt_e
= (fi
.i
>> 23) & 0xff;
348 const int flt_s
= (fi
.i
>> 31) & 0x1;
355 /* handle special cases */
356 if ((flt_e
== 0) && (flt_m
== 0)) {
358 /* m = 0; - already set */
361 else if ((flt_e
== 0) && (flt_m
!= 0)) {
362 /* denorm -- denorm float maps to 0 half */
363 /* m = 0; - already set */
366 else if ((flt_e
== 0xff) && (flt_m
== 0)) {
368 /* m = 0; - already set */
371 else if ((flt_e
== 0xff) && (flt_m
!= 0)) {
378 const int new_exp
= flt_e
- 127;
381 /* m = 0; - already set */
384 else if (new_exp
< -14) {
385 /* this maps to a denorm */
386 unsigned int exp_val
= (unsigned int) (-14 - new_exp
); /* 2^-exp_val*/
391 "float_to_half: logical error in denorm creation!\n");
392 /* m = 0; - already set */
394 case 1: m
= 512 + (flt_m
>> 14); break;
395 case 2: m
= 256 + (flt_m
>> 15); break;
396 case 3: m
= 128 + (flt_m
>> 16); break;
397 case 4: m
= 64 + (flt_m
>> 17); break;
398 case 5: m
= 32 + (flt_m
>> 18); break;
399 case 6: m
= 16 + (flt_m
>> 19); break;
400 case 7: m
= 8 + (flt_m
>> 20); break;
401 case 8: m
= 4 + (flt_m
>> 21); break;
402 case 9: m
= 2 + (flt_m
>> 22); break;
403 case 10: m
= 1; break;
406 else if (new_exp
> 15) {
407 /* map this value to infinity */
408 /* m = 0; - already set */
418 result
= (s
<< 15) | (e
<< 10) | m
;
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
429 _mesa_half_to_float(GLhalfARB val
)
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
;
442 /* handle special cases */
443 if ((e
== 0) && (m
== 0)) {
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
;
455 else if ((e
== 31) && (m
== 0)) {
460 else if ((e
== 31) && (m
!= 0)) {
471 fi
.i
= (flt_s
<< 31) | (flt_e
<< 23) | flt_m
;
479 /**********************************************************************/
480 /** \name Sort & Search */
484 * Wrapper for bsearch().
487 _mesa_bsearch( const void *key
, const void *base
, size_t nmemb
, size_t size
,
488 int (*compar
)(const void *, const void *) )
490 #if defined(_WIN32_WCE)
495 mid
= (char *)base
+ nmemb
* size
;
496 cmp
= (*compar
)(key
, mid
);
500 base
= (char *)mid
+ size
;
506 return bsearch(key
, base
, nmemb
, size
, compar
);
513 /**********************************************************************/
514 /** \name Environment vars */
518 * Wrapper for getenv().
521 _mesa_getenv( const char *var
)
523 #if defined(_XBOX) || defined(_WIN32_WCE)
533 /**********************************************************************/
538 * Implemented using malloc() and strcpy.
539 * Note that NULL is handled accordingly.
542 _mesa_strdup( const char *s
)
545 size_t l
= strlen(s
);
546 char *s2
= malloc(l
+ 1);
556 /** Wrapper around strtof() */
558 _mesa_strtof( const char *s
, char **end
)
560 #if defined(_GNU_SOURCE) && !defined(__CYGWIN__) && !defined(__FreeBSD__) && \
561 !defined(ANDROID) && !defined(__HAIKU__) && !defined(__UCLIBC__)
562 static locale_t loc
= NULL
;
564 loc
= newlocale(LC_CTYPE_MASK
, "C", NULL
);
566 return strtof_l(s
, end
, loc
);
567 #elif defined(_ISOC99_SOURCE) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
568 return strtof(s
, end
);
570 return (float)strtod(s
, end
);
574 /** Compute simple checksum/hash for a string */
576 _mesa_str_checksum(const char *str
)
578 /* This could probably be much better */
582 for (c
= str
; *c
; c
++, i
++)
583 sum
+= *c
* (i
% 100);
591 /** Needed due to #ifdef's, above. */
593 _mesa_vsnprintf(char *str
, size_t size
, const char *fmt
, va_list args
)
595 return vsnprintf( str
, size
, fmt
, args
);
598 /** Wrapper around vsnprintf() */
600 _mesa_snprintf( char *str
, size_t size
, const char *fmt
, ... )
604 va_start( args
, fmt
);
605 r
= vsnprintf( str
, size
, fmt
, args
);