FairRoot/PandaRoot
PndCAMath.h
Go to the documentation of this file.
1 //-*- Mode: C++ -*-
2 // ************************************************************************
3 // This file is property of and copyright by the ALICE HLT Project *
4 // ALICE Experiment at CERN, All rights reserved. *
5 // See cxx source for full Copyright notice *
6 // *
7 //*************************************************************************
8 
9 #ifndef PNDCAMATH_H
10 #define PNDCAMATH_H
11 
12 #include "PndCADef.h"
13 
14 #include <cstdlib>
15 #include <algorithm>
16 using std::abs;
17 
18 #if defined(HLTCA_STANDALONE)
19 #include <limits>
20 #else
21 #include "TMath.h"
22 #endif
23 
24 #ifdef __SSE__
25 #include <xmmintrin.h>
26 #endif
27 
33 namespace CAMath
34 {
35  template<typename T> static inline T Min ( const T &x, const T &y ) { return std::min( x, y ); }
36  template<typename T> static inline T Max ( const T &x, const T &y ) { return std::max( x, y ); }
37  template<typename T> static inline T Sqrt( const T &x ) { return std::sqrt( x ); }
38  template<typename T> static inline T RSqrt( const T &x ) { const T one = T(1.f); return one / std::sqrt( x ); }
39  template<typename T> static inline T Abs ( const T &x ) { return std::abs( x ); }
40  template<typename T> static inline T Log ( const T &x ) { return std::log( x ); }
41  template<typename T> static inline T Log10( const T &x ) { return std::log10( x ); }
42  template<typename T> static inline T Sin ( const T &x ) { return std::sin( x ); }
43  template<typename T> static inline T Cos ( const T &x ) { return std::cos( x ); }
44  template<typename T> static T Reciprocal( const T &x );
45  template<typename T> static T ApproxSqrt( const T &x );
46 #ifdef USE_TBB
47  template<typename T> static T AtomicMax( T volatile *addr, T val );
48 #endif //USE_TBB
49 
50  template<typename T> struct FiniteReturnTypeHelper { typedef bool R; };
51  template<typename T> static typename FiniteReturnTypeHelper<T>::R Finite( const T &x );
52 
53  template<typename T> static T Round( const T &x );
54 
55  template<typename T> static inline T Recip( const T &x ) { return T( 1 ) / x; }
56  template<typename T> static T ATan2( const T &y, const T &x );
57  template<typename T> static T ASin( const T &x );
58 
59  float Tan( float x );
60  float Copysign( float x, float y );
61  static inline float TwoPi() { return 6.283185307179586f; }
62  static inline float Pi() { return 3.1415926535897f; }
63  int Nint( float x );
64 
65 #ifdef USE_TBB
66  int AtomicExch( int volatile *addr, int val );
67  int AtomicAdd ( int volatile *addr, int val );
68  int AtomicMin ( int volatile *addr, int val );
69 #endif //USE_TBB
70 }
71 
72 #if defined( HLTCA_STANDALONE )
73 #define choice(c1,c2,c3) c2
74 #else
75 #define choice(c1,c2,c3) c3
76 #endif
77 
78 namespace CAMath
79 {
80 
81  template<> inline float Reciprocal<float>( const float &x )
82  {
83  return 1.f / x;
84  }
85 
86  template<> inline double Reciprocal<double>( const double &x )
87  {
88  return 1. / x;
89  }
90 
91 #ifdef __SSE__
92  template<> inline float RSqrt<float>( const float &x )
93  {
94  float r = x;
95  __m128 tmp;
96  asm(
97  "rsqrtss %0,%1\n\t"
98  "movss %1,%0\n\t"
99  : "+m"( r ), "=x"( tmp )
100  );
101  return r;
102  }
103 #endif // __SSE__
104 
105  template<> inline float ApproxSqrt<float>( const float &x )
106  {
107  float r = x;
108  asm(
109  "shr %0\n\t"
110  "add $0x1fc00000,%0\n\t"
111  : "+r"( r )
112  );
113  return r;
114  }
115 }
116 
117 inline int CAMath::Nint( float x )
118 {
119 #if defined(HLTCA_STANDALONE)
120  int i;
121  if ( x >= 0 ) {
122  i = int( x + 0.5f );
123  if ( x + 0.5f == float( i ) && i & 1 ) i--;
124  } else {
125  i = int( x - 0.5f );
126  if ( x - 0.5f == float( i ) && i & 1 ) i++;
127  }
128  return i;
129 #else
130  return TMath::Nint( x );
131 #endif
132 }
133 
134 namespace CAMath
135 {
136 template<> inline bool Finite<float>( const float &x )
137 {
138  return choice( 1,
139  x < std::numeric_limits<float>::infinity() && -x < std::numeric_limits<float>::infinity(),
140  TMath::Finite( x ) );
141 }
142 
143 template<> inline float Round<float>( const float &x ) { return static_cast<float>( Nint( x ) ); }
144 
145 template<> inline float ATan2<float>( const float &y, const float &x )
146 {
147  return choice( atan2f( y, x ), atan2( y, x ), TMath::ATan2( y, x ) );
148 }
149 
150 template<> inline float ASin( const float &x )
151 {
152  return choice( asinf( x ), asin( x ), TMath::ASin( x ) );
153 }
154 
155 
156 } // namespace CAMath
157 
158 inline float CAMath::Copysign( float x, float y )
159 {
160  x = CAMath::Abs( x );
161  return ( y >= 0 ) ? x : -x;
162 }
163 
164 
165 inline float CAMath::Tan( float x )
166 {
167  return choice( tanf( x ), tan( x ), TMath::Tan( x ) );
168 }
169 
170 #ifdef USE_TBB
171 
172 #include <tbb/atomic.h>
173 
174 inline int CAMath::AtomicExch( int volatile *addr, int val )
175 {
176  tbb::atomic<int> &a = *reinterpret_cast<tbb::atomic<int> *>( const_cast<int *>( addr ) );
177  return a.fetch_and_store( val );
178 }
179 
180 inline int CAMath::AtomicAdd ( int volatile *addr, int val )
181 {
182  tbb::atomic<int> &a = *reinterpret_cast<tbb::atomic<int> *>( const_cast<int *>( addr ) );
183  return a.fetch_and_add( val );
184 }
185 
186 namespace CAMath
187 {
188 template<> inline int AtomicMax<int>( int volatile *addr, int val )
189 {
190  tbb::atomic<int> &a = *reinterpret_cast<tbb::atomic<int> *>( const_cast<int *>( addr ) );
191  int old = a;
192  if ( old < val ) {
193  while ( old != a.compare_and_swap( val, old ) ) {
194  old = a;
195  if ( old >= val ) {
196  break;
197  }
198  }
199  }
200  return old;
201 }
202 
203 template<> inline unsigned int AtomicMax<unsigned int>( unsigned int volatile *addr, unsigned int val )
204 {
205  tbb::atomic<unsigned int> &a = *reinterpret_cast<tbb::atomic<unsigned int> *>( const_cast<unsigned int *>( addr ) );
206  unsigned int old = a;
207  if ( old < val ) {
208  while ( old != a.compare_and_swap( val, old ) ) {
209  old = a;
210  if ( old >= val ) {
211  break;
212  }
213  }
214  }
215  return old;
216 }
217 } // namespace CAMath
218 
219 inline int CAMath::AtomicMin ( int volatile *addr, int val )
220 {
221  tbb::atomic<int> &a = *reinterpret_cast<tbb::atomic<int> *>( const_cast<int *>( addr ) );
222  int old = a;
223  if ( old > val ) {
224  while ( old != a.compare_and_swap( val, old ) ) {
225  old = a;
226  if ( old <= val ) {
227  break;
228  }
229  }
230  }
231  return old;
232 }
233 #endif //USE_TBB
234 
235 #undef choice
236 
237 #endif
static T ASin(const T &x)
friend F32vec4 cos(const F32vec4 &a)
Definition: P4_F32vec4.h:112
float Round< float >(const float &x)
Definition: PndCAMath.h:143
double r
Definition: RiemannTest.C:14
#define choice(c1, c2, c3)
Definition: PndCAMath.h:75
Int_t i
Definition: run_full.C:25
friend F32vec4 sqrt(const F32vec4 &a)
Definition: P4_F32vec4.h:29
Double_t val[nBoxes][nFEBox]
Definition: createCalib.C:11
static T Sqrt(const T &x)
Definition: PndCAMath.h:37
static T ApproxSqrt(const T &x)
friend F32vec4 sin(const F32vec4 &a)
Definition: P4_F32vec4.h:111
static T Sin(const T &x)
Definition: PndCAMath.h:42
friend F32vec4 log(const F32vec4 &a)
Definition: P4_F32vec4.h:110
float Tan(float x)
Definition: PndCAMath.h:165
static T Round(const T &x)
friend F32vec4 max(const F32vec4 &a, const F32vec4 &b)
Definition: P4_F32vec4.h:26
static T Log10(const T &x)
Definition: PndCAMath.h:41
static T Cos(const T &x)
Definition: PndCAMath.h:43
float Copysign(float x, float y)
Definition: PndCAMath.h:158
static T Abs(const T &x)
Definition: PndCAMath.h:39
TTree * T
Definition: anaLmdReco.C:32
static T RSqrt(const T &x)
Definition: PndCAMath.h:38
Int_t a
Definition: anaLmdDigi.C:126
bool Finite< float >(const float &x)
Definition: PndCAMath.h:136
static T Min(const T &x, const T &y)
Definition: PndCAMath.h:35
static T ATan2(const T &y, const T &x)
TFile * f
Definition: bump_analys.C:12
static T Reciprocal(const T &x)
static float Pi()
Definition: PndCAMath.h:62
friend F32vec4 min(const F32vec4 &a, const F32vec4 &b)
Definition: P4_F32vec4.h:25
float ATan2< float >(const float &y, const float &x)
Definition: PndCAMath.h:145
double Reciprocal< double >(const double &x)
Definition: PndCAMath.h:86
friend F32vec4 atan2(const F32vec4 &y, const F32vec4 &x)
Definition: P4_F32vec4.h:117
static T Max(const T &x, const T &y)
Definition: PndCAMath.h:36
Double_t x
static T Log(const T &x)
Definition: PndCAMath.h:40
static float TwoPi()
Definition: PndCAMath.h:61
float Reciprocal< float >(const float &x)
Definition: PndCAMath.h:81
static FiniteReturnTypeHelper< T >::R Finite(const T &x)
Double_t y
float ApproxSqrt< float >(const float &x)
Definition: PndCAMath.h:105
static T Recip(const T &x)
Definition: PndCAMath.h:55
int Nint(float x)
Definition: PndCAMath.h:117