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 *
7 //*************************************************************************
9 #ifndef ALIHLTTPCCAMATH_H
10 #define ALIHLTTPCCAMATH_H
12 #include "AliHLTTPCCADef.h"
14 #if defined(HLTCA_STANDALONE) || defined(HLTCA_GPUCODE)
15 #if !defined(__OPENCL__) || defined(HLTCA_HOSTCODE)
20 #endif //HLTCA_STANDALONE | HLTCA_GPUCODE
23 * @class ALIHLTTPCCAMath
30 GPUd() static float2 MakeFloat2( float x, float y );
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 GPUd() static float TwoPi() { return 6.28319; }
47 GPUd() static float Pi() { return 3.1415926535897; }
48 GPUd() static int Nint( float x );
49 GPUd() static bool Finite( float x );
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 );
64 typedef AliHLTTPCCAMath CAMath;
67 #if defined( HLTCA_GPUCODE )
68 #define choice(c1,c2,c3) c1
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
82 #define choiceA choice
84 #elif defined( HLTCA_STANDALONE )
85 #define choice(c1,c2,c3) c2
86 #define choiceA choice
88 #define choice(c1,c2,c3) c3
89 #define choiceA choice
90 #endif //HLTCA_GPUCODE
92 GPUd() inline float2 AliHLTTPCCAMath::MakeFloat2( float x, float y )
94 #if !defined( HLTCA_GPUCODE ) || defined(__OPENCL__)
98 return make_float2( x, y );
99 #endif //HLTCA_GPUCODE
103 GPUd() inline int AliHLTTPCCAMath::Nint( float x )
105 #if defined(HLTCA_STANDALONE) || defined( HLTCA_GPUCODE )
109 if ( x + 0.5 == float( i ) && i & 1 ) i--;
112 if ( x - 0.5 == float( i ) && i & 1 ) i++;
116 return TMath::Nint( x );
117 #endif //HLTCA_STANDALONE | HLTCA_GPUCODE
120 GPUd() inline bool AliHLTTPCCAMath::Finite( float x )
122 return choice( 1 /*isfinite( x )*/, finite( x ), finite( x ) );
125 GPUd() inline float AliHLTTPCCAMath::ATan2( float y, float x )
127 return choiceA( atan2f( y, x ), atan2( y, x ), TMath::ATan2( y, x ) );
131 GPUd() inline float AliHLTTPCCAMath::Copysign( float x, float y )
133 #if defined( HLTCA_GPUCODE ) && !defined(__OPENCL__)
134 return copysignf( x, y );
136 x = CAMath::Abs( x );
137 return ( y >= 0 ) ? x : -x;
138 #endif //HLTCA_GPUCODE
142 GPUhd() inline float AliHLTTPCCAMath::Sin( float x )
144 return choiceA( sinf( x ), sin( x ), TMath::Sin( x ) );
147 GPUhd() inline float AliHLTTPCCAMath::Cos( float x )
149 return choiceA( cosf( x ), cos( x ), TMath::Cos( x ) );
152 GPUhd() inline float AliHLTTPCCAMath::Tan( float x )
154 return choiceA( tanf( x ), tan( x ), TMath::Tan( x ) );
157 GPUhd() inline float AliHLTTPCCAMath::Min( float x, float y )
159 return choiceA( fminf( x, y ), ( x < y ? x : y ), TMath::Min( x, y ) );
162 GPUhd() inline float AliHLTTPCCAMath::Max( float x, float y )
164 return choiceA( fmaxf( x, y ), ( x > y ? x : y ), TMath::Max( x, y ) );
167 GPUhd() inline int AliHLTTPCCAMath::Min( int x, int y )
169 return choiceA( min( x, y ), ( x < y ? x : y ), TMath::Min( x, y ) );
172 GPUhd() inline int AliHLTTPCCAMath::Max( int x, int y )
174 return choiceA( max( x, y ), ( x > y ? x : y ), TMath::Max( x, y ) );
177 GPUhd() inline float AliHLTTPCCAMath::Sqrt( float x )
179 return choiceA( sqrtf( x ), sqrt( x ), TMath::Sqrt( x ) );
182 GPUhd() inline float AliHLTTPCCAMath::Abs( float x )
184 return choiceA( fabsf( x ), fabs( x ), TMath::Abs( x ) );
187 GPUhd() inline double AliHLTTPCCAMath::Abs( double x )
189 return choice( fabs( x ), fabs( x ), TMath::Abs( x ) );
192 GPUhd() inline int AliHLTTPCCAMath::Abs( int x )
194 return choice( abs( x ), ( x >= 0 ? x : -x ), TMath::Abs( x ) );
197 GPUhd() inline float AliHLTTPCCAMath::ASin( float x )
199 return choiceA( asinf( x ), asin( x ), TMath::ASin( x ) );
203 GPUd() inline int AliHLTTPCCAMath::Mul24( int a, int b )
205 #if defined(FERMI) || defined(__OPENCL__) || defined(KEPLER)
208 return choice( __mul24( a, b ), a*b, a*b );
212 GPUd() inline float AliHLTTPCCAMath::FMulRZ( float a, float b )
214 return choiceA( __fmul_rz( a, b ), a*b, a*b );
217 GPUhd() inline float AliHLTTPCCAMath::Log(float x)
219 return choice( Log(x), Log(x), TMath::Log(x));
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 );}
229 GPUd() inline int AliHLTTPCCAMath::AtomicExchShared( int *addr, int val ) {return(AliHLTTPCCAMath::AtomicExch(addr, val));}
230 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));}
236 GPUd() inline int AliHLTTPCCAMath::AtomicExch(register GPUglobalref() int *addr, int val )
238 #if defined( HLTCA_GPUCODE ) & !defined(HLTCA_HOSTCODE)
240 return ::atomic_xchg( (volatile __global int*) addr, val );
242 return ::atomicExch( addr, val );
248 #endif //HLTCA_GPUCODE
251 GPUd() inline int AliHLTTPCCAMath::AtomicAdd (register GPUglobalref() int *addr, int val )
253 #if defined( HLTCA_GPUCODE ) & !defined(HLTCA_HOSTCODE)
255 return ::atomic_add( (volatile __global int*) addr, val );
257 return ::atomicAdd( addr, val );
263 #endif //HLTCA_GPUCODE
266 GPUd() inline int AliHLTTPCCAMath::AtomicMax (register GPUglobalref() int *addr, int val )
268 #if defined( HLTCA_GPUCODE ) & !defined(HLTCA_HOSTCODE)
270 return ::atomic_max( (volatile __global int*) addr, val );
272 return ::atomicMax( addr, val );
276 if ( *addr < val ) *addr = val;
278 #endif //HLTCA_GPUCODE
281 GPUd() inline int AliHLTTPCCAMath::AtomicMin (register GPUglobalref() int *addr, int val )
283 #if defined( HLTCA_GPUCODE ) & !defined(HLTCA_HOSTCODE)
285 return ::atomic_min( (volatile __global int*) addr, val );
287 return ::atomicMin( addr, val );
291 if ( *addr > val ) *addr = val;
293 #endif //HLTCA_GPUCODE
298 #endif //ALIHLTTPCCAMATH_H