Line data Source code
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 : #if !defined(__OPENCL__) || defined(HLTCA_HOSTCODE)
16 : #include <math.h>
17 : #endif
18 : #else
19 : #include "TMath.h"
20 : #endif //HLTCA_STANDALONE | HLTCA_GPUCODE
21 :
22 : /**
23 : * @class ALIHLTTPCCAMath
24 : *
25 : *
26 : */
27 : class AliHLTTPCCAMath
28 : {
29 : public:
30 : GPUd() static float2 MakeFloat2( float x, float y );
31 :
32 : GPUhd() static float Min( float x, float y );
33 : GPUhd() static float Max( float x, float y );
34 : GPUhd() static int Min( int x, int y );
35 : GPUhd() static int Max( int x, int y );
36 : GPUhd() static float Sqrt( float x );
37 : GPUhd() static float Abs( float x );
38 : GPUhd() static double Abs( double x );
39 : GPUhd() static int Abs( int x );
40 : GPUhd() static float ASin( float x );
41 : GPUd() static float ATan2( float y, float x );
42 : GPUhd() static float Sin( float x );
43 : GPUhd() static float Cos( float x );
44 : GPUhd() static float Tan( float x );
45 : GPUd() static float Copysign( float x, float y );
46 0 : GPUd() static float TwoPi() { return 6.28319; }
47 0 : GPUd() static float Pi() { return 3.1415926535897; }
48 : GPUd() static int Nint( float x );
49 : GPUd() static bool Finite( float x );
50 :
51 : GPUhd() static float Log(float x);
52 : GPUd() static int AtomicExch( register GPUglobalref() int *addr, int val );
53 : GPUd() static int AtomicAdd (register GPUglobalref() int *addr, int val );
54 : GPUd() static int AtomicMax (register GPUglobalref() int *addr, int val );
55 : GPUd() static int AtomicMin (register GPUglobalref() int *addr, int val );
56 : GPUd() static int AtomicExchShared(register GPUsharedref() int *addr, int val );
57 : GPUd() static int AtomicAddShared (register GPUsharedref() int *addr, int val );
58 : GPUd() static int AtomicMaxShared (register GPUsharedref() int *addr, int val );
59 : GPUd() static int AtomicMinShared (register GPUsharedref() int *addr, int val );
60 : GPUd() static int Mul24( int a, int b );
61 : GPUd() static float FMulRZ( float a, float b );
62 : };
63 :
64 : typedef AliHLTTPCCAMath CAMath;
65 :
66 :
67 : #if defined( HLTCA_GPUCODE )
68 : #define choice(c1,c2,c3) c1
69 :
70 : #if defined( __OPENCL__ )
71 : #if defined( HLTCA_HOSTCODE)
72 : #if defined( HLTCA_STANDALONE )
73 : #define choiceA(c1,c2,c3) c2
74 : #else //HLTCA_STANDALONE
75 : #define choiceA(c1,c2,c3) c2
76 : #endif //HLTCA_STANDALONE
77 : #else //HLTCA_HOSTCODE
78 : #define choiceA(c1, c2, c3) c2
79 : #endif //HLTCA_HOSTCODE
80 : #else //__OPENCL
81 :
82 : #define choiceA choice
83 : #endif //__OPENCL__
84 : #elif defined( HLTCA_STANDALONE )
85 : #define choice(c1,c2,c3) c2
86 : #define choiceA choice
87 : #else
88 : #define choice(c1,c2,c3) c3
89 : #define choiceA choice
90 : #endif //HLTCA_GPUCODE
91 :
92 : GPUd() inline float2 AliHLTTPCCAMath::MakeFloat2( float x, float y )
93 : {
94 : #if !defined( HLTCA_GPUCODE ) || defined(__OPENCL__)
95 0 : float2 ret = {x, y};
96 0 : return ret;
97 : #else
98 : return make_float2( x, y );
99 : #endif //HLTCA_GPUCODE
100 : }
101 :
102 :
103 : GPUd() inline int AliHLTTPCCAMath::Nint( float x )
104 : {
105 : #if defined(HLTCA_STANDALONE) || defined( HLTCA_GPUCODE )
106 : int i;
107 : if ( x >= 0 ) {
108 : i = int( x + 0.5 );
109 : if ( x + 0.5 == float( i ) && i & 1 ) i--;
110 : } else {
111 : i = int( x - 0.5 );
112 : if ( x - 0.5 == float( i ) && i & 1 ) i++;
113 : }
114 : return i;
115 : #else
116 0 : return TMath::Nint( x );
117 : #endif //HLTCA_STANDALONE | HLTCA_GPUCODE
118 : }
119 :
120 : GPUd() inline bool AliHLTTPCCAMath::Finite( float x )
121 : {
122 0 : return choice( 1 /*isfinite( x )*/, finite( x ), isfinite( x ) );
123 : }
124 :
125 : GPUd() inline float AliHLTTPCCAMath::ATan2( float y, float x )
126 : {
127 0 : return choiceA( atan2f( y, x ), atan2( y, x ), TMath::ATan2( y, x ) );
128 : }
129 :
130 :
131 : GPUd() inline float AliHLTTPCCAMath::Copysign( float x, float y )
132 : {
133 : #if defined( HLTCA_GPUCODE ) && !defined(__OPENCL__)
134 : return copysignf( x, y );
135 : #else
136 : x = CAMath::Abs( x );
137 : return ( y >= 0 ) ? x : -x;
138 : #endif //HLTCA_GPUCODE
139 : }
140 :
141 :
142 : GPUhd() inline float AliHLTTPCCAMath::Sin( float x )
143 : {
144 0 : return choiceA( sinf( x ), sin( x ), TMath::Sin( x ) );
145 : }
146 :
147 : GPUhd() inline float AliHLTTPCCAMath::Cos( float x )
148 : {
149 0 : return choiceA( cosf( x ), cos( x ), TMath::Cos( x ) );
150 : }
151 :
152 : GPUhd() inline float AliHLTTPCCAMath::Tan( float x )
153 : {
154 0 : return choiceA( tanf( x ), tan( x ), TMath::Tan( x ) );
155 : }
156 :
157 : GPUhd() inline float AliHLTTPCCAMath::Min( float x, float y )
158 : {
159 0 : return choiceA( fminf( x, y ), ( x < y ? x : y ), TMath::Min( x, y ) );
160 : }
161 :
162 : GPUhd() inline float AliHLTTPCCAMath::Max( float x, float y )
163 : {
164 0 : return choiceA( fmaxf( x, y ), ( x > y ? x : y ), TMath::Max( x, y ) );
165 : }
166 :
167 : GPUhd() inline int AliHLTTPCCAMath::Min( int x, int y )
168 : {
169 0 : return choiceA( min( x, y ), ( x < y ? x : y ), TMath::Min( x, y ) );
170 : }
171 :
172 : GPUhd() inline int AliHLTTPCCAMath::Max( int x, int y )
173 : {
174 0 : return choiceA( max( x, y ), ( x > y ? x : y ), TMath::Max( x, y ) );
175 : }
176 :
177 : GPUhd() inline float AliHLTTPCCAMath::Sqrt( float x )
178 : {
179 0 : return choiceA( sqrtf( x ), sqrt( x ), TMath::Sqrt( x ) );
180 : }
181 :
182 : GPUhd() inline float AliHLTTPCCAMath::Abs( float x )
183 : {
184 0 : return choiceA( fabsf( x ), fabs( x ), TMath::Abs( x ) );
185 : }
186 :
187 : GPUhd() inline double AliHLTTPCCAMath::Abs( double x )
188 : {
189 0 : return choice( fabs( x ), fabs( x ), TMath::Abs( x ) );
190 : }
191 :
192 : GPUhd() inline int AliHLTTPCCAMath::Abs( int x )
193 : {
194 0 : return choice( abs( x ), ( x >= 0 ? x : -x ), TMath::Abs( x ) );
195 : }
196 :
197 : GPUhd() inline float AliHLTTPCCAMath::ASin( float x )
198 : {
199 0 : return choiceA( asinf( x ), asin( x ), TMath::ASin( x ) );
200 : }
201 :
202 :
203 : GPUd() inline int AliHLTTPCCAMath::Mul24( int a, int b )
204 : {
205 : #if defined(FERMI) || defined(__OPENCL__) || defined(KEPLER)
206 : return(a * b);
207 : #else
208 0 : return choice( __mul24( a, b ), a*b, a*b );
209 : #endif
210 : }
211 :
212 : GPUd() inline float AliHLTTPCCAMath::FMulRZ( float a, float b )
213 : {
214 0 : return choiceA( __fmul_rz( a, b ), a*b, a*b );
215 : }
216 :
217 : GPUhd() inline float AliHLTTPCCAMath::Log(float x)
218 : {
219 0 : return choice( Log(x), Log(x), TMath::Log(x));
220 : }
221 :
222 : #if defined(__OPENCL__) && !defined(HLTCA_HOSTCODE)
223 : GPUd() inline int AliHLTTPCCAMath::AtomicExchShared(register GPUsharedref() int *addr, int val ) {return ::atomic_xchg( (volatile __local int*) addr, val );}
224 : GPUd() inline int AliHLTTPCCAMath::AtomicAddShared (register GPUsharedref() int *addr, int val ) {return ::atomic_add( (volatile __local int*) addr, val );}
225 : GPUd() inline int AliHLTTPCCAMath::AtomicMaxShared (register GPUsharedref() int *addr, int val ) {return ::atomic_max( (volatile __local int*) addr, val );}
226 : GPUd() inline int AliHLTTPCCAMath::AtomicMinShared (register GPUsharedref() int *addr, int val ) {return ::atomic_min( (volatile __local int*) addr, val );}
227 :
228 : #else
229 : GPUd() inline int AliHLTTPCCAMath::AtomicExchShared( int *addr, int val ) {return(AliHLTTPCCAMath::AtomicExch(addr, val));}
230 0 : GPUd() inline int AliHLTTPCCAMath::AtomicAddShared ( int *addr, int val ) {return(AliHLTTPCCAMath::AtomicAdd(addr, val));}
231 : GPUd() inline int AliHLTTPCCAMath::AtomicMaxShared ( int *addr, int val ) {return(AliHLTTPCCAMath::AtomicMax(addr, val));}
232 : GPUd() inline int AliHLTTPCCAMath::AtomicMinShared ( int *addr, int val ) {return(AliHLTTPCCAMath::AtomicMin(addr, val));}
233 : #endif
234 :
235 :
236 : GPUd() inline int AliHLTTPCCAMath::AtomicExch(register GPUglobalref() int *addr, int val )
237 : {
238 : #if defined( HLTCA_GPUCODE ) & !defined(HLTCA_HOSTCODE)
239 : #ifdef __OPENCL__
240 : return ::atomic_xchg( (volatile __global int*) addr, val );
241 : #else
242 : return ::atomicExch( addr, val );
243 : #endif
244 : #else
245 : int old = *addr;
246 : *addr = val;
247 : return old;
248 : #endif //HLTCA_GPUCODE
249 : }
250 :
251 : GPUd() inline int AliHLTTPCCAMath::AtomicAdd (register GPUglobalref() int *addr, int val )
252 : {
253 : #if defined( HLTCA_GPUCODE ) & !defined(HLTCA_HOSTCODE)
254 : #ifdef __OPENCL__
255 : return ::atomic_add( (volatile __global int*) addr, val );
256 : #else
257 : return ::atomicAdd( addr, val );
258 : #endif
259 : #else
260 0 : int old = *addr;
261 0 : *addr += val;
262 0 : return old;
263 : #endif //HLTCA_GPUCODE
264 : }
265 :
266 : GPUd() inline int AliHLTTPCCAMath::AtomicMax (register GPUglobalref() int *addr, int val )
267 : {
268 : #if defined( HLTCA_GPUCODE ) & !defined(HLTCA_HOSTCODE)
269 : #ifdef __OPENCL__
270 : return ::atomic_max( (volatile __global int*) addr, val );
271 : #else
272 : return ::atomicMax( addr, val );
273 : #endif
274 : #else
275 0 : int old = *addr;
276 0 : if ( *addr < val ) *addr = val;
277 0 : return old;
278 : #endif //HLTCA_GPUCODE
279 : }
280 :
281 : GPUd() inline int AliHLTTPCCAMath::AtomicMin (register GPUglobalref() int *addr, int val )
282 : {
283 : #if defined( HLTCA_GPUCODE ) & !defined(HLTCA_HOSTCODE)
284 : #ifdef __OPENCL__
285 : return ::atomic_min( (volatile __global int*) addr, val );
286 : #else
287 : return ::atomicMin( addr, val );
288 : #endif
289 : #else
290 : int old = *addr;
291 : if ( *addr > val ) *addr = val;
292 : return old;
293 : #endif //HLTCA_GPUCODE
294 : }
295 :
296 : #undef CHOICE
297 :
298 : #endif //ALIHLTTPCCAMATH_H
|