diff options
Diffstat (limited to 'src/cuda-sim/cuda-math.h')
| -rw-r--r-- | src/cuda-sim/cuda-math.h | 234 |
1 files changed, 231 insertions, 3 deletions
diff --git a/src/cuda-sim/cuda-math.h b/src/cuda-sim/cuda-math.h index 5abf4f3..367563f 100644 --- a/src/cuda-sim/cuda-math.h +++ b/src/cuda-sim/cuda-math.h @@ -47,6 +47,7 @@ namespace cuda_math { #define __attribute__(a) // to remove warnings inside math_functions.h #undef INT_MAX +#if CUDART_VERSION < 3000 // DEVICE_BUILTIN struct int4 { int x, y, z, w; @@ -70,18 +71,245 @@ namespace cuda_math { extern float rsqrtf(float); // CUDA 2.3 beta -#if CUDART_VERSION < 3000 #define CUDA_FLOAT_MATH_FUNCTIONS #include <device_types.h> #define __CUDA_INTERNAL_COMPILATION__ #include <math_functions.h> #undef __CUDA_INTERNAL_COMPILATION__ #undef __attribute__ + +// float to integer conversion +int float2int(float a, enum cudaRoundMode mode) +{ + return __internal_float2uint(a, mode); +} + +// float to unsigned integer conversion +unsigned int float2uint(float a, enum cudaRoundMode mode) +{ + return __internal_float2uint(a, mode); +} + +float __ll2float_rz(long long int a) { + int orig_rnd_mode = fegetround(); + fesetround(FE_TOWARDZERO); + float b = a; + fesetround(orig_rnd_mode); + return b; +} +float __ll2float_ru(long long int a) { + int orig_rnd_mode = fegetround(); + fesetround(FE_UPWARD); + float b = a; + fesetround(orig_rnd_mode); + return b; +} +float __ll2float_rd(long long int a) { + int orig_rnd_mode = fegetround(); + fesetround(FE_DOWNWARD); + float b = a; + fesetround(orig_rnd_mode); + return b; +} + #else + #define CUDA_FLOAT_MATH_FUNCTIONS -#include "/home/taamodt/nvcuda/2.3/cuda/include/device_types.h" +#define __CUDACC__ + +// implementing int to float intrinsics with different rounding modes +#include <device_types.h> +#include <fenv.h> + +// 32-bit integer to float +float __int2float_rn(int a) { + int orig_rnd_mode = fegetround(); + fesetround(FE_TONEAREST); + float b = a; + fesetround(orig_rnd_mode); + return b; +} +float __int2float_rz(int a) { + int orig_rnd_mode = fegetround(); + fesetround(FE_TOWARDZERO); + float b = a; + fesetround(orig_rnd_mode); + return b; +} +float __int2float_ru(int a) { + int orig_rnd_mode = fegetround(); + fesetround(FE_UPWARD); + float b = a; + fesetround(orig_rnd_mode); + return b; +} +float __int2float_rd(int a) { + int orig_rnd_mode = fegetround(); + fesetround(FE_DOWNWARD); + float b = a; + fesetround(orig_rnd_mode); + return b; +} + +// 32-bit unsigned integer to float +float __uint2float_rn(unsigned int a) { + int orig_rnd_mode = fegetround(); + fesetround(FE_TONEAREST); + float b = a; + fesetround(orig_rnd_mode); + return b; +} +float __uint2float_rz(unsigned int a) { + int orig_rnd_mode = fegetround(); + fesetround(FE_TOWARDZERO); + float b = a; + fesetround(orig_rnd_mode); + return b; +} +float __uint2float_ru(unsigned int a) { + int orig_rnd_mode = fegetround(); + fesetround(FE_UPWARD); + float b = a; + fesetround(orig_rnd_mode); + return b; +} +float __uint2float_rd(unsigned int a) { + int orig_rnd_mode = fegetround(); + fesetround(FE_DOWNWARD); + float b = a; + fesetround(orig_rnd_mode); + return b; +} + +// 64-bit integer to float +float __ll2float_rn(long long int a) { + int orig_rnd_mode = fegetround(); + fesetround(FE_TONEAREST); + float b = a; + fesetround(orig_rnd_mode); + return b; +} +float __ll2float_rz(long long int a) { + int orig_rnd_mode = fegetround(); + fesetround(FE_TOWARDZERO); + float b = a; + fesetround(orig_rnd_mode); + return b; +} +float __ll2float_ru(long long int a) { + int orig_rnd_mode = fegetround(); + fesetround(FE_UPWARD); + float b = a; + fesetround(orig_rnd_mode); + return b; +} +float __ll2float_rd(long long int a) { + int orig_rnd_mode = fegetround(); + fesetround(FE_DOWNWARD); + float b = a; + fesetround(orig_rnd_mode); + return b; +} + +// 64-bit unsigned integer to float +float __ull2float_rn(unsigned long long int a) { + int orig_rnd_mode = fegetround(); + fesetround(FE_TONEAREST); + float b = a; + fesetround(orig_rnd_mode); + return b; +} +float __ull2float_rz(unsigned long long int a) { + int orig_rnd_mode = fegetround(); + fesetround(FE_TOWARDZERO); + float b = a; + fesetround(orig_rnd_mode); + return b; +} +float __ull2float_ru(unsigned long long int a) { + int orig_rnd_mode = fegetround(); + fesetround(FE_UPWARD); + float b = a; + fesetround(orig_rnd_mode); + return b; +} +float __ull2float_rd(unsigned long long int a) { + int orig_rnd_mode = fegetround(); + fesetround(FE_DOWNWARD); + float b = a; + fesetround(orig_rnd_mode); + return b; +} + +// float to integer conversion +int float2int(float a, enum cudaRoundMode mode) +{ + int tmp; + switch (mode) { + case cuda_math::cudaRoundZero: tmp = truncf(a); break; + case cuda_math::cudaRoundNearest: tmp = nearbyintf(a); break; + case cuda_math::cudaRoundMinInf: tmp = floorf(a); break; + case cuda_math::cudaRoundPosInf: tmp = ceilf(a); break; + default: abort(); + } + return tmp; +} + +int __internal_float2int(float a, enum cudaRoundMode mode) +{ + return float2int(a, mode); +} + +// float to unsigned integer conversion +unsigned int float2uint(float a, enum cudaRoundMode mode) +{ + unsigned int tmp; + switch (mode) { + case cuda_math::cudaRoundZero: tmp = truncf(a); break; + case cuda_math::cudaRoundNearest: tmp = nearbyintf(a); break; + case cuda_math::cudaRoundMinInf: tmp = floorf(a); break; + case cuda_math::cudaRoundPosInf: tmp = ceilf(a); break; + default: abort(); + } + return tmp; +} + +unsigned int __internal_float2uint(float a, enum cudaRoundMode mode) +{ + return float2uint(a, mode); +} + +// intrinsic for division +float fdividef(float a, float b) +{ + return (a / b); +} + +float __internal_accurate_fdividef(float a, float b) +{ + return fdividef(a, b); +} + +// intrinsic for saturate (clamp values beyond 0 and 1) +float __saturatef(float a) +{ + float b; + if (isnan(a)) b = 0.0f; + else if (a >= 1.0f) b = 1.0f; + else if (a <= 0.0f) b = 0.0f; + else b = a; + return b; +} + +// intrinsic for power +float __powf(float a, float b) +{ + return powf(a, b); +} + +#undef __CUDACC__ #define __CUDA_INTERNAL_COMPILATION__ -#include "/home/taamodt/nvcuda/2.3/cuda/include/math_functions.h" +#include <math_functions.h> #undef __CUDA_INTERNAL_COMPILATION__ #undef __attribute__ #endif |
