GteSqrtEstimate.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 sqrt(x). The polynomial p(x) of
14 // degree D minimizes the quantity maximum{|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 = SqrtEstimate<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 [0,1], call Degree(y), and combine the output with the
32  // proper exponent to obtain the approximation. For example,
33  // float x; // x >= 0
34  // float result = SqrtEstimate<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>
59 inline Real SqrtEstimate<Real>::Degree(Real x)
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_SQRT_DEG1_C1;
82  poly = (Real)GTE_C_SQRT_DEG1_C0 + poly * t;
83  return poly;
84 }
85 
86 template <typename Real>
88 {
89  Real poly;
90  poly = (Real)GTE_C_SQRT_DEG2_C2;
91  poly = (Real)GTE_C_SQRT_DEG2_C1 + poly * t;
92  poly = (Real)GTE_C_SQRT_DEG2_C0 + poly * t;
93  return poly;
94 }
95 
96 template <typename Real>
98 {
99  Real poly;
100  poly = (Real)GTE_C_SQRT_DEG3_C3;
101  poly = (Real)GTE_C_SQRT_DEG3_C2 + poly * t;
102  poly = (Real)GTE_C_SQRT_DEG3_C1 + poly * t;
103  poly = (Real)GTE_C_SQRT_DEG3_C0 + poly * t;
104  return poly;
105 }
106 
107 template <typename Real>
109 {
110  Real poly;
111  poly = (Real)GTE_C_SQRT_DEG4_C4;
112  poly = (Real)GTE_C_SQRT_DEG4_C3 + poly * t;
113  poly = (Real)GTE_C_SQRT_DEG4_C2 + poly * t;
114  poly = (Real)GTE_C_SQRT_DEG4_C1 + poly * t;
115  poly = (Real)GTE_C_SQRT_DEG4_C0 + poly * t;
116  return poly;
117 }
118 
119 template <typename Real>
121 {
122  Real poly;
123  poly = (Real)GTE_C_SQRT_DEG5_C5;
124  poly = (Real)GTE_C_SQRT_DEG5_C4 + poly * t;
125  poly = (Real)GTE_C_SQRT_DEG5_C3 + poly * t;
126  poly = (Real)GTE_C_SQRT_DEG5_C2 + poly * t;
127  poly = (Real)GTE_C_SQRT_DEG5_C1 + poly * t;
128  poly = (Real)GTE_C_SQRT_DEG5_C0 + poly * t;
129  return poly;
130 }
131 
132 template <typename Real>
134 {
135  Real poly;
136  poly = (Real)GTE_C_SQRT_DEG6_C6;
137  poly = (Real)GTE_C_SQRT_DEG6_C5 + poly * t;
138  poly = (Real)GTE_C_SQRT_DEG6_C4 + poly * t;
139  poly = (Real)GTE_C_SQRT_DEG6_C3 + poly * t;
140  poly = (Real)GTE_C_SQRT_DEG6_C2 + poly * t;
141  poly = (Real)GTE_C_SQRT_DEG6_C1 + poly * t;
142  poly = (Real)GTE_C_SQRT_DEG6_C0 + poly * t;
143  return poly;
144 }
145 
146 template <typename Real>
148 {
149  Real poly;
150  poly = (Real)GTE_C_SQRT_DEG7_C7;
151  poly = (Real)GTE_C_SQRT_DEG7_C6 + poly * t;
152  poly = (Real)GTE_C_SQRT_DEG7_C5 + poly * t;
153  poly = (Real)GTE_C_SQRT_DEG7_C4 + poly * t;
154  poly = (Real)GTE_C_SQRT_DEG7_C3 + poly * t;
155  poly = (Real)GTE_C_SQRT_DEG7_C2 + poly * t;
156  poly = (Real)GTE_C_SQRT_DEG7_C1 + poly * t;
157  poly = (Real)GTE_C_SQRT_DEG7_C0 + poly * t;
158  return poly;
159 }
160 
161 template <typename Real>
163 {
164  Real poly;
165  poly = (Real)GTE_C_SQRT_DEG8_C8;
166  poly = (Real)GTE_C_SQRT_DEG8_C7 + poly * t;
167  poly = (Real)GTE_C_SQRT_DEG8_C6 + poly * t;
168  poly = (Real)GTE_C_SQRT_DEG8_C5 + poly * t;
169  poly = (Real)GTE_C_SQRT_DEG8_C4 + poly * t;
170  poly = (Real)GTE_C_SQRT_DEG8_C3 + poly * t;
171  poly = (Real)GTE_C_SQRT_DEG8_C2 + poly * t;
172  poly = (Real)GTE_C_SQRT_DEG8_C1 + poly * t;
173  poly = (Real)GTE_C_SQRT_DEG8_C0 + poly * t;
174  return poly;
175 }
176 
177 template <typename Real>
178 inline void SqrtEstimate<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_SQRT_2 + (1 & ~p)*(Real)1;
184  p >>= 1;
185 }
186 
187 template <typename Real>
188 inline Real SqrtEstimate<Real>::Combine(Real adj, Real y, int p)
189 {
190  return adj*ldexp(y, p);
191 }
192 
193 
194 }
#define GTE_C_SQRT_DEG4_C3
Definition: GteConstants.h:57
#define GTE_C_SQRT_DEG7_C3
Definition: GteConstants.h:81
static Real Combine(Real adj, Real y, int p)
#define GTE_C_SQRT_DEG5_C0
Definition: GteConstants.h:61
#define GTE_C_SQRT_DEG4_C4
Definition: GteConstants.h:58
#define GTE_C_SQRT_DEG8_C6
Definition: GteConstants.h:94
#define GTE_C_SQRT_DEG8_C3
Definition: GteConstants.h:91
#define GTE_C_SQRT_DEG8_C8
Definition: GteConstants.h:96
#define GTE_C_SQRT_DEG3_C3
Definition: GteConstants.h:51
#define GTE_C_SQRT_DEG8_C1
Definition: GteConstants.h:89
#define GTE_C_SQRT_DEG3_C1
Definition: GteConstants.h:49
#define GTE_C_SQRT_DEG2_C1
Definition: GteConstants.h:44
#define GTE_C_SQRT_DEG1_C1
Definition: GteConstants.h:40
#define GTE_C_SQRT_DEG5_C3
Definition: GteConstants.h:64
#define GTE_C_SQRT_DEG7_C7
Definition: GteConstants.h:85
#define GTE_C_SQRT_DEG6_C0
Definition: GteConstants.h:69
static Real Degree(Real x)
#define GTE_C_SQRT_DEG3_C2
Definition: GteConstants.h:50
#define GTE_C_SQRT_DEG8_C5
Definition: GteConstants.h:93
GLint GLenum GLint x
Definition: glcorearb.h:404
#define GTE_C_SQRT_DEG2_C0
Definition: GteConstants.h:43
#define GTE_C_SQRT_DEG6_C5
Definition: GteConstants.h:74
#define GTE_C_SQRT_DEG5_C2
Definition: GteConstants.h:63
#define GTE_C_SQRT_DEG8_C4
Definition: GteConstants.h:92
#define GTE_C_SQRT_2
Definition: GteConstants.h:30
static Real DegreeRR(Real x)
#define GTE_C_SQRT_DEG4_C2
Definition: GteConstants.h:56
#define GTE_C_SQRT_DEG7_C1
Definition: GteConstants.h:79
#define GTE_C_SQRT_DEG4_C1
Definition: GteConstants.h:55
GLdouble GLdouble t
Definition: glext.h:239
#define GTE_C_SQRT_DEG5_C5
Definition: GteConstants.h:66
#define GTE_C_SQRT_DEG7_C0
Definition: GteConstants.h:78
#define GTE_C_SQRT_DEG8_C7
Definition: GteConstants.h:95
static Real Evaluate(degree< 1 >, Real t)
#define GTE_C_SQRT_DEG6_C2
Definition: GteConstants.h:71
#define GTE_C_SQRT_DEG6_C6
Definition: GteConstants.h:75
#define GTE_C_SQRT_DEG7_C4
Definition: GteConstants.h:82
#define GTE_C_SQRT_DEG2_C2
Definition: GteConstants.h:45
#define GTE_C_SQRT_DEG7_C2
Definition: GteConstants.h:80
#define GTE_C_SQRT_DEG8_C0
Definition: GteConstants.h:88
#define GTE_C_SQRT_DEG5_C4
Definition: GteConstants.h:65
static void Reduce(Real x, Real &adj, Real &y, int &p)
#define GTE_C_SQRT_DEG5_C1
Definition: GteConstants.h:62
#define GTE_C_SQRT_DEG7_C6
Definition: GteConstants.h:84
#define GTE_C_SQRT_DEG6_C3
Definition: GteConstants.h:72
#define GTE_C_SQRT_DEG6_C1
Definition: GteConstants.h:70
#define GTE_C_SQRT_DEG8_C2
Definition: GteConstants.h:90
GLfloat GLfloat p
Definition: glext.h:11668
GLuint64EXT * result
Definition: glext.h:10003
GLint y
Definition: glcorearb.h:98
#define GTE_C_SQRT_DEG1_C0
Definition: GteConstants.h:39
#define GTE_C_SQRT_DEG6_C4
Definition: GteConstants.h:73
#define GTE_C_SQRT_DEG4_C0
Definition: GteConstants.h:54
#define GTE_C_SQRT_DEG7_C5
Definition: GteConstants.h:83
#define GTE_C_SQRT_DEG3_C0
Definition: GteConstants.h:48


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