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
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/* $XFree86: xc/extras/Mesa/src/mmath.c,v 1.6 2002/02/22 19:25:36 dawes Exp $ */
/*
* Mesa 3-D graphics library
* Version: 3.5
*
* Copyright (C) 1999-2001 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;
fi_type fi; /* to access the bits of a float in C quickly */
/* we use a union defined in glheader.h */
for(i=0; i<= 0x7f; i++) {
fi.i = 0;
/*
* Build a float with the bit pattern i as mantissa
* and an exponent of 0, stored as 127
*/
fi.i = (i << 16) | (127 << 23);
fi.f = sqrt(fi.f);
/*
* Take the square root then strip the first 7 bits of
* the mantissa into the table
*/
sqrttab[i] = (fi.i & 0x7fffff) >> 16;
/*
* Repeat the process, this time with an exponent of
* 1, stored as 128
*/
fi.i = 0;
fi.i = (i << 16) | (128 << 23);
fi.f = sqrt(fi.f);
sqrttab[i+0x80] = (fi.i & 0x7fffff) >> 16;
}
#else
(void) sqrttab; /* silence compiler warnings */
#endif /*FAST_MATH*/
}
float gl_sqrt( float x )
{
#ifdef FAST_MATH
fi_type num;
/* to access the bits of a float in C
* we use a union from glheader.h */
short e; /* the exponent */
if (x == 0.0F) return 0.0F; /* check for square root of 0 */
num.f = x;
e = (num.i >> 23) - 127; /* get the exponent - on a SPARC the */
/* exponent is stored with 127 added */
num.i &= 0x7fffff; /* leave only the mantissa */
if (e & 0x01) num.i |= 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.i = ((sqrttab[num.i >> 16]) << 16) | ((e + 127) << 23);
return num.f;
#else
return (GLfloat) sqrt(x);
#endif
}
/* ubyte -> float conversion */
float _mesa_ubyte_to_float_color_tab[256];
/*
* Initialize tables, etc for fast math functions.
*/
void
_mesa_init_math(void)
{
static GLboolean initialized = GL_FALSE;
if (!initialized) {
int i;
for (i = 0; i < 256; i++) {
_mesa_ubyte_to_float_color_tab[i] = (float) i / 255.0F;
}
init_sqrt();
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
}
}
/*
* Return number of bits set in given GLuint.
*/
GLuint
_mesa_bitcount(GLuint n)
{
GLuint bits;
for (bits = 0; n > 0; n = n >> 1) {
if (n & 1) {
bits++;
}
}
return bits;
}
|