GteLog2Estimate.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 log2(x). The polynomial p(x) of
14 // degree D minimizes the quantity maximum{|log2(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 = Log2Estimate<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 add the exponent for the power
32  // of two in the binary scientific representation of x. For example,
33  // float x; // x > 0
34  // float result = Log2Estimate<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 
52 
53 template <typename Real>
54 template <int D>
55 inline Real Log2Estimate<Real>::Degree(Real x)
56 {
57  Real t = x - (Real)1; // t in (0,1]
58  return Evaluate(degree<D>(), t);
59 }
60 
61 template <typename Real>
62 template <int D>
64 {
65  int p;
66  Real y = frexp(x, &p); // y in [1/2,1)
67  y = ((Real)2)*y; // y in [1,2)
68  --p;
69  Real poly = Degree<D>(y);
70  Real result = poly + (Real)p;
71  return result;
72 }
73 
74 template <typename Real>
76 {
77  Real poly;
78  poly = (Real)GTE_C_LOG2_DEG1_C1;
79  poly = poly * t;
80  return poly;
81 }
82 
83 template <typename Real>
85 {
86  Real poly;
87  poly = (Real)GTE_C_LOG2_DEG2_C2;
88  poly = (Real)GTE_C_LOG2_DEG2_C1 + poly * t;
89  poly = poly * t;
90  return poly;
91 }
92 
93 template <typename Real>
95 {
96  Real poly;
97  poly = (Real)GTE_C_LOG2_DEG3_C3;
98  poly = (Real)GTE_C_LOG2_DEG3_C2 + poly * t;
99  poly = (Real)GTE_C_LOG2_DEG3_C1 + poly * t;
100  poly = poly * t;
101  return poly;
102 }
103 
104 template <typename Real>
106 {
107  Real poly;
108  poly = (Real)GTE_C_LOG2_DEG4_C4;
109  poly = (Real)GTE_C_LOG2_DEG4_C3 + poly * t;
110  poly = (Real)GTE_C_LOG2_DEG4_C2 + poly * t;
111  poly = (Real)GTE_C_LOG2_DEG4_C1 + poly * t;
112  poly = poly * t;
113  return poly;
114 }
115 
116 template <typename Real>
118 {
119  Real poly;
120  poly = (Real)GTE_C_LOG2_DEG5_C5;
121  poly = (Real)GTE_C_LOG2_DEG5_C4 + poly * t;
122  poly = (Real)GTE_C_LOG2_DEG5_C3 + poly * t;
123  poly = (Real)GTE_C_LOG2_DEG5_C2 + poly * t;
124  poly = (Real)GTE_C_LOG2_DEG5_C1 + poly * t;
125  poly = poly * t;
126  return poly;
127 }
128 
129 template <typename Real>
131 {
132  Real poly;
133  poly = (Real)GTE_C_LOG2_DEG6_C6;
134  poly = (Real)GTE_C_LOG2_DEG6_C5 + poly * t;
135  poly = (Real)GTE_C_LOG2_DEG6_C4 + poly * t;
136  poly = (Real)GTE_C_LOG2_DEG6_C3 + poly * t;
137  poly = (Real)GTE_C_LOG2_DEG6_C2 + poly * t;
138  poly = (Real)GTE_C_LOG2_DEG6_C1 + poly * t;
139  poly = poly * t;
140  return poly;
141 }
142 
143 template <typename Real>
145 {
146  Real poly;
147  poly = (Real)GTE_C_LOG2_DEG7_C7;
148  poly = (Real)GTE_C_LOG2_DEG7_C6 + poly * t;
149  poly = (Real)GTE_C_LOG2_DEG7_C5 + poly * t;
150  poly = (Real)GTE_C_LOG2_DEG7_C4 + poly * t;
151  poly = (Real)GTE_C_LOG2_DEG7_C3 + poly * t;
152  poly = (Real)GTE_C_LOG2_DEG7_C2 + poly * t;
153  poly = (Real)GTE_C_LOG2_DEG7_C1 + poly * t;
154  poly = poly * t;
155  return poly;
156 }
157 
158 template <typename Real>
160 {
161  Real poly;
162  poly = (Real)GTE_C_LOG2_DEG8_C8;
163  poly = (Real)GTE_C_LOG2_DEG8_C7 + poly * t;
164  poly = (Real)GTE_C_LOG2_DEG8_C6 + poly * t;
165  poly = (Real)GTE_C_LOG2_DEG8_C5 + poly * t;
166  poly = (Real)GTE_C_LOG2_DEG8_C4 + poly * t;
167  poly = (Real)GTE_C_LOG2_DEG8_C3 + poly * t;
168  poly = (Real)GTE_C_LOG2_DEG8_C2 + poly * t;
169  poly = (Real)GTE_C_LOG2_DEG8_C1 + poly * t;
170  poly = poly * t;
171  return poly;
172 }
173 
174 
175 }
#define GTE_C_LOG2_DEG6_C6
Definition: GteConstants.h:456
#define GTE_C_LOG2_DEG8_C7
Definition: GteConstants.h:474
#define GTE_C_LOG2_DEG8_C5
Definition: GteConstants.h:472
#define GTE_C_LOG2_DEG2_C2
Definition: GteConstants.h:430
#define GTE_C_LOG2_DEG8_C8
Definition: GteConstants.h:475
#define GTE_C_LOG2_DEG8_C4
Definition: GteConstants.h:471
#define GTE_C_LOG2_DEG2_C1
Definition: GteConstants.h:429
#define GTE_C_LOG2_DEG4_C4
Definition: GteConstants.h:441
#define GTE_C_LOG2_DEG8_C6
Definition: GteConstants.h:473
#define GTE_C_LOG2_DEG1_C1
Definition: GteConstants.h:426
#define GTE_C_LOG2_DEG5_C2
Definition: GteConstants.h:445
GLint GLenum GLint x
Definition: glcorearb.h:404
#define GTE_C_LOG2_DEG3_C3
Definition: GteConstants.h:435
#define GTE_C_LOG2_DEG8_C3
Definition: GteConstants.h:470
#define GTE_C_LOG2_DEG7_C7
Definition: GteConstants.h:465
#define GTE_C_LOG2_DEG3_C2
Definition: GteConstants.h:434
#define GTE_C_LOG2_DEG6_C4
Definition: GteConstants.h:454
#define GTE_C_LOG2_DEG5_C1
Definition: GteConstants.h:444
#define GTE_C_LOG2_DEG7_C1
Definition: GteConstants.h:459
#define GTE_C_LOG2_DEG4_C3
Definition: GteConstants.h:440
#define GTE_C_LOG2_DEG7_C3
Definition: GteConstants.h:461
#define GTE_C_LOG2_DEG8_C2
Definition: GteConstants.h:469
#define GTE_C_LOG2_DEG4_C2
Definition: GteConstants.h:439
static Real Degree(Real x)
#define GTE_C_LOG2_DEG6_C1
Definition: GteConstants.h:451
#define GTE_C_LOG2_DEG4_C1
Definition: GteConstants.h:438
#define GTE_C_LOG2_DEG5_C5
Definition: GteConstants.h:448
GLdouble GLdouble t
Definition: glext.h:239
#define GTE_C_LOG2_DEG6_C5
Definition: GteConstants.h:455
#define GTE_C_LOG2_DEG7_C6
Definition: GteConstants.h:464
#define GTE_C_LOG2_DEG7_C4
Definition: GteConstants.h:462
#define GTE_C_LOG2_DEG7_C2
Definition: GteConstants.h:460
static Real Evaluate(degree< 1 >, Real t)
static Real DegreeRR(Real x)
#define GTE_C_LOG2_DEG6_C3
Definition: GteConstants.h:453
GLfloat GLfloat p
Definition: glext.h:11668
#define GTE_C_LOG2_DEG6_C2
Definition: GteConstants.h:452
GLuint64EXT * result
Definition: glext.h:10003
#define GTE_C_LOG2_DEG5_C3
Definition: GteConstants.h:446
GLint y
Definition: glcorearb.h:98
#define GTE_C_LOG2_DEG5_C4
Definition: GteConstants.h:447
#define GTE_C_LOG2_DEG7_C5
Definition: GteConstants.h:463
#define GTE_C_LOG2_DEG3_C1
Definition: GteConstants.h:433
#define GTE_C_LOG2_DEG8_C1
Definition: GteConstants.h:468


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