2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * Standard C library function wrappers.
30 * This file provides wrappers for all the standard C library functions
31 * like malloc(), free(), printf(), getenv(), etc.
39 /* XXX some of the stuff in glheader.h should be moved into this file.
42 #include <GL/internal/glcore.h>
50 /**********************************************************************/
51 /** \name General macros */
59 /** gcc -pedantic warns about long string literals, LONGSTRING silences that */
60 #if !defined(__GNUC__) || (__GNUC__ < 2) || \
61 ((__GNUC__ == 2) && (__GNUC_MINOR__ <= 7))
64 # define LONGSTRING __extension__
70 /**********************************************************************/
74 /** Allocate \p BYTES bytes */
75 #define MALLOC(BYTES) _mesa_malloc(BYTES)
76 /** Allocate and zero \p BYTES bytes */
77 #define CALLOC(BYTES) _mesa_calloc(BYTES)
78 /** Allocate a structure of type \p T */
79 #define MALLOC_STRUCT(T) (struct T *) _mesa_malloc(sizeof(struct T))
80 /** Allocate and zero a structure of type \p T */
81 #define CALLOC_STRUCT(T) (struct T *) _mesa_calloc(sizeof(struct T))
83 #define FREE(PTR) _mesa_free(PTR)
85 /** Allocate \p BYTES aligned at \p N bytes */
86 #define ALIGN_MALLOC(BYTES, N) _mesa_align_malloc(BYTES, N)
87 /** Allocate and zero \p BYTES bytes aligned at \p N bytes */
88 #define ALIGN_CALLOC(BYTES, N) _mesa_align_calloc(BYTES, N)
89 /** Allocate a structure of type \p T aligned at \p N bytes */
90 #define ALIGN_MALLOC_STRUCT(T, N) (struct T *) _mesa_align_malloc(sizeof(struct T), N)
91 /** Allocate and zero a structure of type \p T aligned at \p N bytes */
92 #define ALIGN_CALLOC_STRUCT(T, N) (struct T *) _mesa_align_calloc(sizeof(struct T), N)
93 /** Free aligned memory */
94 #define ALIGN_FREE(PTR) _mesa_align_free(PTR)
96 /** Copy \p BYTES bytes from \p SRC into \p DST */
97 #define MEMCPY( DST, SRC, BYTES) _mesa_memcpy(DST, SRC, BYTES)
98 /** Set \p N bytes in \p DST to \p VAL */
99 #define MEMSET( DST, VAL, N ) _mesa_memset(DST, VAL, N)
105 * For GL_ARB_vertex_buffer_object we need to treat vertex array pointers
106 * as offsets into buffer stores. Since the vertex array pointer and
107 * buffer store pointer are both pointers and we need to add them, we use
109 * Both pointers/offsets are expressed in bytes.
111 #define ADD_POINTERS(A, B) ( (GLubyte *) (A) + (uintptr_t) (B) )
115 * Sometimes we treat GLfloats as GLints. On x86 systems, moving a float
116 * as a int (thereby using integer registers instead of FP registers) is
117 * a performance win. Typically, this can be done with ordinary casts.
118 * But with gcc's -fstrict-aliasing flag (which defaults to on in gcc 3.0)
119 * these casts generate warnings.
120 * The following union typedef is used to solve that.
122 typedef union { GLfloat f
; GLint i
; } fi_type
;
126 /**********************************************************************
130 #define MAX_GLUSHORT 0xffff
131 #define MAX_GLUINT 0xffffffff
134 #define M_PI (3.1415926536)
138 #define M_E (2.7182818284590452354)
142 #define ONE_DIV_LN2 (1.442695040888963456)
145 #ifndef ONE_DIV_SQRT_LN2
146 #define ONE_DIV_SQRT_LN2 (1.201122408786449815)
150 #define FLT_MAX_EXP 128
153 /* Degrees to radians conversion: */
154 #define DEG2RAD (M_PI/180.0)
158 *** USE_IEEE: Determine if we're using IEEE floating point
160 #if defined(__i386__) || defined(__386__) || defined(__sparc__) || \
161 defined(__s390x__) || defined(__powerpc__) || \
162 defined(__amd64__) || \
163 defined(ia64) || defined(__ia64__) || \
164 defined(__hppa__) || defined(hpux) || \
165 defined(__mips) || defined(_MIPS_ARCH) || \
166 defined(__arm__) || \
167 defined(__sh__) || defined(__m32r__) || \
168 (defined(__alpha__) && (defined(__IEEE_FLOAT) || !defined(VMS)))
170 #define IEEE_ONE 0x3f800000
175 *** SQRTF: single-precision square root
177 #if 0 /* _mesa_sqrtf() not accurate enough - temporarily disabled */
178 # define SQRTF(X) _mesa_sqrtf(X)
180 # define SQRTF(X) (float) sqrt((float) (X))
185 *** INV_SQRTF: single-precision inverse square root
188 #define INV_SQRTF(X) _mesa_inv_sqrt(X)
190 #define INV_SQRTF(X) (1.0F / SQRTF(X)) /* this is faster on a P4 */
195 *** LOG2: Log base 2 of float
199 /* This is pretty fast, but not accurate enough (only 2 fractional bits).
200 * Based on code from http://www.stereopsis.com/log2.html
202 static INLINE GLfloat
LOG2(GLfloat x
)
204 const GLfloat y
= x
* x
* x
* x
;
205 const GLuint ix
= *((GLuint
*) &y
);
206 const GLuint exp
= (ix
>> 23) & 0xFF;
207 const GLint log2
= ((GLint
) exp
) - 127;
208 return (GLfloat
) log2
* (1.0 / 4.0); /* 4, because of x^4 above */
211 /* Pretty fast, and accurate.
212 * Based on code from http://www.flipcode.com/totd/
214 static INLINE GLfloat
LOG2(GLfloat val
)
219 log_2
= ((num
.i
>> 23) & 255) - 128;
220 num
.i
&= ~(255 << 23);
222 num
.f
= ((-1.0f
/3) * num
.f
+ 2) * num
.f
- 2.0f
/3;
223 return num
.f
+ log_2
;
227 * NOTE: log_base_2(x) = log(x) / log(2)
228 * NOTE: 1.442695 = 1/log(2).
230 #define LOG2(x) ((GLfloat) (log(x) * 1.442695F))
235 *** IS_INF_OR_NAN: test if float is infinite or NaN
238 static INLINE
int IS_INF_OR_NAN( float x
)
242 return !(int)((unsigned int)((tmp
.i
& 0x7fffffff)-0x7f800000) >> 31);
244 #elif defined(isfinite)
245 #define IS_INF_OR_NAN(x) (!isfinite(x))
246 #elif defined(finite)
247 #define IS_INF_OR_NAN(x) (!finite(x))
249 #define IS_INF_OR_NAN(x) (!finite(x))
250 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
251 #define IS_INF_OR_NAN(x) (!isfinite(x))
253 #define IS_INF_OR_NAN(x) (!finite(x))
258 *** IS_NEGATIVE: test if float is negative
260 #if defined(USE_IEEE)
261 static INLINE
int GET_FLOAT_BITS( float x
)
267 #define IS_NEGATIVE(x) (GET_FLOAT_BITS(x) < 0)
269 #define IS_NEGATIVE(x) (x < 0.0F)
274 *** DIFFERENT_SIGNS: test if two floats have opposite signs
276 #if defined(USE_IEEE)
277 #define DIFFERENT_SIGNS(x,y) ((GET_FLOAT_BITS(x) ^ GET_FLOAT_BITS(y)) & (1<<31))
279 /* Could just use (x*y<0) except for the flatshading requirements.
280 * Maybe there's a better way?
282 #define DIFFERENT_SIGNS(x,y) ((x) * (y) <= 0.0F && (x) - (y) != 0.0F)
287 *** CEILF: ceiling of float
288 *** FLOORF: floor of float
289 *** FABSF: absolute value of float
290 *** LOGF: the natural logarithm (base e) of the value
291 *** EXPF: raise e to the value
292 *** LDEXPF: multiply value by an integral power of two
293 *** FREXPF: extract mantissa and exponent from value
295 #if defined(__gnu_linux__)
297 #define CEILF(x) ceilf(x)
298 #define FLOORF(x) floorf(x)
299 #define FABSF(x) fabsf(x)
300 #define LOGF(x) logf(x)
301 #define EXPF(x) expf(x)
302 #define LDEXPF(x,y) ldexpf(x,y)
303 #define FREXPF(x,y) frexpf(x,y)
305 #define CEILF(x) ((GLfloat) ceil(x))
306 #define FLOORF(x) ((GLfloat) floor(x))
307 #define FABSF(x) ((GLfloat) fabs(x))
308 #define LOGF(x) ((GLfloat) log(x))
309 #define EXPF(x) ((GLfloat) exp(x))
310 #define LDEXPF(x,y) ((GLfloat) ldexp(x,y))
311 #define FREXPF(x,y) ((GLfloat) frexp(x,y))
316 *** IROUND: return (as an integer) float rounded to nearest integer
318 #if defined(USE_SPARC_ASM) && defined(__GNUC__) && defined(__sparc__)
319 static INLINE
int iround(float f
)
322 __asm__ ("fstoi %1, %0" : "=f" (r
) : "f" (f
));
325 #define IROUND(x) iround(x)
326 #elif defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__) && \
327 (!defined(__BEOS__) || (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)))
328 static INLINE
int iround(float f
)
331 __asm__ ("fistpl %0" : "=m" (r
) : "t" (f
) : "st");
334 #define IROUND(x) iround(x)
335 #elif defined(USE_X86_ASM) && defined(__MSC__) && defined(__WIN32__)
336 static INLINE
int iround(float f
)
345 #define IROUND(x) iround(x)
346 #elif defined(__WATCOMC__) && defined(__386__)
347 long iround(float f
);
348 #pragma aux iround = \
350 "fistp dword ptr [esp]" \
355 #define IROUND(x) iround(x)
357 #define IROUND(f) ((int) (((f) >= 0.0F) ? ((f) + 0.5F) : ((f) - 0.5F)))
362 *** IROUND_POS: return (as an integer) positive float rounded to nearest int
365 #define IROUND_POS(f) (assert((f) >= 0.0F), IROUND(f))
367 #define IROUND_POS(f) (IROUND(f))
372 *** IFLOOR: return (as an integer) floor of float
374 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
376 * IEEE floor for computers that round to nearest or even.
377 * 'f' must be between -4194304 and 4194303.
378 * This floor operation is done by "(iround(f + .5) + iround(f - .5)) >> 1",
379 * but uses some IEEE specific tricks for better speed.
380 * Contributed by Josh Vanderhoof
382 static INLINE
int ifloor(float f
)
386 af
= (3 << 22) + 0.5 + (double)f
;
387 bf
= (3 << 22) + 0.5 - (double)f
;
388 /* GCC generates an extra fstp/fld without this. */
389 __asm__ ("fstps %0" : "=m" (ai
) : "t" (af
) : "st");
390 __asm__ ("fstps %0" : "=m" (bi
) : "t" (bf
) : "st");
391 return (ai
- bi
) >> 1;
393 #define IFLOOR(x) ifloor(x)
394 #elif defined(USE_IEEE)
395 static INLINE
int ifloor(float f
)
401 af
= (3 << 22) + 0.5 + (double)f
;
402 bf
= (3 << 22) + 0.5 - (double)f
;
403 u
.f
= (float) af
; ai
= u
.i
;
404 u
.f
= (float) bf
; bi
= u
.i
;
405 return (ai
- bi
) >> 1;
407 #define IFLOOR(x) ifloor(x)
409 static INLINE
int ifloor(float f
)
412 return (i
> f
) ? i
- 1 : i
;
414 #define IFLOOR(x) ifloor(x)
419 *** ICEIL: return (as an integer) ceiling of float
421 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
423 * IEEE ceil for computers that round to nearest or even.
424 * 'f' must be between -4194304 and 4194303.
425 * This ceil operation is done by "(iround(f + .5) + iround(f - .5) + 1) >> 1",
426 * but uses some IEEE specific tricks for better speed.
427 * Contributed by Josh Vanderhoof
429 static INLINE
int iceil(float f
)
433 af
= (3 << 22) + 0.5 + (double)f
;
434 bf
= (3 << 22) + 0.5 - (double)f
;
435 /* GCC generates an extra fstp/fld without this. */
436 __asm__ ("fstps %0" : "=m" (ai
) : "t" (af
) : "st");
437 __asm__ ("fstps %0" : "=m" (bi
) : "t" (bf
) : "st");
438 return (ai
- bi
+ 1) >> 1;
440 #define ICEIL(x) iceil(x)
441 #elif defined(USE_IEEE)
442 static INLINE
int iceil(float f
)
447 af
= (3 << 22) + 0.5 + (double)f
;
448 bf
= (3 << 22) + 0.5 - (double)f
;
449 u
.f
= (float) af
; ai
= u
.i
;
450 u
.f
= (float) bf
; bi
= u
.i
;
451 return (ai
- bi
+ 1) >> 1;
453 #define ICEIL(x) iceil(x)
455 static INLINE
int iceil(float f
)
458 return (i
< f
) ? i
+ 1 : i
;
460 #define ICEIL(x) iceil(x)
465 *** UNCLAMPED_FLOAT_TO_UBYTE: clamp float to [0,1] and map to ubyte in [0,255]
466 *** CLAMPED_FLOAT_TO_UBYTE: map float known to be in [0,1] to ubyte in [0,255]
468 #if defined(USE_IEEE) && !defined(DEBUG)
469 #define IEEE_0996 0x3f7f0000 /* 0.996 or so */
470 /* This function/macro is sensitive to precision. Test very carefully
473 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
479 else if (__tmp.i >= IEEE_0996) \
480 UB = (GLubyte) 255; \
482 __tmp.f = __tmp.f * (255.0F/256.0F) + 32768.0F; \
483 UB = (GLubyte) __tmp.i; \
486 #define CLAMPED_FLOAT_TO_UBYTE(UB, F) \
489 __tmp.f = (F) * (255.0F/256.0F) + 32768.0F; \
490 UB = (GLubyte) __tmp.i; \
493 #define UNCLAMPED_FLOAT_TO_UBYTE(ub, f) \
494 ub = ((GLubyte) IROUND(CLAMP((f), 0.0F, 1.0F) * 255.0F))
495 #define CLAMPED_FLOAT_TO_UBYTE(ub, f) \
496 ub = ((GLubyte) IROUND((f) * 255.0F))
501 *** START_FAST_MATH: Set x86 FPU to faster, 32-bit precision mode (and save
502 *** original mode to a temporary).
503 *** END_FAST_MATH: Restore x86 FPU to original mode.
505 #if defined(__GNUC__) && defined(__i386__)
507 * Set the x86 FPU control word to guarentee only 32 bits of precision
508 * are stored in registers. Allowing the FPU to store more introduces
509 * differences between situations where numbers are pulled out of memory
510 * vs. situations where the compiler is able to optimize register usage.
512 * In the worst case, we force the compiler to use a memory access to
513 * truncate the float, by specifying the 'volatile' keyword.
515 /* Hardware default: All exceptions masked, extended double precision,
516 * round to nearest (IEEE compliant):
518 #define DEFAULT_X86_FPU 0x037f
519 /* All exceptions masked, single precision, round to nearest:
521 #define FAST_X86_FPU 0x003f
522 /* The fldcw instruction will cause any pending FP exceptions to be
523 * raised prior to entering the block, and we clear any pending
524 * exceptions before exiting the block. Hence, asm code has free
525 * reign over the FPU while in the fast math block.
527 #if defined(NO_FAST_MATH)
528 #define START_FAST_MATH(x) \
530 static GLuint mask = DEFAULT_X86_FPU; \
531 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
532 __asm__ ( "fldcw %0" : : "m" (mask) ); \
535 #define START_FAST_MATH(x) \
537 static GLuint mask = FAST_X86_FPU; \
538 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
539 __asm__ ( "fldcw %0" : : "m" (mask) ); \
542 /* Restore original FPU mode, and clear any exceptions that may have
543 * occurred in the FAST_MATH block.
545 #define END_FAST_MATH(x) \
547 __asm__ ( "fnclex ; fldcw %0" : : "m" (*&(x)) ); \
550 #elif defined(__WATCOMC__) && defined(__386__)
551 #define DEFAULT_X86_FPU 0x037f /* See GCC comments above */
552 #define FAST_X86_FPU 0x003f /* See GCC comments above */
553 void _watcom_start_fast_math(unsigned short *x
,unsigned short *mask
);
554 #pragma aux _watcom_start_fast_math = \
555 "fnstcw word ptr [eax]" \
556 "fldcw word ptr [ecx]" \
559 void _watcom_end_fast_math(unsigned short *x
);
560 #pragma aux _watcom_end_fast_math = \
562 "fldcw word ptr [eax]" \
565 #if defined(NO_FAST_MATH)
566 #define START_FAST_MATH(x) \
568 static GLushort mask = DEFAULT_X86_FPU; \
569 _watcom_start_fast_math(&x,&mask); \
572 #define START_FAST_MATH(x) \
574 static GLushort mask = FAST_X86_FPU; \
575 _watcom_start_fast_math(&x,&mask); \
578 #define END_FAST_MATH(x) _watcom_end_fast_math(&x)
580 #elif defined(_MSC_VER) && defined(_M_IX86)
581 #define DEFAULT_X86_FPU 0x037f /* See GCC comments above */
582 #define FAST_X86_FPU 0x003f /* See GCC comments above */
583 #if defined(NO_FAST_MATH)
584 #define START_FAST_MATH(x) do {\
585 static GLuint mask = DEFAULT_X86_FPU;\
586 __asm fnstcw word ptr [x]\
587 __asm fldcw word ptr [mask]\
590 #define START_FAST_MATH(x) do {\
591 static GLuint mask = FAST_X86_FPU;\
592 __asm fnstcw word ptr [x]\
593 __asm fldcw word ptr [mask]\
596 #define END_FAST_MATH(x) do {\
598 __asm fldcw word ptr [x]\
602 #define START_FAST_MATH(x) x = 0
603 #define END_FAST_MATH(x) (void)(x)
608 * Return 1 if this is a little endian machine, 0 if big endian.
610 static INLINE GLboolean
611 _mesa_little_endian(void)
613 const GLuint ui
= 1; /* intentionally not static */
614 return *((const GLubyte
*) &ui
);
619 /**********************************************************************
624 _mesa_malloc( size_t bytes
);
627 _mesa_calloc( size_t bytes
);
630 _mesa_free( void *ptr
);
633 _mesa_align_malloc( size_t bytes
, unsigned long alignment
);
636 _mesa_align_calloc( size_t bytes
, unsigned long alignment
);
639 _mesa_align_free( void *ptr
);
642 _mesa_align_realloc(void *oldBuffer
, size_t oldSize
, size_t newSize
,
643 unsigned long alignment
);
646 _mesa_exec_malloc( GLuint size
);
649 _mesa_exec_free( void *addr
);
652 _mesa_realloc( void *oldBuffer
, size_t oldSize
, size_t newSize
);
655 _mesa_memcpy( void *dest
, const void *src
, size_t n
);
658 _mesa_memset( void *dst
, int val
, size_t n
);
661 _mesa_memset16( unsigned short *dst
, unsigned short val
, size_t n
);
664 _mesa_bzero( void *dst
, size_t n
);
667 _mesa_memcmp( const void *s1
, const void *s2
, size_t n
);
679 _mesa_asinf(float x
);
682 _mesa_atanf(float x
);
685 _mesa_sqrtd(double x
);
688 _mesa_sqrtf(float x
);
691 _mesa_inv_sqrtf(float x
);
694 _mesa_init_sqrt_table(void);
697 _mesa_pow(double x
, double y
);
706 _mesa_ffsll(long long i
);
710 _mesa_bitcount(unsigned int n
);
713 _mesa_float_to_half(float f
);
716 _mesa_half_to_float(GLhalfARB h
);
720 _mesa_bsearch( const void *key
, const void *base
, size_t nmemb
, size_t size
,
721 int (*compar
)(const void *, const void *) );
724 _mesa_getenv( const char *var
);
727 _mesa_strstr( const char *haystack
, const char *needle
);
730 _mesa_strncat( char *dest
, const char *src
, size_t n
);
733 _mesa_strcpy( char *dest
, const char *src
);
736 _mesa_strncpy( char *dest
, const char *src
, size_t n
);
739 _mesa_strlen( const char *s
);
742 _mesa_strcmp( const char *s1
, const char *s2
);
745 _mesa_strncmp( const char *s1
, const char *s2
, size_t n
);
748 _mesa_strdup( const char *s
);
751 _mesa_atoi( const char *s
);
754 _mesa_strtod( const char *s
, char **end
);
757 _mesa_sprintf( char *str
, const char *fmt
, ... );
760 _mesa_printf( const char *fmtString
, ... );
763 _mesa_vsprintf( char *str
, const char *fmt
, va_list args
);
767 _mesa_warning( __GLcontext
*gc
, const char *fmtString
, ... );
770 _mesa_problem( const __GLcontext
*ctx
, const char *fmtString
, ... );
773 _mesa_error( __GLcontext
*ctx
, GLenum error
, const char *fmtString
, ... );
776 _mesa_debug( const __GLcontext
*ctx
, const char *fmtString
, ... );
779 _mesa_exit( int status
);
787 #endif /* IMPORTS_H */