]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/tracking-ca/AliHLTTPCCAMath.h
c52364bc32e8c72f8fab0a8de3619fed586819ec
[u/mrichter/AliRoot.git] / HLT / TPCLib / tracking-ca / AliHLTTPCCAMath.h
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 ALIHLTTPCCAMATH_H
10 #define ALIHLTTPCCAMATH_H
11
12 #include "AliHLTTPCCADef.h"
13
14 #if defined(HLTCA_STANDALONE) || defined(HLTCA_GPUCODE)
15 #include <math.h>
16 #else
17 #include "TMath.h"
18 #endif
19
20 /**
21  * @class ALIHLTTPCCAMath
22  *
23  *
24  */
25 class AliHLTTPCCAMath
26 {
27 public:
28   GPUd() static float2 MakeFloat2(float x, float y );
29
30   GPUd() static Float_t Min(Float_t x, Float_t y);
31   GPUd() static Float_t Max(Float_t x, Float_t y);
32   GPUd() static Int_t Min(Int_t x, Int_t y);
33   GPUd() static Int_t Max(Int_t x, Int_t y);
34   GPUd() static Float_t Sqrt(Float_t x );
35   GPUd() static Float_t Abs(Float_t x );
36   GPUd() static Double_t Abs(Double_t x );
37   GPUd() static Int_t Abs(Int_t x );
38   GPUd() static Float_t ASin(Float_t x );
39   GPUd() static Float_t ATan2( Float_t y, Float_t x );
40   GPUd() static Float_t Sin( Float_t x );
41   GPUd() static Float_t Cos( Float_t x );
42   GPUd() static Float_t Tan( Float_t x );
43   GPUd() static Float_t Copysign( Float_t x, Float_t y );  
44   GPUd() static Float_t TwoPi(){ return 6.28319; }
45   GPUd() static Float_t Pi(){ return 3.1415926535897; }
46   GPUd() static Int_t Nint(Float_t x );
47   GPUd() static Bool_t Finite(Float_t x );
48
49   GPUd()  static Int_t AtomicExch( Int_t *addr, Int_t val);
50   GPUd()  static Int_t AtomicAdd ( Int_t *addr, Int_t val);
51   GPUd()  static Int_t AtomicMax ( Int_t *addr, Int_t val);
52   GPUd()  static Int_t AtomicMin ( Int_t *addr, Int_t val);
53   GPUd()  static Int_t Mul24( Int_t a, Int_t b );
54   GPUd()  static Float_t FMulRZ( Float_t a, Float_t b );
55 };
56
57 typedef AliHLTTPCCAMath CAMath;
58
59
60 #if defined( HLTCA_GPUCODE )
61 #define choice(c1,c2,c3) c1
62 #elif defined( HLTCA_STANDALONE )
63 #define choice(c1,c2,c3) c2
64 #else
65 #define choice(c1,c2,c3) c3
66 #endif
67
68 GPUd() inline float2 AliHLTTPCCAMath::MakeFloat2(float x, float y )
69 {
70 #if !defined( HLTCA_GPUCODE )
71   float2 ret = {x,y};
72   return ret;
73 #else
74   return make_float2(x,y);
75 #endif
76 }
77
78
79 GPUd() inline Int_t AliHLTTPCCAMath::Nint(Float_t x)
80 {  
81 #if defined(HLTCA_STANDALONE) || defined( HLTCA_GPUCODE )
82   Int_t i;
83   if (x >= 0) {
84     i = int(x + 0.5);
85     if (x + 0.5 == Float_t(i) && i & 1) i--;
86   } else {
87     i = int(x - 0.5);
88     if (x - 0.5 == Float_t(i) && i & 1) i++;    
89   }
90   return i;
91 #else
92   return TMath::Nint(x);
93 #endif
94 }
95
96 GPUd() inline Bool_t AliHLTTPCCAMath::Finite(Float_t x)
97 {  
98   return choice( 1, finite(x), finite(x) );
99 }
100
101 GPUd() inline Float_t AliHLTTPCCAMath::ATan2(Float_t y, Float_t x)
102
103   return choice(atan2f(y,x), atan2(y,x), TMath::ATan2(y,x) );
104 }
105
106
107 GPUd() inline Float_t AliHLTTPCCAMath::Copysign(Float_t x, Float_t y)
108
109 #if defined( HLTCA_GPUCODE )
110   return copysignf(x,y);
111 #else
112   x = CAMath::Abs(x);
113   return (y>=0) ?x : -x;
114 #endif
115 }
116
117
118 GPUd() inline Float_t AliHLTTPCCAMath::Sin(Float_t x)
119
120   return choice( sinf(x), sin(x), TMath::Sin(x) );
121 }
122
123 GPUd() inline Float_t AliHLTTPCCAMath::Cos(Float_t x)
124
125   return choice( cosf(x), cos(x), TMath::Cos(x) );
126 }
127
128 GPUd() inline Float_t AliHLTTPCCAMath::Tan(Float_t x)
129
130   return choice( tanf(x), tan(x), TMath::Tan(x) );
131 }
132
133 GPUd() inline Float_t AliHLTTPCCAMath::Min(Float_t x, Float_t y)
134
135   return choice( fminf(x,y),  (x<y ?x :y), TMath::Min(x,y) );
136 }
137
138 GPUd() inline Float_t AliHLTTPCCAMath::Max(Float_t x, Float_t y)
139
140   return choice( fmaxf(x,y),  (x>y ?x :y), TMath::Max(x,y) );
141 }
142
143 GPUd() inline Int_t AliHLTTPCCAMath::Min(Int_t x, Int_t y)
144
145   return choice( min(x,y),  (x<y ?x :y), TMath::Min(x,y) );
146 }
147
148 GPUd() inline Int_t AliHLTTPCCAMath::Max(Int_t x, Int_t y)
149
150   return choice( max(x,y),  (x>y ?x :y), TMath::Max(x,y) );
151 }
152
153 GPUd() inline Float_t AliHLTTPCCAMath::Sqrt(Float_t x )
154
155   return choice( sqrtf(x), sqrt(x), TMath::Sqrt(x) );
156 }
157
158 GPUd() inline Float_t AliHLTTPCCAMath::Abs(Float_t x )
159
160   return choice( fabsf(x), fabs(x), TMath::Abs(x) );
161 }
162
163 GPUd() inline Double_t AliHLTTPCCAMath::Abs(Double_t x )
164
165   return choice( fabs(x), fabs(x), TMath::Abs(x) );
166 }
167
168 GPUd() inline Int_t AliHLTTPCCAMath::Abs(Int_t x )
169
170   return choice( abs(x), (x>=0 ?x :-x), TMath::Abs(x) );
171 }
172
173 GPUd() inline Float_t AliHLTTPCCAMath::ASin(Float_t x )
174
175   return choice( asinf(x), asin(x), TMath::ASin(x) );
176 }
177
178
179 GPUd() inline Int_t AliHLTTPCCAMath::Mul24( Int_t a, Int_t b )
180 {
181   return choice( __mul24(a,b), a*b, a*b );
182 }
183
184 GPUd() inline Float_t AliHLTTPCCAMath::FMulRZ( Float_t a, Float_t b )
185 {
186   return choice( __fmul_rz(a,b), a*b, a*b );
187 }
188
189
190 GPUd()  inline Int_t AliHLTTPCCAMath::AtomicExch( Int_t *addr, Int_t val)
191 {
192 #if defined( HLTCA_GPUCODE )
193   return ::atomicExch(addr, val );
194 #else  
195   Int_t old = *addr;
196   *addr = val;
197   return old;
198 #endif
199 }
200
201 GPUd()  inline Int_t AliHLTTPCCAMath::AtomicAdd ( Int_t *addr, Int_t val)
202 {
203 #if defined( HLTCA_GPUCODE )
204   return ::atomicAdd(addr, val );
205 #else  
206   Int_t old = *addr;
207   *addr += val;
208   return old;
209 #endif
210 }
211
212 GPUd()  inline Int_t AliHLTTPCCAMath::AtomicMax ( Int_t *addr, Int_t val)
213 {
214 #if defined( HLTCA_GPUCODE )
215   return ::atomicMax(addr, val );
216 #else  
217   Int_t old = *addr;
218   if( *addr< val ) *addr = val;
219   return old;
220 #endif
221 }
222
223 GPUd()  inline Int_t AliHLTTPCCAMath::AtomicMin ( Int_t *addr, Int_t val)
224 {
225 #if defined( HLTCA_GPUCODE )
226   return ::atomicMin(addr, val );
227 #else  
228   Int_t old = *addr;
229   if( *addr> val ) *addr = val;
230   return old;
231 #endif
232 }
233
234 #undef CHOICE
235
236 #endif