-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathegl_fast_math.h
136 lines (118 loc) · 3.96 KB
/
egl_fast_math.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
//============================================================================
// EmberGL
//
// Copyright (c) 2022, Jarkko Lempiainen
// All rights reserved.
//============================================================================
#ifndef EGL_FAST_MATH_H
#define EGL_FAST_MATH_H
//----------------------------------------------------------------------------
//============================================================================
// interface
//============================================================================
// external
#include "egl_core.h"
EGL_NAMESPACE_BEGIN
// new
EGL_INLINE float32_t fast_rsqrt(float32_t);
EGL_INLINE float64_t fast_rsqrt(float64_t);
EGL_INLINE float32_t fast_sqrt(float32_t);
EGL_INLINE float64_t fast_sqrt(float64_t);
EGL_INLINE float32_t fast_rcp(float32_t);
EGL_INLINE float64_t fast_rcp(float64_t);
//----------------------------------------------------------------------------
//============================================================================
//============================================================================
// inline & template implementations
//============================================================================
//============================================================================
//============================================================================
// fast_rsqrt
//============================================================================
EGL_INLINE float32_t fast_rsqrt(float32_t v_)
{
// fast reciprocal square root: https://en.wikipedia.org/wiki/Fast_inverse_square_root
union
{
float32_t f;
uint32_t u;
} v;
v.f=v_;
v.u=0x5f375a86-(v.u>>1); // slightly more precise than the original 0x5f3759df
return v.f;
}
//----
EGL_INLINE float64_t fast_rsqrt(float64_t v_)
{
// fast reciprocal square root: https://en.wikipedia.org/wiki/Fast_inverse_square_root
union
{
float64_t f;
uint64_t u;
} v;
v.f=v_;
v.u=0x5fe6eb50c7b537a9-(v.u>>1);
return v.f;
}
//----------------------------------------------------------------------------
//============================================================================
// fast_sqrt
//============================================================================
EGL_INLINE float32_t fast_sqrt(float32_t v_)
{
// fast square root using fast reciprocal square root: https://en.wikipedia.org/wiki/Fast_inverse_square_root
union
{
float32_t f;
uint32_t u;
} v;
v.f=v_;
v.u=0x5f375a86-(v.u>>1); // slightly more precise than the original 0x5f3759df
return v_*v.f;
}
//----
EGL_INLINE float64_t fast_sqrt(float64_t v_)
{
// fast square root using fast reciprocal square root: https://en.wikipedia.org/wiki/Fast_inverse_square_root
union
{
float64_t f;
uint64_t u;
} v;
v.f=v_;
v.u=0x5fe6eb50c7b537a9-(v.u>>1);
return v_*v.f;
}
//----------------------------------------------------------------------------
//============================================================================
// fast_rcp
//============================================================================
EGL_INLINE float32_t fast_rcp(float32_t v_)
{
// fast reciprocal using fast reciprocal square root: https://en.wikipedia.org/wiki/Fast_inverse_square_root
union
{
float32_t f;
uint32_t u;
} v;
v.f=v_;
v.u=0x5f375a85-(v.u>>1); // slightly more precise than the original 0x5f3759df
return v.f*v.f;
}
//----
EGL_INLINE float64_t fast_rcp(float64_t v_)
{
// fast reciprocal using fast reciprocal square root: https://en.wikipedia.org/wiki/Fast_inverse_square_root
union
{
float64_t f;
uint64_t u;
} v;
v.f=v_;
v.u=0x5fe6eb50c7b537a9-(v.u>>1);
return v.f*v.f;
}
//----------------------------------------------------------------------------
//============================================================================
EGL_NAMESPACE_END
#endif