GteInvSqrtEstimate.h
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #pragma once
9 
11 #include <cmath>
12 
13 // Minimax polynomial approximations to 1/sqrt(x). The polynomial p(x) of
14 // degree D minimizes the quantity maximum{|1/sqrt(x) - p(x)| : x in [1,2]}
15 // over all polynomials of degree D.
16 
17 namespace gte
18 {
19 
20 template <typename Real>
22 {
23 public:
24  // The input constraint is x in [1,2]. For example,
25  // float x; // in [1,2]
26  // float result = InvSqrtEstimate<float>::Degree<3>(x);
27  template <int D>
28  inline static Real Degree(Real x);
29 
30  // The input constraint is x > 0. Range reduction is used to generate a
31  // value y in [1,2], call Evaluate(y), and combine the output with the
32  // proper exponent to obtain the approximation. For example,
33  // float x; // x > 0
34  // float result = InvSqrtEstimate<float>::DegreeRR<3>(x);
35  template <int D>
36  inline static Real DegreeRR(Real x);
37 
38 private:
39  // Metaprogramming and private implementation to allow specialization of
40  // a template member function.
41  template <int D> struct degree {};
42  inline static Real Evaluate(degree<1>, Real t);
43  inline static Real Evaluate(degree<2>, Real t);
44  inline static Real Evaluate(degree<3>, Real t);
45  inline static Real Evaluate(degree<4>, Real t);
46  inline static Real Evaluate(degree<5>, Real t);
47  inline static Real Evaluate(degree<6>, Real t);
48  inline static Real Evaluate(degree<7>, Real t);
49  inline static Real Evaluate(degree<8>, Real t);
50 
51  // Support for range reduction.
52  inline static void Reduce(Real x, Real& adj, Real& y, int& p);
53  inline static Real Combine(Real adj, Real y, int p);
54 };
55 
56 
57 template <typename Real>
58 template <int D>
60 {
61  Real t = x - (Real)1; // t in (0,1]
62  return Evaluate(degree<D>(), t);
63 }
64 
65 template <typename Real>
66 template <int D>
68 {
69  Real adj, y;
70  int p;
71  Reduce(x, adj, y, p);
72  Real poly = Degree<D>(y);
73  Real result = Combine(adj, poly, p);
74  return result;
75 }
76 
77 template <typename Real>
79 {
80  Real poly;
81  poly = (Real)GTE_C_INVSQRT_DEG1_C1;
82  poly = (Real)GTE_C_INVSQRT_DEG1_C0 + poly * t;
83  return poly;
84 }
85 
86 template <typename Real>
88 {
89  Real poly;
90  poly = (Real)GTE_C_INVSQRT_DEG2_C2;
91  poly = (Real)GTE_C_INVSQRT_DEG2_C1 + poly * t;
92  poly = (Real)GTE_C_INVSQRT_DEG2_C0 + poly * t;
93  return poly;
94 }
95 
96 template <typename Real>
98 {
99  Real poly;
100  poly = (Real)GTE_C_INVSQRT_DEG3_C3;
101  poly = (Real)GTE_C_INVSQRT_DEG3_C2 + poly * t;
102  poly = (Real)GTE_C_INVSQRT_DEG3_C1 + poly * t;
103  poly = (Real)GTE_C_INVSQRT_DEG3_C0 + poly * t;
104  return poly;
105 }
106 
107 template <typename Real>
109 {
110  Real poly;
111  poly = (Real)GTE_C_INVSQRT_DEG4_C4;
112  poly = (Real)GTE_C_INVSQRT_DEG4_C3 + poly * t;
113  poly = (Real)GTE_C_INVSQRT_DEG4_C2 + poly * t;
114  poly = (Real)GTE_C_INVSQRT_DEG4_C1 + poly * t;
115  poly = (Real)GTE_C_INVSQRT_DEG4_C0 + poly * t;
116  return poly;
117 }
118 
119 template <typename Real>
121 {
122  Real poly;
123  poly = (Real)GTE_C_INVSQRT_DEG5_C5;
124  poly = (Real)GTE_C_INVSQRT_DEG5_C4 + poly * t;
125  poly = (Real)GTE_C_INVSQRT_DEG5_C3 + poly * t;
126  poly = (Real)GTE_C_INVSQRT_DEG5_C2 + poly * t;
127  poly = (Real)GTE_C_INVSQRT_DEG5_C1 + poly * t;
128  poly = (Real)GTE_C_INVSQRT_DEG5_C0 + poly * t;
129  return poly;
130 }
131 
132 template <typename Real>
134 {
135  Real poly;
136  poly = (Real)GTE_C_INVSQRT_DEG6_C6;
137  poly = (Real)GTE_C_INVSQRT_DEG6_C5 + poly * t;
138  poly = (Real)GTE_C_INVSQRT_DEG6_C4 + poly * t;
139  poly = (Real)GTE_C_INVSQRT_DEG6_C3 + poly * t;
140  poly = (Real)GTE_C_INVSQRT_DEG6_C2 + poly * t;
141  poly = (Real)GTE_C_INVSQRT_DEG6_C1 + poly * t;
142  poly = (Real)GTE_C_INVSQRT_DEG6_C0 + poly * t;
143  return poly;
144 }
145 
146 template <typename Real>
148 {
149  Real poly;
150  poly = (Real)GTE_C_INVSQRT_DEG7_C7;
151  poly = (Real)GTE_C_INVSQRT_DEG7_C6 + poly * t;
152  poly = (Real)GTE_C_INVSQRT_DEG7_C5 + poly * t;
153  poly = (Real)GTE_C_INVSQRT_DEG7_C4 + poly * t;
154  poly = (Real)GTE_C_INVSQRT_DEG7_C3 + poly * t;
155  poly = (Real)GTE_C_INVSQRT_DEG7_C2 + poly * t;
156  poly = (Real)GTE_C_INVSQRT_DEG7_C1 + poly * t;
157  poly = (Real)GTE_C_INVSQRT_DEG7_C0 + poly * t;
158  return poly;
159 }
160 
161 template <typename Real>
163 {
164  Real poly;
165  poly = (Real)GTE_C_INVSQRT_DEG8_C8;
166  poly = (Real)GTE_C_INVSQRT_DEG8_C7 + poly * t;
167  poly = (Real)GTE_C_INVSQRT_DEG8_C6 + poly * t;
168  poly = (Real)GTE_C_INVSQRT_DEG8_C5 + poly * t;
169  poly = (Real)GTE_C_INVSQRT_DEG8_C4 + poly * t;
170  poly = (Real)GTE_C_INVSQRT_DEG8_C3 + poly * t;
171  poly = (Real)GTE_C_INVSQRT_DEG8_C2 + poly * t;
172  poly = (Real)GTE_C_INVSQRT_DEG8_C1 + poly * t;
173  poly = (Real)GTE_C_INVSQRT_DEG8_C0 + poly * t;
174  return poly;
175 }
176 
177 template <typename Real>
178 inline void InvSqrtEstimate<Real>::Reduce(Real x, Real& adj, Real& y, int& p)
179 {
180  y = frexp(x, &p); // y in [1/2,1)
181  y = ((Real)2)*y; // y in [1,2)
182  --p;
183  adj = (1 & p)*(Real)GTE_C_INV_SQRT_2 + (1 & ~p)*(Real)1;
184  p = -(p >> 1);
185 }
186 
187 template <typename Real>
188 inline Real InvSqrtEstimate<Real>::Combine(Real adj, Real y, int p)
189 {
190  return adj*ldexp(y, p);
191 }
192 
193 
194 }
#define GTE_C_INVSQRT_DEG7_C4
Definition: GteConstants.h:144
static Real Degree(Real x)
#define GTE_C_INVSQRT_DEG8_C8
Definition: GteConstants.h:158
#define GTE_C_INVSQRT_DEG8_C7
Definition: GteConstants.h:157
#define GTE_C_INVSQRT_DEG8_C4
Definition: GteConstants.h:154
#define GTE_C_INVSQRT_DEG6_C6
Definition: GteConstants.h:137
#define GTE_C_INVSQRT_DEG4_C4
Definition: GteConstants.h:120
#define GTE_C_INVSQRT_DEG8_C5
Definition: GteConstants.h:155
#define GTE_C_INVSQRT_DEG3_C0
Definition: GteConstants.h:110
#define GTE_C_INVSQRT_DEG1_C1
Definition: GteConstants.h:102
#define GTE_C_INVSQRT_DEG6_C2
Definition: GteConstants.h:133
#define GTE_C_INVSQRT_DEG7_C5
Definition: GteConstants.h:145
#define GTE_C_INVSQRT_DEG6_C4
Definition: GteConstants.h:135
#define GTE_C_INVSQRT_DEG3_C1
Definition: GteConstants.h:111
#define GTE_C_INVSQRT_DEG7_C6
Definition: GteConstants.h:146
static Real Combine(Real adj, Real y, int p)
#define GTE_C_INVSQRT_DEG6_C3
Definition: GteConstants.h:134
#define GTE_C_INVSQRT_DEG5_C1
Definition: GteConstants.h:124
static Real DegreeRR(Real x)
GLint GLenum GLint x
Definition: glcorearb.h:404
#define GTE_C_INVSQRT_DEG8_C2
Definition: GteConstants.h:152
#define GTE_C_INVSQRT_DEG8_C6
Definition: GteConstants.h:156
#define GTE_C_INVSQRT_DEG8_C3
Definition: GteConstants.h:153
#define GTE_C_INVSQRT_DEG5_C2
Definition: GteConstants.h:125
#define GTE_C_INVSQRT_DEG5_C4
Definition: GteConstants.h:127
#define GTE_C_INVSQRT_DEG7_C2
Definition: GteConstants.h:142
#define GTE_C_INVSQRT_DEG2_C1
Definition: GteConstants.h:106
#define GTE_C_INVSQRT_DEG4_C1
Definition: GteConstants.h:117
#define GTE_C_INVSQRT_DEG7_C1
Definition: GteConstants.h:141
GLdouble GLdouble t
Definition: glext.h:239
#define GTE_C_INVSQRT_DEG6_C1
Definition: GteConstants.h:132
#define GTE_C_INVSQRT_DEG7_C7
Definition: GteConstants.h:147
static Real Evaluate(degree< 1 >, Real t)
#define GTE_C_INVSQRT_DEG2_C2
Definition: GteConstants.h:107
#define GTE_C_INVSQRT_DEG8_C0
Definition: GteConstants.h:150
#define GTE_C_INVSQRT_DEG5_C0
Definition: GteConstants.h:123
#define GTE_C_INVSQRT_DEG1_C0
Definition: GteConstants.h:101
static void Reduce(Real x, Real &adj, Real &y, int &p)
#define GTE_C_INVSQRT_DEG7_C3
Definition: GteConstants.h:143
#define GTE_C_INVSQRT_DEG3_C3
Definition: GteConstants.h:113
#define GTE_C_INV_SQRT_2
Definition: GteConstants.h:31
#define GTE_C_INVSQRT_DEG3_C2
Definition: GteConstants.h:112
#define GTE_C_INVSQRT_DEG7_C0
Definition: GteConstants.h:140
#define GTE_C_INVSQRT_DEG5_C5
Definition: GteConstants.h:128
#define GTE_C_INVSQRT_DEG4_C0
Definition: GteConstants.h:116
#define GTE_C_INVSQRT_DEG6_C5
Definition: GteConstants.h:136
GLfloat GLfloat p
Definition: glext.h:11668
#define GTE_C_INVSQRT_DEG5_C3
Definition: GteConstants.h:126
GLuint64EXT * result
Definition: glext.h:10003
#define GTE_C_INVSQRT_DEG4_C2
Definition: GteConstants.h:118
GLint y
Definition: glcorearb.h:98
#define GTE_C_INVSQRT_DEG4_C3
Definition: GteConstants.h:119
#define GTE_C_INVSQRT_DEG8_C1
Definition: GteConstants.h:151
#define GTE_C_INVSQRT_DEG6_C0
Definition: GteConstants.h:131
#define GTE_C_INVSQRT_DEG2_C0
Definition: GteConstants.h:105


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:00