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__) || ( defined(__VMS) && __CRTL_VER < 70312000 )
63 extern int vsnprintf(char *str
, size_t count
, const char *fmt
, va_list arg
);
65 #include "vsnprintf.c"
69 /**********************************************************************/
74 * Allocate aligned memory.
76 * \param bytes number of bytes to allocate.
77 * \param alignment alignment (must be greater than zero).
79 * Allocates extra memory to accommodate rounding up the address for
80 * alignment and to record the real malloc address.
82 * \sa _mesa_align_free().
85 _mesa_align_malloc(size_t bytes
, unsigned long alignment
)
87 #if defined(HAVE_POSIX_MEMALIGN)
89 int err
= posix_memalign(& mem
, alignment
, bytes
);
93 #elif defined(_WIN32) && defined(_MSC_VER)
94 return _aligned_malloc(bytes
, alignment
);
98 ASSERT( alignment
> 0 );
100 ptr
= malloc(bytes
+ alignment
+ sizeof(void *));
104 buf
= (ptr
+ alignment
+ sizeof(void *)) & ~(uintptr_t)(alignment
- 1);
105 *(uintptr_t *)(buf
- sizeof(void *)) = ptr
;
108 /* mark the non-aligned area */
109 while ( ptr
< buf
- sizeof(void *) ) {
110 *(unsigned long *)ptr
= 0xcdcdcdcd;
111 ptr
+= sizeof(unsigned long);
116 #endif /* defined(HAVE_POSIX_MEMALIGN) */
120 * Same as _mesa_align_malloc(), but using calloc(1, ) instead of
124 _mesa_align_calloc(size_t bytes
, unsigned long alignment
)
126 #if defined(HAVE_POSIX_MEMALIGN)
129 mem
= _mesa_align_malloc(bytes
, alignment
);
131 (void) memset(mem
, 0, bytes
);
135 #elif defined(_WIN32) && defined(_MSC_VER)
138 mem
= _aligned_malloc(bytes
, alignment
);
140 (void) memset(mem
, 0, bytes
);
147 ASSERT( alignment
> 0 );
149 ptr
= calloc(1, bytes
+ alignment
+ sizeof(void *));
153 buf
= (ptr
+ alignment
+ sizeof(void *)) & ~(uintptr_t)(alignment
- 1);
154 *(uintptr_t *)(buf
- sizeof(void *)) = ptr
;
157 /* mark the non-aligned area */
158 while ( ptr
< buf
- sizeof(void *) ) {
159 *(unsigned long *)ptr
= 0xcdcdcdcd;
160 ptr
+= sizeof(unsigned long);
165 #endif /* defined(HAVE_POSIX_MEMALIGN) */
169 * Free memory which was allocated with either _mesa_align_malloc()
170 * or _mesa_align_calloc().
171 * \param ptr pointer to the memory to be freed.
172 * The actual address to free is stored in the word immediately before the
173 * address the client sees.
176 _mesa_align_free(void *ptr
)
178 #if defined(HAVE_POSIX_MEMALIGN)
180 #elif defined(_WIN32) && defined(_MSC_VER)
183 void **cubbyHole
= (void **) ((char *) ptr
- sizeof(void *));
184 void *realAddr
= *cubbyHole
;
186 #endif /* defined(HAVE_POSIX_MEMALIGN) */
190 * Reallocate memory, with alignment.
193 _mesa_align_realloc(void *oldBuffer
, size_t oldSize
, size_t newSize
,
194 unsigned long alignment
)
196 #if defined(_WIN32) && defined(_MSC_VER)
198 return _aligned_realloc(oldBuffer
, newSize
, alignment
);
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
);
206 _mesa_align_free(oldBuffer
);
213 /** Reallocate memory */
215 _mesa_realloc(void *oldBuffer
, size_t oldSize
, size_t newSize
)
217 const size_t copySize
= (oldSize
< newSize
) ? oldSize
: newSize
;
218 void *newBuffer
= malloc(newSize
);
219 if (newBuffer
&& oldBuffer
&& copySize
> 0)
220 memcpy(newBuffer
, oldBuffer
, copySize
);
229 /**********************************************************************/
236 * Find the first bit set in a word.
241 register int bit
= 0;
243 if ((i
& 0xffff) == 0) {
247 if ((i
& 0xff) == 0) {
251 if ((i
& 0xf) == 0) {
255 while ((i
& 1) == 0) {
266 * Find position of first bit set in given value.
267 * XXX Warning: this function can only be used on 64-bit systems!
268 * \return position of least-significant bit set, starting at 1, return zero
272 ffsll(long long int val
)
276 assert(sizeof(val
) == 8);
278 bit
= ffs((int) val
);
282 bit
= ffs((int) (val
>> 32));
288 #endif /* __GNUC__ */
291 #if !defined(__GNUC__) ||\
292 ((__GNUC__ * 100 + __GNUC_MINOR__) < 304) /* Not gcc 3.4 or later */
294 * Return number of bits set in given GLuint.
297 _mesa_bitcount(unsigned int n
)
300 for (bits
= 0; n
> 0; n
= n
>> 1) {
307 * Return number of bits set in given 64-bit uint.
310 _mesa_bitcount_64(uint64_t n
)
313 for (bits
= 0; n
> 0; n
= n
>> 1) {
322 * Convert a 4-byte float to a 2-byte half float.
323 * Based on code from:
324 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
327 _mesa_float_to_half(float val
)
329 const fi_type fi
= {val
};
330 const int flt_m
= fi
.i
& 0x7fffff;
331 const int flt_e
= (fi
.i
>> 23) & 0xff;
332 const int flt_s
= (fi
.i
>> 31) & 0x1;
339 /* handle special cases */
340 if ((flt_e
== 0) && (flt_m
== 0)) {
342 /* m = 0; - already set */
345 else if ((flt_e
== 0) && (flt_m
!= 0)) {
346 /* denorm -- denorm float maps to 0 half */
347 /* m = 0; - already set */
350 else if ((flt_e
== 0xff) && (flt_m
== 0)) {
352 /* m = 0; - already set */
355 else if ((flt_e
== 0xff) && (flt_m
!= 0)) {
362 const int new_exp
= flt_e
- 127;
365 /* m = 0; - already set */
368 else if (new_exp
< -14) {
369 /* this maps to a denorm */
370 unsigned int exp_val
= (unsigned int) (-14 - new_exp
); /* 2^-exp_val*/
375 "float_to_half: logical error in denorm creation!\n");
376 /* m = 0; - already set */
378 case 1: m
= 512 + (flt_m
>> 14); break;
379 case 2: m
= 256 + (flt_m
>> 15); break;
380 case 3: m
= 128 + (flt_m
>> 16); break;
381 case 4: m
= 64 + (flt_m
>> 17); break;
382 case 5: m
= 32 + (flt_m
>> 18); break;
383 case 6: m
= 16 + (flt_m
>> 19); break;
384 case 7: m
= 8 + (flt_m
>> 20); break;
385 case 8: m
= 4 + (flt_m
>> 21); break;
386 case 9: m
= 2 + (flt_m
>> 22); break;
387 case 10: m
= 1; break;
390 else if (new_exp
> 15) {
391 /* map this value to infinity */
392 /* m = 0; - already set */
402 result
= (s
<< 15) | (e
<< 10) | m
;
408 * Convert a 2-byte half float to a 4-byte float.
409 * Based on code from:
410 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
413 _mesa_half_to_float(GLhalfARB val
)
415 /* XXX could also use a 64K-entry lookup table */
416 const int m
= val
& 0x3ff;
417 const int e
= (val
>> 10) & 0x1f;
418 const int s
= (val
>> 15) & 0x1;
419 int flt_m
, flt_e
, flt_s
;
426 /* handle special cases */
427 if ((e
== 0) && (m
== 0)) {
432 else if ((e
== 0) && (m
!= 0)) {
433 /* denorm -- denorm half will fit in non-denorm single */
434 const float half_denorm
= 1.0f
/ 16384.0f
; /* 2^-14 */
435 float mantissa
= ((float) (m
)) / 1024.0f
;
436 float sign
= s
? -1.0f
: 1.0f
;
437 return sign
* mantissa
* half_denorm
;
439 else if ((e
== 31) && (m
== 0)) {
444 else if ((e
== 31) && (m
!= 0)) {
455 fi
.i
= (flt_s
<< 31) | (flt_e
<< 23) | flt_m
;
463 /**********************************************************************/
464 /** \name Sort & Search */
468 * Wrapper for bsearch().
471 _mesa_bsearch( const void *key
, const void *base
, size_t nmemb
, size_t size
,
472 int (*compar
)(const void *, const void *) )
474 #if defined(_WIN32_WCE)
479 mid
= (char *)base
+ nmemb
* size
;
480 cmp
= (*compar
)(key
, mid
);
484 base
= (char *)mid
+ size
;
490 return bsearch(key
, base
, nmemb
, size
, compar
);
497 /**********************************************************************/
498 /** \name Environment vars */
502 * Wrapper for getenv().
505 _mesa_getenv( const char *var
)
507 #if defined(_XBOX) || defined(_WIN32_WCE)
517 /**********************************************************************/
522 * Implemented using malloc() and strcpy.
523 * Note that NULL is handled accordingly.
526 _mesa_strdup( const char *s
)
529 size_t l
= strlen(s
);
530 char *s2
= malloc(l
+ 1);
540 /** Wrapper around strtof() */
542 _mesa_strtof( const char *s
, char **end
)
544 #if defined(_GNU_SOURCE) && !defined(__CYGWIN__) && !defined(__FreeBSD__) && \
545 !defined(ANDROID) && !defined(__HAIKU__) && !defined(__UCLIBC__)
546 static locale_t loc
= NULL
;
548 loc
= newlocale(LC_CTYPE_MASK
, "C", NULL
);
550 return strtof_l(s
, end
, loc
);
551 #elif defined(_ISOC99_SOURCE) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
552 return strtof(s
, end
);
554 return (float)strtod(s
, end
);
558 /** Compute simple checksum/hash for a string */
560 _mesa_str_checksum(const char *str
)
562 /* This could probably be much better */
566 for (c
= str
; *c
; c
++, i
++)
567 sum
+= *c
* (i
% 100);
575 /** Needed due to #ifdef's, above. */
577 _mesa_vsnprintf(char *str
, size_t size
, const char *fmt
, va_list args
)
579 return vsnprintf( str
, size
, fmt
, args
);
582 /** Wrapper around vsnprintf() */
584 _mesa_snprintf( char *str
, size_t size
, const char *fmt
, ... )
588 va_start( args
, fmt
);
589 r
= vsnprintf( str
, size
, fmt
, args
);