CMS 3D CMS Logo

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) { return 1. / std::sqrt(in); }
23 
24 } // namespace fastmath
25 
26 namespace fastmath_details {
27  const double _2pi = (2.0 * 3.1415926535897932384626434);
28  const float _2pif = float(_2pi);
29  extern float atanbuf_[257 * 2];
30  extern double datanbuf_[513 * 2];
31 } // namespace fastmath_details
32 
33 namespace fastmath {
34 
35  // =====================================================================
36  // arctan, single-precision; returns phi and r (or 1/r if overR=true)
37  // =====================================================================
38  inline std::pair<float, float> atan2r(float y_, float x_, bool overR = false) {
39  using namespace fastmath_details;
40  float mag2 = x_ * x_ + y_ * y_;
41  if (!(mag2 > 0)) {
42  return std::pair<float, float>(0.f, 0.f);
43  } // 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) {
55  flags |= 4;
56  y_ = -y_;
57  }
58  if (x_ < 0) {
59  flags |= 2;
60  x_ = -x_;
61  }
62  if (y_ > x_) {
63  flags |= 1;
64  x = rinv * y_;
65  y = rinv * x_;
66  yp.f += y;
67  } else {
68  x = rinv * x_;
69  y = rinv * y_;
70  yp.f += y;
71  }
72  int ind = (yp.i & 0x01FF) * 2;
73 
74  float* asbuf = (float*)(atanbuf_ + ind);
75  float sv = yp.f - 32768.f;
76  float cv = asbuf[0];
77  float asv = asbuf[1];
78  sv = y * cv - x * sv; // delta sin value
79  // ____ compute arcsin directly
80  float asvd = 6.f + sv * sv;
81  sv *= float(1.0f / 6.0f);
82  float th = asv + asvd * sv;
83  if (flags & 1) {
84  th = (_2pif / 4.f) - th;
85  }
86  if (flags & 2) {
87  th = (_2pif / 2.f) - th;
88  }
89  if (flags & 4) {
90  th = -th;
91  }
92  return std::pair<float, float>(th, overR ? rinv : rinv * mag2);
93  }
94 
95  // =====================================================================
96  // arctan, double-precision; returns phi and r (or 1/r if overR=true)
97  // =====================================================================
98  inline std::pair<double, double> atan2r(double y_, double x_, bool overR = false) {
99  using namespace fastmath_details;
100  // assert(ataninited);
101  double mag2 = x_ * x_ + y_ * y_;
102  if (!(mag2 > 0)) {
103  return std::pair<double, double>(0., 0.);
104  } // degenerate case
105 
106  double r_ = std::sqrt(mag2);
107  double rinv = 1. / r_;
108  unsigned int flags = 0;
109  double x, y;
110  const double _2p43 = 65536.0 * 65536.0 * 2048.0;
111  union {
112  double d;
113  int i[2];
114  } yp;
115 
116  yp.d = _2p43;
117  if (y_ < 0) {
118  flags |= 4;
119  y_ = -y_;
120  }
121  if (x_ < 0) {
122  flags |= 2;
123  x_ = -x_;
124  }
125  if (y_ > x_) {
126  flags |= 1;
127  x = rinv * y_;
128  y = rinv * x_;
129  yp.d += y;
130  } else {
131  x = rinv * x_;
132  y = rinv * y_;
133  yp.d += y;
134  }
135 
136  int ind = (yp.i[0] & 0x03FF) * 2; // 0 for little indian
137 
138  double* dasbuf = (double*)(datanbuf_ + ind);
139  double sv = yp.d - _2p43; // index fraction
140  double cv = dasbuf[0];
141  double asv = dasbuf[1];
142  sv = y * cv - x * sv; // delta sin value
143  // double sv = y *(cv-x);
144  // ____ compute arcsin directly
145  double asvd = 6 + sv * sv;
146  sv *= double(1.0 / 6.0);
147  double th = asv + asvd * sv;
148  if (flags & 1) {
149  th = (_2pi / 4) - th;
150  }
151  if (flags & 2) {
152  th = (_2pi / 2) - th;
153  }
154  if (flags & 4) {
155  th = -th;
156  }
157  return std::pair<double, double>(th, overR ? rinv : r_);
158  }
159 
160  // return eta phi saving some computation
161  template <typename T>
162  inline std::pair<T, T> etaphi(T x, T y, T z) {
163  std::pair<T, T> por = atan2r(y, x, true);
164  x = z * por.second;
165  return std::pair<float, float>(std::log(x + std::sqrt(x * x + T(1))), por.first);
166  }
167 
168 } // namespace fastmath
169 
170 #endif
mps_fire.i
i
Definition: mps_fire.py:428
dqmMemoryStats.float
float
Definition: dqmMemoryStats.py:127
fastmath::invSqrt
float invSqrt(float in)
Definition: FastMath.h:11
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
cuy.cv
cv
Definition: cuy.py:364
fastmath
Definition: FastMath.h:10
fastmath_details::atanbuf_
float atanbuf_[257 *2]
Definition: FastMath.cc:3
fastmath_details::_2pi
const double _2pi
Definition: FastMath.h:27
mathSSE::sqrt
T sqrt(T t)
Definition: SSEVec.h:19
pfDeepBoostedJetPreprocessParams_cfi.sv
sv
Definition: pfDeepBoostedJetPreprocessParams_cfi.py:352
fastmath_details::_2pif
const float _2pif
Definition: FastMath.h:28
trklet::rinv
double rinv(double phi1, double phi2, double r1, double r2)
Definition: Util.h:173
recoMuon::in
Definition: RecoMuonEnumerators.h:6
mag2
T mag2() const
The vector magnitude squared. Equivalent to vec.dot(vec)
Definition: Basic3DVectorLD.h:124
fastmath::atan2r
std::pair< float, float > atan2r(float y_, float x_, bool overR=false)
Definition: FastMath.h:38
fastmath_details
Definition: FastMath.h:26
fastmath_details::datanbuf_
double datanbuf_[513 *2]
Definition: FastMath.cc:4
T
long double T
Definition: Basic3DVectorLD.h:48
dqm-mbProfile.log
log
Definition: dqm-mbProfile.py:17
MillePedeFileConverter_cfg.out
out
Definition: MillePedeFileConverter_cfg.py:31
ztail.d
d
Definition: ztail.py:151
HLT_FULL_cff.flags
flags
Definition: HLT_FULL_cff.py:13150
fastmath::etaphi
std::pair< T, T > etaphi(T x, T y, T z)
Definition: FastMath.h:162