-
Notifications
You must be signed in to change notification settings - Fork 32
/
ShaderFastMathLib.h
307 lines (261 loc) · 8.31 KB
/
ShaderFastMathLib.h
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/******************************************************************************
Shader Fast Math Lib (v0.41)
A shader math library for optimized approximate transcendental functions.
Optimized and tested on AMD GCN architecture.
Release notes:
v0.41 minor bug fixes, missing references
v0.4 new constants calculated for new ranges, minor optimization and precision improvements
Developed during production of : Far Cry 4, Ubisoft Montreal
v0.3 added Newton Raphson 1 and 2 iterations
Newton Raphson methods provided for reference purpose (some code / architectures might still benefit from single NR).
v0.2 fast IEEE float math sqrt() rsqrt() rcp()
v0.1 4th order polynomial approximations for atan() asin() acos()
Developed during production of : Killzone : Shadow Fall, Guerrilla Games, SCEE
Ubisoft and Guerrilla Games granted permission for open source distribution.
Contact information:
Michal Drobot - @MichalDrobot
hello@drobot.org
Presented publicly part of a course:
Low Level Optimizations for AMD GCN
(available @ http://michaldrobot.com/publications/)
********************************************************************************/
/******************************************************************************
The MIT License (MIT)
Copyright (c) <2014> <Michal Drobot>
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 THE
AUTHORS OR COPYRIGHT HOLDERS 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.
********************************************************************************/
#ifndef SHADER_FAST_MATH_INC_FX
#define SHADER_FAST_MATH_INC_FX
// Define switch for PC compilation
#ifdef _PC
#define asint(_x) *reinterpret_cast<int*>(&_x);
#define asfloat(_x) *reinterpret_cast<float*>(&_x);
#include <math.h>
#endif
// Derived from batch testing
// TODO : Should be improved
#define IEEE_INT_RCP_CONST_NR0 0x7EF311C2
#define IEEE_INT_RCP_CONST_NR1 0x7EF311C3
#define IEEE_INT_RCP_CONST_NR2 0x7EF312AC
// Derived from batch testing
#define IEEE_INT_SQRT_CONST_NR0 0x1FBD1DF5
// Biases for global ranges
// 0-1 or 1-2 specific ranges might improve from different bias
// Derived from batch testing
// TODO : Should be improved
#define IEEE_INT_RCP_SQRT_CONST_NR0 0x5f3759df
#define IEEE_INT_RCP_SQRT_CONST_NR1 0x5F375A86
#define IEEE_INT_RCP_SQRT_CONST_NR2 0x5F375A86
//
// Normalized range [0,1] Constants
//
#define IEEE_INT_RCP_CONST_NR0_SNORM 0x7EEF370B
#define IEEE_INT_SQRT_CONST_NR0_SNORM 0x1FBD1DF5
#define IEEE_INT_RCP_SQRT_CONST_NR0_SNORM 0x5F341A43
//
// Distance [0,1000] based constants
//
#define IEEE_INT_RCP_CONST_NR0_SNORM 0x7EF3210C
#define IEEE_INT_SQRT_CONST_NR0_SNORM 0x1FBD22DF
#define IEEE_INT_RCP_SQRT_CONST_NR0_SNORM 0x5F33E79F
//
// RCP SQRT
//
// Approximate guess using integer float arithmetics based on IEEE floating point standard
float rcpSqrtIEEEIntApproximation(float inX, const int inRcpSqrtConst)
{
int x = asint(inX);
x = inRcpSqrtConst - (x >> 1);
return asfloat(x);
}
float rcpSqrtNewtonRaphson(float inXHalf, float inRcpX)
{
return inRcpX * (-inXHalf * (inRcpX * inRcpX) + 1.5f);
}
//
// Using 0 Newton Raphson iterations
// Relative error : ~3.4% over full
// Precise format : ~small float
// 2 ALU
//
float fastRcpSqrtNR0(float inX)
{
float xRcpSqrt = rcpSqrtIEEEIntApproximation(inX, IEEE_INT_RCP_SQRT_CONST_NR0);
return xRcpSqrt;
}
//
// Using 1 Newton Raphson iterations
// Relative error : ~0.2% over full
// Precise format : ~half float
// 6 ALU
//
float fastRcpSqrtNR1(float inX)
{
float xhalf = 0.5f * inX;
float xRcpSqrt = rcpSqrtIEEEIntApproximation(inX, IEEE_INT_RCP_SQRT_CONST_NR1);
xRcpSqrt = rcpSqrtNewtonRaphson(xhalf, xRcpSqrt);
return xRcpSqrt;
}
//
// Using 2 Newton Raphson iterations
// Relative error : ~4.6e-004% over full
// Precise format : ~full float
// 9 ALU
//
float fastRcpSqrtNR2(float inX)
{
float xhalf = 0.5f * inX;
float xRcpSqrt = rcpSqrtIEEEIntApproximation(inX, IEEE_INT_RCP_SQRT_CONST_NR2);
xRcpSqrt = rcpSqrtNewtonRaphson(xhalf, xRcpSqrt);
xRcpSqrt = rcpSqrtNewtonRaphson(xhalf, xRcpSqrt);
return xRcpSqrt;
}
//
// SQRT
//
float sqrtIEEEIntApproximation(float inX, const int inSqrtConst)
{
int x = asint(inX);
x = inSqrtConst + (x >> 1);
return asfloat(x);
}
//
// Using 0 Newton Raphson iterations
// Relative error : < 0.7% over full
// Precise format : ~small float
// 1 ALU
//
float fastSqrtNR0(float inX)
{
float xRcp = sqrtIEEEIntApproximation(inX, IEEE_INT_SQRT_CONST_NR0);
return xRcp;
}
//
// Use inverse Rcp Sqrt
// Using 1 Newton Raphson iterations
// Relative error : ~0.2% over full
// Precise format : ~half float
// 6 ALU
//
float fastSqrtNR1(float inX)
{
// Inverse Rcp Sqrt
return inX * fastRcpSqrtNR1(inX);
}
//
// Use inverse Rcp Sqrt
// Using 2 Newton Raphson iterations
// Relative error : ~4.6e-004% over full
// Precise format : ~full float
// 9 ALU
//
float fastSqrtNR2(float inX)
{
// Inverse Rcp Sqrt
return inX * fastRcpSqrtNR2(inX);
}
//
// RCP
//
float rcpIEEEIntApproximation(float inX, const int inRcpConst)
{
int x = asint(inX);
x = inRcpConst - x;
return asfloat(x);
}
float rcpNewtonRaphson(float inX, float inRcpX)
{
return inRcpX * (-inRcpX * inX + 2.0f);
}
//
// Using 0 Newton Raphson iterations
// Relative error : < 0.4% over full
// Precise format : ~small float
// 1 ALU
//
float fastRcpNR0(float inX)
{
float xRcp = rcpIEEEIntApproximation(inX, IEEE_INT_RCP_CONST_NR0);
return xRcp;
}
//
// Using 1 Newton Raphson iterations
// Relative error : < 0.02% over full
// Precise format : ~half float
// 3 ALU
//
float fastRcpNR1(float inX)
{
float xRcp = rcpIEEEIntApproximation(inX, IEEE_INT_RCP_CONST_NR1);
xRcp = rcpNewtonRaphson(inX, xRcp);
return xRcp;
}
//
// Using 2 Newton Raphson iterations
// Relative error : < 5.0e-005% over full
// Precise format : ~full float
// 5 ALU
//
float fastRcpNR2(float inX)
{
float xRcp = rcpIEEEIntApproximation(inX, IEEE_INT_RCP_CONST_NR2);
xRcp = rcpNewtonRaphson(inX, xRcp);
xRcp = rcpNewtonRaphson(inX, xRcp);
return xRcp;
}
//
// Trigonometric functions
//
static const float fsl_PI = 3.1415926535897932384626433f;
static const float fsl_HALF_PI = 0.5f * fsl_PI;
// 4th order polynomial approximation
// 4 VGRP, 16 ALU Full Rate
// 7 * 10^-5 radians precision
// Reference : Handbook of Mathematical Functions (chapter : Elementary Transcendental Functions), M. Abramowitz and I.A. Stegun, Ed.
float acosFast4(float inX)
{
float x1 = abs(inX);
float x2 = x1 * x1;
float x3 = x2 * x1;
float s;
s = -0.2121144f * x1 + 1.5707288f;
s = 0.0742610f * x2 + s;
s = -0.0187293f * x3 + s;
s = sqrt(1.0f - x1) * s;
// acos function mirroring
// check per platform if compiles to a selector - no branch neeeded
return inX >= 0.0f ? s : fsl_PI - s;
}
// 4th order polynomial approximation
// 4 VGRP, 16 ALU Full Rate
// 7 * 10^-5 radians precision
float asinFast4(float inX)
{
float x = inX;
// asin is offset of acos
return fsl_HALF_PI - acosFast4(x);
}
// 4th order hyperbolical approximation
// 4 VGRP, 12 ALU Full Rate
// 7 * 10^-5 radians precision
// Reference : Efficient approximations for the arctangent function, Rajan, S. Sichun Wang Inkol, R. Joyal, A., May 2006
float atanFast4(float inX)
{
float x = inX;
return x*(-0.1784f * abs(x) - 0.0663f * x * x + 1.0301f);
}
#endif //SHADER_FAST_MATH_INC_FX