1 /* $Id: imports.h,v 1.13 2003/03/01 01:50:21 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * This file provides wrappers for all the standard C library functions
30 * like malloc, free, printf, getenv, etc.
38 /* XXX some of the stuff in glheader.h should be moved into this file.
43 /**********************************************************************
53 /**********************************************************************
57 #define MALLOC(BYTES) _mesa_malloc(BYTES)
58 #define CALLOC(BYTES) _mesa_calloc(BYTES)
59 #define MALLOC_STRUCT(T) (struct T *) _mesa_malloc(sizeof(struct T))
60 #define CALLOC_STRUCT(T) (struct T *) _mesa_calloc(sizeof(struct T))
61 #define FREE(PTR) _mesa_free(PTR)
63 #define ALIGN_MALLOC(BYTES, N) _mesa_align_malloc(BYTES, N)
64 #define ALIGN_CALLOC(BYTES, N) _mesa_align_calloc(BYTES, N)
65 #define ALIGN_MALLOC_STRUCT(T, N) (struct T *) _mesa_align_malloc(sizeof(struct T), N)
66 #define ALIGN_CALLOC_STRUCT(T, N) (struct T *) _mesa_align_calloc(sizeof(struct T), N)
67 #define ALIGN_FREE(PTR) _mesa_align_free(PTR)
69 #define MEMCPY( DST, SRC, BYTES) _mesa_memcpy(DST, SRC, BYTES)
70 #define MEMSET( DST, VAL, N ) _mesa_memset(DST, VAL, N)
71 extern void _mesa_memset16( GLushort
*dst
, GLushort val
, size_t n
);
73 #define MEMSET16( DST, VAL, N ) \
74 _mesa_memset16( (GLushort *) (DST), (GLushort) (VAL), (size_t) (N) )
77 /* MACs and BeOS don't support static larger than 32kb, so... */
78 #if defined(macintosh) && !defined(__MRC__)
79 /*extern char *AGLAlloc(int size);*/
80 /*extern void AGLFree(char* ptr);*/
81 # define DEFARRAY(TYPE,NAME,SIZE) TYPE *NAME = (TYPE*)_mesa_alloc(sizeof(TYPE)*(SIZE))
82 # define DEFMARRAY(TYPE,NAME,SIZE1,SIZE2) TYPE (*NAME)[SIZE2] = (TYPE(*)[SIZE2])_mesa_alloc(sizeof(TYPE)*(SIZE1)*(SIZE2))
83 # define DEFMNARRAY(TYPE,NAME,SIZE1,SIZE2,SIZE3) TYPE (*NAME)[SIZE2][SIZE3] = (TYPE(*)[SIZE2][SIZE3])_mesa_alloc(sizeof(TYPE)*(SIZE1)*(SIZE2)*(SIZE3))
85 # define CHECKARRAY(NAME,CMD) do {if (!(NAME)) {CMD;}} while (0)
86 # define UNDEFARRAY(NAME) do {if ((NAME)) {_mesa_free((char*)NAME);} }while (0)
87 #elif defined(__BEOS__)
88 # define DEFARRAY(TYPE,NAME,SIZE) TYPE *NAME = (TYPE*)_mesa_malloc(sizeof(TYPE)*(SIZE))
89 # define DEFMARRAY(TYPE,NAME,SIZE1,SIZE2) TYPE (*NAME)[SIZE2] = (TYPE(*)[SIZE2])_mesa_malloc(sizeof(TYPE)*(SIZE1)*(SIZE2))
90 # define DEFMNARRAY(TYPE,NAME,SIZE1,SIZE2,SIZE3) TYPE (*NAME)[SIZE2][SIZE3] = (TYPE(*)[SIZE2][SIZE3])_mesa_malloc(sizeof(TYPE)*(SIZE1)*(SIZE2)*(SIZE3))
91 # define CHECKARRAY(NAME,CMD) do {if (!(NAME)) {CMD;}} while (0)
92 # define UNDEFARRAY(NAME) do {if ((NAME)) {_mesa_free((char*)NAME);} }while (0)
94 # define DEFARRAY(TYPE,NAME,SIZE) TYPE NAME[SIZE]
95 # define DEFMARRAY(TYPE,NAME,SIZE1,SIZE2) TYPE NAME[SIZE1][SIZE2]
96 # define DEFMNARRAY(TYPE,NAME,SIZE1,SIZE2,SIZE3) TYPE NAME[SIZE1][SIZE2][SIZE3]
97 # define CHECKARRAY(NAME,CMD) do {} while(0)
98 # define UNDEFARRAY(NAME)
102 #ifdef MESA_EXTERNAL_BUFFERALLOC
104 * If you want Mesa's depth/stencil/accum/etc buffers to be allocated
105 * with a specialized allocator you can define MESA_EXTERNAL_BUFFERALLOC
106 * and implement _ext_mesa_alloc/free_pixelbuffer() in your app.
107 * Contributed by Gerk Huisma (gerk@five-d.demon.nl).
109 extern void *_ext_mesa_alloc_pixelbuffer( unsigned int size
);
110 extern void _ext_mesa_free_pixelbuffer( void *pb
);
112 #define MESA_PBUFFER_ALLOC(BYTES) (void *) _ext_mesa_alloc_pixelbuffer(BYTES)
113 #define MESA_PBUFFER_FREE(PTR) _ext_mesa_free_pixelbuffer(PTR)
115 /* Default buffer allocation uses the aligned allocation routines: */
116 #define MESA_PBUFFER_ALLOC(BYTES) (void *) _mesa_align_malloc(BYTES, 512)
117 #define MESA_PBUFFER_FREE(PTR) _mesa_align_free(PTR)
122 /**********************************************************************
126 #define MAX_GLUSHORT 0xffff
127 #define MAX_GLUINT 0xffffffff
130 #define M_PI (3.1415926)
133 /* Degrees to radians conversion: */
134 #define DEG2RAD (M_PI/180.0)
138 *** USE_IEEE: Determine if we're using IEEE floating point
140 #if defined(__i386__) || defined(__sparc__) || defined(__s390x__) || \
141 defined(__powerpc__) || \
142 ( defined(__alpha__) && ( defined(__IEEE_FLOAT) || !defined(VMS) ) )
144 #define IEEE_ONE 0x3f800000
149 *** SQRTF: single-precision square root
152 # define SQRTF(X) ((float)_mesa_sqrtd((float) X))
153 #elif defined(__WATCOMC__) && defined(USE_X86_ASM)
154 float asm_sqrt (float x
);
155 #pragma aux asm_sqrt = \
160 # define SQRTF(X) asm_sqrt(X)
162 # define SQRTF(X) _mesa_sqrtf(X)
167 *** LOG2: Log base 2 of float
171 /* This is pretty fast, but not accurate enough (only 2 fractional bits).
172 * Based on code from http://www.stereopsis.com/log2.html
174 static INLINE GLfloat
LOG2(GLfloat x
)
176 const GLfloat y
= x
* x
* x
* x
;
177 const GLuint ix
= *((GLuint
*) &y
);
178 const GLuint exp
= (ix
>> 23) & 0xFF;
179 const GLint log2
= ((GLint
) exp
) - 127;
180 return (GLfloat
) log2
* (1.0 / 4.0); /* 4, because of x^4 above */
183 /* Pretty fast, and accurate.
184 * Based on code from http://www.flipcode.com/totd/
186 static INLINE GLfloat
LOG2(GLfloat val
)
188 GLint
*exp_ptr
= (GLint
*) &val
;
190 const GLint log_2
= ((x
>> 23) & 255) - 128;
194 val
= ((-1.0f
/3) * val
+ 2) * val
- 2.0f
/3;
197 #elif defined(XFree86LOADER) && defined(IN_MODULE)
198 #define LOG2(x) ((GLfloat) (xf86log(x) * 1.442695))
201 * NOTE: log_base_2(x) = log(x) / log(2)
202 * NOTE: 1.442695 = 1/log(2).
204 #define LOG2(x) ((GLfloat) (log(x) * 1.442695F))
209 *** IS_INF_OR_NAN: test if float is infinite or NaN
212 static INLINE
int IS_INF_OR_NAN( float x
)
216 return !(int)((unsigned int)((tmp
.i
& 0x7fffffff)-0x7f800000) >> 31);
218 #elif defined(isfinite)
219 #define IS_INF_OR_NAN(x) (!isfinite(x))
220 #elif defined(finite)
221 #define IS_INF_OR_NAN(x) (!finite(x))
223 #define IS_INF_OR_NAN(x) (!finite(x))
224 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
225 #define IS_INF_OR_NAN(x) (!isfinite(x))
227 #define IS_INF_OR_NAN(x) (!finite(x))
232 *** IS_NEGATIVE: test if float is negative
234 #if defined(USE_IEEE)
235 #define GET_FLOAT_BITS(x) ((fi_type *) &(x))->i
236 #define IS_NEGATIVE(x) (GET_FLOAT_BITS(x) & (1<<31))
238 #define IS_NEGATIVE(x) (x < 0.0F)
243 *** DIFFERENT_SIGNS: test if two floats have opposite signs
245 #if defined(USE_IEEE)
246 #define DIFFERENT_SIGNS(x,y) ((GET_FLOAT_BITS(x) ^ GET_FLOAT_BITS(y)) & (1<<31))
248 /* Could just use (x*y<0) except for the flatshading requirements.
249 * Maybe there's a better way?
251 #define DIFFERENT_SIGNS(x,y) ((x) * (y) <= 0.0F && (x) - (y) != 0.0F)
256 *** CEILF: ceiling of float
257 *** FLOORF: floor of float
258 *** FABSF: absolute value of float
260 #if defined(__sparc__) || defined(__NeXT__) /* XXX improve? */
261 #define CEILF(x) ceil(x)
262 #define FLOORF(x) floor(x)
263 #define FABSF(x) fabs(x)
264 #elif defined(__WIN32__) || defined(__IBMC__) || defined(__IBMCPP__)
265 #define CEILF(x) ((GLfloat) ceil(x))
266 #define FLOORF(x) ((GLfloat) floor(x))
267 #define FABSF(x) ((GLfloat) fabs(x))
268 #elif defined(XFree86LOADER) && defined(IN_MODULE)
269 #define CEILF(x) ((GLfloat) xf86ceil(x))
270 #define FLOORF(x) ((GLfloat) xf86floor(x))
271 #define FABSF(x) ((GLfloat) xf86fabs(x))
273 #define CEILF(x) ceilf(x)
274 #define FLOORF(x) floorf(x)
275 #define FABSF(x) fabsf(x)
280 *** IROUND: return (as an integer) float rounded to nearest integer
282 #if defined(USE_SPARC_ASM) && defined(__GNUC__) && defined(__sparc__)
283 static INLINE
int iround(float f
)
286 __asm__ ("fstoi %1, %0" : "=f" (r
) : "f" (f
));
289 #define IROUND(x) iround(x)
290 #elif defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
291 static INLINE
int iround(float f
)
294 __asm__ ("fistpl %0" : "=m" (r
) : "t" (f
) : "st");
297 #define IROUND(x) iround(x)
298 #elif defined(USE_X86_ASM) && defined(__MSC__) && defined(__WIN32__)
299 static INLINE
int iround(float f
)
308 #define IROUND(x) iround(x)
309 #elif defined(USE_X86_ASM) && defined(__WATCOMC__)
310 long iround(float f
);
311 #pragma aux iround = \
313 "fistp dword ptr [esp]" \
319 #define IROUND(x) iround(x)
321 #define IROUND(f) ((int) (((f) >= 0.0F) ? ((f) + 0.5F) : ((f) - 0.5F)))
326 *** IROUND_POS: return (as an integer) positive float rounded to nearest int
329 #define IROUND_POS(f) (assert((f) >= 0.0F), IROUND(f))
331 #define IROUND_POS(f) (IROUND(f))
336 *** IFLOOR: return (as an integer) floor of float
338 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
340 * IEEE floor for computers that round to nearest or even.
341 * 'f' must be between -4194304 and 4194303.
342 * This floor operation is done by "(iround(f + .5) + iround(f - .5)) >> 1",
343 * but uses some IEEE specific tricks for better speed.
344 * Contributed by Josh Vanderhoof
346 static INLINE
int ifloor(float f
)
350 af
= (3 << 22) + 0.5 + (double)f
;
351 bf
= (3 << 22) + 0.5 - (double)f
;
352 /* GCC generates an extra fstp/fld without this. */
353 __asm__ ("fstps %0" : "=m" (ai
) : "t" (af
) : "st");
354 __asm__ ("fstps %0" : "=m" (bi
) : "t" (bf
) : "st");
355 return (ai
- bi
) >> 1;
357 #define IFLOOR(x) ifloor(x)
358 #elif defined(USE_IEEE)
359 static INLINE
int ifloor(float f
)
365 af
= (3 << 22) + 0.5 + (double)f
;
366 bf
= (3 << 22) + 0.5 - (double)f
;
369 return (ai
- bi
) >> 1;
371 #define IFLOOR(x) ifloor(x)
373 static INLINE
int ifloor(float f
)
376 return (i
> f
) ? i
- 1 : i
;
378 #define IFLOOR(x) ifloor(x)
383 *** ICEIL: return (as an integer) ceiling of float
385 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
387 * IEEE ceil for computers that round to nearest or even.
388 * 'f' must be between -4194304 and 4194303.
389 * This ceil operation is done by "(iround(f + .5) + iround(f - .5) + 1) >> 1",
390 * but uses some IEEE specific tricks for better speed.
391 * Contributed by Josh Vanderhoof
393 static INLINE
int iceil(float f
)
397 af
= (3 << 22) + 0.5 + (double)f
;
398 bf
= (3 << 22) + 0.5 - (double)f
;
399 /* GCC generates an extra fstp/fld without this. */
400 __asm__ ("fstps %0" : "=m" (ai
) : "t" (af
) : "st");
401 __asm__ ("fstps %0" : "=m" (bi
) : "t" (bf
) : "st");
402 return (ai
- bi
+ 1) >> 1;
404 #define ICEIL(x) iceil(x)
405 #elif defined(USE_IEEE)
406 static INLINE
int iceil(float f
)
411 af
= (3 << 22) + 0.5 + (double)f
;
412 bf
= (3 << 22) + 0.5 - (double)f
;
415 return (ai
- bi
+ 1) >> 1;
417 #define ICEIL(x) iceil(x)
419 static INLINE
int iceil(float f
)
422 return (i
< f
) ? i
+ 1 : i
;
424 #define ICEIL(x) iceil(x)
429 *** UNCLAMPED_FLOAT_TO_UBYTE: map float from {0,1} to ubyte in [0,255]
430 *** CLAMPED_FLOAT_TO_UBYTE: map float in [0,1] to ubyte in [0,255]
432 #if defined(USE_IEEE) && !defined(DEBUG)
433 #define IEEE_0996 0x3f7f0000 /* 0.996 or so */
434 /* This function/macro is sensitive to precision. Test very carefully
437 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
441 UB = ((__tmp.i >= IEEE_0996) \
442 ? ((GLint)__tmp.i < 0) ? (GLubyte)0 : (GLubyte)255 \
443 : (__tmp.f = __tmp.f*(255.0F/256.0F) + 32768.0F, \
444 (GLubyte)__tmp.i)); \
446 #define CLAMPED_FLOAT_TO_UBYTE(ub, f) \
447 UNCLAMPED_FLOAT_TO_UBYTE(ub, f)
449 #define UNCLAMPED_FLOAT_TO_UBYTE(ub, f) \
450 ub = ((GLubyte) IROUND(CLAMP((f), 0.0F, 1.0F) * 255.0F))
451 #define CLAMPED_FLOAT_TO_UBYTE(ub, f) \
452 ub = ((GLubyte) IROUND((f) * 255.0F))
457 *** COPY_FLOAT: copy a float from src to dest, avoid slow FP regs if possible
459 #if defined(USE_IEEE) && !defined(DEBUG)
460 #define COPY_FLOAT( dst, src ) \
461 ((fi_type *) &(dst))->i = ((fi_type *) &(src))->i
463 #define COPY_FLOAT( dst, src ) (dst) = (src)
468 *** START_FAST_MATH: Set x86 FPU to faster, 32-bit precision mode (and save
469 *** original mode to a temporary).
470 *** END_FAST_MATH: Restore x86 FPU to original mode.
472 #if defined(__GNUC__) && defined(__i386__)
474 * Set the x86 FPU control word to guarentee only 32 bits of precision
475 * are stored in registers. Allowing the FPU to store more introduces
476 * differences between situations where numbers are pulled out of memory
477 * vs. situations where the compiler is able to optimize register usage.
479 * In the worst case, we force the compiler to use a memory access to
480 * truncate the float, by specifying the 'volatile' keyword.
482 /* Hardware default: All exceptions masked, extended double precision,
483 * round to nearest (IEEE compliant):
485 #define DEFAULT_X86_FPU 0x037f
486 /* All exceptions masked, single precision, round to nearest:
488 #define FAST_X86_FPU 0x003f
489 /* The fldcw instruction will cause any pending FP exceptions to be
490 * raised prior to entering the block, and we clear any pending
491 * exceptions before exiting the block. Hence, asm code has free
492 * reign over the FPU while in the fast math block.
494 #if defined(NO_FAST_MATH)
495 #define START_FAST_MATH(x) \
497 static GLuint mask = DEFAULT_X86_FPU; \
498 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
499 __asm__ ( "fldcw %0" : : "m" (mask) ); \
502 #define START_FAST_MATH(x) \
504 static GLuint mask = FAST_X86_FPU; \
505 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
506 __asm__ ( "fldcw %0" : : "m" (mask) ); \
509 /* Restore original FPU mode, and clear any exceptions that may have
510 * occurred in the FAST_MATH block.
512 #define END_FAST_MATH(x) \
514 __asm__ ( "fnclex ; fldcw %0" : : "m" (*&(x)) ); \
517 #elif defined(__WATCOMC__) && !defined(NO_FAST_MATH)
518 void _wacom_start_fast_math(unsigned short *x
);
519 #pragma aux _wacom_start_fast_math = \
520 "fstcw word ptr [esi]" \
521 "or word ptr [esi], 0x3f" \
522 "fldcw word ptr [esi]" \
525 void _wacom_end_fast_math(unsigned short *x
);
526 #pragma aux _wacom_end_fast_math = \
527 "fldcw word ptr [esi]" \
530 #define START_FAST_MATH(x) _wacom_start_fast_math(& x)
531 #define END_FAST_MATH(x) _wacom_end_fast_math(& x)
533 #define START_FAST_MATH(x) x = 0
534 #define END_FAST_MATH(x) (void)(x)
539 /**********************************************************************
544 _mesa_malloc( size_t bytes
);
547 _mesa_calloc( size_t bytes
);
550 _mesa_free( void *ptr
);
553 _mesa_align_malloc( size_t bytes
, unsigned long alignment
);
556 _mesa_align_calloc( size_t bytes
, unsigned long alignment
);
559 _mesa_align_free( void *ptr
);
562 _mesa_memcpy( void *dest
, const void *src
, size_t n
);
565 _mesa_memset( void *dst
, int val
, size_t n
);
568 _mesa_memset16( unsigned short *dst
, unsigned short val
, size_t n
);
571 _mesa_bzero( void *dst
, size_t n
);
581 _mesa_sqrtd(double x
);
584 _mesa_sqrtf(float x
);
587 _mesa_pow(double x
, double y
);
593 _mesa_bitcount(unsigned int n
);
597 _mesa_getenv( const char *var
);
600 _mesa_strstr( const char *haystack
, const char *needle
);
603 _mesa_strncat( char *dest
, const char *src
, size_t n
);
606 _mesa_strcpy( char *dest
, const char *src
);
609 _mesa_strncpy( char *dest
, const char *src
, size_t n
);
612 _mesa_strlen( const char *s
);
615 _mesa_strcmp( const char *s1
, const char *s2
);
618 _mesa_strncmp( const char *s1
, const char *s2
, size_t n
);
621 _mesa_strdup( const char *s
);
624 _mesa_atoi( const char *s
);
627 _mesa_strtod( const char *s
, char **end
);
630 _mesa_sprintf( char *str
, const char *fmt
, ... );
633 _mesa_printf( const char *fmtString
, ... );
637 _mesa_warning( __GLcontext
*gc
, const char *fmtString
, ... );
640 _mesa_problem( const __GLcontext
*ctx
, const char *fmtString
, ... );
643 _mesa_error( __GLcontext
*ctx
, GLenum error
, const char *fmtString
, ... );
646 _mesa_debug( const __GLcontext
*ctx
, const char *fmtString
, ... );
650 _mesa_init_default_imports( __GLimports
*imports
, void *driverCtx
);
653 #endif /* IMPORTS_H */