2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2003 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.
27 * This file provides wrappers for all the standard C library functions
28 * like malloc, free, printf, getenv, etc.
36 /* XXX some of the stuff in glheader.h should be moved into this file.
46 /**********************************************************************
56 /**********************************************************************
60 #define MALLOC(BYTES) _mesa_malloc(BYTES)
61 #define CALLOC(BYTES) _mesa_calloc(BYTES)
62 #define MALLOC_STRUCT(T) (struct T *) _mesa_malloc(sizeof(struct T))
63 #define CALLOC_STRUCT(T) (struct T *) _mesa_calloc(sizeof(struct T))
64 #define FREE(PTR) _mesa_free(PTR)
66 #define ALIGN_MALLOC(BYTES, N) _mesa_align_malloc(BYTES, N)
67 #define ALIGN_CALLOC(BYTES, N) _mesa_align_calloc(BYTES, N)
68 #define ALIGN_MALLOC_STRUCT(T, N) (struct T *) _mesa_align_malloc(sizeof(struct T), N)
69 #define ALIGN_CALLOC_STRUCT(T, N) (struct T *) _mesa_align_calloc(sizeof(struct T), N)
70 #define ALIGN_FREE(PTR) _mesa_align_free(PTR)
72 #define MEMCPY( DST, SRC, BYTES) _mesa_memcpy(DST, SRC, BYTES)
73 #define MEMSET( DST, VAL, N ) _mesa_memset(DST, VAL, N)
75 #define MEMSET16( DST, VAL, N ) _mesa_memset16( (DST), (VAL), (size_t) (N) )
78 /* MACs and BeOS don't support static larger than 32kb, so... */
79 #if defined(macintosh) && !defined(__MRC__)
80 /*extern char *AGLAlloc(int size);*/
81 /*extern void AGLFree(char* ptr);*/
82 # define DEFARRAY(TYPE,NAME,SIZE) TYPE *NAME = (TYPE*)_mesa_alloc(sizeof(TYPE)*(SIZE))
83 # define DEFMARRAY(TYPE,NAME,SIZE1,SIZE2) TYPE (*NAME)[SIZE2] = (TYPE(*)[SIZE2])_mesa_alloc(sizeof(TYPE)*(SIZE1)*(SIZE2))
84 # define DEFMNARRAY(TYPE,NAME,SIZE1,SIZE2,SIZE3) TYPE (*NAME)[SIZE2][SIZE3] = (TYPE(*)[SIZE2][SIZE3])_mesa_alloc(sizeof(TYPE)*(SIZE1)*(SIZE2)*(SIZE3))
86 # define CHECKARRAY(NAME,CMD) do {if (!(NAME)) {CMD;}} while (0)
87 # define UNDEFARRAY(NAME) do {if ((NAME)) {_mesa_free((char*)NAME);} }while (0)
88 #elif defined(__BEOS__)
89 # define DEFARRAY(TYPE,NAME,SIZE) TYPE *NAME = (TYPE*)_mesa_malloc(sizeof(TYPE)*(SIZE))
90 # define DEFMARRAY(TYPE,NAME,SIZE1,SIZE2) TYPE (*NAME)[SIZE2] = (TYPE(*)[SIZE2])_mesa_malloc(sizeof(TYPE)*(SIZE1)*(SIZE2))
91 # define DEFMNARRAY(TYPE,NAME,SIZE1,SIZE2,SIZE3) TYPE (*NAME)[SIZE2][SIZE3] = (TYPE(*)[SIZE2][SIZE3])_mesa_malloc(sizeof(TYPE)*(SIZE1)*(SIZE2)*(SIZE3))
92 # define CHECKARRAY(NAME,CMD) do {if (!(NAME)) {CMD;}} while (0)
93 # define UNDEFARRAY(NAME) do {if ((NAME)) {_mesa_free((char*)NAME);} }while (0)
95 # define DEFARRAY(TYPE,NAME,SIZE) TYPE NAME[SIZE]
96 # define DEFMARRAY(TYPE,NAME,SIZE1,SIZE2) TYPE NAME[SIZE1][SIZE2]
97 # define DEFMNARRAY(TYPE,NAME,SIZE1,SIZE2,SIZE3) TYPE NAME[SIZE1][SIZE2][SIZE3]
98 # define CHECKARRAY(NAME,CMD) do {} while(0)
99 # define UNDEFARRAY(NAME)
103 #ifdef MESA_EXTERNAL_BUFFERALLOC
105 * If you want Mesa's depth/stencil/accum/etc buffers to be allocated
106 * with a specialized allocator you can define MESA_EXTERNAL_BUFFERALLOC
107 * and implement _ext_mesa_alloc/free_pixelbuffer() in your app.
108 * Contributed by Gerk Huisma (gerk@five-d.demon.nl).
110 extern void *_ext_mesa_alloc_pixelbuffer( unsigned int size
);
111 extern void _ext_mesa_free_pixelbuffer( void *pb
);
113 #define MESA_PBUFFER_ALLOC(BYTES) (void *) _ext_mesa_alloc_pixelbuffer(BYTES)
114 #define MESA_PBUFFER_FREE(PTR) _ext_mesa_free_pixelbuffer(PTR)
116 /* Default buffer allocation uses the aligned allocation routines: */
117 #define MESA_PBUFFER_ALLOC(BYTES) (void *) _mesa_align_malloc(BYTES, 512)
118 #define MESA_PBUFFER_FREE(PTR) _mesa_align_free(PTR)
123 /**********************************************************************
127 #define MAX_GLUSHORT 0xffff
128 #define MAX_GLUINT 0xffffffff
131 #define M_PI (3.1415926)
134 /* Degrees to radians conversion: */
135 #define DEG2RAD (M_PI/180.0)
139 *** USE_IEEE: Determine if we're using IEEE floating point
141 #if defined(__i386__) || defined(__sparc__) || defined(__s390x__) || \
142 defined(__powerpc__) || \
143 ( defined(__alpha__) && ( defined(__IEEE_FLOAT) || !defined(VMS) ) )
145 #define IEEE_ONE 0x3f800000
150 *** SQRTF: single-precision square root
152 #if defined(__WATCOMC__) && defined(USE_X86_ASM)
153 float asm_sqrt (float x
);
154 #pragma aux asm_sqrt = \
159 # define SQRTF(X) asm_sqrt(X)
160 #elif 0 /* _mesa_sqrtf() not accurate enough - temporarily disabled */
161 # define SQRTF(X) _mesa_sqrtf(X)
162 #elif defined(XFree86LOADER) && defined(IN_MODULE)
163 # define SQRTF(X) (float) xf86sqrt((float) (X))
165 # define SQRTF(X) (float) sqrt((float) (X))
170 *** INV_SQRTF: single-precision inverse square root
173 #define INV_SQRTF(X) _mesa_inv_sqrt(X)
175 #define INV_SQRTF(X) (1.0F / SQRTF(X)) /* this is faster on a P4 */
180 *** LOG2: Log base 2 of float
184 /* This is pretty fast, but not accurate enough (only 2 fractional bits).
185 * Based on code from http://www.stereopsis.com/log2.html
187 static INLINE GLfloat
LOG2(GLfloat x
)
189 const GLfloat y
= x
* x
* x
* x
;
190 const GLuint ix
= *((GLuint
*) &y
);
191 const GLuint exp
= (ix
>> 23) & 0xFF;
192 const GLint log2
= ((GLint
) exp
) - 127;
193 return (GLfloat
) log2
* (1.0 / 4.0); /* 4, because of x^4 above */
196 /* Pretty fast, and accurate.
197 * Based on code from http://www.flipcode.com/totd/
199 static INLINE GLfloat
LOG2(GLfloat val
)
204 log_2
= ((num
.i
>> 23) & 255) - 128;
205 num
.i
&= ~(255 << 23);
207 num
.f
= ((-1.0f
/3) * num
.f
+ 2) * num
.f
- 2.0f
/3;
208 return num
.f
+ log_2
;
210 #elif defined(XFree86LOADER) && defined(IN_MODULE)
211 #define LOG2(x) ((GLfloat) (xf86log(x) * 1.442695))
214 * NOTE: log_base_2(x) = log(x) / log(2)
215 * NOTE: 1.442695 = 1/log(2).
217 #define LOG2(x) ((GLfloat) (log(x) * 1.442695F))
222 *** IS_INF_OR_NAN: test if float is infinite or NaN
225 static INLINE
int IS_INF_OR_NAN( float x
)
229 return !(int)((unsigned int)((tmp
.i
& 0x7fffffff)-0x7f800000) >> 31);
231 #elif defined(isfinite)
232 #define IS_INF_OR_NAN(x) (!isfinite(x))
233 #elif defined(finite)
234 #define IS_INF_OR_NAN(x) (!finite(x))
236 #define IS_INF_OR_NAN(x) (!finite(x))
237 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
238 #define IS_INF_OR_NAN(x) (!isfinite(x))
240 #define IS_INF_OR_NAN(x) (!finite(x))
245 *** IS_NEGATIVE: test if float is negative
247 #if defined(USE_IEEE)
248 #define GET_FLOAT_BITS(x) ((fi_type *) &(x))->i
249 #define IS_NEGATIVE(x) (GET_FLOAT_BITS(x) & (1<<31))
251 #define IS_NEGATIVE(x) (x < 0.0F)
256 *** DIFFERENT_SIGNS: test if two floats have opposite signs
258 #if defined(USE_IEEE)
259 #define DIFFERENT_SIGNS(x,y) ((GET_FLOAT_BITS(x) ^ GET_FLOAT_BITS(y)) & (1<<31))
261 /* Could just use (x*y<0) except for the flatshading requirements.
262 * Maybe there's a better way?
264 #define DIFFERENT_SIGNS(x,y) ((x) * (y) <= 0.0F && (x) - (y) != 0.0F)
269 *** CEILF: ceiling of float
270 *** FLOORF: floor of float
271 *** FABSF: absolute value of float
273 #if defined(XFree86LOADER) && defined(IN_MODULE)
274 #define CEILF(x) ((GLfloat) xf86ceil(x))
275 #define FLOORF(x) ((GLfloat) xf86floor(x))
276 #define FABSF(x) ((GLfloat) xf86fabs(x))
277 #elif defined(__gnu_linux__)
279 #define CEILF(x) ceilf(x)
280 #define FLOORF(x) floorf(x)
281 #define FABSF(x) fabsf(x)
283 #define CEILF(x) ((GLfloat) ceil(x))
284 #define FLOORF(x) ((GLfloat) floor(x))
285 #define FABSF(x) ((GLfloat) fabs(x))
290 *** IROUND: return (as an integer) float rounded to nearest integer
292 #if defined(USE_SPARC_ASM) && defined(__GNUC__) && defined(__sparc__)
293 static INLINE
int iround(float f
)
296 __asm__ ("fstoi %1, %0" : "=f" (r
) : "f" (f
));
299 #define IROUND(x) iround(x)
300 #elif defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
301 static INLINE
int iround(float f
)
304 __asm__ ("fistpl %0" : "=m" (r
) : "t" (f
) : "st");
307 #define IROUND(x) iround(x)
308 #elif defined(USE_X86_ASM) && defined(__MSC__) && defined(__WIN32__)
309 static INLINE
int iround(float f
)
318 #define IROUND(x) iround(x)
319 #elif defined(USE_X86_ASM) && defined(__WATCOMC__)
320 long iround(float f
);
321 #pragma aux iround = \
323 "fistp dword ptr [esp]" \
328 #define IROUND(x) iround(x)
329 #elif defined(__OS2__)
331 #define FIST_MAGIC ((((65536.0 * 65536.0 * 16)+(65536.0 * 0.5))* 65536.0))
333 inline int iround(float x
)
335 double dtemp
= FIST_MAGIC
+ x
;
336 return ((*(int *)&dtemp
) - 0x80000000);
338 #define IROUND(f) iround((float)f)
340 #define IROUND(f) ((int) (((f) >= 0.0F) ? ((f) + 0.5F) : ((f) - 0.5F)))
345 *** IROUND_POS: return (as an integer) positive float rounded to nearest int
348 #define IROUND_POS(f) (assert((f) >= 0.0F), IROUND(f))
350 #define IROUND_POS(f) (IROUND(f))
355 *** IFLOOR: return (as an integer) floor of float
357 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
359 * IEEE floor for computers that round to nearest or even.
360 * 'f' must be between -4194304 and 4194303.
361 * This floor operation is done by "(iround(f + .5) + iround(f - .5)) >> 1",
362 * but uses some IEEE specific tricks for better speed.
363 * Contributed by Josh Vanderhoof
365 static INLINE
int ifloor(float f
)
369 af
= (3 << 22) + 0.5 + (double)f
;
370 bf
= (3 << 22) + 0.5 - (double)f
;
371 /* GCC generates an extra fstp/fld without this. */
372 __asm__ ("fstps %0" : "=m" (ai
) : "t" (af
) : "st");
373 __asm__ ("fstps %0" : "=m" (bi
) : "t" (bf
) : "st");
374 return (ai
- bi
) >> 1;
376 #define IFLOOR(x) ifloor(x)
377 #elif defined(USE_IEEE)
378 static INLINE
int ifloor(float f
)
384 af
= (3 << 22) + 0.5 + (double)f
;
385 bf
= (3 << 22) + 0.5 - (double)f
;
386 u
.f
= (float) af
; ai
= u
.i
;
387 u
.f
= (float) bf
; bi
= u
.i
;
388 return (ai
- bi
) >> 1;
390 #define IFLOOR(x) ifloor(x)
392 static INLINE
int ifloor(float f
)
395 return (i
> f
) ? i
- 1 : i
;
397 #define IFLOOR(x) ifloor(x)
402 *** ICEIL: return (as an integer) ceiling of float
404 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
406 * IEEE ceil for computers that round to nearest or even.
407 * 'f' must be between -4194304 and 4194303.
408 * This ceil operation is done by "(iround(f + .5) + iround(f - .5) + 1) >> 1",
409 * but uses some IEEE specific tricks for better speed.
410 * Contributed by Josh Vanderhoof
412 static INLINE
int iceil(float f
)
416 af
= (3 << 22) + 0.5 + (double)f
;
417 bf
= (3 << 22) + 0.5 - (double)f
;
418 /* GCC generates an extra fstp/fld without this. */
419 __asm__ ("fstps %0" : "=m" (ai
) : "t" (af
) : "st");
420 __asm__ ("fstps %0" : "=m" (bi
) : "t" (bf
) : "st");
421 return (ai
- bi
+ 1) >> 1;
423 #define ICEIL(x) iceil(x)
424 #elif defined(USE_IEEE)
425 static INLINE
int iceil(float f
)
430 af
= (3 << 22) + 0.5 + (double)f
;
431 bf
= (3 << 22) + 0.5 - (double)f
;
432 u
.f
= (float) af
; ai
= u
.i
;
433 u
.f
= (float) bf
; bi
= u
.i
;
434 return (ai
- bi
+ 1) >> 1;
436 #define ICEIL(x) iceil(x)
438 static INLINE
int iceil(float f
)
441 return (i
< f
) ? i
+ 1 : i
;
443 #define ICEIL(x) iceil(x)
448 *** UNCLAMPED_FLOAT_TO_UBYTE: map float from {0,1} to ubyte in [0,255]
449 *** CLAMPED_FLOAT_TO_UBYTE: map float in [0,1] to ubyte in [0,255]
451 #if defined(USE_IEEE) && !defined(DEBUG)
452 #define IEEE_0996 0x3f7f0000 /* 0.996 or so */
453 /* This function/macro is sensitive to precision. Test very carefully
456 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
460 UB = ((__tmp.i >= IEEE_0996) \
461 ? ((GLint)__tmp.i < 0) ? (GLubyte)0 : (GLubyte)255 \
462 : (__tmp.f = __tmp.f*(255.0F/256.0F) + 32768.0F, \
463 (GLubyte)__tmp.i)); \
465 #define CLAMPED_FLOAT_TO_UBYTE(ub, f) \
466 UNCLAMPED_FLOAT_TO_UBYTE(ub, f)
468 #define UNCLAMPED_FLOAT_TO_UBYTE(ub, f) \
469 ub = ((GLubyte) IROUND(CLAMP((f), 0.0F, 1.0F) * 255.0F))
470 #define CLAMPED_FLOAT_TO_UBYTE(ub, f) \
471 ub = ((GLubyte) IROUND((f) * 255.0F))
476 *** COPY_FLOAT: copy a float from src to dest, avoid slow FP regs if possible
478 #if defined(USE_IEEE) && !defined(DEBUG)
479 #define COPY_FLOAT( dst, src ) \
480 ((fi_type *) &(dst))->i = ((fi_type *) &(src))->i
482 #define COPY_FLOAT( dst, src ) (dst) = (src)
487 *** START_FAST_MATH: Set x86 FPU to faster, 32-bit precision mode (and save
488 *** original mode to a temporary).
489 *** END_FAST_MATH: Restore x86 FPU to original mode.
491 #if defined(__GNUC__) && defined(__i386__)
493 * Set the x86 FPU control word to guarentee only 32 bits of precision
494 * are stored in registers. Allowing the FPU to store more introduces
495 * differences between situations where numbers are pulled out of memory
496 * vs. situations where the compiler is able to optimize register usage.
498 * In the worst case, we force the compiler to use a memory access to
499 * truncate the float, by specifying the 'volatile' keyword.
501 /* Hardware default: All exceptions masked, extended double precision,
502 * round to nearest (IEEE compliant):
504 #define DEFAULT_X86_FPU 0x037f
505 /* All exceptions masked, single precision, round to nearest:
507 #define FAST_X86_FPU 0x003f
508 /* The fldcw instruction will cause any pending FP exceptions to be
509 * raised prior to entering the block, and we clear any pending
510 * exceptions before exiting the block. Hence, asm code has free
511 * reign over the FPU while in the fast math block.
513 #if defined(NO_FAST_MATH)
514 #define START_FAST_MATH(x) \
516 static GLuint mask = DEFAULT_X86_FPU; \
517 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
518 __asm__ ( "fldcw %0" : : "m" (mask) ); \
521 #define START_FAST_MATH(x) \
523 static GLuint mask = FAST_X86_FPU; \
524 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
525 __asm__ ( "fldcw %0" : : "m" (mask) ); \
528 /* Restore original FPU mode, and clear any exceptions that may have
529 * occurred in the FAST_MATH block.
531 #define END_FAST_MATH(x) \
533 __asm__ ( "fnclex ; fldcw %0" : : "m" (*&(x)) ); \
536 #elif defined(__WATCOMC__) && !defined(NO_FAST_MATH)
537 void _wacom_start_fast_math(unsigned short *x
);
538 #pragma aux _wacom_start_fast_math = \
539 "fstcw word ptr [esi]" \
540 "or word ptr [esi], 0x3f" \
541 "fldcw word ptr [esi]" \
544 void _wacom_end_fast_math(unsigned short *x
);
545 #pragma aux _wacom_end_fast_math = \
546 "fldcw word ptr [esi]" \
549 #define START_FAST_MATH(x) _wacom_start_fast_math(& x)
550 #define END_FAST_MATH(x) _wacom_end_fast_math(& x)
552 #define START_FAST_MATH(x) x = 0
553 #define END_FAST_MATH(x) (void)(x)
558 /**********************************************************************
563 _mesa_malloc( size_t bytes
);
566 _mesa_calloc( size_t bytes
);
569 _mesa_free( void *ptr
);
572 _mesa_align_malloc( size_t bytes
, unsigned long alignment
);
575 _mesa_align_calloc( size_t bytes
, unsigned long alignment
);
578 _mesa_align_free( void *ptr
);
581 _mesa_realloc( void *oldBuffer
, size_t oldSize
, size_t newSize
);
584 _mesa_memcpy( void *dest
, const void *src
, size_t n
);
587 _mesa_memset( void *dst
, int val
, size_t n
);
590 _mesa_memset16( unsigned short *dst
, unsigned short val
, size_t n
);
593 _mesa_bzero( void *dst
, size_t n
);
603 _mesa_sqrtd(double x
);
606 _mesa_sqrtf(float x
);
609 _mesa_inv_sqrtf(float x
);
612 _mesa_pow(double x
, double y
);
618 _mesa_bitcount(unsigned int n
);
622 _mesa_getenv( const char *var
);
625 _mesa_strstr( const char *haystack
, const char *needle
);
628 _mesa_strncat( char *dest
, const char *src
, size_t n
);
631 _mesa_strcpy( char *dest
, const char *src
);
634 _mesa_strncpy( char *dest
, const char *src
, size_t n
);
637 _mesa_strlen( const char *s
);
640 _mesa_strcmp( const char *s1
, const char *s2
);
643 _mesa_strncmp( const char *s1
, const char *s2
, size_t n
);
646 _mesa_strdup( const char *s
);
649 _mesa_atoi( const char *s
);
652 _mesa_strtod( const char *s
, char **end
);
655 _mesa_sprintf( char *str
, const char *fmt
, ... );
658 _mesa_printf( const char *fmtString
, ... );
662 _mesa_warning( __GLcontext
*gc
, const char *fmtString
, ... );
665 _mesa_problem( const __GLcontext
*ctx
, const char *fmtString
, ... );
668 _mesa_error( __GLcontext
*ctx
, GLenum error
, const char *fmtString
, ... );
671 _mesa_debug( const __GLcontext
*ctx
, const char *fmtString
, ... );
675 _mesa_init_default_imports( __GLimports
*imports
, void *driverCtx
);
683 #endif /* IMPORTS_H */