CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FastMath.h
Go to the documentation of this file.
1 #ifndef DataFormats_Math_FastMath_h
2 #define DataFormats_Math_FastMath_h
3 // faster function will a limited precision
4 
5 #include<cmath>
6 #include<utility>
7 #ifdef __SSE2__
8 # include <emmintrin.h>
9 #endif
10 namespace fastmath {
11  inline float invSqrt( float in ) {
12 #ifndef __SSE2__
13  return 1.f/std::sqrt(in);
14 #else
15  float out;
16  _mm_store_ss( &out, _mm_rsqrt_ss( _mm_load_ss( &in ) ) ); // compiles to movss, rsqrtss, movss
17  // return out; // already good enough!
18  return out * (1.5f - 0.5f * in * out * out); // One (more?) round of Newton's method
19 #endif
20  }
21 
22  inline double invSqrt(double in ) {
23  return 1./std::sqrt(in);
24  }
25 
26 }
27 
28 namespace fastmath_details {
29  const double _2pi = (2.0 * 3.1415926535897932384626434);
30  const float _2pif = float(_2pi);
31  extern float atanbuf_[257 * 2];
32  extern double datanbuf_[513 * 2];
33 }
34 
35 namespace fastmath {
36 
37  // =====================================================================
38  // arctan, single-precision; returns phi and r (or 1/r if overR=true)
39  // =====================================================================
40  inline std::pair<float,float> atan2r(float y_, float x_, bool overR=false) {
41  using namespace fastmath_details;
42  float mag2 = x_ * x_ + y_ * y_;
43  if(!(mag2 > 0)) { return std::pair<float,float>(0.f,0.f); } // degenerate case
44 
45  // float r_ = std::sqrt(mag2);
46  float rinv = invSqrt(mag2);
47  unsigned int flags = 0;
48  float x, y;
49  union {
50  float f;
51  int i;
52  } yp;
53  yp.f = 32768.f;
54  if (y_ < 0 ) { flags |= 4; y_ = -y_; }
55  if (x_ < 0 ) { flags |= 2; x_ = -x_; }
56  if (y_ > x_) {
57  flags |= 1;
58  x = rinv * y_; y = rinv * x_; yp.f += y;
59  }
60  else {
61  x = rinv * x_; y = rinv * y_; yp.f += y;
62  }
63  int ind = (yp.i & 0x01FF) * 2;
64 
65  float* asbuf = (float*)(atanbuf_ + ind);
66  float sv = yp.f - 32768.f;
67  float cv = asbuf[0];
68  float asv = asbuf[1];
69  sv = y * cv - x * sv; // delta sin value
70  // ____ compute arcsin directly
71  float asvd = 6.f + sv * sv; sv *= float(1.0f / 6.0f);
72  float th = asv + asvd * sv;
73  if (flags & 1) { th = (_2pif / 4.f) - th; }
74  if (flags & 2) { th = (_2pif / 2.f) - th; }
75  if (flags & 4) { th = -th; }
76  return std::pair<float,float>(th,overR ? rinv : rinv*mag2);
77  }
78 
79  // =====================================================================
80  // arctan, double-precision; returns phi and r (or 1/r if overR=true)
81  // =====================================================================
82  inline std::pair<double, double> atan2r(double y_, double x_, bool overR=false) {
83  using namespace fastmath_details;
84  // assert(ataninited);
85  double mag2 = x_ * x_ + y_ * y_;
86  if(!(mag2 > 0)) { return std::pair<double, double>(0.,0.); } // degenerate case
87 
88  double r_ = std::sqrt(mag2);
89  double rinv = 1./r_;
90  unsigned int flags = 0;
91  double x, y;
92  const double _2p43 = 65536.0 * 65536.0 * 2048.0;
93  union {
94  double d;
95  int i[2];
96  } yp;
97 
98  yp.d = _2p43;
99  if (y_ < 0) { flags |= 4; y_ = -y_; }
100  if (x_ < 0) { flags |= 2; x_ = -x_; }
101  if (y_ > x_) {
102  flags |= 1;
103  x = rinv * y_; y = rinv * x_; yp.d += y;
104  }
105  else {
106  x = rinv * x_; y = rinv * y_; yp.d += y;
107  }
108 
109  int ind = (yp.i[0] & 0x03FF) * 2; // 0 for little indian
110 
111  double* dasbuf = (double*)(datanbuf_ + ind);
112  double sv = yp.d - _2p43; // index fraction
113  double cv = dasbuf[0];
114  double asv = dasbuf[1];
115  sv = y * cv - x * sv; // delta sin value
116  // double sv = y *(cv-x);
117  // ____ compute arcsin directly
118  double asvd = 6 + sv * sv; sv *= double(1.0 / 6.0);
119  double th = asv + asvd * sv;
120  if (flags & 1) { th = (_2pi / 4) - th; }
121  if (flags & 2) { th = (_2pi / 2) - th; }
122  if (flags & 4) { th = -th; }
123  return std::pair<double,double>(th,overR ? rinv : r_);
124  }
125 
126  // return eta phi saving some computation
127  template<typename T>
128  inline std::pair<T,T> etaphi(T x, T y, T z) {
129  std::pair<T,T> por = atan2r(y,x, true);
130  x = z*por.second;
131  return std::pair<float,float>( std::log(x+std::sqrt(x*x+T(1))), por.first);
132  }
133 
134 }
135 
136 
137 #endif
int i
Definition: DBlmapReader.cc:9
const float _2pif
Definition: FastMath.h:30
const double _2pi
Definition: FastMath.h:29
std::vector< Variable::Flags > flags
Definition: MVATrainer.cc:135
float invSqrt(float in)
Definition: FastMath.h:11
float atanbuf_[257 *2]
Definition: FastMath.cc:3
tuple d
Definition: ztail.py:151
T x() const
Cartesian x coordinate.
T sqrt(T t)
Definition: SSEVec.h:48
T mag2() const
The vector magnitude squared. Equivalent to vec.dot(vec)
std::pair< T, T > etaphi(T x, T y, T z)
Definition: FastMath.h:128
double f[11][100]
dictionary cv
Definition: cuy.py:362
tuple out
Definition: dbtoconf.py:99
std::pair< float, float > atan2r(float y_, float x_, bool overR=false)
Definition: FastMath.h:40
double datanbuf_[513 *2]
Definition: FastMath.cc:4
long double T