2d592a68ecb81cd28156d9c0b1e7cc9d2c4797ec
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
= (uintptr_t) 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
= (uintptr_t) 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
);
227 * Fill memory with a constant 16bit word.
228 * \param dst destination pointer.
230 * \param n number of words.
233 _mesa_memset16( unsigned short *dst
, unsigned short val
, size_t n
)
242 /**********************************************************************/
246 /** Wrapper around sqrt() */
248 _mesa_sqrtd(double x
)
255 * A High Speed, Low Precision Square Root
256 * by Paul Lalonde and Robert Dawson
257 * from "Graphics Gems", Academic Press, 1990
259 * SPARC implementation of a fast square root by table
261 * SPARC floating point format is as follows:
264 * sign exponent mantissa
266 static short sqrttab
[0x100]; /* declare table of square roots */
269 _mesa_init_sqrt_table(void)
271 #if defined(USE_IEEE) && !defined(DEBUG)
273 fi_type fi
; /* to access the bits of a float in C quickly */
274 /* we use a union defined in glheader.h */
276 for(i
=0; i
<= 0x7f; i
++) {
280 * Build a float with the bit pattern i as mantissa
281 * and an exponent of 0, stored as 127
284 fi
.i
= (i
<< 16) | (127 << 23);
285 fi
.f
= _mesa_sqrtd(fi
.f
);
288 * Take the square root then strip the first 7 bits of
289 * the mantissa into the table
292 sqrttab
[i
] = (fi
.i
& 0x7fffff) >> 16;
295 * Repeat the process, this time with an exponent of
300 fi
.i
= (i
<< 16) | (128 << 23);
302 sqrttab
[i
+0x80] = (fi
.i
& 0x7fffff) >> 16;
305 (void) sqrttab
; /* silence compiler warnings */
306 #endif /*HAVE_FAST_MATH*/
311 * Single precision square root.
314 _mesa_sqrtf( float x
)
316 #if defined(USE_IEEE) && !defined(DEBUG)
318 /* to access the bits of a float in C
319 * we use a union from glheader.h */
321 short e
; /* the exponent */
322 if (x
== 0.0F
) return 0.0F
; /* check for square root of 0 */
324 e
= (num
.i
>> 23) - 127; /* get the exponent - on a SPARC the */
325 /* exponent is stored with 127 added */
326 num
.i
&= 0x7fffff; /* leave only the mantissa */
327 if (e
& 0x01) num
.i
|= 0x800000;
328 /* the exponent is odd so we have to */
329 /* look it up in the second half of */
330 /* the lookup table, so we set the */
332 e
>>= 1; /* divide the exponent by two */
333 /* note that in C the shift */
334 /* operators are sign preserving */
335 /* for signed operands */
336 /* Do the table lookup, based on the quaternary mantissa,
337 * then reconstruct the result back into a float
339 num
.i
= ((sqrttab
[num
.i
>> 16]) << 16) | ((e
+ 127) << 23);
343 return (float) _mesa_sqrtd((double) x
);
349 inv_sqrt - A single precision 1/sqrt routine for IEEE format floats.
350 written by Josh Vanderhoof, based on newsgroup posts by James Van Buskirk
354 _mesa_inv_sqrtf(float n
)
356 #if defined(USE_IEEE) && !defined(DEBUG)
360 #if 0 /* not used, see below -BP */
367 Exponent part of the magic number -
370 1. subtract the bias from the exponent,
372 3. divide by two (rounding towards -inf)
375 Which is the same as subtracting the exponent from 381 and dividing
378 floor(-(x - 127) / 2) + 127 = floor((381 - x) / 2)
384 Significand part of magic number -
386 With the current magic number, "(magic - u.i) >> 1" will give you:
388 for 1 <= u.f <= 2: 1.25 - u.f / 4
389 for 2 <= u.f <= 4: 1.00 - u.f / 8
391 This isn't a bad approximation of 1/sqrt. The maximum difference from
392 1/sqrt will be around .06. After three Newton-Raphson iterations, the
393 maximum difference is less than 4.5e-8. (Which is actually close
394 enough to make the following bias academic...)
396 To get a better approximation you can add a bias to the magic
397 number. For example, if you subtract 1/2 of the maximum difference in
398 the first approximation (.03), you will get the following function:
400 for 1 <= u.f <= 2: 1.22 - u.f / 4
401 for 2 <= u.f <= 3.76: 0.97 - u.f / 8
402 for 3.76 <= u.f <= 4: 0.72 - u.f / 16
403 (The 3.76 to 4 range is where the result is < .5.)
405 This is the closest possible initial approximation, but with a maximum
406 error of 8e-11 after three NR iterations, it is still not perfect. If
407 you subtract 0.0332281 instead of .03, the maximum error will be
408 2.5e-11 after three NR iterations, which should be about as close as
411 for 1 <= u.f <= 2: 1.2167719 - u.f / 4
412 for 2 <= u.f <= 3.73: 0.9667719 - u.f / 8
413 for 3.73 <= u.f <= 4: 0.7167719 - u.f / 16
417 magic
-= (int)(0.0332281 * (1 << 25));
420 u
.i
= (magic
- u
.i
) >> 1;
423 Instead of Newton-Raphson, we use Goldschmidt's algorithm, which
424 allows more parallelism. From what I understand, the parallelism
425 comes at the cost of less precision, because it lets error
426 accumulate across iterations.
441 return x2
* r2
; /* we can stop here, and be conformant -BP */
450 return (float) (1.0 / sqrt(n
));
456 * Find the first bit set in a word.
461 register int bit
= 0;
463 if ((i
& 0xffff) == 0) {
467 if ((i
& 0xff) == 0) {
471 if ((i
& 0xf) == 0) {
475 while ((i
& 1) == 0) {
486 * Find position of first bit set in given value.
487 * XXX Warning: this function can only be used on 64-bit systems!
488 * \return position of least-significant bit set, starting at 1, return zero
492 ffsll(long long int val
)
496 assert(sizeof(val
) == 8);
498 bit
= ffs((int) val
);
502 bit
= ffs((int) (val
>> 32));
508 #endif /* __GNUC__ */
511 #if !defined(__GNUC__) ||\
512 ((__GNUC__ * 100 + __GNUC_MINOR__) < 304) /* Not gcc 3.4 or later */
514 * Return number of bits set in given GLuint.
517 _mesa_bitcount(unsigned int n
)
520 for (bits
= 0; n
> 0; n
= n
>> 1) {
527 * Return number of bits set in given 64-bit uint.
530 _mesa_bitcount_64(uint64_t n
)
533 for (bits
= 0; n
> 0; n
= n
>> 1) {
542 * Convert a 4-byte float to a 2-byte half float.
543 * Based on code from:
544 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
547 _mesa_float_to_half(float val
)
549 const fi_type fi
= {val
};
550 const int flt_m
= fi
.i
& 0x7fffff;
551 const int flt_e
= (fi
.i
>> 23) & 0xff;
552 const int flt_s
= (fi
.i
>> 31) & 0x1;
559 /* handle special cases */
560 if ((flt_e
== 0) && (flt_m
== 0)) {
562 /* m = 0; - already set */
565 else if ((flt_e
== 0) && (flt_m
!= 0)) {
566 /* denorm -- denorm float maps to 0 half */
567 /* m = 0; - already set */
570 else if ((flt_e
== 0xff) && (flt_m
== 0)) {
572 /* m = 0; - already set */
575 else if ((flt_e
== 0xff) && (flt_m
!= 0)) {
582 const int new_exp
= flt_e
- 127;
585 /* m = 0; - already set */
588 else if (new_exp
< -14) {
589 /* this maps to a denorm */
590 unsigned int exp_val
= (unsigned int) (-14 - new_exp
); /* 2^-exp_val*/
595 "float_to_half: logical error in denorm creation!\n");
596 /* m = 0; - already set */
598 case 1: m
= 512 + (flt_m
>> 14); break;
599 case 2: m
= 256 + (flt_m
>> 15); break;
600 case 3: m
= 128 + (flt_m
>> 16); break;
601 case 4: m
= 64 + (flt_m
>> 17); break;
602 case 5: m
= 32 + (flt_m
>> 18); break;
603 case 6: m
= 16 + (flt_m
>> 19); break;
604 case 7: m
= 8 + (flt_m
>> 20); break;
605 case 8: m
= 4 + (flt_m
>> 21); break;
606 case 9: m
= 2 + (flt_m
>> 22); break;
607 case 10: m
= 1; break;
610 else if (new_exp
> 15) {
611 /* map this value to infinity */
612 /* m = 0; - already set */
622 result
= (s
<< 15) | (e
<< 10) | m
;
628 * Convert a 2-byte half float to a 4-byte float.
629 * Based on code from:
630 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
633 _mesa_half_to_float(GLhalfARB val
)
635 /* XXX could also use a 64K-entry lookup table */
636 const int m
= val
& 0x3ff;
637 const int e
= (val
>> 10) & 0x1f;
638 const int s
= (val
>> 15) & 0x1;
639 int flt_m
, flt_e
, flt_s
;
646 /* handle special cases */
647 if ((e
== 0) && (m
== 0)) {
652 else if ((e
== 0) && (m
!= 0)) {
653 /* denorm -- denorm half will fit in non-denorm single */
654 const float half_denorm
= 1.0f
/ 16384.0f
; /* 2^-14 */
655 float mantissa
= ((float) (m
)) / 1024.0f
;
656 float sign
= s
? -1.0f
: 1.0f
;
657 return sign
* mantissa
* half_denorm
;
659 else if ((e
== 31) && (m
== 0)) {
664 else if ((e
== 31) && (m
!= 0)) {
675 fi
.i
= (flt_s
<< 31) | (flt_e
<< 23) | flt_m
;
683 /**********************************************************************/
684 /** \name Sort & Search */
688 * Wrapper for bsearch().
691 _mesa_bsearch( const void *key
, const void *base
, size_t nmemb
, size_t size
,
692 int (*compar
)(const void *, const void *) )
694 #if defined(_WIN32_WCE)
699 mid
= (char *)base
+ nmemb
* size
;
700 cmp
= (*compar
)(key
, mid
);
704 base
= (char *)mid
+ size
;
710 return bsearch(key
, base
, nmemb
, size
, compar
);
717 /**********************************************************************/
718 /** \name Environment vars */
722 * Wrapper for getenv().
725 _mesa_getenv( const char *var
)
727 #if defined(_XBOX) || defined(_WIN32_WCE)
737 /**********************************************************************/
742 * Implemented using malloc() and strcpy.
743 * Note that NULL is handled accordingly.
746 _mesa_strdup( const char *s
)
749 size_t l
= strlen(s
);
750 char *s2
= (char *) malloc(l
+ 1);
760 /** Wrapper around strtof() */
762 _mesa_strtof( const char *s
, char **end
)
764 #if defined(_GNU_SOURCE) && !defined(__CYGWIN__) && !defined(__FreeBSD__) && \
765 !defined(ANDROID) && !defined(__HAIKU__)
766 static locale_t loc
= NULL
;
768 loc
= newlocale(LC_CTYPE_MASK
, "C", NULL
);
770 return strtof_l(s
, end
, loc
);
771 #elif defined(_ISOC99_SOURCE) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
772 return strtof(s
, end
);
774 return (float)strtod(s
, end
);
778 /** Compute simple checksum/hash for a string */
780 _mesa_str_checksum(const char *str
)
782 /* This could probably be much better */
786 for (c
= str
; *c
; c
++, i
++)
787 sum
+= *c
* (i
% 100);
795 /** Needed due to #ifdef's, above. */
797 _mesa_vsnprintf(char *str
, size_t size
, const char *fmt
, va_list args
)
799 return vsnprintf( str
, size
, fmt
, args
);
802 /** Wrapper around vsnprintf() */
804 _mesa_snprintf( char *str
, size_t size
, const char *fmt
, ... )
808 va_start( args
, fmt
);
809 r
= vsnprintf( str
, size
, fmt
, args
);