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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
41 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
42 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
43 * OTHER DEALINGS IN THE SOFTWARE.
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
);
67 /**********************************************************************/
72 * Allocate aligned memory.
74 * \param bytes number of bytes to allocate.
75 * \param alignment alignment (must be greater than zero).
77 * Allocates extra memory to accommodate rounding up the address for
78 * alignment and to record the real malloc address.
80 * \sa _mesa_align_free().
83 _mesa_align_malloc(size_t bytes
, unsigned long alignment
)
85 #if defined(HAVE_POSIX_MEMALIGN)
87 int err
= posix_memalign(& mem
, alignment
, bytes
);
91 #elif defined(_WIN32) && defined(_MSC_VER)
92 return _aligned_malloc(bytes
, alignment
);
96 ASSERT( alignment
> 0 );
98 ptr
= (uintptr_t)malloc(bytes
+ alignment
+ sizeof(void *));
102 buf
= (ptr
+ alignment
+ sizeof(void *)) & ~(uintptr_t)(alignment
- 1);
103 *(uintptr_t *)(buf
- sizeof(void *)) = ptr
;
106 /* mark the non-aligned area */
107 while ( ptr
< buf
- sizeof(void *) ) {
108 *(unsigned long *)ptr
= 0xcdcdcdcd;
109 ptr
+= sizeof(unsigned long);
114 #endif /* defined(HAVE_POSIX_MEMALIGN) */
118 * Same as _mesa_align_malloc(), but using calloc(1, ) instead of
122 _mesa_align_calloc(size_t bytes
, unsigned long alignment
)
124 #if defined(HAVE_POSIX_MEMALIGN)
127 mem
= _mesa_align_malloc(bytes
, alignment
);
129 (void) memset(mem
, 0, bytes
);
133 #elif defined(_WIN32) && defined(_MSC_VER)
136 mem
= _aligned_malloc(bytes
, alignment
);
138 (void) memset(mem
, 0, bytes
);
145 ASSERT( alignment
> 0 );
147 ptr
= (uintptr_t)calloc(1, bytes
+ alignment
+ sizeof(void *));
151 buf
= (ptr
+ alignment
+ sizeof(void *)) & ~(uintptr_t)(alignment
- 1);
152 *(uintptr_t *)(buf
- sizeof(void *)) = ptr
;
155 /* mark the non-aligned area */
156 while ( ptr
< buf
- sizeof(void *) ) {
157 *(unsigned long *)ptr
= 0xcdcdcdcd;
158 ptr
+= sizeof(unsigned long);
163 #endif /* defined(HAVE_POSIX_MEMALIGN) */
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.
174 _mesa_align_free(void *ptr
)
176 #if defined(HAVE_POSIX_MEMALIGN)
178 #elif defined(_WIN32) && defined(_MSC_VER)
181 void **cubbyHole
= (void **) ((char *) ptr
- sizeof(void *));
182 void *realAddr
= *cubbyHole
;
184 #endif /* defined(HAVE_POSIX_MEMALIGN) */
188 * Reallocate memory, with alignment.
191 _mesa_align_realloc(void *oldBuffer
, size_t oldSize
, size_t newSize
,
192 unsigned long alignment
)
194 #if defined(_WIN32) && defined(_MSC_VER)
196 return _aligned_realloc(oldBuffer
, newSize
, alignment
);
198 const size_t copySize
= (oldSize
< newSize
) ? oldSize
: newSize
;
199 void *newBuf
= _mesa_align_malloc(newSize
, alignment
);
200 if (newBuf
&& oldBuffer
&& copySize
> 0) {
201 memcpy(newBuf
, oldBuffer
, copySize
);
204 _mesa_align_free(oldBuffer
);
211 /** Reallocate memory */
213 _mesa_realloc(void *oldBuffer
, size_t oldSize
, size_t newSize
)
215 const size_t copySize
= (oldSize
< newSize
) ? oldSize
: newSize
;
216 void *newBuffer
= malloc(newSize
);
217 if (newBuffer
&& oldBuffer
&& copySize
> 0)
218 memcpy(newBuffer
, oldBuffer
, copySize
);
226 /**********************************************************************/
233 * Find the first bit set in a word.
238 register int bit
= 0;
240 if ((i
& 0xffff) == 0) {
244 if ((i
& 0xff) == 0) {
248 if ((i
& 0xf) == 0) {
252 while ((i
& 1) == 0) {
263 * Find position of first bit set in given value.
264 * XXX Warning: this function can only be used on 64-bit systems!
265 * \return position of least-significant bit set, starting at 1, return zero
269 ffsll(long long int val
)
273 assert(sizeof(val
) == 8);
275 bit
= ffs((int) val
);
279 bit
= ffs((int) (val
>> 32));
285 #endif /* __GNUC__ */
288 #if !defined(__GNUC__) ||\
289 ((__GNUC__ * 100 + __GNUC_MINOR__) < 304) /* Not gcc 3.4 or later */
291 * Return number of bits set in given GLuint.
294 _mesa_bitcount(unsigned int n
)
297 for (bits
= 0; n
> 0; n
= n
>> 1) {
304 * Return number of bits set in given 64-bit uint.
307 _mesa_bitcount_64(uint64_t n
)
310 for (bits
= 0; n
> 0; n
= n
>> 1) {
318 /* Using C99 rounding functions for roundToEven() implementation is
319 * difficult, because round(), rint, and nearbyint() are affected by
320 * fesetenv(), which the application may have done for its own
321 * purposes. Mesa's IROUND macro is close to what we want, but it
322 * rounds away from 0 on n + 0.5.
325 _mesa_round_to_even(float val
)
327 int rounded
= IROUND(val
);
329 if (val
- floor(val
) == 0.5) {
330 if (rounded
% 2 != 0)
331 rounded
+= val
> 0 ? -1 : 1;
339 * Convert a 4-byte float to a 2-byte half float.
341 * Not all float32 values can be represented exactly as a float16 value. We
342 * round such intermediate float32 values to the nearest float16. When the
343 * float32 lies exactly between to float16 values, we round to the one with
346 * This rounding behavior has several benefits:
347 * - It has no sign bias.
349 * - It reproduces the behavior of real hardware: opcode F32TO16 in Intel's
352 * - By reproducing the behavior of the GPU (at least on Intel hardware),
353 * compile-time evaluation of constant packHalf2x16 GLSL expressions will
354 * result in the same value as if the expression were executed on the GPU.
357 _mesa_float_to_half(float val
)
359 const fi_type fi
= {val
};
360 const int flt_m
= fi
.i
& 0x7fffff;
361 const int flt_e
= (fi
.i
>> 23) & 0xff;
362 const int flt_s
= (fi
.i
>> 31) & 0x1;
369 /* handle special cases */
370 if ((flt_e
== 0) && (flt_m
== 0)) {
372 /* m = 0; - already set */
375 else if ((flt_e
== 0) && (flt_m
!= 0)) {
376 /* denorm -- denorm float maps to 0 half */
377 /* m = 0; - already set */
380 else if ((flt_e
== 0xff) && (flt_m
== 0)) {
382 /* m = 0; - already set */
385 else if ((flt_e
== 0xff) && (flt_m
!= 0)) {
392 const int new_exp
= flt_e
- 127;
394 /* The float32 lies in the range (0.0, min_normal16) and is rounded
395 * to a nearby float16 value. The result will be either zero, subnormal,
399 m
= _mesa_round_to_even((1 << 24) * fabsf(fi
.f
));
401 else if (new_exp
> 15) {
402 /* map this value to infinity */
403 /* m = 0; - already set */
407 /* The float32 lies in the range
408 * [min_normal16, max_normal16 + max_step16)
409 * and is rounded to a nearby float16 value. The result will be
410 * either normal or infinite.
413 m
= _mesa_round_to_even(flt_m
/ (float) (1 << 13));
417 assert(0 <= m
&& m
<= 1024);
419 /* The float32 was rounded upwards into the range of the next exponent,
420 * so bump the exponent. This correctly handles the case where f32
421 * should be rounded up to float16 infinity.
427 result
= (s
<< 15) | (e
<< 10) | m
;
433 * Convert a 2-byte half float to a 4-byte float.
434 * Based on code from:
435 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
438 _mesa_half_to_float(GLhalfARB val
)
440 /* XXX could also use a 64K-entry lookup table */
441 const int m
= val
& 0x3ff;
442 const int e
= (val
>> 10) & 0x1f;
443 const int s
= (val
>> 15) & 0x1;
444 int flt_m
, flt_e
, flt_s
;
451 /* handle special cases */
452 if ((e
== 0) && (m
== 0)) {
457 else if ((e
== 0) && (m
!= 0)) {
458 /* denorm -- denorm half will fit in non-denorm single */
459 const float half_denorm
= 1.0f
/ 16384.0f
; /* 2^-14 */
460 float mantissa
= ((float) (m
)) / 1024.0f
;
461 float sign
= s
? -1.0f
: 1.0f
;
462 return sign
* mantissa
* half_denorm
;
464 else if ((e
== 31) && (m
== 0)) {
469 else if ((e
== 31) && (m
!= 0)) {
480 fi
.i
= (flt_s
<< 31) | (flt_e
<< 23) | flt_m
;
488 /**********************************************************************/
489 /** \name Sort & Search */
493 * Wrapper for bsearch().
496 _mesa_bsearch( const void *key
, const void *base
, size_t nmemb
, size_t size
,
497 int (*compar
)(const void *, const void *) )
499 #if defined(_WIN32_WCE)
504 mid
= (char *)base
+ nmemb
* size
;
505 cmp
= (*compar
)(key
, mid
);
509 base
= (char *)mid
+ size
;
515 return bsearch(key
, base
, nmemb
, size
, compar
);
522 /**********************************************************************/
523 /** \name Environment vars */
527 * Wrapper for getenv().
530 _mesa_getenv( const char *var
)
532 #if defined(_XBOX) || defined(_WIN32_WCE)
542 /**********************************************************************/
547 * Implemented using malloc() and strcpy.
548 * Note that NULL is handled accordingly.
551 _mesa_strdup( const char *s
)
554 size_t l
= strlen(s
);
555 char *s2
= malloc(l
+ 1);
565 /** Wrapper around strtof() */
567 _mesa_strtof( const char *s
, char **end
)
569 #if defined(_GNU_SOURCE) && !defined(__CYGWIN__) && !defined(__FreeBSD__) && \
570 !defined(ANDROID) && !defined(__HAIKU__) && !defined(__UCLIBC__)
571 static locale_t loc
= NULL
;
573 loc
= newlocale(LC_CTYPE_MASK
, "C", NULL
);
575 return strtof_l(s
, end
, loc
);
576 #elif defined(_ISOC99_SOURCE) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
577 return strtof(s
, end
);
579 return (float)strtod(s
, end
);
583 /** Compute simple checksum/hash for a string */
585 _mesa_str_checksum(const char *str
)
587 /* This could probably be much better */
591 for (c
= str
; *c
; c
++, i
++)
592 sum
+= *c
* (i
% 100);
600 /** Needed due to #ifdef's, above. */
602 _mesa_vsnprintf(char *str
, size_t size
, const char *fmt
, va_list args
)
604 return vsnprintf( str
, size
, fmt
, args
);
607 /** Wrapper around vsnprintf() */
609 _mesa_snprintf( char *str
, size_t size
, const char *fmt
, ... )
613 va_start( args
, fmt
);
614 r
= vsnprintf( str
, size
, fmt
, args
);