set table size to 1023 and use new HASH_FUNC() macro
[mesa.git] / src / mesa / main / imports.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.2
4 *
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25
26 /**
27 * \file imports.h
28 * Standard C library function wrappers.
29 *
30 * This file provides wrappers for all the standard C library functions
31 * like malloc(), free(), printf(), getenv(), etc.
32 */
33
34
35 #ifndef IMPORTS_H
36 #define IMPORTS_H
37
38
39 /* XXX some of the stuff in glheader.h should be moved into this file.
40 */
41 #include "glheader.h"
42
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48
49 /**********************************************************************/
50 /** \name General macros */
51 /*@{*/
52
53 #ifndef NULL
54 #define NULL 0
55 #endif
56
57 /*@}*/
58
59
60 /**********************************************************************/
61 /** Memory macros */
62 /*@{*/
63
64 /** Allocate \p BYTES bytes */
65 #define MALLOC(BYTES) _mesa_malloc(BYTES)
66 /** Allocate and zero \p BYTES bytes */
67 #define CALLOC(BYTES) _mesa_calloc(BYTES)
68 /** Allocate a structure of type \p T */
69 #define MALLOC_STRUCT(T) (struct T *) _mesa_malloc(sizeof(struct T))
70 /** Allocate and zero a structure of type \p T */
71 #define CALLOC_STRUCT(T) (struct T *) _mesa_calloc(sizeof(struct T))
72 /** Free memory */
73 #define FREE(PTR) _mesa_free(PTR)
74
75 /** Allocate \p BYTES aligned at \p N bytes */
76 #define ALIGN_MALLOC(BYTES, N) _mesa_align_malloc(BYTES, N)
77 /** Allocate and zero \p BYTES bytes aligned at \p N bytes */
78 #define ALIGN_CALLOC(BYTES, N) _mesa_align_calloc(BYTES, N)
79 /** Allocate a structure of type \p T aligned at \p N bytes */
80 #define ALIGN_MALLOC_STRUCT(T, N) (struct T *) _mesa_align_malloc(sizeof(struct T), N)
81 /** Allocate and zero a structure of type \p T aligned at \p N bytes */
82 #define ALIGN_CALLOC_STRUCT(T, N) (struct T *) _mesa_align_calloc(sizeof(struct T), N)
83 /** Free aligned memory */
84 #define ALIGN_FREE(PTR) _mesa_align_free(PTR)
85
86 /** Copy \p BYTES bytes from \p SRC into \p DST */
87 #define MEMCPY( DST, SRC, BYTES) _mesa_memcpy(DST, SRC, BYTES)
88 /** Set \p N bytes in \p DST to \p VAL */
89 #define MEMSET( DST, VAL, N ) _mesa_memset(DST, VAL, N)
90
91 #define MEMSET16( DST, VAL, N ) _mesa_memset16( (DST), (VAL), (size_t) (N) )
92
93 /*@}*/
94
95
96 /*
97 * For GL_ARB_vertex_buffer_object we need to treat vertex array pointers
98 * as offsets into buffer stores. Since the vertex array pointer and
99 * buffer store pointer are both pointers and we need to add them, we use
100 * this macro.
101 * Both pointers/offsets are expressed in bytes.
102 */
103 #define ADD_POINTERS(A, B) ( (GLubyte *) (A) + (unsigned long) (B) )
104
105
106 /**********************************************************************/
107 /** \name [Pseudo] static array declaration.
108 *
109 * MACs and BeOS don't support static larger than 32kb, so ...
110 */
111 /*@{*/
112
113 /**
114 * \def DEFARRAY
115 * Define a [static] unidimensional array
116 */
117
118 /**
119 * \def DEFMARRAY
120 * Define a [static] bi-dimensional array
121 */
122
123 /**
124 * \def DEFMNARRAY
125 * Define a [static] tri-dimensional array
126 */
127
128 /**
129 * \def CHECKARRAY
130 * Verifies a [static] array was properly allocated.
131 */
132
133 /**
134 * \def UNDEFARRAY
135 * Undefine (free) a [static] array.
136 */
137
138 #if defined(macintosh) && !defined(__MRC__)
139 /*extern char *AGLAlloc(int size);*/
140 /*extern void AGLFree(char* ptr);*/
141 # define DEFARRAY(TYPE,NAME,SIZE) TYPE *NAME = (TYPE*)_mesa_alloc(sizeof(TYPE)*(SIZE))
142 # define DEFMARRAY(TYPE,NAME,SIZE1,SIZE2) TYPE (*NAME)[SIZE2] = (TYPE(*)[SIZE2])_mesa_alloc(sizeof(TYPE)*(SIZE1)*(SIZE2))
143 # define DEFMNARRAY(TYPE,NAME,SIZE1,SIZE2,SIZE3) TYPE (*NAME)[SIZE2][SIZE3] = (TYPE(*)[SIZE2][SIZE3])_mesa_alloc(sizeof(TYPE)*(SIZE1)*(SIZE2)*(SIZE3))
144 # define CHECKARRAY(NAME,CMD) do {if (!(NAME)) {CMD;}} while (0)
145 # define UNDEFARRAY(NAME) do {if ((NAME)) {_mesa_free((char*)NAME);} }while (0)
146 #elif defined(__BEOS__)
147 # define DEFARRAY(TYPE,NAME,SIZE) TYPE *NAME = (TYPE*)_mesa_malloc(sizeof(TYPE)*(SIZE))
148 # define DEFMARRAY(TYPE,NAME,SIZE1,SIZE2) TYPE (*NAME)[SIZE2] = (TYPE(*)[SIZE2])_mesa_malloc(sizeof(TYPE)*(SIZE1)*(SIZE2))
149 # define DEFMNARRAY(TYPE,NAME,SIZE1,SIZE2,SIZE3) TYPE (*NAME)[SIZE2][SIZE3] = (TYPE(*)[SIZE2][SIZE3])_mesa_malloc(sizeof(TYPE)*(SIZE1)*(SIZE2)*(SIZE3))
150 # define CHECKARRAY(NAME,CMD) do {if (!(NAME)) {CMD;}} while (0)
151 # define UNDEFARRAY(NAME) do {if ((NAME)) {_mesa_free((char*)NAME);} }while (0)
152 #else
153 # define DEFARRAY(TYPE,NAME,SIZE) TYPE NAME[SIZE]
154 # define DEFMARRAY(TYPE,NAME,SIZE1,SIZE2) TYPE NAME[SIZE1][SIZE2]
155 # define DEFMNARRAY(TYPE,NAME,SIZE1,SIZE2,SIZE3) TYPE NAME[SIZE1][SIZE2][SIZE3]
156 # define CHECKARRAY(NAME,CMD) do {} while(0)
157 # define UNDEFARRAY(NAME)
158 #endif
159
160 /*@}*/
161
162
163 /**********************************************************************/
164 /** \name External pixel buffer allocation.
165 *
166 * If you want Mesa's depth/stencil/accum/etc buffers to be allocated with a
167 * specialized allocator you can define MESA_EXTERNAL_BUFFERALLOC and implement
168 * _ext_mesa_alloc_pixelbuffer() _ext_mesa_free_pixelbuffer() in your
169 * application.
170 *
171 * \author
172 * Contributed by Gerk Huisma (gerk@five-d.demon.nl).
173 */
174 /*@{*/
175
176 /**
177 * \def MESA_PBUFFER_ALLOC
178 * Allocate a pixel buffer.
179 */
180
181 /**
182 * \def MESA_PBUFFER_FREE
183 * Free a pixel buffer.
184 */
185
186 #ifdef MESA_EXTERNAL_BUFFERALLOC
187 extern void *_ext_mesa_alloc_pixelbuffer( unsigned int size );
188 extern void _ext_mesa_free_pixelbuffer( void *pb );
189
190 #define MESA_PBUFFER_ALLOC(BYTES) (void *) _ext_mesa_alloc_pixelbuffer(BYTES)
191 #define MESA_PBUFFER_FREE(PTR) _ext_mesa_free_pixelbuffer(PTR)
192 #else
193 /* Default buffer allocation uses the aligned allocation routines: */
194 #define MESA_PBUFFER_ALLOC(BYTES) (void *) _mesa_align_malloc(BYTES, 512)
195 #define MESA_PBUFFER_FREE(PTR) _mesa_align_free(PTR)
196 #endif
197
198 /*@}*/
199
200
201 /**********************************************************************/
202
203
204 /**
205 * Sometimes we treat GLfloats as GLints. On x86 systems, moving a float
206 * as a int (thereby using integer registers instead of FP registers) is
207 * a performance win. Typically, this can be done with ordinary casts.
208 * But with gcc's -fstrict-aliasing flag (which defaults to on in gcc 3.0)
209 * these casts generate warnings.
210 * The following union typedef is used to solve that.
211 */
212 typedef union { GLfloat f; GLint i; } fi_type;
213
214
215
216 /**********************************************************************
217 * Math macros
218 */
219
220 #define MAX_GLUSHORT 0xffff
221 #define MAX_GLUINT 0xffffffff
222
223 #ifndef M_PI
224 #define M_PI (3.1415926536)
225 #endif
226
227 /* XXX this is a bit of a hack needed for compilation within XFree86 */
228 #ifndef FLT_MIN
229 #define FLT_MIN (1.0e-37)
230 #endif
231
232 /* Degrees to radians conversion: */
233 #define DEG2RAD (M_PI/180.0)
234
235
236 /***
237 *** USE_IEEE: Determine if we're using IEEE floating point
238 ***/
239 #if defined(__i386__) || defined(__386__) || defined(__sparc__) || \
240 defined(__s390x__) || defined(__powerpc__) || \
241 defined(__amd64__) || \
242 defined(ia64) || defined(__ia64__) || \
243 defined(__hppa__) || defined(hpux) || \
244 defined(__mips) || defined(_MIPS_ARCH) || \
245 defined(__arm__) || \
246 (defined(__alpha__) && (defined(__IEEE_FLOAT) || !defined(VMS)))
247 #define USE_IEEE
248 #define IEEE_ONE 0x3f800000
249 #endif
250
251
252 /***
253 *** SQRTF: single-precision square root
254 ***/
255 #if 0 /* _mesa_sqrtf() not accurate enough - temporarily disabled */
256 # define SQRTF(X) _mesa_sqrtf(X)
257 #elif defined(XFree86LOADER) && defined(IN_MODULE)
258 # define SQRTF(X) (float) xf86sqrt((float) (X))
259 #else
260 # define SQRTF(X) (float) sqrt((float) (X))
261 #endif
262
263
264 /***
265 *** INV_SQRTF: single-precision inverse square root
266 ***/
267 #if 0
268 #define INV_SQRTF(X) _mesa_inv_sqrt(X)
269 #else
270 #define INV_SQRTF(X) (1.0F / SQRTF(X)) /* this is faster on a P4 */
271 #endif
272
273
274 /***
275 *** LOG2: Log base 2 of float
276 ***/
277 #ifdef USE_IEEE
278 #if 0
279 /* This is pretty fast, but not accurate enough (only 2 fractional bits).
280 * Based on code from http://www.stereopsis.com/log2.html
281 */
282 static INLINE GLfloat LOG2(GLfloat x)
283 {
284 const GLfloat y = x * x * x * x;
285 const GLuint ix = *((GLuint *) &y);
286 const GLuint exp = (ix >> 23) & 0xFF;
287 const GLint log2 = ((GLint) exp) - 127;
288 return (GLfloat) log2 * (1.0 / 4.0); /* 4, because of x^4 above */
289 }
290 #endif
291 /* Pretty fast, and accurate.
292 * Based on code from http://www.flipcode.com/totd/
293 */
294 static INLINE GLfloat LOG2(GLfloat val)
295 {
296 fi_type num;
297 GLint log_2;
298 num.f = val;
299 log_2 = ((num.i >> 23) & 255) - 128;
300 num.i &= ~(255 << 23);
301 num.i += 127 << 23;
302 num.f = ((-1.0f/3) * num.f + 2) * num.f - 2.0f/3;
303 return num.f + log_2;
304 }
305 #elif defined(XFree86LOADER) && defined(IN_MODULE)
306 #define LOG2(x) ((GLfloat) (xf86log(x) * 1.442695))
307 #else
308 /*
309 * NOTE: log_base_2(x) = log(x) / log(2)
310 * NOTE: 1.442695 = 1/log(2).
311 */
312 #define LOG2(x) ((GLfloat) (log(x) * 1.442695F))
313 #endif
314
315
316 /***
317 *** IS_INF_OR_NAN: test if float is infinite or NaN
318 ***/
319 #ifdef USE_IEEE
320 static INLINE int IS_INF_OR_NAN( float x )
321 {
322 fi_type tmp;
323 tmp.f = x;
324 return !(int)((unsigned int)((tmp.i & 0x7fffffff)-0x7f800000) >> 31);
325 }
326 #elif defined(isfinite)
327 #define IS_INF_OR_NAN(x) (!isfinite(x))
328 #elif defined(finite)
329 #define IS_INF_OR_NAN(x) (!finite(x))
330 #elif defined(__VMS)
331 #define IS_INF_OR_NAN(x) (!finite(x))
332 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
333 #define IS_INF_OR_NAN(x) (!isfinite(x))
334 #else
335 #define IS_INF_OR_NAN(x) (!finite(x))
336 #endif
337
338
339 /***
340 *** IS_NEGATIVE: test if float is negative
341 ***/
342 #if defined(USE_IEEE)
343 static INLINE int GET_FLOAT_BITS( float x )
344 {
345 fi_type fi;
346 fi.f = x;
347 return fi.i;
348 }
349 #define IS_NEGATIVE(x) (GET_FLOAT_BITS(x) < 0)
350 #else
351 #define IS_NEGATIVE(x) (x < 0.0F)
352 #endif
353
354
355 /***
356 *** DIFFERENT_SIGNS: test if two floats have opposite signs
357 ***/
358 #if defined(USE_IEEE)
359 #define DIFFERENT_SIGNS(x,y) ((GET_FLOAT_BITS(x) ^ GET_FLOAT_BITS(y)) & (1<<31))
360 #else
361 /* Could just use (x*y<0) except for the flatshading requirements.
362 * Maybe there's a better way?
363 */
364 #define DIFFERENT_SIGNS(x,y) ((x) * (y) <= 0.0F && (x) - (y) != 0.0F)
365 #endif
366
367
368 /***
369 *** CEILF: ceiling of float
370 *** FLOORF: floor of float
371 *** FABSF: absolute value of float
372 ***/
373 #if defined(XFree86LOADER) && defined(IN_MODULE)
374 #define CEILF(x) ((GLfloat) xf86ceil(x))
375 #define FLOORF(x) ((GLfloat) xf86floor(x))
376 #define FABSF(x) ((GLfloat) xf86fabs(x))
377 #elif defined(__gnu_linux__)
378 /* C99 functions */
379 #define CEILF(x) ceilf(x)
380 #define FLOORF(x) floorf(x)
381 #define FABSF(x) fabsf(x)
382 #else
383 #define CEILF(x) ((GLfloat) ceil(x))
384 #define FLOORF(x) ((GLfloat) floor(x))
385 #define FABSF(x) ((GLfloat) fabs(x))
386 #endif
387
388
389 /***
390 *** IROUND: return (as an integer) float rounded to nearest integer
391 ***/
392 #if defined(USE_SPARC_ASM) && defined(__GNUC__) && defined(__sparc__)
393 static INLINE int iround(float f)
394 {
395 int r;
396 __asm__ ("fstoi %1, %0" : "=f" (r) : "f" (f));
397 return r;
398 }
399 #define IROUND(x) iround(x)
400 #elif defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__) && \
401 (!defined(__BEOS__) || (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)))
402 static INLINE int iround(float f)
403 {
404 int r;
405 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
406 return r;
407 }
408 #define IROUND(x) iround(x)
409 #elif defined(USE_X86_ASM) && defined(__MSC__) && defined(__WIN32__)
410 static INLINE int iround(float f)
411 {
412 int r;
413 _asm {
414 fld f
415 fistp r
416 }
417 return r;
418 }
419 #define IROUND(x) iround(x)
420 #elif defined(__WATCOMC__) && defined(__386__)
421 long iround(float f);
422 #pragma aux iround = \
423 "push eax" \
424 "fistp dword ptr [esp]" \
425 "pop eax" \
426 parm [8087] \
427 value [eax] \
428 modify exact [eax];
429 #define IROUND(x) iround(x)
430 #else
431 #define IROUND(f) ((int) (((f) >= 0.0F) ? ((f) + 0.5F) : ((f) - 0.5F)))
432 #endif
433
434
435 /***
436 *** IROUND_POS: return (as an integer) positive float rounded to nearest int
437 ***/
438 #ifdef DEBUG
439 #define IROUND_POS(f) (assert((f) >= 0.0F), IROUND(f))
440 #else
441 #define IROUND_POS(f) (IROUND(f))
442 #endif
443
444
445 /***
446 *** IFLOOR: return (as an integer) floor of float
447 ***/
448 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
449 /*
450 * IEEE floor for computers that round to nearest or even.
451 * 'f' must be between -4194304 and 4194303.
452 * This floor operation is done by "(iround(f + .5) + iround(f - .5)) >> 1",
453 * but uses some IEEE specific tricks for better speed.
454 * Contributed by Josh Vanderhoof
455 */
456 static INLINE int ifloor(float f)
457 {
458 int ai, bi;
459 double af, bf;
460 af = (3 << 22) + 0.5 + (double)f;
461 bf = (3 << 22) + 0.5 - (double)f;
462 /* GCC generates an extra fstp/fld without this. */
463 __asm__ ("fstps %0" : "=m" (ai) : "t" (af) : "st");
464 __asm__ ("fstps %0" : "=m" (bi) : "t" (bf) : "st");
465 return (ai - bi) >> 1;
466 }
467 #define IFLOOR(x) ifloor(x)
468 #elif defined(USE_IEEE)
469 static INLINE int ifloor(float f)
470 {
471 int ai, bi;
472 double af, bf;
473 fi_type u;
474
475 af = (3 << 22) + 0.5 + (double)f;
476 bf = (3 << 22) + 0.5 - (double)f;
477 u.f = (float) af; ai = u.i;
478 u.f = (float) bf; bi = u.i;
479 return (ai - bi) >> 1;
480 }
481 #define IFLOOR(x) ifloor(x)
482 #else
483 static INLINE int ifloor(float f)
484 {
485 int i = IROUND(f);
486 return (i > f) ? i - 1 : i;
487 }
488 #define IFLOOR(x) ifloor(x)
489 #endif
490
491
492 /***
493 *** ICEIL: return (as an integer) ceiling of float
494 ***/
495 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
496 /*
497 * IEEE ceil for computers that round to nearest or even.
498 * 'f' must be between -4194304 and 4194303.
499 * This ceil operation is done by "(iround(f + .5) + iround(f - .5) + 1) >> 1",
500 * but uses some IEEE specific tricks for better speed.
501 * Contributed by Josh Vanderhoof
502 */
503 static INLINE int iceil(float f)
504 {
505 int ai, bi;
506 double af, bf;
507 af = (3 << 22) + 0.5 + (double)f;
508 bf = (3 << 22) + 0.5 - (double)f;
509 /* GCC generates an extra fstp/fld without this. */
510 __asm__ ("fstps %0" : "=m" (ai) : "t" (af) : "st");
511 __asm__ ("fstps %0" : "=m" (bi) : "t" (bf) : "st");
512 return (ai - bi + 1) >> 1;
513 }
514 #define ICEIL(x) iceil(x)
515 #elif defined(USE_IEEE)
516 static INLINE int iceil(float f)
517 {
518 int ai, bi;
519 double af, bf;
520 fi_type u;
521 af = (3 << 22) + 0.5 + (double)f;
522 bf = (3 << 22) + 0.5 - (double)f;
523 u.f = (float) af; ai = u.i;
524 u.f = (float) bf; bi = u.i;
525 return (ai - bi + 1) >> 1;
526 }
527 #define ICEIL(x) iceil(x)
528 #else
529 static INLINE int iceil(float f)
530 {
531 int i = IROUND(f);
532 return (i < f) ? i + 1 : i;
533 }
534 #define ICEIL(x) iceil(x)
535 #endif
536
537
538 /***
539 *** UNCLAMPED_FLOAT_TO_UBYTE: clamp float to [0,1] and map to ubyte in [0,255]
540 *** CLAMPED_FLOAT_TO_UBYTE: map float known to be in [0,1] to ubyte in [0,255]
541 ***/
542 #if defined(USE_IEEE) && !defined(DEBUG)
543 #define IEEE_0996 0x3f7f0000 /* 0.996 or so */
544 /* This function/macro is sensitive to precision. Test very carefully
545 * if you change it!
546 */
547 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
548 do { \
549 fi_type __tmp; \
550 __tmp.f = (F); \
551 if (__tmp.i < 0) \
552 UB = (GLubyte) 0; \
553 else if (__tmp.i >= IEEE_0996) \
554 UB = (GLubyte) 255; \
555 else { \
556 __tmp.f = __tmp.f * (255.0F/256.0F) + 32768.0F; \
557 UB = (GLubyte) __tmp.i; \
558 } \
559 } while (0)
560 #define CLAMPED_FLOAT_TO_UBYTE(UB, F) \
561 do { \
562 fi_type __tmp; \
563 __tmp.f = (F) * (255.0F/256.0F) + 32768.0F; \
564 UB = (GLubyte) __tmp.i; \
565 } while (0)
566 #else
567 #define UNCLAMPED_FLOAT_TO_UBYTE(ub, f) \
568 ub = ((GLubyte) IROUND(CLAMP((f), 0.0F, 1.0F) * 255.0F))
569 #define CLAMPED_FLOAT_TO_UBYTE(ub, f) \
570 ub = ((GLubyte) IROUND((f) * 255.0F))
571 #endif
572
573
574 /***
575 *** COPY_FLOAT: copy a float from src to dest.
576 ***/
577 #define COPY_FLOAT( dst, src ) (dst) = (src)
578
579
580 /***
581 *** START_FAST_MATH: Set x86 FPU to faster, 32-bit precision mode (and save
582 *** original mode to a temporary).
583 *** END_FAST_MATH: Restore x86 FPU to original mode.
584 ***/
585 #if defined(__GNUC__) && defined(__i386__)
586 /*
587 * Set the x86 FPU control word to guarentee only 32 bits of precision
588 * are stored in registers. Allowing the FPU to store more introduces
589 * differences between situations where numbers are pulled out of memory
590 * vs. situations where the compiler is able to optimize register usage.
591 *
592 * In the worst case, we force the compiler to use a memory access to
593 * truncate the float, by specifying the 'volatile' keyword.
594 */
595 /* Hardware default: All exceptions masked, extended double precision,
596 * round to nearest (IEEE compliant):
597 */
598 #define DEFAULT_X86_FPU 0x037f
599 /* All exceptions masked, single precision, round to nearest:
600 */
601 #define FAST_X86_FPU 0x003f
602 /* The fldcw instruction will cause any pending FP exceptions to be
603 * raised prior to entering the block, and we clear any pending
604 * exceptions before exiting the block. Hence, asm code has free
605 * reign over the FPU while in the fast math block.
606 */
607 #if defined(NO_FAST_MATH)
608 #define START_FAST_MATH(x) \
609 do { \
610 static GLuint mask = DEFAULT_X86_FPU; \
611 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
612 __asm__ ( "fldcw %0" : : "m" (mask) ); \
613 } while (0)
614 #else
615 #define START_FAST_MATH(x) \
616 do { \
617 static GLuint mask = FAST_X86_FPU; \
618 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
619 __asm__ ( "fldcw %0" : : "m" (mask) ); \
620 } while (0)
621 #endif
622 /* Restore original FPU mode, and clear any exceptions that may have
623 * occurred in the FAST_MATH block.
624 */
625 #define END_FAST_MATH(x) \
626 do { \
627 __asm__ ( "fnclex ; fldcw %0" : : "m" (*&(x)) ); \
628 } while (0)
629
630 #elif defined(__WATCOMC__) && defined(__386__)
631 #define DEFAULT_X86_FPU 0x037f /* See GCC comments above */
632 #define FAST_X86_FPU 0x003f /* See GCC comments above */
633 void _watcom_start_fast_math(unsigned short *x,unsigned short *mask);
634 #pragma aux _watcom_start_fast_math = \
635 "fnstcw word ptr [eax]" \
636 "fldcw word ptr [ecx]" \
637 parm [eax] [ecx] \
638 modify exact [];
639 void _watcom_end_fast_math(unsigned short *x);
640 #pragma aux _watcom_end_fast_math = \
641 "fnclex" \
642 "fldcw word ptr [eax]" \
643 parm [eax] \
644 modify exact [];
645 #if defined(NO_FAST_MATH)
646 #define START_FAST_MATH(x) \
647 do { \
648 static GLushort mask = DEFAULT_X86_FPU; \
649 _watcom_start_fast_math(&x,&mask); \
650 } while (0)
651 #else
652 #define START_FAST_MATH(x) \
653 do { \
654 static GLushort mask = FAST_X86_FPU; \
655 _watcom_start_fast_math(&x,&mask); \
656 } while (0)
657 #endif
658 #define END_FAST_MATH(x) _watcom_end_fast_math(&x)
659 #else
660 #define START_FAST_MATH(x) x = 0
661 #define END_FAST_MATH(x) (void)(x)
662 #endif
663
664
665
666 /**********************************************************************
667 * Functions
668 */
669
670 extern void *
671 _mesa_malloc( size_t bytes );
672
673 extern void *
674 _mesa_calloc( size_t bytes );
675
676 extern void
677 _mesa_free( void *ptr );
678
679 extern void *
680 _mesa_align_malloc( size_t bytes, unsigned long alignment );
681
682 extern void *
683 _mesa_align_calloc( size_t bytes, unsigned long alignment );
684
685 extern void
686 _mesa_align_free( void *ptr );
687
688 extern void *
689 _mesa_realloc( void *oldBuffer, size_t oldSize, size_t newSize );
690
691 extern void *
692 _mesa_memcpy( void *dest, const void *src, size_t n );
693
694 extern void
695 _mesa_memset( void *dst, int val, size_t n );
696
697 extern void
698 _mesa_memset16( unsigned short *dst, unsigned short val, size_t n );
699
700 extern void
701 _mesa_bzero( void *dst, size_t n );
702
703
704 extern double
705 _mesa_sin(double a);
706
707 extern double
708 _mesa_cos(double a);
709
710 extern double
711 _mesa_sqrtd(double x);
712
713 extern float
714 _mesa_sqrtf(float x);
715
716 extern float
717 _mesa_inv_sqrtf(float x);
718
719 extern double
720 _mesa_pow(double x, double y);
721
722 extern float
723 _mesa_log2(float x);
724
725 extern unsigned int
726 _mesa_bitcount(unsigned int n);
727
728 extern GLhalfARB
729 _mesa_float_to_half(float f);
730
731 extern float
732 _mesa_half_to_float(GLhalfARB h);
733
734
735 extern char *
736 _mesa_getenv( const char *var );
737
738 extern char *
739 _mesa_strstr( const char *haystack, const char *needle );
740
741 extern char *
742 _mesa_strncat( char *dest, const char *src, size_t n );
743
744 extern char *
745 _mesa_strcpy( char *dest, const char *src );
746
747 extern char *
748 _mesa_strncpy( char *dest, const char *src, size_t n );
749
750 extern size_t
751 _mesa_strlen( const char *s );
752
753 extern int
754 _mesa_strcmp( const char *s1, const char *s2 );
755
756 extern int
757 _mesa_strncmp( const char *s1, const char *s2, size_t n );
758
759 extern char *
760 _mesa_strdup( const char *s );
761
762 extern int
763 _mesa_atoi( const char *s );
764
765 extern double
766 _mesa_strtod( const char *s, char **end );
767
768 extern int
769 _mesa_sprintf( char *str, const char *fmt, ... );
770
771 extern void
772 _mesa_printf( const char *fmtString, ... );
773
774
775 extern void
776 _mesa_warning( __GLcontext *gc, const char *fmtString, ... );
777
778 extern void
779 _mesa_problem( const __GLcontext *ctx, const char *fmtString, ... );
780
781 extern void
782 _mesa_error( __GLcontext *ctx, GLenum error, const char *fmtString, ... );
783
784 extern void
785 _mesa_debug( const __GLcontext *ctx, const char *fmtString, ... );
786
787
788 extern void
789 _mesa_init_default_imports( __GLimports *imports, void *driverCtx );
790
791
792 #ifdef __cplusplus
793 }
794 #endif
795
796
797 #endif /* IMPORTS_H */