1 | /*
|
---|
2 | * sqrt.h
|
---|
3 | * Implementations born on the Quake 3 fast inverse square root
|
---|
4 | * function.
|
---|
5 | * http://en.wikipedia.org/wiki/Fast_inverse_square_root
|
---|
6 | *
|
---|
7 | * Created on: Jun 24, 2012
|
---|
8 | * Author: Danilo Piparo, Thomas Hauth, Vincenzo Innocente
|
---|
9 | */
|
---|
10 |
|
---|
11 | /*
|
---|
12 | * VDT is free software: you can redistribute it and/or modify
|
---|
13 | * it under the terms of the GNU Lesser Public License as published by
|
---|
14 | * the Free Software Foundation, either version 3 of the License, or
|
---|
15 | * (at your option) any later version.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful,
|
---|
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | * GNU Lesser Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU Lesser Public License
|
---|
23 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef SQRT_H_
|
---|
27 | #define SQRT_H_
|
---|
28 |
|
---|
29 | #include "vdtcore_common.h"
|
---|
30 |
|
---|
31 | namespace vdt{
|
---|
32 |
|
---|
33 | //------------------------------------------------------------------------------
|
---|
34 |
|
---|
35 |
|
---|
36 | /// Sqrt implmentation from Quake3
|
---|
37 | inline double fast_isqrt_general(double x, const uint32_t ISQRT_ITERATIONS) {
|
---|
38 |
|
---|
39 | const double threehalfs = 1.5;
|
---|
40 | const double x2 = x * 0.5;
|
---|
41 | double y = x;
|
---|
42 | uint64_t i = details::dp2uint64(y);
|
---|
43 | // Evil!
|
---|
44 | i = 0x5fe6eb50c7aa19f9ULL - ( i >> 1 );
|
---|
45 | y = details::uint642dp(i);
|
---|
46 | for (uint32_t j=0;j<ISQRT_ITERATIONS;++j)
|
---|
47 | y *= threehalfs - ( x2 * y * y ) ;
|
---|
48 |
|
---|
49 | return y;
|
---|
50 | }
|
---|
51 |
|
---|
52 | //------------------------------------------------------------------------------
|
---|
53 |
|
---|
54 | /// Four iterations
|
---|
55 | inline double fast_isqrt(double x) {return fast_isqrt_general(x,4);}
|
---|
56 |
|
---|
57 | /// Three iterations
|
---|
58 | inline double fast_approx_isqrt(double x) {return fast_isqrt_general(x,3);}
|
---|
59 |
|
---|
60 | //------------------------------------------------------------------------------
|
---|
61 |
|
---|
62 | /// For comparisons
|
---|
63 | inline double isqrt (double x) {return 1./std::sqrt(x);}
|
---|
64 |
|
---|
65 | //------------------------------------------------------------------------------
|
---|
66 |
|
---|
67 | /// Sqrt implmentation from Quake3
|
---|
68 | inline float fast_isqrtf_general(float x, const uint32_t ISQRT_ITERATIONS) {
|
---|
69 |
|
---|
70 | const float threehalfs = 1.5f;
|
---|
71 | const float x2 = x * 0.5f;
|
---|
72 | float y = x;
|
---|
73 | uint32_t i = details::sp2uint32(y);
|
---|
74 | i = 0x5f3759df - ( i >> 1 );
|
---|
75 | y = details::uint322sp(i);
|
---|
76 | for (uint32_t j=0;j<ISQRT_ITERATIONS;++j)
|
---|
77 | y *= ( threehalfs - ( x2 * y * y ) );
|
---|
78 |
|
---|
79 | return y;
|
---|
80 | }
|
---|
81 |
|
---|
82 | //------------------------------------------------------------------------------
|
---|
83 |
|
---|
84 | /// Two iterations
|
---|
85 | inline float fast_isqrtf(float x) {return fast_isqrtf_general(x,2);}
|
---|
86 |
|
---|
87 | /// One (!) iterations
|
---|
88 | inline float fast_approx_isqrtf(float x) {return fast_isqrtf_general(x,1);}
|
---|
89 |
|
---|
90 | //------------------------------------------------------------------------------
|
---|
91 |
|
---|
92 | /// For comparisons
|
---|
93 | inline float isqrtf (float x) {return 1.f/std::sqrt(x);}
|
---|
94 |
|
---|
95 | //------------------------------------------------------------------------------
|
---|
96 |
|
---|
97 | } // end namespace vdt
|
---|
98 |
|
---|
99 | #endif /* SQRT_H_ */
|
---|