1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
/*
* Mesa 3-D graphics library
* Version: 3.3
*
* Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifdef PC_HEADER
#include "all.h"
#else
#include "glheader.h"
#include "mmath.h"
#endif
static int in_fast_math;
/*
* A High Speed, Low Precision Square Root
* by Paul Lalonde and Robert Dawson
* from "Graphics Gems", Academic Press, 1990
*/
/*
* SPARC implementation of a fast square root by table
* lookup.
* SPARC floating point format is as follows:
*
* BIT 31 30 23 22 0
* sign exponent mantissa
*/
static short sqrttab[0x100]; /* declare table of square roots */
static void init_sqrt(void)
{
#ifdef FAST_MATH
unsigned short i;
float f;
unsigned int *fi = (unsigned int *)&f;
/* to access the bits of a float in */
/* C quickly we must misuse pointers */
for(i=0; i<= 0x7f; i++) {
*fi = 0;
/*
* Build a float with the bit pattern i as mantissa
* and an exponent of 0, stored as 127
*/
*fi = (i << 16) | (127 << 23);
f = sqrt(f);
/*
* Take the square root then strip the first 7 bits of
* the mantissa into the table
*/
sqrttab[i] = (*fi & 0x7fffff) >> 16;
/*
* Repeat the process, this time with an exponent of
* 1, stored as 128
*/
*fi = 0;
*fi = (i << 16) | (128 << 23);
f = sqrt(f);
sqrttab[i+0x80] = (*fi & 0x7fffff) >> 16;
}
#else
(void) sqrttab; /* silence compiler warnings */
#endif /*FAST_MATH*/
}
float gl_sqrt( float x )
{
#ifdef FAST_MATH
unsigned int *num = (unsigned int *)&x;
/* to access the bits of a float in C
* we must misuse pointers */
short e; /* the exponent */
if (x == 0.0F) return 0.0F; /* check for square root of 0 */
e = (*num >> 23) - 127; /* get the exponent - on a SPARC the */
/* exponent is stored with 127 added */
*num &= 0x7fffff; /* leave only the mantissa */
if (e & 0x01) *num |= 0x800000;
/* the exponent is odd so we have to */
/* look it up in the second half of */
/* the lookup table, so we set the */
/* high bit */
e >>= 1; /* divide the exponent by two */
/* note that in C the shift */
/* operators are sign preserving */
/* for signed operands */
/* Do the table lookup, based on the quaternary mantissa,
* then reconstruct the result back into a float
*/
*num = ((sqrttab[*num >> 16]) << 16) | ((e + 127) << 23);
return x;
#else
return sqrt(x);
#endif
}
float gl_ubyte_to_float_color_tab[256];
float gl_ubyte_to_float_255_color_tab[256];
static void
init_ubyte_color_tab(void)
{
int i;
for (i = 0 ; i < 256 ; i++) {
gl_ubyte_to_float_color_tab[i] = (float) i * (1.0/255.0);
gl_ubyte_to_float_255_color_tab[i] = (float) i;
}
}
/*
* Initialize tables, etc for fast math functions.
*/
void _mesa_init_math(void)
{
static GLboolean initialized = GL_FALSE;
if (!initialized) {
init_sqrt();
init_ubyte_color_tab();
initialized = GL_TRUE;
in_fast_math = 0;
#if defined(_FPU_GETCW) && defined(_FPU_SETCW)
{
const char *debug = getenv("MESA_DEBUG");
if (debug && strcmp(debug, "FP")==0) {
/* die on FP exceptions */
fpu_control_t mask;
_FPU_GETCW(mask);
mask &= ~(_FPU_MASK_IM | _FPU_MASK_DM | _FPU_MASK_ZM
| _FPU_MASK_OM | _FPU_MASK_UM);
_FPU_SETCW(mask);
}
}
#endif
}
}
|