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)
21 * @class ALIHLTTPCCAMath
28 GPUd() static float2 MakeFloat2( float x, float y );
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 );
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 );
57 typedef AliHLTTPCCAMath CAMath;
60 #if defined( HLTCA_GPUCODE )
61 #define choice(c1,c2,c3) c1
62 #elif defined( HLTCA_STANDALONE )
63 #define choice(c1,c2,c3) c2
65 #define choice(c1,c2,c3) c3
68 GPUd() inline float2 AliHLTTPCCAMath::MakeFloat2( float x, float y )
70 #if !defined( HLTCA_GPUCODE )
74 return make_float2( x, y );
79 GPUd() inline int AliHLTTPCCAMath::Nint( float x )
81 #if defined(HLTCA_STANDALONE) || defined( HLTCA_GPUCODE )
85 if ( x + 0.5 == float( i ) && i & 1 ) i--;
88 if ( x - 0.5 == float( i ) && i & 1 ) i++;
92 return TMath::Nint( x );
96 GPUd() inline bool AliHLTTPCCAMath::Finite( float x )
98 return choice( 1, finite( x ), finite( x ) );
101 GPUd() inline float AliHLTTPCCAMath::ATan2( float y, float x )
103 return choice( atan2f( y, x ), atan2( y, x ), TMath::ATan2( y, x ) );
107 GPUd() inline float AliHLTTPCCAMath::Copysign( float x, float y )
109 #if defined( HLTCA_GPUCODE )
110 return copysignf( x, y );
112 x = CAMath::Abs( x );
113 return ( y >= 0 ) ? x : -x;
118 GPUd() inline float AliHLTTPCCAMath::Sin( float x )
120 return choice( sinf( x ), sin( x ), TMath::Sin( x ) );
123 GPUd() inline float AliHLTTPCCAMath::Cos( float x )
125 return choice( cosf( x ), cos( x ), TMath::Cos( x ) );
128 GPUd() inline float AliHLTTPCCAMath::Tan( float x )
130 return choice( tanf( x ), tan( x ), TMath::Tan( x ) );
133 GPUd() inline float AliHLTTPCCAMath::Min( float x, float y )
135 return choice( fminf( x, y ), ( x < y ? x : y ), TMath::Min( x, y ) );
138 GPUd() inline float AliHLTTPCCAMath::Max( float x, float y )
140 return choice( fmaxf( x, y ), ( x > y ? x : y ), TMath::Max( x, y ) );
143 GPUd() inline int AliHLTTPCCAMath::Min( int x, int y )
145 return choice( min( x, y ), ( x < y ? x : y ), TMath::Min( x, y ) );
148 GPUd() inline int AliHLTTPCCAMath::Max( int x, int y )
150 return choice( max( x, y ), ( x > y ? x : y ), TMath::Max( x, y ) );
153 GPUd() inline float AliHLTTPCCAMath::Sqrt( float x )
155 return choice( sqrtf( x ), sqrt( x ), TMath::Sqrt( x ) );
158 GPUd() inline float AliHLTTPCCAMath::Abs( float x )
160 return choice( fabsf( x ), fabs( x ), TMath::Abs( x ) );
163 GPUd() inline double AliHLTTPCCAMath::Abs( double x )
165 return choice( fabs( x ), fabs( x ), TMath::Abs( x ) );
168 GPUd() inline int AliHLTTPCCAMath::Abs( int x )
170 return choice( abs( x ), ( x >= 0 ? x : -x ), TMath::Abs( x ) );
173 GPUd() inline float AliHLTTPCCAMath::ASin( float x )
175 return choice( asinf( x ), asin( x ), TMath::ASin( x ) );
179 GPUd() inline int AliHLTTPCCAMath::Mul24( int a, int b )
181 return choice( __mul24( a, b ), a*b, a*b );
184 GPUd() inline float AliHLTTPCCAMath::FMulRZ( float a, float b )
186 return choice( __fmul_rz( a, b ), a*b, a*b );
190 GPUd() inline int AliHLTTPCCAMath::AtomicExch( int *addr, int val )
192 #if defined( HLTCA_GPUCODE )
193 return ::atomicExch( addr, val );
201 GPUd() inline int AliHLTTPCCAMath::AtomicAdd ( int *addr, int val )
203 #if defined( HLTCA_GPUCODE )
204 return ::atomicAdd( addr, val );
212 GPUd() inline int AliHLTTPCCAMath::AtomicMax ( int *addr, int val )
214 #if defined( HLTCA_GPUCODE )
215 return ::atomicMax( addr, val );
218 if ( *addr < val ) *addr = val;
223 GPUd() inline int AliHLTTPCCAMath::AtomicMin ( int *addr, int val )
225 #if defined( HLTCA_GPUCODE )
226 return ::atomicMin( addr, val );
229 if ( *addr > val ) *addr = val;