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.
60 #define MAXSTRING 4000 /* for vsnprintf() */
63 #define vsnprintf _vsnprintf
64 #elif defined(__IBMC__) || defined(__IBMCPP__) || ( defined(__VMS) && __CRTL_VER < 70312000 )
65 extern int vsnprintf(char *str
, size_t count
, const char *fmt
, va_list arg
);
67 #include "vsnprintf.c"
71 /**********************************************************************/
76 * Allocate aligned memory.
78 * \param bytes number of bytes to allocate.
79 * \param alignment alignment (must be greater than zero).
81 * Allocates extra memory to accommodate rounding up the address for
82 * alignment and to record the real malloc address.
84 * \sa _mesa_align_free().
87 _mesa_align_malloc(size_t bytes
, unsigned long alignment
)
89 #if defined(HAVE_POSIX_MEMALIGN)
91 int err
= posix_memalign(& mem
, alignment
, bytes
);
95 #elif defined(_WIN32) && defined(_MSC_VER)
96 return _aligned_malloc(bytes
, alignment
);
100 ASSERT( alignment
> 0 );
102 ptr
= (uintptr_t) malloc(bytes
+ alignment
+ sizeof(void *));
106 buf
= (ptr
+ alignment
+ sizeof(void *)) & ~(uintptr_t)(alignment
- 1);
107 *(uintptr_t *)(buf
- sizeof(void *)) = ptr
;
110 /* mark the non-aligned area */
111 while ( ptr
< buf
- sizeof(void *) ) {
112 *(unsigned long *)ptr
= 0xcdcdcdcd;
113 ptr
+= sizeof(unsigned long);
118 #endif /* defined(HAVE_POSIX_MEMALIGN) */
122 * Same as _mesa_align_malloc(), but using calloc(1, ) instead of
126 _mesa_align_calloc(size_t bytes
, unsigned long alignment
)
128 #if defined(HAVE_POSIX_MEMALIGN)
131 mem
= _mesa_align_malloc(bytes
, alignment
);
133 (void) memset(mem
, 0, bytes
);
137 #elif defined(_WIN32) && defined(_MSC_VER)
140 mem
= _aligned_malloc(bytes
, alignment
);
142 (void) memset(mem
, 0, bytes
);
149 ASSERT( alignment
> 0 );
151 ptr
= (uintptr_t) calloc(1, bytes
+ alignment
+ sizeof(void *));
155 buf
= (ptr
+ alignment
+ sizeof(void *)) & ~(uintptr_t)(alignment
- 1);
156 *(uintptr_t *)(buf
- sizeof(void *)) = ptr
;
159 /* mark the non-aligned area */
160 while ( ptr
< buf
- sizeof(void *) ) {
161 *(unsigned long *)ptr
= 0xcdcdcdcd;
162 ptr
+= sizeof(unsigned long);
167 #endif /* defined(HAVE_POSIX_MEMALIGN) */
171 * Free memory which was allocated with either _mesa_align_malloc()
172 * or _mesa_align_calloc().
173 * \param ptr pointer to the memory to be freed.
174 * The actual address to free is stored in the word immediately before the
175 * address the client sees.
178 _mesa_align_free(void *ptr
)
180 #if defined(HAVE_POSIX_MEMALIGN)
182 #elif defined(_WIN32) && defined(_MSC_VER)
185 void **cubbyHole
= (void **) ((char *) ptr
- sizeof(void *));
186 void *realAddr
= *cubbyHole
;
188 #endif /* defined(HAVE_POSIX_MEMALIGN) */
192 * Reallocate memory, with alignment.
195 _mesa_align_realloc(void *oldBuffer
, size_t oldSize
, size_t newSize
,
196 unsigned long alignment
)
198 #if defined(_WIN32) && defined(_MSC_VER)
200 return _aligned_realloc(oldBuffer
, newSize
, alignment
);
202 const size_t copySize
= (oldSize
< newSize
) ? oldSize
: newSize
;
203 void *newBuf
= _mesa_align_malloc(newSize
, alignment
);
204 if (newBuf
&& oldBuffer
&& copySize
> 0) {
205 memcpy(newBuf
, oldBuffer
, copySize
);
208 _mesa_align_free(oldBuffer
);
215 /** Reallocate memory */
217 _mesa_realloc(void *oldBuffer
, size_t oldSize
, size_t newSize
)
219 const size_t copySize
= (oldSize
< newSize
) ? oldSize
: newSize
;
220 void *newBuffer
= malloc(newSize
);
221 if (newBuffer
&& oldBuffer
&& copySize
> 0)
222 memcpy(newBuffer
, oldBuffer
, copySize
);
229 * Fill memory with a constant 16bit word.
230 * \param dst destination pointer.
232 * \param n number of words.
235 _mesa_memset16( unsigned short *dst
, unsigned short val
, size_t n
)
244 /**********************************************************************/
248 /** Wrapper around sqrt() */
250 _mesa_sqrtd(double x
)
257 * A High Speed, Low Precision Square Root
258 * by Paul Lalonde and Robert Dawson
259 * from "Graphics Gems", Academic Press, 1990
261 * SPARC implementation of a fast square root by table
263 * SPARC floating point format is as follows:
266 * sign exponent mantissa
268 static short sqrttab
[0x100]; /* declare table of square roots */
271 _mesa_init_sqrt_table(void)
273 #if defined(USE_IEEE) && !defined(DEBUG)
275 fi_type fi
; /* to access the bits of a float in C quickly */
276 /* we use a union defined in glheader.h */
278 for(i
=0; i
<= 0x7f; i
++) {
282 * Build a float with the bit pattern i as mantissa
283 * and an exponent of 0, stored as 127
286 fi
.i
= (i
<< 16) | (127 << 23);
287 fi
.f
= _mesa_sqrtd(fi
.f
);
290 * Take the square root then strip the first 7 bits of
291 * the mantissa into the table
294 sqrttab
[i
] = (fi
.i
& 0x7fffff) >> 16;
297 * Repeat the process, this time with an exponent of
302 fi
.i
= (i
<< 16) | (128 << 23);
304 sqrttab
[i
+0x80] = (fi
.i
& 0x7fffff) >> 16;
307 (void) sqrttab
; /* silence compiler warnings */
308 #endif /*HAVE_FAST_MATH*/
313 * Single precision square root.
316 _mesa_sqrtf( float x
)
318 #if defined(USE_IEEE) && !defined(DEBUG)
320 /* to access the bits of a float in C
321 * we use a union from glheader.h */
323 short e
; /* the exponent */
324 if (x
== 0.0F
) return 0.0F
; /* check for square root of 0 */
326 e
= (num
.i
>> 23) - 127; /* get the exponent - on a SPARC the */
327 /* exponent is stored with 127 added */
328 num
.i
&= 0x7fffff; /* leave only the mantissa */
329 if (e
& 0x01) num
.i
|= 0x800000;
330 /* the exponent is odd so we have to */
331 /* look it up in the second half of */
332 /* the lookup table, so we set the */
334 e
>>= 1; /* divide the exponent by two */
335 /* note that in C the shift */
336 /* operators are sign preserving */
337 /* for signed operands */
338 /* Do the table lookup, based on the quaternary mantissa,
339 * then reconstruct the result back into a float
341 num
.i
= ((sqrttab
[num
.i
>> 16]) << 16) | ((e
+ 127) << 23);
345 return (float) _mesa_sqrtd((double) x
);
351 inv_sqrt - A single precision 1/sqrt routine for IEEE format floats.
352 written by Josh Vanderhoof, based on newsgroup posts by James Van Buskirk
356 _mesa_inv_sqrtf(float n
)
358 #if defined(USE_IEEE) && !defined(DEBUG)
362 #if 0 /* not used, see below -BP */
369 Exponent part of the magic number -
372 1. subtract the bias from the exponent,
374 3. divide by two (rounding towards -inf)
377 Which is the same as subtracting the exponent from 381 and dividing
380 floor(-(x - 127) / 2) + 127 = floor((381 - x) / 2)
386 Significand part of magic number -
388 With the current magic number, "(magic - u.i) >> 1" will give you:
390 for 1 <= u.f <= 2: 1.25 - u.f / 4
391 for 2 <= u.f <= 4: 1.00 - u.f / 8
393 This isn't a bad approximation of 1/sqrt. The maximum difference from
394 1/sqrt will be around .06. After three Newton-Raphson iterations, the
395 maximum difference is less than 4.5e-8. (Which is actually close
396 enough to make the following bias academic...)
398 To get a better approximation you can add a bias to the magic
399 number. For example, if you subtract 1/2 of the maximum difference in
400 the first approximation (.03), you will get the following function:
402 for 1 <= u.f <= 2: 1.22 - u.f / 4
403 for 2 <= u.f <= 3.76: 0.97 - u.f / 8
404 for 3.76 <= u.f <= 4: 0.72 - u.f / 16
405 (The 3.76 to 4 range is where the result is < .5.)
407 This is the closest possible initial approximation, but with a maximum
408 error of 8e-11 after three NR iterations, it is still not perfect. If
409 you subtract 0.0332281 instead of .03, the maximum error will be
410 2.5e-11 after three NR iterations, which should be about as close as
413 for 1 <= u.f <= 2: 1.2167719 - u.f / 4
414 for 2 <= u.f <= 3.73: 0.9667719 - u.f / 8
415 for 3.73 <= u.f <= 4: 0.7167719 - u.f / 16
419 magic
-= (int)(0.0332281 * (1 << 25));
422 u
.i
= (magic
- u
.i
) >> 1;
425 Instead of Newton-Raphson, we use Goldschmidt's algorithm, which
426 allows more parallelism. From what I understand, the parallelism
427 comes at the cost of less precision, because it lets error
428 accumulate across iterations.
443 return x2
* r2
; /* we can stop here, and be conformant -BP */
452 return (float) (1.0 / sqrt(n
));
458 * Find the first bit set in a word.
463 register int bit
= 0;
465 if ((i
& 0xffff) == 0) {
469 if ((i
& 0xff) == 0) {
473 if ((i
& 0xf) == 0) {
477 while ((i
& 1) == 0) {
488 * Find position of first bit set in given value.
489 * XXX Warning: this function can only be used on 64-bit systems!
490 * \return position of least-significant bit set, starting at 1, return zero
494 ffsll(long long int val
)
498 assert(sizeof(val
) == 8);
500 bit
= ffs((int) val
);
504 bit
= ffs((int) (val
>> 32));
510 #endif /* __GNUC__ */
513 #if !defined(__GNUC__) ||\
514 ((__GNUC__ * 100 + __GNUC_MINOR__) < 304) /* Not gcc 3.4 or later */
516 * Return number of bits set in given GLuint.
519 _mesa_bitcount(unsigned int n
)
522 for (bits
= 0; n
> 0; n
= n
>> 1) {
529 * Return number of bits set in given 64-bit uint.
532 _mesa_bitcount_64(uint64_t n
)
535 for (bits
= 0; n
> 0; n
= n
>> 1) {
544 * Convert a 4-byte float to a 2-byte half float.
545 * Based on code from:
546 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
549 _mesa_float_to_half(float val
)
551 const fi_type fi
= {val
};
552 const int flt_m
= fi
.i
& 0x7fffff;
553 const int flt_e
= (fi
.i
>> 23) & 0xff;
554 const int flt_s
= (fi
.i
>> 31) & 0x1;
561 /* handle special cases */
562 if ((flt_e
== 0) && (flt_m
== 0)) {
564 /* m = 0; - already set */
567 else if ((flt_e
== 0) && (flt_m
!= 0)) {
568 /* denorm -- denorm float maps to 0 half */
569 /* m = 0; - already set */
572 else if ((flt_e
== 0xff) && (flt_m
== 0)) {
574 /* m = 0; - already set */
577 else if ((flt_e
== 0xff) && (flt_m
!= 0)) {
584 const int new_exp
= flt_e
- 127;
587 /* m = 0; - already set */
590 else if (new_exp
< -14) {
591 /* this maps to a denorm */
592 unsigned int exp_val
= (unsigned int) (-14 - new_exp
); /* 2^-exp_val*/
597 "float_to_half: logical error in denorm creation!\n");
598 /* m = 0; - already set */
600 case 1: m
= 512 + (flt_m
>> 14); break;
601 case 2: m
= 256 + (flt_m
>> 15); break;
602 case 3: m
= 128 + (flt_m
>> 16); break;
603 case 4: m
= 64 + (flt_m
>> 17); break;
604 case 5: m
= 32 + (flt_m
>> 18); break;
605 case 6: m
= 16 + (flt_m
>> 19); break;
606 case 7: m
= 8 + (flt_m
>> 20); break;
607 case 8: m
= 4 + (flt_m
>> 21); break;
608 case 9: m
= 2 + (flt_m
>> 22); break;
609 case 10: m
= 1; break;
612 else if (new_exp
> 15) {
613 /* map this value to infinity */
614 /* m = 0; - already set */
624 result
= (s
<< 15) | (e
<< 10) | m
;
630 * Convert a 2-byte half float to a 4-byte float.
631 * Based on code from:
632 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
635 _mesa_half_to_float(GLhalfARB val
)
637 /* XXX could also use a 64K-entry lookup table */
638 const int m
= val
& 0x3ff;
639 const int e
= (val
>> 10) & 0x1f;
640 const int s
= (val
>> 15) & 0x1;
641 int flt_m
, flt_e
, flt_s
;
648 /* handle special cases */
649 if ((e
== 0) && (m
== 0)) {
654 else if ((e
== 0) && (m
!= 0)) {
655 /* denorm -- denorm half will fit in non-denorm single */
656 const float half_denorm
= 1.0f
/ 16384.0f
; /* 2^-14 */
657 float mantissa
= ((float) (m
)) / 1024.0f
;
658 float sign
= s
? -1.0f
: 1.0f
;
659 return sign
* mantissa
* half_denorm
;
661 else if ((e
== 31) && (m
== 0)) {
666 else if ((e
== 31) && (m
!= 0)) {
677 fi
.i
= (flt_s
<< 31) | (flt_e
<< 23) | flt_m
;
685 /**********************************************************************/
686 /** \name Sort & Search */
690 * Wrapper for bsearch().
693 _mesa_bsearch( const void *key
, const void *base
, size_t nmemb
, size_t size
,
694 int (*compar
)(const void *, const void *) )
696 #if defined(_WIN32_WCE)
701 mid
= (char *)base
+ nmemb
* size
;
702 cmp
= (*compar
)(key
, mid
);
706 base
= (char *)mid
+ size
;
712 return bsearch(key
, base
, nmemb
, size
, compar
);
719 /**********************************************************************/
720 /** \name Environment vars */
724 * Wrapper for getenv().
727 _mesa_getenv( const char *var
)
729 #if defined(_XBOX) || defined(_WIN32_WCE)
739 /**********************************************************************/
744 * Implemented using malloc() and strcpy.
745 * Note that NULL is handled accordingly.
748 _mesa_strdup( const char *s
)
751 size_t l
= strlen(s
);
752 char *s2
= (char *) malloc(l
+ 1);
762 /** Wrapper around strtof() */
764 _mesa_strtof( const char *s
, char **end
)
766 #if defined(_GNU_SOURCE) && !defined(__CYGWIN__) && !defined(__FreeBSD__) && \
768 static locale_t loc
= NULL
;
770 loc
= newlocale(LC_CTYPE_MASK
, "C", NULL
);
772 return strtof_l(s
, end
, loc
);
773 #elif defined(_ISOC99_SOURCE) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600)
774 return strtof(s
, end
);
776 return (float)strtod(s
, end
);
780 /** Compute simple checksum/hash for a string */
782 _mesa_str_checksum(const char *str
)
784 /* This could probably be much better */
788 for (c
= str
; *c
; c
++, i
++)
789 sum
+= *c
* (i
% 100);
797 /** Wrapper around vsnprintf() */
799 _mesa_snprintf( char *str
, size_t size
, const char *fmt
, ... )
803 va_start( args
, fmt
);
804 r
= vsnprintf( str
, size
, fmt
, args
);
810 /**********************************************************************/
811 /** \name Diagnostics */
815 output_if_debug(const char *prefixString
, const char *outputString
,
818 static int debug
= -1;
820 /* Check the MESA_DEBUG environment variable if it hasn't
821 * been checked yet. We only have to check it once...
824 char *env
= _mesa_getenv("MESA_DEBUG");
826 /* In a debug build, we print warning messages *unless*
827 * MESA_DEBUG is 0. In a non-debug build, we don't
828 * print warning messages *unless* MESA_DEBUG is
829 * set *to any value*.
832 debug
= (env
!= NULL
&& atoi(env
) == 0) ? 0 : 1;
834 debug
= (env
!= NULL
) ? 1 : 0;
838 /* Now only print the string if we're required to do so. */
840 fprintf(stderr
, "%s: %s", prefixString
, outputString
);
842 fprintf(stderr
, "\n");
844 #if defined(_WIN32) && !defined(_WIN32_WCE)
845 /* stderr from windows applications without console is not usually
846 * visible, so communicate with the debugger instead */
849 _mesa_snprintf(buf
, sizeof(buf
), "%s: %s%s", prefixString
, outputString
, newline
? "\n" : "");
850 OutputDebugStringA(buf
);
858 * Return string version of GL error code.
861 error_string( GLenum error
)
865 return "GL_NO_ERROR";
866 case GL_INVALID_VALUE
:
867 return "GL_INVALID_VALUE";
868 case GL_INVALID_ENUM
:
869 return "GL_INVALID_ENUM";
870 case GL_INVALID_OPERATION
:
871 return "GL_INVALID_OPERATION";
872 case GL_STACK_OVERFLOW
:
873 return "GL_STACK_OVERFLOW";
874 case GL_STACK_UNDERFLOW
:
875 return "GL_STACK_UNDERFLOW";
876 case GL_OUT_OF_MEMORY
:
877 return "GL_OUT_OF_MEMORY";
878 case GL_TABLE_TOO_LARGE
:
879 return "GL_TABLE_TOO_LARGE";
880 case GL_INVALID_FRAMEBUFFER_OPERATION_EXT
:
881 return "GL_INVALID_FRAMEBUFFER_OPERATION";
889 * When a new type of error is recorded, print a message describing
890 * previous errors which were accumulated.
893 flush_delayed_errors( struct gl_context
*ctx
)
897 if (ctx
->ErrorDebugCount
) {
898 _mesa_snprintf(s
, MAXSTRING
, "%d similar %s errors",
899 ctx
->ErrorDebugCount
,
900 error_string(ctx
->ErrorValue
));
902 output_if_debug("Mesa", s
, GL_TRUE
);
904 ctx
->ErrorDebugCount
= 0;
910 * Report a warning (a recoverable error condition) to stderr if
911 * either DEBUG is defined or the MESA_DEBUG env var is set.
913 * \param ctx GL context.
914 * \param fmtString printf()-like format string.
917 _mesa_warning( struct gl_context
*ctx
, const char *fmtString
, ... )
921 va_start( args
, fmtString
);
922 (void) vsnprintf( str
, MAXSTRING
, fmtString
, args
);
926 flush_delayed_errors( ctx
);
928 output_if_debug("Mesa warning", str
, GL_TRUE
);
933 * Report an internal implementation problem.
934 * Prints the message to stderr via fprintf().
936 * \param ctx GL context.
937 * \param fmtString problem description string.
940 _mesa_problem( const struct gl_context
*ctx
, const char *fmtString
, ... )
944 static int numCalls
= 0;
951 va_start( args
, fmtString
);
952 vsnprintf( str
, MAXSTRING
, fmtString
, args
);
954 fprintf(stderr
, "Mesa %s implementation error: %s\n",
955 MESA_VERSION_STRING
, str
);
956 fprintf(stderr
, "Please report at bugs.freedesktop.org\n");
962 * Record an OpenGL state error. These usually occur when the user
963 * passes invalid parameters to a GL function.
965 * If debugging is enabled (either at compile-time via the DEBUG macro, or
966 * run-time via the MESA_DEBUG environment variable), report the error with
969 * \param ctx the GL context.
970 * \param error the error value.
971 * \param fmtString printf() style format string, followed by optional args
974 _mesa_error( struct gl_context
*ctx
, GLenum error
, const char *fmtString
, ... )
976 static GLint debug
= -1;
978 /* Check debug environment variable only once:
981 const char *debugEnv
= _mesa_getenv("MESA_DEBUG");
984 if (debugEnv
&& strstr(debugEnv
, "silent"))
997 if (ctx
->ErrorValue
== error
&&
998 ctx
->ErrorDebugFmtString
== fmtString
) {
999 ctx
->ErrorDebugCount
++;
1002 char s
[MAXSTRING
], s2
[MAXSTRING
];
1005 flush_delayed_errors( ctx
);
1007 va_start(args
, fmtString
);
1008 vsnprintf(s
, MAXSTRING
, fmtString
, args
);
1011 _mesa_snprintf(s2
, MAXSTRING
, "%s in %s", error_string(error
), s
);
1012 output_if_debug("Mesa: User error", s2
, GL_TRUE
);
1014 ctx
->ErrorDebugFmtString
= fmtString
;
1015 ctx
->ErrorDebugCount
= 0;
1019 _mesa_record_error(ctx
, error
);
1024 * Report debug information. Print error message to stderr via fprintf().
1025 * No-op if DEBUG mode not enabled.
1027 * \param ctx GL context.
1028 * \param fmtString printf()-style format string, followed by optional args.
1031 _mesa_debug( const struct gl_context
*ctx
, const char *fmtString
, ... )
1036 va_start(args
, fmtString
);
1037 vsnprintf(s
, MAXSTRING
, fmtString
, args
);
1039 output_if_debug("Mesa", s
, GL_FALSE
);