LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
LSSTDataManagementBasePackage
PowFast.cc
Go to the documentation of this file.
1 
32 #include <cmath>
33 
34 #include "lsst/utils/PowFast.h"
35 
36 // implementation -------------------------------------------------------------
37 
61 namespace lsst { namespace utils {
62 
63 namespace {
64 
65 const float _2p23 = 8388608.0f;
66 
67 
68 /*
69  * Initialize powFast lookup table.
70  *
71  * @param pTable length must be 2 ^ precision
72  * @param precision number of mantissa bits used, >= 0 and <= 18
73  */
74 void powFastSetTable(
75  unsigned int* const pTable,
76  const unsigned int precision
77  )
78 {
79  // step along table elements and x-axis positions
80  float zeroToOne = 1.0f / (static_cast<float>(1 << precision) * 2.0f);
81  for( int i = 0; i < (1 << precision); ++i )
82  {
83  // make y-axis value for table element
84  const float f = (::powf( 2.0f, zeroToOne ) - 1.0f) * _2p23;
85  pTable[i] = static_cast<unsigned int>( f < _2p23 ? f : (_2p23 - 1.0f) );
86 
87  zeroToOne += 1.0f / static_cast<float>(1 << precision);
88  }
89 }
90 
91 
100 inline
101 float powFastLookup
102 (
103  const float val,
104  const float ilog2,
105  unsigned int* const pTable,
106  const unsigned int precision
107 )
108 {
109  // build float bits
110  const int i = static_cast<int>( (val * (_2p23 * ilog2)) + (127.0f * _2p23) );
111 
112  // replace mantissa with lookup
113  const int it = (i & 0xFF800000) | pTable[(i & 0x7FFFFF) >> (23 - precision)];
114 
115  // convert bits to float
116  union { int i; float f; } pun;
117  return pun.i = it, pun.f;
118 }
119 
120 }
121 
122 // wrapper class --------------------------------------------------------------
123 
125 (
126  const unsigned int precision
127 )
128  : precision_m( precision <= 18u ? precision : 18u )
129  , pTable_m ( new unsigned int[ 1 << precision_m ] )
130 {
131  powFastSetTable( pTable_m, precision_m );
132 }
133 
134 
136 {
137  delete[] pTable_m;
138 }
139 
140 
141 float PowFast::two
142 (
143  const float f
144 ) const
145 {
146  return powFastLookup( f, 1.0f, pTable_m, precision_m );
147 }
148 
149 
150 float PowFast::exp
151 (
152  const float f
153 ) const
154 {
155  return powFastLookup( f, 1.44269504088896f, pTable_m, precision_m );
156 }
157 
158 
159 float PowFast::ten
160 (
161  const float f
162 ) const
163 {
164  return powFastLookup( f, 3.32192809488736f, pTable_m, precision_m );
165 }
166 
167 
168 float PowFast::r
169 (
170  const float logr,
171  const float f
172 ) const
173 {
174  return powFastLookup( f, (logr * 1.44269504088896f), pTable_m, precision_m );
175 }
176 
177 
178 unsigned int PowFast::getPrecision() const
179 {
180  return precision_m;
181 }
182 }}
unsigned int getPrecision() const
Return this PowFast&#39;s precision.
Definition: PowFast.cc:178
unsigned int precision_m
Definition: PowFast.h:81
float r(float logr, float x) const
Evaluate r^x.
Definition: PowFast.cc:169
PowFast(unsigned int precision=11)
Definition: PowFast.cc:125
float exp(float x) const
Evaluate exp(x). (x must be in (-87.3ish, 88.7ish))
Definition: PowFast.cc:151
unsigned int * pTable_m
Definition: PowFast.h:82
float ten(float x) const
Evaluate 10^x. (x must be in (-37.9ish, 38.5ish))
Definition: PowFast.cc:160
ImageT val
Definition: CR.cc:154
float two(float x) const
Evaluate 2^x . (x must be in (-125, 128))
Definition: PowFast.cc:142