]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/tracking-ca/AliHLTTPCCAMath.h
cosmetical changes
[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 Min( float x, float y );
31     GPUd() static float Max( float x, float y );
32     GPUd() static int Min( int x, int y );
33     GPUd() static int Max( int x, int y );
34     GPUd() static float Sqrt( float x );
35     GPUd() static float Abs( float x );
36     GPUd() static double Abs( double x );
37     GPUd() static int Abs( int x );
38     GPUd() static float ASin( float x );
39     GPUd() static float ATan2( float y, float x );
40     GPUd() static float Sin( float x );
41     GPUd() static float Cos( float x );
42     GPUd() static float Tan( float x );
43     GPUd() static float Copysign( float x, float y );
44     GPUd() static float TwoPi() { return 6.28319; }
45     GPUd() static float Pi() { return 3.1415926535897; }
46     GPUd() static int Nint( float x );
47     GPUd() static bool Finite( float x );
48
49     GPUd()  static int AtomicExch( int *addr, int val );
50     GPUd()  static int AtomicAdd ( int *addr, int val );
51     GPUd()  static int AtomicMax ( int *addr, int val );
52     GPUd()  static int AtomicMin ( int *addr, int val );
53     GPUd()  static int Mul24( int a, int b );
54     GPUd()  static float FMulRZ( float a, float 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 AliHLTTPCCAMath::Nint( float x )
80 {
81 #if defined(HLTCA_STANDALONE) || defined( HLTCA_GPUCODE )
82   int i;
83   if ( x >= 0 ) {
84     i = int( x + 0.5 );
85     if ( x + 0.5 == float( i ) && i & 1 ) i--;
86   } else {
87     i = int( x - 0.5 );
88     if ( x - 0.5 == float( i ) && i & 1 ) i++;
89   }
90   return i;
91 #else
92   return TMath::Nint( x );
93 #endif
94 }
95
96 GPUd() inline bool AliHLTTPCCAMath::Finite( float x )
97 {
98   return choice( 1, finite( x ), finite( x ) );
99 }
100
101 GPUd() inline float AliHLTTPCCAMath::ATan2( float y, float x )
102 {
103   return choice( atan2f( y, x ), atan2( y, x ), TMath::ATan2( y, x ) );
104 }
105
106
107 GPUd() inline float AliHLTTPCCAMath::Copysign( float x, float 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 AliHLTTPCCAMath::Sin( float x )
119 {
120   return choice( sinf( x ), sin( x ), TMath::Sin( x ) );
121 }
122
123 GPUd() inline float AliHLTTPCCAMath::Cos( float x )
124 {
125   return choice( cosf( x ), cos( x ), TMath::Cos( x ) );
126 }
127
128 GPUd() inline float AliHLTTPCCAMath::Tan( float x )
129 {
130   return choice( tanf( x ), tan( x ), TMath::Tan( x ) );
131 }
132
133 GPUd() inline float AliHLTTPCCAMath::Min( float x, float y )
134 {
135   return choice( fminf( x, y ),  ( x < y ? x : y ), TMath::Min( x, y ) );
136 }
137
138 GPUd() inline float AliHLTTPCCAMath::Max( float x, float y )
139 {
140   return choice( fmaxf( x, y ),  ( x > y ? x : y ), TMath::Max( x, y ) );
141 }
142
143 GPUd() inline int AliHLTTPCCAMath::Min( int x, int y )
144 {
145   return choice( min( x, y ),  ( x < y ? x : y ), TMath::Min( x, y ) );
146 }
147
148 GPUd() inline int AliHLTTPCCAMath::Max( int x, int y )
149 {
150   return choice( max( x, y ),  ( x > y ? x : y ), TMath::Max( x, y ) );
151 }
152
153 GPUd() inline float AliHLTTPCCAMath::Sqrt( float x )
154 {
155   return choice( sqrtf( x ), sqrt( x ), TMath::Sqrt( x ) );
156 }
157
158 GPUd() inline float AliHLTTPCCAMath::Abs( float x )
159 {
160   return choice( fabsf( x ), fabs( x ), TMath::Abs( x ) );
161 }
162
163 GPUd() inline double AliHLTTPCCAMath::Abs( double x )
164 {
165   return choice( fabs( x ), fabs( x ), TMath::Abs( x ) );
166 }
167
168 GPUd() inline int AliHLTTPCCAMath::Abs( int x )
169 {
170   return choice( abs( x ), ( x >= 0 ? x : -x ), TMath::Abs( x ) );
171 }
172
173 GPUd() inline float AliHLTTPCCAMath::ASin( float x )
174 {
175   return choice( asinf( x ), asin( x ), TMath::ASin( x ) );
176 }
177
178
179 GPUd() inline int AliHLTTPCCAMath::Mul24( int a, int b )
180 {
181   return choice( __mul24( a, b ), a*b, a*b );
182 }
183
184 GPUd() inline float AliHLTTPCCAMath::FMulRZ( float a, float b )
185 {
186   return choice( __fmul_rz( a, b ), a*b, a*b );
187 }
188
189
190 GPUd()  inline int AliHLTTPCCAMath::AtomicExch( int *addr, int val )
191 {
192 #if defined( HLTCA_GPUCODE )
193   return ::atomicExch( addr, val );
194 #else
195   int old = *addr;
196   *addr = val;
197   return old;
198 #endif
199 }
200
201 GPUd()  inline int AliHLTTPCCAMath::AtomicAdd ( int *addr, int val )
202 {
203 #if defined( HLTCA_GPUCODE )
204   return ::atomicAdd( addr, val );
205 #else
206   int old = *addr;
207   *addr += val;
208   return old;
209 #endif
210 }
211
212 GPUd()  inline int AliHLTTPCCAMath::AtomicMax ( int *addr, int val )
213 {
214 #if defined( HLTCA_GPUCODE )
215   return ::atomicMax( addr, val );
216 #else
217   int old = *addr;
218   if ( *addr < val ) *addr = val;
219   return old;
220 #endif
221 }
222
223 GPUd()  inline int AliHLTTPCCAMath::AtomicMin ( int *addr, int val )
224 {
225 #if defined( HLTCA_GPUCODE )
226   return ::atomicMin( addr, val );
227 #else
228   int old = *addr;
229   if ( *addr > val ) *addr = val;
230   return old;
231 #endif
232 }
233
234 #undef CHOICE
235
236 #endif