]>
Commit | Line | Data |
---|---|---|
00d07bcd | 1 | //-*- Mode: C++ -*- |
ce565086 | 2 | // ************************************************************************ |
fbb9b71b | 3 | // This file is property of and copyright by the ALICE HLT Project * |
ce565086 | 4 | // ALICE Experiment at CERN, All rights reserved. * |
5 | // See cxx source for full Copyright notice * | |
6 | // * | |
7 | //************************************************************************* | |
00d07bcd | 8 | |
9 | #ifndef ALIHLTTPCCAMATH_H | |
10 | #define ALIHLTTPCCAMATH_H | |
11 | ||
12 | #include "AliHLTTPCCADef.h" | |
13 | ||
14 | #if defined(HLTCA_STANDALONE) || defined(HLTCA_GPUCODE) | |
43422963 | 15 | #if !defined(__OPENCL__) || defined(HLTCA_HOSTCODE) |
00d07bcd | 16 | #include <math.h> |
43422963 | 17 | #endif |
00d07bcd | 18 | #else |
19 | #include "TMath.h" | |
31649d4b | 20 | #endif //HLTCA_STANDALONE | HLTCA_GPUCODE |
00d07bcd | 21 | |
22 | /** | |
23 | * @class ALIHLTTPCCAMath | |
24 | * | |
25 | * | |
26 | */ | |
27 | class AliHLTTPCCAMath | |
28 | { | |
fbb9b71b | 29 | public: |
30 | GPUd() static float2 MakeFloat2( float x, float y ); | |
31 | ||
43422963 | 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 ); | |
fbb9b71b | 41 | GPUd() static float ATan2( float y, float x ); |
43422963 | 42 | GPUhd() static float Sin( float x ); |
43 | GPUhd() static float Cos( float x ); | |
44 | GPUhd() static float Tan( float x ); | |
fbb9b71b | 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 ); | |
50 | ||
43422963 | 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 ); | |
fbb9b71b | 60 | GPUd() static int Mul24( int a, int b ); |
61 | GPUd() static float FMulRZ( float a, float b ); | |
00d07bcd | 62 | }; |
63 | ||
64 | typedef AliHLTTPCCAMath CAMath; | |
65 | ||
66 | ||
67 | #if defined( HLTCA_GPUCODE ) | |
68 | #define choice(c1,c2,c3) c1 | |
43422963 | 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 | |
d3821846 | 75 | #define choiceA(c1,c2,c3) c2 |
43422963 | 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__ | |
00d07bcd | 84 | #elif defined( HLTCA_STANDALONE ) |
85 | #define choice(c1,c2,c3) c2 | |
43422963 | 86 | #define choiceA choice |
00d07bcd | 87 | #else |
88 | #define choice(c1,c2,c3) c3 | |
43422963 | 89 | #define choiceA choice |
31649d4b | 90 | #endif //HLTCA_GPUCODE |
00d07bcd | 91 | |
fbb9b71b | 92 | GPUd() inline float2 AliHLTTPCCAMath::MakeFloat2( float x, float y ) |
ce565086 | 93 | { |
43422963 | 94 | #if !defined( HLTCA_GPUCODE ) || defined(__OPENCL__) |
fbb9b71b | 95 | float2 ret = {x, y}; |
ce565086 | 96 | return ret; |
97 | #else | |
fbb9b71b | 98 | return make_float2( x, y ); |
31649d4b | 99 | #endif //HLTCA_GPUCODE |
ce565086 | 100 | } |
101 | ||
00d07bcd | 102 | |
fbb9b71b | 103 | GPUd() inline int AliHLTTPCCAMath::Nint( float x ) |
104 | { | |
00d07bcd | 105 | #if defined(HLTCA_STANDALONE) || defined( HLTCA_GPUCODE ) |
fbb9b71b | 106 | int i; |
107 | if ( x >= 0 ) { | |
108 | i = int( x + 0.5 ); | |
109 | if ( x + 0.5 == float( i ) && i & 1 ) i--; | |
00d07bcd | 110 | } else { |
fbb9b71b | 111 | i = int( x - 0.5 ); |
112 | if ( x - 0.5 == float( i ) && i & 1 ) i++; | |
00d07bcd | 113 | } |
114 | return i; | |
115 | #else | |
fbb9b71b | 116 | return TMath::Nint( x ); |
31649d4b | 117 | #endif //HLTCA_STANDALONE | HLTCA_GPUCODE |
00d07bcd | 118 | } |
119 | ||
fbb9b71b | 120 | GPUd() inline bool AliHLTTPCCAMath::Finite( float x ) |
121 | { | |
e4818148 | 122 | return choice( 1 /*isfinite( x )*/, finite( x ), finite( x ) ); |
00d07bcd | 123 | } |
124 | ||
fbb9b71b | 125 | GPUd() inline float AliHLTTPCCAMath::ATan2( float y, float x ) |
126 | { | |
43422963 | 127 | return choiceA( atan2f( y, x ), atan2( y, x ), TMath::ATan2( y, x ) ); |
00d07bcd | 128 | } |
129 | ||
130 | ||
fbb9b71b | 131 | GPUd() inline float AliHLTTPCCAMath::Copysign( float x, float y ) |
132 | { | |
43422963 | 133 | #if defined( HLTCA_GPUCODE ) && !defined(__OPENCL__) |
fbb9b71b | 134 | return copysignf( x, y ); |
00d07bcd | 135 | #else |
fbb9b71b | 136 | x = CAMath::Abs( x ); |
137 | return ( y >= 0 ) ? x : -x; | |
31649d4b | 138 | #endif //HLTCA_GPUCODE |
00d07bcd | 139 | } |
140 | ||
141 | ||
7be9b0d7 | 142 | GPUhd() inline float AliHLTTPCCAMath::Sin( float x ) |
fbb9b71b | 143 | { |
43422963 | 144 | return choiceA( sinf( x ), sin( x ), TMath::Sin( x ) ); |
00d07bcd | 145 | } |
146 | ||
7be9b0d7 | 147 | GPUhd() inline float AliHLTTPCCAMath::Cos( float x ) |
fbb9b71b | 148 | { |
43422963 | 149 | return choiceA( cosf( x ), cos( x ), TMath::Cos( x ) ); |
00d07bcd | 150 | } |
151 | ||
7be9b0d7 | 152 | GPUhd() inline float AliHLTTPCCAMath::Tan( float x ) |
fbb9b71b | 153 | { |
43422963 | 154 | return choiceA( tanf( x ), tan( x ), TMath::Tan( x ) ); |
00d07bcd | 155 | } |
156 | ||
7be9b0d7 | 157 | GPUhd() inline float AliHLTTPCCAMath::Min( float x, float y ) |
fbb9b71b | 158 | { |
43422963 | 159 | return choiceA( fminf( x, y ), ( x < y ? x : y ), TMath::Min( x, y ) ); |
00d07bcd | 160 | } |
161 | ||
7be9b0d7 | 162 | GPUhd() inline float AliHLTTPCCAMath::Max( float x, float y ) |
fbb9b71b | 163 | { |
43422963 | 164 | return choiceA( fmaxf( x, y ), ( x > y ? x : y ), TMath::Max( x, y ) ); |
00d07bcd | 165 | } |
166 | ||
7be9b0d7 | 167 | GPUhd() inline int AliHLTTPCCAMath::Min( int x, int y ) |
fbb9b71b | 168 | { |
43422963 | 169 | return choiceA( min( x, y ), ( x < y ? x : y ), TMath::Min( x, y ) ); |
00d07bcd | 170 | } |
171 | ||
7be9b0d7 | 172 | GPUhd() inline int AliHLTTPCCAMath::Max( int x, int y ) |
fbb9b71b | 173 | { |
43422963 | 174 | return choiceA( max( x, y ), ( x > y ? x : y ), TMath::Max( x, y ) ); |
00d07bcd | 175 | } |
176 | ||
7be9b0d7 | 177 | GPUhd() inline float AliHLTTPCCAMath::Sqrt( float x ) |
fbb9b71b | 178 | { |
43422963 | 179 | return choiceA( sqrtf( x ), sqrt( x ), TMath::Sqrt( x ) ); |
00d07bcd | 180 | } |
181 | ||
7be9b0d7 | 182 | GPUhd() inline float AliHLTTPCCAMath::Abs( float x ) |
fbb9b71b | 183 | { |
43422963 | 184 | return choiceA( fabsf( x ), fabs( x ), TMath::Abs( x ) ); |
00d07bcd | 185 | } |
186 | ||
7be9b0d7 | 187 | GPUhd() inline double AliHLTTPCCAMath::Abs( double x ) |
fbb9b71b | 188 | { |
189 | return choice( fabs( x ), fabs( x ), TMath::Abs( x ) ); | |
00d07bcd | 190 | } |
191 | ||
7be9b0d7 | 192 | GPUhd() inline int AliHLTTPCCAMath::Abs( int x ) |
fbb9b71b | 193 | { |
194 | return choice( abs( x ), ( x >= 0 ? x : -x ), TMath::Abs( x ) ); | |
00d07bcd | 195 | } |
196 | ||
7be9b0d7 | 197 | GPUhd() inline float AliHLTTPCCAMath::ASin( float x ) |
fbb9b71b | 198 | { |
43422963 | 199 | return choiceA( asinf( x ), asin( x ), TMath::ASin( x ) ); |
00d07bcd | 200 | } |
201 | ||
202 | ||
fbb9b71b | 203 | GPUd() inline int AliHLTTPCCAMath::Mul24( int a, int b ) |
00d07bcd | 204 | { |
2c3d0869 | 205 | #if defined(FERMI) || defined(__OPENCL__) || defined(KEPLER) |
6f0cdd46 | 206 | return(a * b); |
207 | #else | |
fbb9b71b | 208 | return choice( __mul24( a, b ), a*b, a*b ); |
6f0cdd46 | 209 | #endif |
00d07bcd | 210 | } |
211 | ||
fbb9b71b | 212 | GPUd() inline float AliHLTTPCCAMath::FMulRZ( float a, float b ) |
00d07bcd | 213 | { |
43422963 | 214 | return choiceA( __fmul_rz( a, b ), a*b, a*b ); |
00d07bcd | 215 | } |
216 | ||
7be9b0d7 | 217 | GPUhd() inline float AliHLTTPCCAMath::Log(float x) |
218 | { | |
219 | return choice( Log(x), Log(x), TMath::Log(x)); | |
220 | } | |
221 | ||
43422963 | 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 | 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 | ||
00d07bcd | 235 | |
43422963 | 236 | GPUd() inline int AliHLTTPCCAMath::AtomicExch(register GPUglobalref() int *addr, int val ) |
00d07bcd | 237 | { |
43422963 | 238 | #if defined( HLTCA_GPUCODE ) & !defined(HLTCA_HOSTCODE) |
239 | #ifdef __OPENCL__ | |
240 | return ::atomic_xchg( (volatile __global int*) addr, val ); | |
241 | #else | |
fbb9b71b | 242 | return ::atomicExch( addr, val ); |
43422963 | 243 | #endif |
fbb9b71b | 244 | #else |
245 | int old = *addr; | |
00d07bcd | 246 | *addr = val; |
247 | return old; | |
31649d4b | 248 | #endif //HLTCA_GPUCODE |
00d07bcd | 249 | } |
250 | ||
43422963 | 251 | GPUd() inline int AliHLTTPCCAMath::AtomicAdd (register GPUglobalref() int *addr, int val ) |
00d07bcd | 252 | { |
43422963 | 253 | #if defined( HLTCA_GPUCODE ) & !defined(HLTCA_HOSTCODE) |
254 | #ifdef __OPENCL__ | |
255 | return ::atomic_add( (volatile __global int*) addr, val ); | |
256 | #else | |
fbb9b71b | 257 | return ::atomicAdd( addr, val ); |
43422963 | 258 | #endif |
fbb9b71b | 259 | #else |
260 | int old = *addr; | |
00d07bcd | 261 | *addr += val; |
262 | return old; | |
31649d4b | 263 | #endif //HLTCA_GPUCODE |
00d07bcd | 264 | } |
265 | ||
43422963 | 266 | GPUd() inline int AliHLTTPCCAMath::AtomicMax (register GPUglobalref() int *addr, int val ) |
00d07bcd | 267 | { |
43422963 | 268 | #if defined( HLTCA_GPUCODE ) & !defined(HLTCA_HOSTCODE) |
269 | #ifdef __OPENCL__ | |
270 | return ::atomic_max( (volatile __global int*) addr, val ); | |
271 | #else | |
fbb9b71b | 272 | return ::atomicMax( addr, val ); |
43422963 | 273 | #endif |
fbb9b71b | 274 | #else |
275 | int old = *addr; | |
276 | if ( *addr < val ) *addr = val; | |
00d07bcd | 277 | return old; |
31649d4b | 278 | #endif //HLTCA_GPUCODE |
00d07bcd | 279 | } |
280 | ||
43422963 | 281 | GPUd() inline int AliHLTTPCCAMath::AtomicMin (register GPUglobalref() int *addr, int val ) |
00d07bcd | 282 | { |
43422963 | 283 | #if defined( HLTCA_GPUCODE ) & !defined(HLTCA_HOSTCODE) |
284 | #ifdef __OPENCL__ | |
285 | return ::atomic_min( (volatile __global int*) addr, val ); | |
286 | #else | |
fbb9b71b | 287 | return ::atomicMin( addr, val ); |
43422963 | 288 | #endif |
fbb9b71b | 289 | #else |
290 | int old = *addr; | |
291 | if ( *addr > val ) *addr = val; | |
00d07bcd | 292 | return old; |
31649d4b | 293 | #endif //HLTCA_GPUCODE |
00d07bcd | 294 | } |
295 | ||
296 | #undef CHOICE | |
297 | ||
31649d4b | 298 | #endif //ALIHLTTPCCAMATH_H |