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.
52 #define MAXSTRING 4000 /* for vsnprintf() */
55 #define vsnprintf _vsnprintf
56 #elif defined(__IBMC__) || defined(__IBMCPP__) || ( defined(__VMS) && __CRTL_VER < 70312000 )
57 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list arg);
59 #include "vsnprintf.c"
63 /**********************************************************************/
67 /** Wrapper around malloc() */
69 _mesa_malloc(size_t bytes)
74 /** Wrapper around calloc() */
76 _mesa_calloc(size_t bytes)
78 return calloc(1, bytes);
81 /** Wrapper around free() */
89 * Allocate aligned memory.
91 * \param bytes number of bytes to allocate.
92 * \param alignment alignment (must be greater than zero).
94 * Allocates extra memory to accommodate rounding up the address for
95 * alignment and to record the real malloc address.
97 * \sa _mesa_align_free().
100 _mesa_align_malloc(size_t bytes, unsigned long alignment)
102 #if defined(HAVE_POSIX_MEMALIGN)
105 (void) posix_memalign(& mem, alignment, bytes);
110 ASSERT( alignment > 0 );
112 ptr = (uintptr_t) _mesa_malloc(bytes + alignment + sizeof(void *));
116 buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1);
117 *(uintptr_t *)(buf - sizeof(void *)) = ptr;
120 /* mark the non-aligned area */
121 while ( ptr < buf - sizeof(void *) ) {
122 *(unsigned long *)ptr = 0xcdcdcdcd;
123 ptr += sizeof(unsigned long);
128 #endif /* defined(HAVE_POSIX_MEMALIGN) */
132 * Same as _mesa_align_malloc(), but using _mesa_calloc() instead of
136 _mesa_align_calloc(size_t bytes, unsigned long alignment)
138 #if defined(HAVE_POSIX_MEMALIGN)
141 mem = _mesa_align_malloc(bytes, alignment);
143 (void) memset(mem, 0, bytes);
150 ASSERT( alignment > 0 );
152 ptr = (uintptr_t) _mesa_calloc(bytes + alignment + sizeof(void *));
156 buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1);
157 *(uintptr_t *)(buf - sizeof(void *)) = ptr;
160 /* mark the non-aligned area */
161 while ( ptr < buf - sizeof(void *) ) {
162 *(unsigned long *)ptr = 0xcdcdcdcd;
163 ptr += sizeof(unsigned long);
168 #endif /* defined(HAVE_POSIX_MEMALIGN) */
172 * Free memory which was allocated with either _mesa_align_malloc()
173 * or _mesa_align_calloc().
174 * \param ptr pointer to the memory to be freed.
175 * The actual address to free is stored in the word immediately before the
176 * address the client sees.
179 _mesa_align_free(void *ptr)
181 #if defined(HAVE_POSIX_MEMALIGN)
184 void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
185 void *realAddr = *cubbyHole;
186 _mesa_free(realAddr);
187 #endif /* defined(HAVE_POSIX_MEMALIGN) */
191 * Reallocate memory, with alignment.
194 _mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize,
195 unsigned long 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 _mesa_memcpy(newBuf, oldBuffer, copySize);
203 _mesa_align_free(oldBuffer);
209 /** Reallocate memory */
211 _mesa_realloc(void *oldBuffer, size_t oldSize, size_t newSize)
213 const size_t copySize = (oldSize < newSize) ? oldSize : newSize;
214 void *newBuffer = _mesa_malloc(newSize);
215 if (newBuffer && oldBuffer && copySize > 0)
216 _mesa_memcpy(newBuffer, oldBuffer, copySize);
218 _mesa_free(oldBuffer);
222 /** memcpy wrapper */
224 _mesa_memcpy(void *dest, const void *src, size_t n)
227 return memcpy((char *) dest, (char *) src, (int) n);
229 return memcpy(dest, src, n);
233 /** Wrapper around memset() */
235 _mesa_memset( void *dst, int val, size_t n )
238 memset( (char *) dst, (int) val, (int) n );
245 * Fill memory with a constant 16bit word.
246 * \param dst destination pointer.
248 * \param n number of words.
251 _mesa_memset16( unsigned short *dst, unsigned short val, size_t n )
257 /** Wrapper around either memset() or bzero() */
259 _mesa_bzero( void *dst, size_t n )
261 #if defined(__FreeBSD__)
268 /** Wrapper around memcmp() */
270 _mesa_memcmp( const void *s1, const void *s2, size_t n )
273 return memcmp( (char *) s1, (char *) s2, (int) n );
275 return memcmp(s1, s2, n);
282 /**********************************************************************/
286 /** Wrapper around sin() */
293 /** Single precision wrapper around sin() */
297 return (float) sin((double) a);
300 /** Wrapper around cos() */
307 /** Single precision wrapper around asin() */
311 return (float) asin((double) x);
314 /** Single precision wrapper around atan() */
318 return (float) atan((double) x);
321 /** Wrapper around sqrt() */
323 _mesa_sqrtd(double x)
330 * A High Speed, Low Precision Square Root
331 * by Paul Lalonde and Robert Dawson
332 * from "Graphics Gems", Academic Press, 1990
334 * SPARC implementation of a fast square root by table
336 * SPARC floating point format is as follows:
339 * sign exponent mantissa
341 static short sqrttab[0x100]; /* declare table of square roots */
344 _mesa_init_sqrt_table(void)
346 #if defined(USE_IEEE) && !defined(DEBUG)
348 fi_type fi; /* to access the bits of a float in C quickly */
349 /* we use a union defined in glheader.h */
351 for(i=0; i<= 0x7f; i++) {
355 * Build a float with the bit pattern i as mantissa
356 * and an exponent of 0, stored as 127
359 fi.i = (i << 16) | (127 << 23);
360 fi.f = _mesa_sqrtd(fi.f);
363 * Take the square root then strip the first 7 bits of
364 * the mantissa into the table
367 sqrttab[i] = (fi.i & 0x7fffff) >> 16;
370 * Repeat the process, this time with an exponent of
375 fi.i = (i << 16) | (128 << 23);
377 sqrttab[i+0x80] = (fi.i & 0x7fffff) >> 16;
380 (void) sqrttab; /* silence compiler warnings */
381 #endif /*HAVE_FAST_MATH*/
386 * Single precision square root.
389 _mesa_sqrtf( float x )
391 #if defined(USE_IEEE) && !defined(DEBUG)
393 /* to access the bits of a float in C
394 * we use a union from glheader.h */
396 short e; /* the exponent */
397 if (x == 0.0F) return 0.0F; /* check for square root of 0 */
399 e = (num.i >> 23) - 127; /* get the exponent - on a SPARC the */
400 /* exponent is stored with 127 added */
401 num.i &= 0x7fffff; /* leave only the mantissa */
402 if (e & 0x01) num.i |= 0x800000;
403 /* the exponent is odd so we have to */
404 /* look it up in the second half of */
405 /* the lookup table, so we set the */
407 e >>= 1; /* divide the exponent by two */
408 /* note that in C the shift */
409 /* operators are sign preserving */
410 /* for signed operands */
411 /* Do the table lookup, based on the quaternary mantissa,
412 * then reconstruct the result back into a float
414 num.i = ((sqrttab[num.i >> 16]) << 16) | ((e + 127) << 23);
418 return (float) _mesa_sqrtd((double) x);
424 inv_sqrt - A single precision 1/sqrt routine for IEEE format floats.
425 written by Josh Vanderhoof, based on newsgroup posts by James Van Buskirk
429 _mesa_inv_sqrtf(float n)
431 #if defined(USE_IEEE) && !defined(DEBUG)
435 #if 0 /* not used, see below -BP */
438 union { float f; unsigned int i; } u;
442 Exponent part of the magic number -
445 1. subtract the bias from the exponent,
447 3. divide by two (rounding towards -inf)
450 Which is the same as subtracting the exponent from 381 and dividing
453 floor(-(x - 127) / 2) + 127 = floor((381 - x) / 2)
459 Significand part of magic number -
461 With the current magic number, "(magic - u.i) >> 1" will give you:
463 for 1 <= u.f <= 2: 1.25 - u.f / 4
464 for 2 <= u.f <= 4: 1.00 - u.f / 8
466 This isn't a bad approximation of 1/sqrt. The maximum difference from
467 1/sqrt will be around .06. After three Newton-Raphson iterations, the
468 maximum difference is less than 4.5e-8. (Which is actually close
469 enough to make the following bias academic...)
471 To get a better approximation you can add a bias to the magic
472 number. For example, if you subtract 1/2 of the maximum difference in
473 the first approximation (.03), you will get the following function:
475 for 1 <= u.f <= 2: 1.22 - u.f / 4
476 for 2 <= u.f <= 3.76: 0.97 - u.f / 8
477 for 3.76 <= u.f <= 4: 0.72 - u.f / 16
478 (The 3.76 to 4 range is where the result is < .5.)
480 This is the closest possible initial approximation, but with a maximum
481 error of 8e-11 after three NR iterations, it is still not perfect. If
482 you subtract 0.0332281 instead of .03, the maximum error will be
483 2.5e-11 after three NR iterations, which should be about as close as
486 for 1 <= u.f <= 2: 1.2167719 - u.f / 4
487 for 2 <= u.f <= 3.73: 0.9667719 - u.f / 8
488 for 3.73 <= u.f <= 4: 0.7167719 - u.f / 16
492 magic -= (int)(0.0332281 * (1 << 25));
495 u.i = (magic - u.i) >> 1;
498 Instead of Newton-Raphson, we use Goldschmidt's algorithm, which
499 allows more parallelism. From what I understand, the parallelism
500 comes at the cost of less precision, because it lets error
501 accumulate across iterations.
516 return x2 * r2; /* we can stop here, and be conformant -BP */
525 return (float) (1.0 / sqrt(n));
530 /** Wrapper around pow() */
532 _mesa_pow(double x, double y)
539 * Find the first bit set in a word.
544 #if (defined(_WIN32) && !defined(__MINGW32__) ) || defined(__IBMC__) || defined(__IBMCPP__)
545 register int bit = 0;
547 if ((i & 0xffff) == 0) {
551 if ((i & 0xff) == 0) {
555 if ((i & 0xf) == 0) {
559 while ((i & 1) == 0) {
572 * Find position of first bit set in given value.
573 * XXX Warning: this function can only be used on 64-bit systems!
574 * \return position of least-significant bit set, starting at 1, return zero
578 _mesa_ffsll(long long val)
585 assert(sizeof(val) == 8);
587 bit = _mesa_ffs(val);
591 bit = _mesa_ffs(val >> 32);
601 * Return number of bits set in given GLuint.
604 _mesa_bitcount(unsigned int n)
607 for (bits = 0; n > 0; n = n >> 1) {
615 * Convert a 4-byte float to a 2-byte half float.
616 * Based on code from:
617 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
620 _mesa_float_to_half(float val)
622 const int flt = *((int *) (void *) &val);
623 const int flt_m = flt & 0x7fffff;
624 const int flt_e = (flt >> 23) & 0xff;
625 const int flt_s = (flt >> 31) & 0x1;
632 /* handle special cases */
633 if ((flt_e == 0) && (flt_m == 0)) {
635 /* m = 0; - already set */
638 else if ((flt_e == 0) && (flt_m != 0)) {
639 /* denorm -- denorm float maps to 0 half */
640 /* m = 0; - already set */
643 else if ((flt_e == 0xff) && (flt_m == 0)) {
645 /* m = 0; - already set */
648 else if ((flt_e == 0xff) && (flt_m != 0)) {
655 const int new_exp = flt_e - 127;
658 /* m = 0; - already set */
661 else if (new_exp < -14) {
662 /* this maps to a denorm */
663 unsigned int exp_val = (unsigned int) (-14 - new_exp); /* 2^-exp_val*/
668 "float_to_half: logical error in denorm creation!\n");
669 /* m = 0; - already set */
671 case 1: m = 512 + (flt_m >> 14); break;
672 case 2: m = 256 + (flt_m >> 15); break;
673 case 3: m = 128 + (flt_m >> 16); break;
674 case 4: m = 64 + (flt_m >> 17); break;
675 case 5: m = 32 + (flt_m >> 18); break;
676 case 6: m = 16 + (flt_m >> 19); break;
677 case 7: m = 8 + (flt_m >> 20); break;
678 case 8: m = 4 + (flt_m >> 21); break;
679 case 9: m = 2 + (flt_m >> 22); break;
680 case 10: m = 1; break;
683 else if (new_exp > 15) {
684 /* map this value to infinity */
685 /* m = 0; - already set */
695 result = (s << 15) | (e << 10) | m;
701 * Convert a 2-byte half float to a 4-byte float.
702 * Based on code from:
703 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
706 _mesa_half_to_float(GLhalfARB val)
708 /* XXX could also use a 64K-entry lookup table */
709 const int m = val & 0x3ff;
710 const int e = (val >> 10) & 0x1f;
711 const int s = (val >> 15) & 0x1;
712 int flt_m, flt_e, flt_s, flt;
718 /* handle special cases */
719 if ((e == 0) && (m == 0)) {
724 else if ((e == 0) && (m != 0)) {
725 /* denorm -- denorm half will fit in non-denorm single */
726 const float half_denorm = 1.0f / 16384.0f; /* 2^-14 */
727 float mantissa = ((float) (m)) / 1024.0f;
728 float sign = s ? -1.0f : 1.0f;
729 return sign * mantissa * half_denorm;
731 else if ((e == 31) && (m == 0)) {
736 else if ((e == 31) && (m != 0)) {
747 flt = (flt_s << 31) | (flt_e << 23) | flt_m;
748 result = *((float *) (void *) &flt);
755 /**********************************************************************/
756 /** \name Sort & Search */
760 * Wrapper for bsearch().
763 _mesa_bsearch( const void *key, const void *base, size_t nmemb, size_t size,
764 int (*compar)(const void *, const void *) )
766 return bsearch(key, base, nmemb, size, compar);
772 /**********************************************************************/
773 /** \name Environment vars */
777 * Wrapper for getenv().
780 _mesa_getenv( const char *var )
792 /**********************************************************************/
796 /** Wrapper around strstr() */
798 _mesa_strstr( const char *haystack, const char *needle )
800 return strstr(haystack, needle);
803 /** Wrapper around strncat() */
805 _mesa_strncat( char *dest, const char *src, size_t n )
807 return strncat(dest, src, n);
810 /** Wrapper around strcpy() */
812 _mesa_strcpy( char *dest, const char *src )
814 return strcpy(dest, src);
817 /** Wrapper around strncpy() */
819 _mesa_strncpy( char *dest, const char *src, size_t n )
821 return strncpy(dest, src, n);
824 /** Wrapper around strlen() */
826 _mesa_strlen( const char *s )
831 /** Wrapper around strcmp() */
833 _mesa_strcmp( const char *s1, const char *s2 )
835 return strcmp(s1, s2);
838 /** Wrapper around strncmp() */
840 _mesa_strncmp( const char *s1, const char *s2, size_t n )
842 return strncmp(s1, s2, n);
846 * Implemented using _mesa_malloc() and _mesa_strcpy.
847 * Note that NULL is handled accordingly.
850 _mesa_strdup( const char *s )
853 size_t l = _mesa_strlen(s);
854 char *s2 = (char *) _mesa_malloc(l + 1);
864 /** Wrapper around atoi() */
866 _mesa_atoi(const char *s)
871 /** Wrapper around strtod() */
873 _mesa_strtod( const char *s, char **end )
875 return strtod(s, end);
881 /**********************************************************************/
885 /** Wrapper around vsprintf() */
887 _mesa_sprintf( char *str, const char *fmt, ... )
891 va_start( args, fmt );
892 r = vsprintf( str, fmt, args );
897 /** Wrapper around printf(), using vsprintf() for the formatting. */
899 _mesa_printf( const char *fmtString, ... )
903 va_start( args, fmtString );
904 vsnprintf(s, MAXSTRING, fmtString, args);
906 fprintf(stderr,"%s", s);
909 /** Wrapper around vsprintf() */
911 _mesa_vsprintf( char *str, const char *fmt, va_list args )
913 return vsprintf( str, fmt, args );
919 /**********************************************************************/
920 /** \name Diagnostics */
924 * Report a warning (a recoverable error condition) to stderr if
925 * either DEBUG is defined or the MESA_DEBUG env var is set.
927 * \param ctx GL context.
928 * \param fmtString printf() alike format string.
931 _mesa_warning( GLcontext *ctx, const char *fmtString, ... )
937 va_start( args, fmtString );
938 (void) vsnprintf( str, MAXSTRING, fmtString, args );
941 debug = GL_TRUE; /* always print warning */
943 debug = _mesa_getenv("MESA_DEBUG") ? GL_TRUE : GL_FALSE;
946 fprintf(stderr, "Mesa warning: %s\n", str);
951 * Report an internla implementation problem.
952 * Prints the message to stderr via fprintf().
954 * \param ctx GL context.
955 * \param s problem description string.
958 _mesa_problem( const GLcontext *ctx, const char *fmtString, ... )
964 va_start( args, fmtString );
965 vsnprintf( str, MAXSTRING, fmtString, args );
968 fprintf(stderr, "Mesa %s implementation error: %s\n", MESA_VERSION_STRING, str);
969 fprintf(stderr, "Please report at bugzilla.freedesktop.org\n");
973 * Record an OpenGL state error. These usually occur when the users
974 * passes invalid parameters to a GL function.
976 * If debugging is enabled (either at compile-time via the DEBUG macro, or
977 * run-time via the MESA_DEBUG environment variable), report the error with
980 * \param ctx the GL context.
981 * \param error the error value.
982 * \param fmtString printf() style format string, followed by optional args
985 _mesa_error( GLcontext *ctx, GLenum error, const char *fmtString, ... )
987 const char *debugEnv;
990 debugEnv = _mesa_getenv("MESA_DEBUG");
993 if (debugEnv && _mesa_strstr(debugEnv, "silent"))
1006 char where[MAXSTRING];
1009 va_start( args, fmtString );
1010 vsnprintf( where, MAXSTRING, fmtString, args );
1015 errstr = "GL_NO_ERROR";
1017 case GL_INVALID_VALUE:
1018 errstr = "GL_INVALID_VALUE";
1020 case GL_INVALID_ENUM:
1021 errstr = "GL_INVALID_ENUM";
1023 case GL_INVALID_OPERATION:
1024 errstr = "GL_INVALID_OPERATION";
1026 case GL_STACK_OVERFLOW:
1027 errstr = "GL_STACK_OVERFLOW";
1029 case GL_STACK_UNDERFLOW:
1030 errstr = "GL_STACK_UNDERFLOW";
1032 case GL_OUT_OF_MEMORY:
1033 errstr = "GL_OUT_OF_MEMORY";
1035 case GL_TABLE_TOO_LARGE:
1036 errstr = "GL_TABLE_TOO_LARGE";
1038 case GL_INVALID_FRAMEBUFFER_OPERATION_EXT:
1039 errstr = "GL_INVALID_FRAMEBUFFER_OPERATION";
1045 _mesa_debug(ctx, "User error: %s in %s\n", errstr, where);
1048 _mesa_record_error(ctx, error);
1052 * Report debug information. Print error message to stderr via fprintf().
1053 * No-op if DEBUG mode not enabled.
1055 * \param ctx GL context.
1056 * \param fmtString printf()-style format string, followed by optional args.
1059 _mesa_debug( const GLcontext *ctx, const char *fmtString, ... )
1064 va_start(args, fmtString);
1065 vsnprintf(s, MAXSTRING, fmtString, args);
1067 fprintf(stderr, "Mesa: %s", s);
1077 * Wrapper for exit().
1080 _mesa_exit( int status )