Runtime generate sse/sse2 code for some vertex programs. Experimental
[mesa.git] / src / mesa / main / imports.c
1 /**
2 * \file imports.c
3 * Standard C library function wrappers.
4 *
5 * Imports are services which the device driver or window system or
6 * operating system provides to the core renderer. The core renderer (Mesa)
7 * will call these functions in order to do memory allocation, simple I/O,
8 * etc.
9 *
10 * Some drivers will want to override/replace this file with something
11 * specialized, but that'll be rare.
12 *
13 * Eventually, I want to move roll the glheader.h file into this.
14 *
15 * The OpenGL SI's __GLimports structure allows per-context specification of
16 * replacements for the standard C lib functions. In practice that's probably
17 * never needed; compile-time replacements are far more likely.
18 *
19 * The _mesa_*() functions defined here don't in general take a context
20 * parameter. I guess we can change that someday, if need be.
21 * So for now, the __GLimports stuff really isn't used.
22 *
23 * \todo Functions still needed:
24 * - scanf
25 * - qsort
26 * - bsearch
27 * - rand and RAND_MAX
28 *
29 * \note When compiled into a XFree86 module these functions wrap around
30 * XFree86 own wrappers.
31 */
32
33 /*
34 * Mesa 3-D graphics library
35 * Version: 6.3
36 *
37 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
38 *
39 * Permission is hereby granted, free of charge, to any person obtaining a
40 * copy of this software and associated documentation files (the "Software"),
41 * to deal in the Software without restriction, including without limitation
42 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
43 * and/or sell copies of the Software, and to permit persons to whom the
44 * Software is furnished to do so, subject to the following conditions:
45 *
46 * The above copyright notice and this permission notice shall be included
47 * in all copies or substantial portions of the Software.
48 *
49 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
50 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
52 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
53 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
54 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
55 */
56
57
58
59 #include "imports.h"
60 #include "context.h"
61 #include "version.h"
62
63
64 #define MAXSTRING 4000 /* for vsnprintf() */
65
66 #ifdef WIN32
67 #define vsnprintf _vsnprintf
68 #elif defined(__IBMC__) || defined(__IBMCPP__) || ( defined(__VMS) && __CRTL_VER < 70312000 )
69 extern int vsnprintf(char *str, size_t count, const char *fmt, va_list arg);
70 #ifdef __VMS
71 #include "vsnprintf.c"
72 #endif
73 #endif
74
75
76 /**********************************************************************/
77 /** \name Memory */
78 /*@{*/
79
80 /** Wrapper around either malloc() or xf86malloc() */
81 void *
82 _mesa_malloc(size_t bytes)
83 {
84 #if defined(XFree86LOADER) && defined(IN_MODULE)
85 return xf86malloc(bytes);
86 #else
87 return malloc(bytes);
88 #endif
89 }
90
91 /** Wrapper around either calloc() or xf86calloc() */
92 void *
93 _mesa_calloc(size_t bytes)
94 {
95 #if defined(XFree86LOADER) && defined(IN_MODULE)
96 return xf86calloc(1, bytes);
97 #else
98 return calloc(1, bytes);
99 #endif
100 }
101
102 /** Wrapper around either free() or xf86free() */
103 void
104 _mesa_free(void *ptr)
105 {
106 #if defined(XFree86LOADER) && defined(IN_MODULE)
107 xf86free(ptr);
108 #else
109 free(ptr);
110 #endif
111 }
112
113 /**
114 * Allocate aligned memory.
115 *
116 * \param bytes number of bytes to allocate.
117 * \param alignment alignment (must be greater than zero).
118 *
119 * Allocates extra memory to accommodate rounding up the address for
120 * alignment and to record the real malloc address.
121 *
122 * \sa _mesa_align_free().
123 */
124 void *
125 _mesa_align_malloc(size_t bytes, unsigned long alignment)
126 {
127 uintptr_t ptr, buf;
128
129 ASSERT( alignment > 0 );
130
131 ptr = (uintptr_t) _mesa_malloc(bytes + alignment + sizeof(void *));
132 if (!ptr)
133 return NULL;
134
135 buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1);
136 *(uintptr_t *)(buf - sizeof(void *)) = ptr;
137
138 #ifdef DEBUG
139 /* mark the non-aligned area */
140 while ( ptr < buf - sizeof(void *) ) {
141 *(unsigned long *)ptr = 0xcdcdcdcd;
142 ptr += sizeof(unsigned long);
143 }
144 #endif
145
146 return (void *) buf;
147 }
148
149 /** Same as _mesa_align_malloc(), but using _mesa_calloc() instead of
150 * _mesa_malloc() */
151 void *
152 _mesa_align_calloc(size_t bytes, unsigned long alignment)
153 {
154 uintptr_t ptr, buf;
155
156 ASSERT( alignment > 0 );
157
158 ptr = (uintptr_t) _mesa_calloc(bytes + alignment + sizeof(void *));
159 if (!ptr)
160 return NULL;
161
162 buf = (ptr + alignment + sizeof(void *)) & ~(uintptr_t)(alignment - 1);
163 *(uintptr_t *)(buf - sizeof(void *)) = ptr;
164
165 #ifdef DEBUG
166 /* mark the non-aligned area */
167 while ( ptr < buf - sizeof(void *) ) {
168 *(unsigned long *)ptr = 0xcdcdcdcd;
169 ptr += sizeof(unsigned long);
170 }
171 #endif
172
173 return (void *)buf;
174 }
175
176 /**
177 * Free memory allocated with _mesa_align_malloc() or _mesa_align_calloc().
178 *
179 * \param ptr pointer to the memory to be freed.
180 *
181 * The actual address to free is stored in the word immediately before the
182 * address the client sees.
183 */
184 void
185 _mesa_align_free(void *ptr)
186 {
187 #if 0
188 _mesa_free( (void *)(*(unsigned long *)((unsigned long)ptr - sizeof(void *))) );
189 #else
190 void **cubbyHole = (void **) ((char *) ptr - sizeof(void *));
191 void *realAddr = *cubbyHole;
192 _mesa_free(realAddr);
193 #endif
194 }
195
196 /** Wrapper around either memcpy() or xf86memcpy() */
197 void *
198 _mesa_realloc(void *oldBuffer, size_t oldSize, size_t newSize)
199 {
200 const size_t copySize = (oldSize < newSize) ? oldSize : newSize;
201 void *newBuffer = _mesa_malloc(newSize);
202 if (newBuffer && copySize > 0)
203 _mesa_memcpy(newBuffer, oldBuffer, copySize);
204 if (oldBuffer)
205 _mesa_free(oldBuffer);
206 return newBuffer;
207 }
208
209
210 void *
211 _mesa_memcpy(void *dest, const void *src, size_t n)
212 {
213 #if defined(XFree86LOADER) && defined(IN_MODULE)
214 return xf86memcpy(dest, src, n);
215 #elif defined(SUNOS4)
216 return memcpy((char *) dest, (char *) src, (int) n);
217 #else
218 return memcpy(dest, src, n);
219 #endif
220 }
221
222 /** Wrapper around either memset() or xf86memset() */
223 void
224 _mesa_memset( void *dst, int val, size_t n )
225 {
226 #if defined(XFree86LOADER) && defined(IN_MODULE)
227 xf86memset( dst, val, n );
228 #elif defined(SUNOS4)
229 memset( (char *) dst, (int) val, (int) n );
230 #else
231 memset(dst, val, n);
232 #endif
233 }
234
235 /** Fill memory with a constant 16bit word.
236 *
237 * \param dst destination pointer.
238 * \param val value.
239 * \param n number of words.
240 */
241 void
242 _mesa_memset16( unsigned short *dst, unsigned short val, size_t n )
243 {
244 while (n-- > 0)
245 *dst++ = val;
246 }
247
248 /** Wrapper around either memcpy() or xf86memcpy() or bzero() */
249 void
250 _mesa_bzero( void *dst, size_t n )
251 {
252 #if defined(XFree86LOADER) && defined(IN_MODULE)
253 xf86memset( dst, 0, n );
254 #elif defined(__FreeBSD__)
255 bzero( dst, n );
256 #else
257 memset( dst, 0, n );
258 #endif
259 }
260
261 /*@}*/
262
263
264 /**********************************************************************/
265 /** \name Math */
266 /*@{*/
267
268 /** Wrapper around either sin() or xf86sin() */
269 double
270 _mesa_sin(double a)
271 {
272 #if defined(XFree86LOADER) && defined(IN_MODULE)
273 return xf86sin(a);
274 #else
275 return sin(a);
276 #endif
277 }
278
279 /** Wrapper around either cos() or xf86cos() */
280 double
281 _mesa_cos(double a)
282 {
283 #if defined(XFree86LOADER) && defined(IN_MODULE)
284 return xf86cos(a);
285 #else
286 return cos(a);
287 #endif
288 }
289
290 /** Wrapper around either sqrt() or xf86sqrt() */
291 double
292 _mesa_sqrtd(double x)
293 {
294 #if defined(XFree86LOADER) && defined(IN_MODULE)
295 return xf86sqrt(x);
296 #else
297 return sqrt(x);
298 #endif
299 }
300
301
302 /*
303 * A High Speed, Low Precision Square Root
304 * by Paul Lalonde and Robert Dawson
305 * from "Graphics Gems", Academic Press, 1990
306 *
307 * SPARC implementation of a fast square root by table
308 * lookup.
309 * SPARC floating point format is as follows:
310 *
311 * BIT 31 30 23 22 0
312 * sign exponent mantissa
313 */
314 static short sqrttab[0x100]; /* declare table of square roots */
315
316 static void init_sqrt_table(void)
317 {
318 #if defined(USE_IEEE) && !defined(DEBUG)
319 unsigned short i;
320 fi_type fi; /* to access the bits of a float in C quickly */
321 /* we use a union defined in glheader.h */
322
323 for(i=0; i<= 0x7f; i++) {
324 fi.i = 0;
325
326 /*
327 * Build a float with the bit pattern i as mantissa
328 * and an exponent of 0, stored as 127
329 */
330
331 fi.i = (i << 16) | (127 << 23);
332 fi.f = _mesa_sqrtd(fi.f);
333
334 /*
335 * Take the square root then strip the first 7 bits of
336 * the mantissa into the table
337 */
338
339 sqrttab[i] = (fi.i & 0x7fffff) >> 16;
340
341 /*
342 * Repeat the process, this time with an exponent of
343 * 1, stored as 128
344 */
345
346 fi.i = 0;
347 fi.i = (i << 16) | (128 << 23);
348 fi.f = sqrt(fi.f);
349 sqrttab[i+0x80] = (fi.i & 0x7fffff) >> 16;
350 }
351 #else
352 (void) sqrttab; /* silence compiler warnings */
353 #endif /*HAVE_FAST_MATH*/
354 }
355
356
357 /**
358 * Single precision square root.
359 */
360 float
361 _mesa_sqrtf( float x )
362 {
363 #if defined(USE_IEEE) && !defined(DEBUG)
364 fi_type num;
365 /* to access the bits of a float in C
366 * we use a union from glheader.h */
367
368 short e; /* the exponent */
369 if (x == 0.0F) return 0.0F; /* check for square root of 0 */
370 num.f = x;
371 e = (num.i >> 23) - 127; /* get the exponent - on a SPARC the */
372 /* exponent is stored with 127 added */
373 num.i &= 0x7fffff; /* leave only the mantissa */
374 if (e & 0x01) num.i |= 0x800000;
375 /* the exponent is odd so we have to */
376 /* look it up in the second half of */
377 /* the lookup table, so we set the */
378 /* high bit */
379 e >>= 1; /* divide the exponent by two */
380 /* note that in C the shift */
381 /* operators are sign preserving */
382 /* for signed operands */
383 /* Do the table lookup, based on the quaternary mantissa,
384 * then reconstruct the result back into a float
385 */
386 num.i = ((sqrttab[num.i >> 16]) << 16) | ((e + 127) << 23);
387
388 return num.f;
389 #else
390 return (float) _mesa_sqrtd((double) x);
391 #endif
392 }
393
394
395 /**
396 inv_sqrt - A single precision 1/sqrt routine for IEEE format floats.
397 written by Josh Vanderhoof, based on newsgroup posts by James Van Buskirk
398 and Vesa Karvonen.
399 */
400 float
401 _mesa_inv_sqrtf(float n)
402 {
403 #if defined(USE_IEEE) && !defined(DEBUG)
404 float r0, x0, y0;
405 float r1, x1, y1;
406 float r2, x2, y2;
407 #if 0 /* not used, see below -BP */
408 float r3, x3, y3;
409 #endif
410 union { float f; unsigned int i; } u;
411 unsigned int magic;
412
413 /*
414 Exponent part of the magic number -
415
416 We want to:
417 1. subtract the bias from the exponent,
418 2. negate it
419 3. divide by two (rounding towards -inf)
420 4. add the bias back
421
422 Which is the same as subtracting the exponent from 381 and dividing
423 by 2.
424
425 floor(-(x - 127) / 2) + 127 = floor((381 - x) / 2)
426 */
427
428 magic = 381 << 23;
429
430 /*
431 Significand part of magic number -
432
433 With the current magic number, "(magic - u.i) >> 1" will give you:
434
435 for 1 <= u.f <= 2: 1.25 - u.f / 4
436 for 2 <= u.f <= 4: 1.00 - u.f / 8
437
438 This isn't a bad approximation of 1/sqrt. The maximum difference from
439 1/sqrt will be around .06. After three Newton-Raphson iterations, the
440 maximum difference is less than 4.5e-8. (Which is actually close
441 enough to make the following bias academic...)
442
443 To get a better approximation you can add a bias to the magic
444 number. For example, if you subtract 1/2 of the maximum difference in
445 the first approximation (.03), you will get the following function:
446
447 for 1 <= u.f <= 2: 1.22 - u.f / 4
448 for 2 <= u.f <= 3.76: 0.97 - u.f / 8
449 for 3.76 <= u.f <= 4: 0.72 - u.f / 16
450 (The 3.76 to 4 range is where the result is < .5.)
451
452 This is the closest possible initial approximation, but with a maximum
453 error of 8e-11 after three NR iterations, it is still not perfect. If
454 you subtract 0.0332281 instead of .03, the maximum error will be
455 2.5e-11 after three NR iterations, which should be about as close as
456 is possible.
457
458 for 1 <= u.f <= 2: 1.2167719 - u.f / 4
459 for 2 <= u.f <= 3.73: 0.9667719 - u.f / 8
460 for 3.73 <= u.f <= 4: 0.7167719 - u.f / 16
461
462 */
463
464 magic -= (int)(0.0332281 * (1 << 25));
465
466 u.f = n;
467 u.i = (magic - u.i) >> 1;
468
469 /*
470 Instead of Newton-Raphson, we use Goldschmidt's algorithm, which
471 allows more parallelism. From what I understand, the parallelism
472 comes at the cost of less precision, because it lets error
473 accumulate across iterations.
474 */
475 x0 = 1.0f;
476 y0 = 0.5f * n;
477 r0 = u.f;
478
479 x1 = x0 * r0;
480 y1 = y0 * r0 * r0;
481 r1 = 1.5f - y1;
482
483 x2 = x1 * r1;
484 y2 = y1 * r1 * r1;
485 r2 = 1.5f - y2;
486
487 #if 1
488 return x2 * r2; /* we can stop here, and be conformant -BP */
489 #else
490 x3 = x2 * r2;
491 y3 = y2 * r2 * r2;
492 r3 = 1.5f - y3;
493
494 return x3 * r3;
495 #endif
496 #elif defined(XFree86LOADER) && defined(IN_MODULE)
497 return 1.0F / xf86sqrt(n);
498 #else
499 return (float) (1.0 / sqrt(n));
500 #endif
501 }
502
503
504 /**
505 * Wrapper around either pow() or xf86pow().
506 */
507 double
508 _mesa_pow(double x, double y)
509 {
510 #if defined(XFree86LOADER) && defined(IN_MODULE)
511 return xf86pow(x, y);
512 #else
513 return pow(x, y);
514 #endif
515 }
516
517
518 /**
519 * Return number of bits set in given GLuint.
520 */
521 unsigned int
522 _mesa_bitcount(unsigned int n)
523 {
524 unsigned int bits;
525 for (bits = 0; n > 0; n = n >> 1) {
526 bits += (n & 1);
527 }
528 return bits;
529 }
530
531
532 /**
533 * Convert a 4-byte float to a 2-byte half float.
534 * Based on code from:
535 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
536 */
537 GLhalfARB
538 _mesa_float_to_half(float val)
539 {
540 const int flt = *((int *) (void *) &val);
541 const int flt_m = flt & 0x7fffff;
542 const int flt_e = (flt >> 23) & 0xff;
543 const int flt_s = (flt >> 31) & 0x1;
544 int s, e, m = 0;
545 GLhalfARB result;
546
547 /* sign bit */
548 s = flt_s;
549
550 /* handle special cases */
551 if ((flt_e == 0) && (flt_m == 0)) {
552 /* zero */
553 /* m = 0; - already set */
554 e = 0;
555 }
556 else if ((flt_e == 0) && (flt_m != 0)) {
557 /* denorm -- denorm float maps to 0 half */
558 /* m = 0; - already set */
559 e = 0;
560 }
561 else if ((flt_e == 0xff) && (flt_m == 0)) {
562 /* infinity */
563 /* m = 0; - already set */
564 e = 31;
565 }
566 else if ((flt_e == 0xff) && (flt_m != 0)) {
567 /* NaN */
568 m = 1;
569 e = 31;
570 }
571 else {
572 /* regular number */
573 const int new_exp = flt_e - 127;
574 if (new_exp < -24) {
575 /* this maps to 0 */
576 /* m = 0; - already set */
577 e = 0;
578 }
579 else if (new_exp < -14) {
580 /* this maps to a denorm */
581 unsigned int exp_val = (unsigned int) (-14 - new_exp); /* 2^-exp_val*/
582 e = 0;
583 switch (exp_val) {
584 case 0:
585 _mesa_warning(NULL,
586 "float_to_half: logical error in denorm creation!\n");
587 /* m = 0; - already set */
588 break;
589 case 1: m = 512 + (flt_m >> 14); break;
590 case 2: m = 256 + (flt_m >> 15); break;
591 case 3: m = 128 + (flt_m >> 16); break;
592 case 4: m = 64 + (flt_m >> 17); break;
593 case 5: m = 32 + (flt_m >> 18); break;
594 case 6: m = 16 + (flt_m >> 19); break;
595 case 7: m = 8 + (flt_m >> 20); break;
596 case 8: m = 4 + (flt_m >> 21); break;
597 case 9: m = 2 + (flt_m >> 22); break;
598 case 10: m = 1; break;
599 }
600 }
601 else if (new_exp > 15) {
602 /* map this value to infinity */
603 /* m = 0; - already set */
604 e = 31;
605 }
606 else {
607 /* regular */
608 e = new_exp + 15;
609 m = flt_m >> 13;
610 }
611 }
612
613 result = (s << 15) | (e << 10) | m;
614 return result;
615 }
616
617
618 /**
619 * Convert a 2-byte half float to a 4-byte float.
620 * Based on code from:
621 * http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/008786.html
622 */
623 float
624 _mesa_half_to_float(GLhalfARB val)
625 {
626 /* XXX could also use a 64K-entry lookup table */
627 const int m = val & 0x3ff;
628 const int e = (val >> 10) & 0x1f;
629 const int s = (val >> 15) & 0x1;
630 int flt_m, flt_e, flt_s, flt;
631 float result;
632
633 /* sign bit */
634 flt_s = s;
635
636 /* handle special cases */
637 if ((e == 0) && (m == 0)) {
638 /* zero */
639 flt_m = 0;
640 flt_e = 0;
641 }
642 else if ((e == 0) && (m != 0)) {
643 /* denorm -- denorm half will fit in non-denorm single */
644 const float half_denorm = 1.0f / 16384.0f; /* 2^-14 */
645 float mantissa = ((float) (m)) / 1024.0f;
646 float sign = s ? -1.0f : 1.0f;
647 return sign * mantissa * half_denorm;
648 }
649 else if ((e == 31) && (m == 0)) {
650 /* infinity */
651 flt_e = 0xff;
652 flt_m = 0;
653 }
654 else if ((e == 31) && (m != 0)) {
655 /* NaN */
656 flt_e = 0xff;
657 flt_m = 1;
658 }
659 else {
660 /* regular */
661 flt_e = e + 112;
662 flt_m = m << 13;
663 }
664
665 flt = (flt_s << 31) | (flt_e << 23) | flt_m;
666 result = *((float *) (void *) &flt);
667 return result;
668 }
669
670 /*@}*/
671
672
673 /**********************************************************************/
674 /** \name Environment vars */
675 /*@{*/
676
677 /**
678 * Wrapper for getenv().
679 */
680 char *
681 _mesa_getenv( const char *var )
682 {
683 #if defined(XFree86LOADER) && defined(IN_MODULE)
684 return xf86getenv(var);
685 #elif defined(_XBOX)
686 return NULL;
687 #else
688 return getenv(var);
689 #endif
690 }
691
692 /*@}*/
693
694
695 /**********************************************************************/
696 /** \name String */
697 /*@{*/
698
699 /** Wrapper around either strstr() or xf86strstr() */
700 char *
701 _mesa_strstr( const char *haystack, const char *needle )
702 {
703 #if defined(XFree86LOADER) && defined(IN_MODULE)
704 return xf86strstr(haystack, needle);
705 #else
706 return strstr(haystack, needle);
707 #endif
708 }
709
710 /** Wrapper around either strncat() or xf86strncat() */
711 char *
712 _mesa_strncat( char *dest, const char *src, size_t n )
713 {
714 #if defined(XFree86LOADER) && defined(IN_MODULE)
715 return xf86strncat(dest, src, n);
716 #else
717 return strncat(dest, src, n);
718 #endif
719 }
720
721 /** Wrapper around either strcpy() or xf86strcpy() */
722 char *
723 _mesa_strcpy( char *dest, const char *src )
724 {
725 #if defined(XFree86LOADER) && defined(IN_MODULE)
726 return xf86strcpy(dest, src);
727 #else
728 return strcpy(dest, src);
729 #endif
730 }
731
732 /** Wrapper around either strncpy() or xf86strncpy() */
733 char *
734 _mesa_strncpy( char *dest, const char *src, size_t n )
735 {
736 #if defined(XFree86LOADER) && defined(IN_MODULE)
737 return xf86strncpy(dest, src, n);
738 #else
739 return strncpy(dest, src, n);
740 #endif
741 }
742
743 /** Wrapper around either strlen() or xf86strlen() */
744 size_t
745 _mesa_strlen( const char *s )
746 {
747 #if defined(XFree86LOADER) && defined(IN_MODULE)
748 return xf86strlen(s);
749 #else
750 return strlen(s);
751 #endif
752 }
753
754 /** Wrapper around either strcmp() or xf86strcmp() */
755 int
756 _mesa_strcmp( const char *s1, const char *s2 )
757 {
758 #if defined(XFree86LOADER) && defined(IN_MODULE)
759 return xf86strcmp(s1, s2);
760 #else
761 return strcmp(s1, s2);
762 #endif
763 }
764
765 /** Wrapper around either strncmp() or xf86strncmp() */
766 int
767 _mesa_strncmp( const char *s1, const char *s2, size_t n )
768 {
769 #if defined(XFree86LOADER) && defined(IN_MODULE)
770 return xf86strncmp(s1, s2, n);
771 #else
772 return strncmp(s1, s2, n);
773 #endif
774 }
775
776 /** Implemented using _mesa_malloc() and _mesa_strcpy */
777 char *
778 _mesa_strdup( const char *s )
779 {
780 size_t l = _mesa_strlen(s);
781 char *s2 = (char *) _mesa_malloc(l + 1);
782 if (s2)
783 _mesa_strcpy(s2, s);
784 return s2;
785 }
786
787 /** Wrapper around either atoi() or xf86atoi() */
788 int
789 _mesa_atoi(const char *s)
790 {
791 #if defined(XFree86LOADER) && defined(IN_MODULE)
792 return xf86atoi(s);
793 #else
794 return atoi(s);
795 #endif
796 }
797
798 /** Wrapper around either strtod() or xf86strtod() */
799 double
800 _mesa_strtod( const char *s, char **end )
801 {
802 #if defined(XFree86LOADER) && defined(IN_MODULE)
803 return xf86strtod(s, end);
804 #else
805 return strtod(s, end);
806 #endif
807 }
808
809 /*@}*/
810
811
812 /**********************************************************************/
813 /** \name I/O */
814 /*@{*/
815
816 /** Wrapper around either vsprintf() or xf86vsprintf() */
817 int
818 _mesa_sprintf( char *str, const char *fmt, ... )
819 {
820 int r;
821 va_list args;
822 va_start( args, fmt );
823 va_end( args );
824 #if defined(XFree86LOADER) && defined(IN_MODULE)
825 r = xf86vsprintf( str, fmt, args );
826 #else
827 r = vsprintf( str, fmt, args );
828 #endif
829 return r;
830 }
831
832 /** Wrapper around either printf() or xf86printf(), using vsprintf() for
833 * the formatting. */
834 void
835 _mesa_printf( const char *fmtString, ... )
836 {
837 char s[MAXSTRING];
838 va_list args;
839 va_start( args, fmtString );
840 vsnprintf(s, MAXSTRING, fmtString, args);
841 va_end( args );
842 #if defined(XFree86LOADER) && defined(IN_MODULE)
843 xf86printf("%s", s);
844 #else
845 printf("%s", s);
846 #endif
847 }
848
849 /*@}*/
850
851
852 /**********************************************************************/
853 /** \name Diagnostics */
854 /*@{*/
855
856 /**
857 * Display a warning.
858 *
859 * \param ctx GL context.
860 * \param fmtString printf() alike format string.
861 *
862 * If debugging is enabled (either at compile-time via the DEBUG macro, or
863 * run-time via the MESA_DEBUG environment variable), prints the warning to
864 * stderr, either via fprintf() or xf86printf().
865 */
866 void
867 _mesa_warning( GLcontext *ctx, const char *fmtString, ... )
868 {
869 GLboolean debug;
870 char str[MAXSTRING];
871 va_list args;
872 (void) ctx;
873 va_start( args, fmtString );
874 (void) vsnprintf( str, MAXSTRING, fmtString, args );
875 va_end( args );
876 #ifdef DEBUG
877 debug = GL_TRUE; /* always print warning */
878 #else
879 debug = _mesa_getenv("MESA_DEBUG") ? GL_TRUE : GL_FALSE;
880 #endif
881 if (debug) {
882 #if defined(XFree86LOADER) && defined(IN_MODULE)
883 xf86fprintf(stderr, "Mesa warning: %s\n", str);
884 #else
885 fprintf(stderr, "Mesa warning: %s\n", str);
886 #endif
887 }
888 }
889
890 /**
891 * This function is called when the Mesa user has stumbled into a code
892 * path which may not be implemented fully or correctly.
893 *
894 * \param ctx GL context.
895 * \param s problem description string.
896 *
897 * Prints the message to stderr, either via fprintf() or xf86fprintf().
898 */
899 void
900 _mesa_problem( const GLcontext *ctx, const char *fmtString, ... )
901 {
902 va_list args;
903 char str[MAXSTRING];
904 (void) ctx;
905
906 va_start( args, fmtString );
907 vsnprintf( str, MAXSTRING, fmtString, args );
908 va_end( args );
909
910 #if defined(XFree86LOADER) && defined(IN_MODULE)
911 xf86fprintf(stderr, "Mesa %s implementation error: %s\n", MESA_VERSION_STRING, str);
912 xf86fprintf(stderr, "Please report at bugzilla.freedesktop.org\n");
913 #else
914 fprintf(stderr, "Mesa %s implementation error: %s\n", MESA_VERSION_STRING, str);
915 fprintf(stderr, "Please report at bugzilla.freedesktop.org\n");
916 #endif
917 }
918
919 /**
920 * Display an error message.
921 *
922 * If in debug mode, print error message.
923 * Also, record the error code by calling _mesa_record_error().
924 *
925 * \param ctx the GL context.
926 * \param error the error value.
927 * \param fmtString printf() style format string, followed by optional args
928 *
929 * If debugging is enabled (either at compile-time via the DEBUG macro, or
930 * run-time via the MESA_DEBUG environment variable), interperts the error code and
931 * prints the error message via _mesa_debug().
932 */
933 void
934 _mesa_error( GLcontext *ctx, GLenum error, const char *fmtString, ... )
935 {
936 const char *debugEnv;
937 GLboolean debug;
938
939 debugEnv = _mesa_getenv("MESA_DEBUG");
940
941 #ifdef DEBUG
942 if (debugEnv && _mesa_strstr(debugEnv, "silent"))
943 debug = GL_FALSE;
944 else
945 debug = GL_TRUE;
946 #else
947 if (debugEnv)
948 debug = GL_TRUE;
949 else
950 debug = GL_FALSE;
951 #endif
952
953 if (debug) {
954 va_list args;
955 char where[MAXSTRING];
956 const char *errstr;
957
958 va_start( args, fmtString );
959 vsnprintf( where, MAXSTRING, fmtString, args );
960 va_end( args );
961
962 switch (error) {
963 case GL_NO_ERROR:
964 errstr = "GL_NO_ERROR";
965 break;
966 case GL_INVALID_VALUE:
967 errstr = "GL_INVALID_VALUE";
968 break;
969 case GL_INVALID_ENUM:
970 errstr = "GL_INVALID_ENUM";
971 break;
972 case GL_INVALID_OPERATION:
973 errstr = "GL_INVALID_OPERATION";
974 break;
975 case GL_STACK_OVERFLOW:
976 errstr = "GL_STACK_OVERFLOW";
977 break;
978 case GL_STACK_UNDERFLOW:
979 errstr = "GL_STACK_UNDERFLOW";
980 break;
981 case GL_OUT_OF_MEMORY:
982 errstr = "GL_OUT_OF_MEMORY";
983 break;
984 case GL_TABLE_TOO_LARGE:
985 errstr = "GL_TABLE_TOO_LARGE";
986 break;
987 default:
988 errstr = "unknown";
989 break;
990 }
991 _mesa_debug(ctx, "User error: %s in %s\n", errstr, where);
992 }
993
994 _mesa_record_error(ctx, error);
995 }
996
997 /**
998 * Report debug information.
999 *
1000 * \param ctx GL context.
1001 * \param fmtString printf() alike format string.
1002 *
1003 * Prints the message to stderr, either via fprintf() or xf86printf().
1004 */
1005 void
1006 _mesa_debug( const GLcontext *ctx, const char *fmtString, ... )
1007 {
1008 char s[MAXSTRING];
1009 va_list args;
1010 (void) ctx;
1011 va_start(args, fmtString);
1012 vsnprintf(s, MAXSTRING, fmtString, args);
1013 va_end(args);
1014 #if defined(XFree86LOADER) && defined(IN_MODULE)
1015 xf86fprintf(stderr, "Mesa: %s", s);
1016 #else
1017 fprintf(stderr, "Mesa: %s", s);
1018 #endif
1019 }
1020
1021 /*@}*/
1022
1023
1024 /**********************************************************************/
1025 /** \name Default Imports Wrapper */
1026 /*@{*/
1027
1028 /** Wrapper around _mesa_malloc() */
1029 static void *
1030 default_malloc(__GLcontext *gc, size_t size)
1031 {
1032 (void) gc;
1033 return _mesa_malloc(size);
1034 }
1035
1036 /** Wrapper around _mesa_malloc() */
1037 static void *
1038 default_calloc(__GLcontext *gc, size_t numElem, size_t elemSize)
1039 {
1040 (void) gc;
1041 return _mesa_calloc(numElem * elemSize);
1042 }
1043
1044 /** Wrapper around either realloc() or xf86realloc() */
1045 static void *
1046 default_realloc(__GLcontext *gc, void *oldAddr, size_t newSize)
1047 {
1048 (void) gc;
1049 #if defined(XFree86LOADER) && defined(IN_MODULE)
1050 return xf86realloc(oldAddr, newSize);
1051 #else
1052 return realloc(oldAddr, newSize);
1053 #endif
1054 }
1055
1056 /** Wrapper around _mesa_free() */
1057 static void
1058 default_free(__GLcontext *gc, void *addr)
1059 {
1060 (void) gc;
1061 _mesa_free(addr);
1062 }
1063
1064 /** Wrapper around _mesa_getenv() */
1065 static char * CAPI
1066 default_getenv( __GLcontext *gc, const char *var )
1067 {
1068 (void) gc;
1069 return _mesa_getenv(var);
1070 }
1071
1072 /** Wrapper around _mesa_warning() */
1073 static void
1074 default_warning(__GLcontext *gc, char *str)
1075 {
1076 _mesa_warning(gc, str);
1077 }
1078
1079 /** Wrapper around _mesa_problem() */
1080 static void
1081 default_fatal(__GLcontext *gc, char *str)
1082 {
1083 _mesa_problem(gc, str);
1084 abort();
1085 }
1086
1087 /** Wrapper around atoi() */
1088 static int CAPI
1089 default_atoi(__GLcontext *gc, const char *str)
1090 {
1091 (void) gc;
1092 return atoi(str);
1093 }
1094
1095 /** Wrapper around vsprintf() */
1096 static int CAPI
1097 default_sprintf(__GLcontext *gc, char *str, const char *fmt, ...)
1098 {
1099 int r;
1100 va_list args;
1101 (void) gc;
1102 va_start( args, fmt );
1103 r = vsprintf( str, fmt, args );
1104 va_end( args );
1105 return r;
1106 }
1107
1108 /** Wrapper around fopen() */
1109 static void * CAPI
1110 default_fopen(__GLcontext *gc, const char *path, const char *mode)
1111 {
1112 (void) gc;
1113 return fopen(path, mode);
1114 }
1115
1116 /** Wrapper around fclose() */
1117 static int CAPI
1118 default_fclose(__GLcontext *gc, void *stream)
1119 {
1120 (void) gc;
1121 return fclose((FILE *) stream);
1122 }
1123
1124 /** Wrapper around vfprintf() */
1125 static int CAPI
1126 default_fprintf(__GLcontext *gc, void *stream, const char *fmt, ...)
1127 {
1128 int r;
1129 va_list args;
1130 (void) gc;
1131 va_start( args, fmt );
1132 r = vfprintf( (FILE *) stream, fmt, args );
1133 va_end( args );
1134 return r;
1135 }
1136
1137 /**
1138 * \todo this really is driver-specific and can't be here
1139 */
1140 static __GLdrawablePrivate *
1141 default_GetDrawablePrivate(__GLcontext *gc)
1142 {
1143 (void) gc;
1144 return NULL;
1145 }
1146
1147 /*@}*/
1148
1149
1150 /**
1151 * Initialize a __GLimports object to point to the functions in this
1152 * file.
1153 *
1154 * This is to be called from device drivers.
1155 *
1156 * Also, do some one-time initializations.
1157 *
1158 * \param imports the object to initialize.
1159 * \param driverCtx pointer to device driver-specific data.
1160 */
1161 void
1162 _mesa_init_default_imports(__GLimports *imports, void *driverCtx)
1163 {
1164 /* XXX maybe move this one-time init stuff into context.c */
1165 static GLboolean initialized = GL_FALSE;
1166 if (!initialized) {
1167 init_sqrt_table();
1168
1169 #if defined(_FPU_GETCW) && defined(_FPU_SETCW)
1170 {
1171 const char *debug = _mesa_getenv("MESA_DEBUG");
1172 if (debug && _mesa_strcmp(debug, "FP")==0) {
1173 /* die on FP exceptions */
1174 fpu_control_t mask;
1175 _FPU_GETCW(mask);
1176 mask &= ~(_FPU_MASK_IM | _FPU_MASK_DM | _FPU_MASK_ZM
1177 | _FPU_MASK_OM | _FPU_MASK_UM);
1178 _FPU_SETCW(mask);
1179 }
1180 }
1181 #endif
1182 initialized = GL_TRUE;
1183 }
1184
1185 imports->malloc = default_malloc;
1186 imports->calloc = default_calloc;
1187 imports->realloc = default_realloc;
1188 imports->free = default_free;
1189 imports->warning = default_warning;
1190 imports->fatal = default_fatal;
1191 imports->getenv = default_getenv; /* not used for now */
1192 imports->atoi = default_atoi;
1193 imports->sprintf = default_sprintf;
1194 imports->fopen = default_fopen;
1195 imports->fclose = default_fclose;
1196 imports->fprintf = default_fprintf;
1197 imports->getDrawablePrivate = default_GetDrawablePrivate;
1198 imports->other = driverCtx;
1199 }