| 1 | /* $Id: mmath.c,v 1.2 2000-05-23 20:40:41 jeroen Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * Mesa 3-D graphics library | 
|---|
| 5 | * Version:  3.3 | 
|---|
| 6 | * | 
|---|
| 7 | * Copyright (C) 1999  Brian Paul   All Rights Reserved. | 
|---|
| 8 | * | 
|---|
| 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: | 
|---|
| 15 | * | 
|---|
| 16 | * The above copyright notice and this permission notice shall be included | 
|---|
| 17 | * in all copies or substantial portions of the Software. | 
|---|
| 18 | * | 
|---|
| 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. | 
|---|
| 25 | */ | 
|---|
| 26 |  | 
|---|
| 27 |  | 
|---|
| 28 | #ifdef PC_HEADER | 
|---|
| 29 | #include "all.h" | 
|---|
| 30 | #else | 
|---|
| 31 | #include "glheader.h" | 
|---|
| 32 | #include "mmath.h" | 
|---|
| 33 | #endif | 
|---|
| 34 |  | 
|---|
| 35 |  | 
|---|
| 36 | static int in_fast_math; | 
|---|
| 37 |  | 
|---|
| 38 | /* | 
|---|
| 39 | * A High Speed, Low Precision Square Root | 
|---|
| 40 | * by Paul Lalonde and Robert Dawson | 
|---|
| 41 | * from "Graphics Gems", Academic Press, 1990 | 
|---|
| 42 | */ | 
|---|
| 43 |  | 
|---|
| 44 | /* | 
|---|
| 45 | * SPARC implementation of a fast square root by table | 
|---|
| 46 | * lookup. | 
|---|
| 47 | * SPARC floating point format is as follows: | 
|---|
| 48 | * | 
|---|
| 49 | * BIT 31       30      23      22      0 | 
|---|
| 50 | *     sign     exponent        mantissa | 
|---|
| 51 | */ | 
|---|
| 52 | static short sqrttab[0x100];    /* declare table of square roots */ | 
|---|
| 53 |  | 
|---|
| 54 | static void init_sqrt(void) | 
|---|
| 55 | { | 
|---|
| 56 | #ifdef FAST_MATH | 
|---|
| 57 | unsigned short i; | 
|---|
| 58 | float f; | 
|---|
| 59 | unsigned int *fi = (unsigned int *)&f; | 
|---|
| 60 | /* to access the bits of a float in  */ | 
|---|
| 61 | /* C quickly we must misuse pointers */ | 
|---|
| 62 |  | 
|---|
| 63 | for(i=0; i<= 0x7f; i++) { | 
|---|
| 64 | *fi = 0; | 
|---|
| 65 |  | 
|---|
| 66 | /* | 
|---|
| 67 | * Build a float with the bit pattern i as mantissa | 
|---|
| 68 | * and an exponent of 0, stored as 127 | 
|---|
| 69 | */ | 
|---|
| 70 |  | 
|---|
| 71 | *fi = (i << 16) | (127 << 23); | 
|---|
| 72 | f = sqrt(f); | 
|---|
| 73 |  | 
|---|
| 74 | /* | 
|---|
| 75 | * Take the square root then strip the first 7 bits of | 
|---|
| 76 | * the mantissa into the table | 
|---|
| 77 | */ | 
|---|
| 78 |  | 
|---|
| 79 | sqrttab[i] = (*fi & 0x7fffff) >> 16; | 
|---|
| 80 |  | 
|---|
| 81 | /* | 
|---|
| 82 | * Repeat the process, this time with an exponent of | 
|---|
| 83 | * 1, stored as 128 | 
|---|
| 84 | */ | 
|---|
| 85 |  | 
|---|
| 86 | *fi = 0; | 
|---|
| 87 | *fi = (i << 16) | (128 << 23); | 
|---|
| 88 | f = sqrt(f); | 
|---|
| 89 | sqrttab[i+0x80] = (*fi & 0x7fffff) >> 16; | 
|---|
| 90 | } | 
|---|
| 91 | #else | 
|---|
| 92 | (void) sqrttab;  /* silence compiler warnings */ | 
|---|
| 93 | #endif /*FAST_MATH*/ | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 |  | 
|---|
| 97 | float gl_sqrt( float x ) | 
|---|
| 98 | { | 
|---|
| 99 | #ifdef FAST_MATH | 
|---|
| 100 | unsigned int *num = (unsigned int *)&x; | 
|---|
| 101 | /* to access the bits of a float in C | 
|---|
| 102 | * we must misuse pointers */ | 
|---|
| 103 |  | 
|---|
| 104 | short e;                     /* the exponent */ | 
|---|
| 105 | if (x == 0.0F) return 0.0F;  /* check for square root of 0 */ | 
|---|
| 106 | e = (*num >> 23) - 127;      /* get the exponent - on a SPARC the */ | 
|---|
| 107 | /* exponent is stored with 127 added */ | 
|---|
| 108 | *num &= 0x7fffff;            /* leave only the mantissa */ | 
|---|
| 109 | if (e & 0x01) *num |= 0x800000; | 
|---|
| 110 | /* the exponent is odd so we have to */ | 
|---|
| 111 | /* look it up in the second half of  */ | 
|---|
| 112 | /* the lookup table, so we set the   */ | 
|---|
| 113 | /* high bit                                */ | 
|---|
| 114 | e >>= 1;                     /* divide the exponent by two */ | 
|---|
| 115 | /* note that in C the shift */ | 
|---|
| 116 | /* operators are sign preserving */ | 
|---|
| 117 | /* for signed operands */ | 
|---|
| 118 | /* Do the table lookup, based on the quaternary mantissa, | 
|---|
| 119 | * then reconstruct the result back into a float | 
|---|
| 120 | */ | 
|---|
| 121 | *num = ((sqrttab[*num >> 16]) << 16) | ((e + 127) << 23); | 
|---|
| 122 | return x; | 
|---|
| 123 | #else | 
|---|
| 124 | return sqrt(x); | 
|---|
| 125 | #endif | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | float gl_ubyte_to_float_color_tab[256]; | 
|---|
| 129 | float gl_ubyte_to_float_255_color_tab[256]; | 
|---|
| 130 |  | 
|---|
| 131 | static void | 
|---|
| 132 | init_ubyte_color_tab(void) | 
|---|
| 133 | { | 
|---|
| 134 | int i; | 
|---|
| 135 | for (i = 0 ; i < 256 ; i++) { | 
|---|
| 136 | gl_ubyte_to_float_color_tab[i] = (float) i * (1.0/255.0); | 
|---|
| 137 | gl_ubyte_to_float_255_color_tab[i] = (float) i; | 
|---|
| 138 | } | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | /* | 
|---|
| 142 | * Initialize tables, etc for fast math functions. | 
|---|
| 143 | */ | 
|---|
| 144 | void _mesa_init_math(void) | 
|---|
| 145 | { | 
|---|
| 146 | static GLboolean initialized = GL_FALSE; | 
|---|
| 147 |  | 
|---|
| 148 | if (!initialized) { | 
|---|
| 149 | init_sqrt(); | 
|---|
| 150 | init_ubyte_color_tab(); | 
|---|
| 151 |  | 
|---|
| 152 | initialized = GL_TRUE; | 
|---|
| 153 | in_fast_math = 0; | 
|---|
| 154 |  | 
|---|
| 155 | #if defined(_FPU_GETCW) && defined(_FPU_SETCW) | 
|---|
| 156 | { | 
|---|
| 157 | const char *debug = getenv("MESA_DEBUG"); | 
|---|
| 158 | if (debug && strcmp(debug, "FP")==0) { | 
|---|
| 159 | /* die on FP exceptions */ | 
|---|
| 160 | fpu_control_t mask; | 
|---|
| 161 | _FPU_GETCW(mask); | 
|---|
| 162 | mask &= ~(_FPU_MASK_IM | _FPU_MASK_DM | _FPU_MASK_ZM | 
|---|
| 163 | | _FPU_MASK_OM | _FPU_MASK_UM); | 
|---|
| 164 | _FPU_SETCW(mask); | 
|---|
| 165 | } | 
|---|
| 166 | } | 
|---|
| 167 | #endif | 
|---|
| 168 | } | 
|---|
| 169 | } | 
|---|