Support ARB_texture_env_crossbar. Changes the way programs are
[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) + (uintptr_t) (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 #ifndef M_E
228 #define M_E (2.7182818284590452354)
229 #endif
230
231
232 /* XXX this is a bit of a hack needed for compilation within XFree86 */
233 #ifndef FLT_MIN
234 #define FLT_MIN (1.0e-37)
235 #endif
236
237 /* Degrees to radians conversion: */
238 #define DEG2RAD (M_PI/180.0)
239
240
241 /***
242 *** USE_IEEE: Determine if we're using IEEE floating point
243 ***/
244 #if defined(__i386__) || defined(__386__) || defined(__sparc__) || \
245 defined(__s390x__) || defined(__powerpc__) || \
246 defined(__amd64__) || \
247 defined(ia64) || defined(__ia64__) || \
248 defined(__hppa__) || defined(hpux) || \
249 defined(__mips) || defined(_MIPS_ARCH) || \
250 defined(__arm__) || \
251 (defined(__alpha__) && (defined(__IEEE_FLOAT) || !defined(VMS)))
252 #define USE_IEEE
253 #define IEEE_ONE 0x3f800000
254 #endif
255
256
257 /***
258 *** SQRTF: single-precision square root
259 ***/
260 #if 0 /* _mesa_sqrtf() not accurate enough - temporarily disabled */
261 # define SQRTF(X) _mesa_sqrtf(X)
262 #elif defined(XFree86LOADER) && defined(IN_MODULE)
263 # define SQRTF(X) (float) xf86sqrt((float) (X))
264 #else
265 # define SQRTF(X) (float) sqrt((float) (X))
266 #endif
267
268
269 /***
270 *** INV_SQRTF: single-precision inverse square root
271 ***/
272 #if 0
273 #define INV_SQRTF(X) _mesa_inv_sqrt(X)
274 #else
275 #define INV_SQRTF(X) (1.0F / SQRTF(X)) /* this is faster on a P4 */
276 #endif
277
278
279 /***
280 *** LOG2: Log base 2 of float
281 ***/
282 #ifdef USE_IEEE
283 #if 0
284 /* This is pretty fast, but not accurate enough (only 2 fractional bits).
285 * Based on code from http://www.stereopsis.com/log2.html
286 */
287 static INLINE GLfloat LOG2(GLfloat x)
288 {
289 const GLfloat y = x * x * x * x;
290 const GLuint ix = *((GLuint *) &y);
291 const GLuint exp = (ix >> 23) & 0xFF;
292 const GLint log2 = ((GLint) exp) - 127;
293 return (GLfloat) log2 * (1.0 / 4.0); /* 4, because of x^4 above */
294 }
295 #endif
296 /* Pretty fast, and accurate.
297 * Based on code from http://www.flipcode.com/totd/
298 */
299 static INLINE GLfloat LOG2(GLfloat val)
300 {
301 fi_type num;
302 GLint log_2;
303 num.f = val;
304 log_2 = ((num.i >> 23) & 255) - 128;
305 num.i &= ~(255 << 23);
306 num.i += 127 << 23;
307 num.f = ((-1.0f/3) * num.f + 2) * num.f - 2.0f/3;
308 return num.f + log_2;
309 }
310 #elif defined(XFree86LOADER) && defined(IN_MODULE)
311 #define LOG2(x) ((GLfloat) (xf86log(x) * 1.442695))
312 #else
313 /*
314 * NOTE: log_base_2(x) = log(x) / log(2)
315 * NOTE: 1.442695 = 1/log(2).
316 */
317 #define LOG2(x) ((GLfloat) (log(x) * 1.442695F))
318 #endif
319
320
321 /***
322 *** IS_INF_OR_NAN: test if float is infinite or NaN
323 ***/
324 #ifdef USE_IEEE
325 static INLINE int IS_INF_OR_NAN( float x )
326 {
327 fi_type tmp;
328 tmp.f = x;
329 return !(int)((unsigned int)((tmp.i & 0x7fffffff)-0x7f800000) >> 31);
330 }
331 #elif defined(isfinite)
332 #define IS_INF_OR_NAN(x) (!isfinite(x))
333 #elif defined(finite)
334 #define IS_INF_OR_NAN(x) (!finite(x))
335 #elif defined(__VMS)
336 #define IS_INF_OR_NAN(x) (!finite(x))
337 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
338 #define IS_INF_OR_NAN(x) (!isfinite(x))
339 #else
340 #define IS_INF_OR_NAN(x) (!finite(x))
341 #endif
342
343
344 /***
345 *** IS_NEGATIVE: test if float is negative
346 ***/
347 #if defined(USE_IEEE)
348 static INLINE int GET_FLOAT_BITS( float x )
349 {
350 fi_type fi;
351 fi.f = x;
352 return fi.i;
353 }
354 #define IS_NEGATIVE(x) (GET_FLOAT_BITS(x) < 0)
355 #else
356 #define IS_NEGATIVE(x) (x < 0.0F)
357 #endif
358
359
360 /***
361 *** DIFFERENT_SIGNS: test if two floats have opposite signs
362 ***/
363 #if defined(USE_IEEE)
364 #define DIFFERENT_SIGNS(x,y) ((GET_FLOAT_BITS(x) ^ GET_FLOAT_BITS(y)) & (1<<31))
365 #else
366 /* Could just use (x*y<0) except for the flatshading requirements.
367 * Maybe there's a better way?
368 */
369 #define DIFFERENT_SIGNS(x,y) ((x) * (y) <= 0.0F && (x) - (y) != 0.0F)
370 #endif
371
372
373 /***
374 *** CEILF: ceiling of float
375 *** FLOORF: floor of float
376 *** FABSF: absolute value of float
377 ***/
378 #if defined(XFree86LOADER) && defined(IN_MODULE)
379 #define CEILF(x) ((GLfloat) xf86ceil(x))
380 #define FLOORF(x) ((GLfloat) xf86floor(x))
381 #define FABSF(x) ((GLfloat) xf86fabs(x))
382 #elif defined(__gnu_linux__)
383 /* C99 functions */
384 #define CEILF(x) ceilf(x)
385 #define FLOORF(x) floorf(x)
386 #define FABSF(x) fabsf(x)
387 #else
388 #define CEILF(x) ((GLfloat) ceil(x))
389 #define FLOORF(x) ((GLfloat) floor(x))
390 #define FABSF(x) ((GLfloat) fabs(x))
391 #endif
392
393
394 /***
395 *** IROUND: return (as an integer) float rounded to nearest integer
396 ***/
397 #if defined(USE_SPARC_ASM) && defined(__GNUC__) && defined(__sparc__)
398 static INLINE int iround(float f)
399 {
400 int r;
401 __asm__ ("fstoi %1, %0" : "=f" (r) : "f" (f));
402 return r;
403 }
404 #define IROUND(x) iround(x)
405 #elif defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__) && \
406 (!defined(__BEOS__) || (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)))
407 static INLINE int iround(float f)
408 {
409 int r;
410 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
411 return r;
412 }
413 #define IROUND(x) iround(x)
414 #elif defined(USE_X86_ASM) && defined(__MSC__) && defined(__WIN32__)
415 static INLINE int iround(float f)
416 {
417 int r;
418 _asm {
419 fld f
420 fistp r
421 }
422 return r;
423 }
424 #define IROUND(x) iround(x)
425 #elif defined(__WATCOMC__) && defined(__386__)
426 long iround(float f);
427 #pragma aux iround = \
428 "push eax" \
429 "fistp dword ptr [esp]" \
430 "pop eax" \
431 parm [8087] \
432 value [eax] \
433 modify exact [eax];
434 #define IROUND(x) iround(x)
435 #else
436 #define IROUND(f) ((int) (((f) >= 0.0F) ? ((f) + 0.5F) : ((f) - 0.5F)))
437 #endif
438
439
440 /***
441 *** IROUND_POS: return (as an integer) positive float rounded to nearest int
442 ***/
443 #ifdef DEBUG
444 #define IROUND_POS(f) (assert((f) >= 0.0F), IROUND(f))
445 #else
446 #define IROUND_POS(f) (IROUND(f))
447 #endif
448
449
450 /***
451 *** IFLOOR: return (as an integer) floor of float
452 ***/
453 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
454 /*
455 * IEEE floor for computers that round to nearest or even.
456 * 'f' must be between -4194304 and 4194303.
457 * This floor operation is done by "(iround(f + .5) + iround(f - .5)) >> 1",
458 * but uses some IEEE specific tricks for better speed.
459 * Contributed by Josh Vanderhoof
460 */
461 static INLINE int ifloor(float f)
462 {
463 int ai, bi;
464 double af, bf;
465 af = (3 << 22) + 0.5 + (double)f;
466 bf = (3 << 22) + 0.5 - (double)f;
467 /* GCC generates an extra fstp/fld without this. */
468 __asm__ ("fstps %0" : "=m" (ai) : "t" (af) : "st");
469 __asm__ ("fstps %0" : "=m" (bi) : "t" (bf) : "st");
470 return (ai - bi) >> 1;
471 }
472 #define IFLOOR(x) ifloor(x)
473 #elif defined(USE_IEEE)
474 static INLINE int ifloor(float f)
475 {
476 int ai, bi;
477 double af, bf;
478 fi_type u;
479
480 af = (3 << 22) + 0.5 + (double)f;
481 bf = (3 << 22) + 0.5 - (double)f;
482 u.f = (float) af; ai = u.i;
483 u.f = (float) bf; bi = u.i;
484 return (ai - bi) >> 1;
485 }
486 #define IFLOOR(x) ifloor(x)
487 #else
488 static INLINE int ifloor(float f)
489 {
490 int i = IROUND(f);
491 return (i > f) ? i - 1 : i;
492 }
493 #define IFLOOR(x) ifloor(x)
494 #endif
495
496
497 /***
498 *** ICEIL: return (as an integer) ceiling of float
499 ***/
500 #if defined(USE_X86_ASM) && defined(__GNUC__) && defined(__i386__)
501 /*
502 * IEEE ceil for computers that round to nearest or even.
503 * 'f' must be between -4194304 and 4194303.
504 * This ceil operation is done by "(iround(f + .5) + iround(f - .5) + 1) >> 1",
505 * but uses some IEEE specific tricks for better speed.
506 * Contributed by Josh Vanderhoof
507 */
508 static INLINE int iceil(float f)
509 {
510 int ai, bi;
511 double af, bf;
512 af = (3 << 22) + 0.5 + (double)f;
513 bf = (3 << 22) + 0.5 - (double)f;
514 /* GCC generates an extra fstp/fld without this. */
515 __asm__ ("fstps %0" : "=m" (ai) : "t" (af) : "st");
516 __asm__ ("fstps %0" : "=m" (bi) : "t" (bf) : "st");
517 return (ai - bi + 1) >> 1;
518 }
519 #define ICEIL(x) iceil(x)
520 #elif defined(USE_IEEE)
521 static INLINE int iceil(float f)
522 {
523 int ai, bi;
524 double af, bf;
525 fi_type u;
526 af = (3 << 22) + 0.5 + (double)f;
527 bf = (3 << 22) + 0.5 - (double)f;
528 u.f = (float) af; ai = u.i;
529 u.f = (float) bf; bi = u.i;
530 return (ai - bi + 1) >> 1;
531 }
532 #define ICEIL(x) iceil(x)
533 #else
534 static INLINE int iceil(float f)
535 {
536 int i = IROUND(f);
537 return (i < f) ? i + 1 : i;
538 }
539 #define ICEIL(x) iceil(x)
540 #endif
541
542
543 /***
544 *** UNCLAMPED_FLOAT_TO_UBYTE: clamp float to [0,1] and map to ubyte in [0,255]
545 *** CLAMPED_FLOAT_TO_UBYTE: map float known to be in [0,1] to ubyte in [0,255]
546 ***/
547 #if defined(USE_IEEE) && !defined(DEBUG)
548 #define IEEE_0996 0x3f7f0000 /* 0.996 or so */
549 /* This function/macro is sensitive to precision. Test very carefully
550 * if you change it!
551 */
552 #define UNCLAMPED_FLOAT_TO_UBYTE(UB, F) \
553 do { \
554 fi_type __tmp; \
555 __tmp.f = (F); \
556 if (__tmp.i < 0) \
557 UB = (GLubyte) 0; \
558 else if (__tmp.i >= IEEE_0996) \
559 UB = (GLubyte) 255; \
560 else { \
561 __tmp.f = __tmp.f * (255.0F/256.0F) + 32768.0F; \
562 UB = (GLubyte) __tmp.i; \
563 } \
564 } while (0)
565 #define CLAMPED_FLOAT_TO_UBYTE(UB, F) \
566 do { \
567 fi_type __tmp; \
568 __tmp.f = (F) * (255.0F/256.0F) + 32768.0F; \
569 UB = (GLubyte) __tmp.i; \
570 } while (0)
571 #else
572 #define UNCLAMPED_FLOAT_TO_UBYTE(ub, f) \
573 ub = ((GLubyte) IROUND(CLAMP((f), 0.0F, 1.0F) * 255.0F))
574 #define CLAMPED_FLOAT_TO_UBYTE(ub, f) \
575 ub = ((GLubyte) IROUND((f) * 255.0F))
576 #endif
577
578
579 /***
580 *** COPY_FLOAT: copy a float from src to dest.
581 ***/
582 #define COPY_FLOAT( dst, src ) (dst) = (src)
583
584
585 /***
586 *** START_FAST_MATH: Set x86 FPU to faster, 32-bit precision mode (and save
587 *** original mode to a temporary).
588 *** END_FAST_MATH: Restore x86 FPU to original mode.
589 ***/
590 #if defined(__GNUC__) && defined(__i386__)
591 /*
592 * Set the x86 FPU control word to guarentee only 32 bits of precision
593 * are stored in registers. Allowing the FPU to store more introduces
594 * differences between situations where numbers are pulled out of memory
595 * vs. situations where the compiler is able to optimize register usage.
596 *
597 * In the worst case, we force the compiler to use a memory access to
598 * truncate the float, by specifying the 'volatile' keyword.
599 */
600 /* Hardware default: All exceptions masked, extended double precision,
601 * round to nearest (IEEE compliant):
602 */
603 #define DEFAULT_X86_FPU 0x037f
604 /* All exceptions masked, single precision, round to nearest:
605 */
606 #define FAST_X86_FPU 0x003f
607 /* The fldcw instruction will cause any pending FP exceptions to be
608 * raised prior to entering the block, and we clear any pending
609 * exceptions before exiting the block. Hence, asm code has free
610 * reign over the FPU while in the fast math block.
611 */
612 #if defined(NO_FAST_MATH)
613 #define START_FAST_MATH(x) \
614 do { \
615 static GLuint mask = DEFAULT_X86_FPU; \
616 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
617 __asm__ ( "fldcw %0" : : "m" (mask) ); \
618 } while (0)
619 #else
620 #define START_FAST_MATH(x) \
621 do { \
622 static GLuint mask = FAST_X86_FPU; \
623 __asm__ ( "fnstcw %0" : "=m" (*&(x)) ); \
624 __asm__ ( "fldcw %0" : : "m" (mask) ); \
625 } while (0)
626 #endif
627 /* Restore original FPU mode, and clear any exceptions that may have
628 * occurred in the FAST_MATH block.
629 */
630 #define END_FAST_MATH(x) \
631 do { \
632 __asm__ ( "fnclex ; fldcw %0" : : "m" (*&(x)) ); \
633 } while (0)
634
635 #elif defined(__WATCOMC__) && defined(__386__)
636 #define DEFAULT_X86_FPU 0x037f /* See GCC comments above */
637 #define FAST_X86_FPU 0x003f /* See GCC comments above */
638 void _watcom_start_fast_math(unsigned short *x,unsigned short *mask);
639 #pragma aux _watcom_start_fast_math = \
640 "fnstcw word ptr [eax]" \
641 "fldcw word ptr [ecx]" \
642 parm [eax] [ecx] \
643 modify exact [];
644 void _watcom_end_fast_math(unsigned short *x);
645 #pragma aux _watcom_end_fast_math = \
646 "fnclex" \
647 "fldcw word ptr [eax]" \
648 parm [eax] \
649 modify exact [];
650 #if defined(NO_FAST_MATH)
651 #define START_FAST_MATH(x) \
652 do { \
653 static GLushort mask = DEFAULT_X86_FPU; \
654 _watcom_start_fast_math(&x,&mask); \
655 } while (0)
656 #else
657 #define START_FAST_MATH(x) \
658 do { \
659 static GLushort mask = FAST_X86_FPU; \
660 _watcom_start_fast_math(&x,&mask); \
661 } while (0)
662 #endif
663 #define END_FAST_MATH(x) _watcom_end_fast_math(&x)
664 #else
665 #define START_FAST_MATH(x) x = 0
666 #define END_FAST_MATH(x) (void)(x)
667 #endif
668
669
670
671 /**********************************************************************
672 * Functions
673 */
674
675 extern void *
676 _mesa_malloc( size_t bytes );
677
678 extern void *
679 _mesa_calloc( size_t bytes );
680
681 extern void
682 _mesa_free( void *ptr );
683
684 extern void *
685 _mesa_align_malloc( size_t bytes, unsigned long alignment );
686
687 extern void *
688 _mesa_align_calloc( size_t bytes, unsigned long alignment );
689
690 extern void
691 _mesa_align_free( void *ptr );
692
693 extern void *
694 _mesa_realloc( void *oldBuffer, size_t oldSize, size_t newSize );
695
696 extern void *
697 _mesa_memcpy( void *dest, const void *src, size_t n );
698
699 extern void
700 _mesa_memset( void *dst, int val, size_t n );
701
702 extern void
703 _mesa_memset16( unsigned short *dst, unsigned short val, size_t n );
704
705 extern void
706 _mesa_bzero( void *dst, size_t n );
707
708
709 extern double
710 _mesa_sin(double a);
711
712 extern double
713 _mesa_cos(double a);
714
715 extern double
716 _mesa_sqrtd(double x);
717
718 extern float
719 _mesa_sqrtf(float x);
720
721 extern float
722 _mesa_inv_sqrtf(float x);
723
724 extern double
725 _mesa_pow(double x, double y);
726
727 extern float
728 _mesa_log2(float x);
729
730 extern unsigned int
731 _mesa_bitcount(unsigned int n);
732
733 extern GLhalfARB
734 _mesa_float_to_half(float f);
735
736 extern float
737 _mesa_half_to_float(GLhalfARB h);
738
739
740 extern char *
741 _mesa_getenv( const char *var );
742
743 extern char *
744 _mesa_strstr( const char *haystack, const char *needle );
745
746 extern char *
747 _mesa_strncat( char *dest, const char *src, size_t n );
748
749 extern char *
750 _mesa_strcpy( char *dest, const char *src );
751
752 extern char *
753 _mesa_strncpy( char *dest, const char *src, size_t n );
754
755 extern size_t
756 _mesa_strlen( const char *s );
757
758 extern int
759 _mesa_strcmp( const char *s1, const char *s2 );
760
761 extern int
762 _mesa_strncmp( const char *s1, const char *s2, size_t n );
763
764 extern char *
765 _mesa_strdup( const char *s );
766
767 extern int
768 _mesa_atoi( const char *s );
769
770 extern double
771 _mesa_strtod( const char *s, char **end );
772
773 extern int
774 _mesa_sprintf( char *str, const char *fmt, ... );
775
776 extern void
777 _mesa_printf( const char *fmtString, ... );
778
779
780 extern void
781 _mesa_warning( __GLcontext *gc, const char *fmtString, ... );
782
783 extern void
784 _mesa_problem( const __GLcontext *ctx, const char *fmtString, ... );
785
786 extern void
787 _mesa_error( __GLcontext *ctx, GLenum error, const char *fmtString, ... );
788
789 extern void
790 _mesa_debug( const __GLcontext *ctx, const char *fmtString, ... );
791
792
793 extern void
794 _mesa_init_default_imports( __GLimports *imports, void *driverCtx );
795
796
797 #ifdef __cplusplus
798 }
799 #endif
800
801
802 #endif /* IMPORTS_H */