-
Notifications
You must be signed in to change notification settings - Fork 110
/
p_invsqrt.c
54 lines (49 loc) · 1.36 KB
/
p_invsqrt.c
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
#include <pal.h>
#if (P_FLOAT_TYPE == P_FLOAT_SINGLE)
# define ISQRT_APPROX 0x5f375a86
#else
# define ISQRT_APPROX 0x5fe6eb50c7b537a9ULL
#endif
/**
*
* Calculates the inverse square root of the input vector 'a'.
*
* This function uses a method of computing the inverse square root
* made popular by the Quake 3 source code release. Chris Lomont has
* provided an exhaustive analysis here:
* http://www.lomont.org/Math/Papers/2003/InvSqrt.pdf
*
* @param a Pointer to input vector
*
* @param c Pointer to output vector
*
* @param n Size of 'a' and 'c' vector.
*
* @return None
*
*/
void PSYM(p_invsqrt)(const PTYPE *a, PTYPE *c, int n)
{
// This union allows us to type-pun between integers and PTYPEs
// with fewer strict aliasing concerns than the pointer casts
// used in the original source.
union
{
PUTYPE i;
PTYPE f;
} u;
int i;
for (i = 0; i < n; i++) {
PTYPE x = a[i];
const PTYPE x2 = x * PCONST(0.5);
// Use some bit hacks to get a decent first approximation
u.f = x;
u.i = ISQRT_APPROX - (u.i >> 1);
x = u.f;
// Perform a couple steps of Newton's method to refine our guess
x = x * (PCONST(1.5) - (x2 * x * x));
x = x * (PCONST(1.5) - (x2 * x * x));
x = x * (PCONST(1.5) - (x2 * x * x));
c[i] = x;
}
}