[2e44ac7] | 1 | #ifdef __OPENCL_VERSION__ |
---|
| 2 | # define USE_OPENCL |
---|
[03cac08] | 3 | #elif defined(_OPENMP) |
---|
| 4 | # define USE_OPENMP |
---|
[2e44ac7] | 5 | #endif |
---|
| 6 | |
---|
| 7 | // If opencl is not available, then we are compiling a C function |
---|
| 8 | // Note: if using a C++ compiler, then define kernel as extern "C" |
---|
[1557a1e] | 9 | #ifdef USE_OPENCL |
---|
| 10 | typedef int int32_t; |
---|
| 11 | # if defined(USE_SINCOS) |
---|
| 12 | # define SINCOS(angle,svar,cvar) svar=sincos(angle,&cvar) |
---|
| 13 | # else |
---|
| 14 | # define SINCOS(angle,svar,cvar) do {const double _t_=angle; svar=sin(_t_);cvar=cos(_t_);} while (0) |
---|
| 15 | # endif |
---|
| 16 | // OpenCL only has type generic math |
---|
| 17 | #define erff erf |
---|
| 18 | #define erfcf erfc |
---|
| 19 | #else // !USE_OPENCL |
---|
[b966a96] | 20 | // Use SAS_DOUBLE to force the use of double even for float kernels |
---|
| 21 | # define SAS_DOUBLE dou ## ble |
---|
[2e44ac7] | 22 | # ifdef __cplusplus |
---|
| 23 | #include <cstdio> |
---|
| 24 | #include <cmath> |
---|
| 25 | using namespace std; |
---|
| 26 | #if defined(_MSC_VER) |
---|
| 27 | #include <limits> |
---|
| 28 | #include <float.h> |
---|
| 29 | #define kernel extern "C" __declspec( dllexport ) |
---|
[b966a96] | 30 | inline double trunc(double x) { return x>=0?floor(x):-floor(-x); } |
---|
| 31 | inline double fmin(double x, double y) { return x>y ? y : x; } |
---|
| 32 | inline double fmax(double x, double y) { return x<y ? y : x; } |
---|
| 33 | #define isnan(x) _isnan(x) |
---|
| 34 | #define isinf(x) (!_finite(x)) |
---|
| 35 | #define isfinite(x) _finite(x) |
---|
[2e44ac7] | 36 | #define NAN (std::numeric_limits<double>::quiet_NaN()) // non-signalling NaN |
---|
[b966a96] | 37 | #define INFINITY (std::numeric_limits<double>::infinity()) |
---|
[1557a1e] | 38 | #define NEED_ERF |
---|
[b966a96] | 39 | #define NEED_EXPM1 |
---|
| 40 | #define NEED_TGAMMA |
---|
[2e44ac7] | 41 | #else |
---|
| 42 | #define kernel extern "C" |
---|
[5cf3c33] | 43 | #include <cstdint> |
---|
[2e44ac7] | 44 | #endif |
---|
[b966a96] | 45 | inline void SINCOS(double angle, double &svar, double &cvar) { svar=sin(angle); cvar=cos(angle); } |
---|
[1557a1e] | 46 | # else // !__cplusplus |
---|
[5cf3c33] | 47 | #include <inttypes.h> // C99 guarantees that int32_t types is here |
---|
[2e44ac7] | 48 | #include <stdio.h> |
---|
[b966a96] | 49 | #if defined(__TINYC__) |
---|
| 50 | typedef int int32_t; |
---|
| 51 | #include <math.h> |
---|
| 52 | // TODO: test isnan |
---|
| 53 | inline double _isnan(double x) { return x != x; } // hope this doesn't optimize away! |
---|
| 54 | #undef isnan |
---|
| 55 | #define isnan(x) _isnan(x) |
---|
| 56 | // Defeat the double->float conversion since we don't have tgmath |
---|
| 57 | inline SAS_DOUBLE trunc(SAS_DOUBLE x) { return x>=0?floor(x):-floor(-x); } |
---|
| 58 | inline SAS_DOUBLE fmin(SAS_DOUBLE x, SAS_DOUBLE y) { return x>y ? y : x; } |
---|
| 59 | inline SAS_DOUBLE fmax(SAS_DOUBLE x, SAS_DOUBLE y) { return x<y ? y : x; } |
---|
[1557a1e] | 60 | #define NEED_ERF |
---|
[b966a96] | 61 | #define NEED_EXPM1 |
---|
| 62 | #define NEED_TGAMMA |
---|
[edf06e1] | 63 | // expf missing from windows? |
---|
| 64 | #define expf exp |
---|
[b966a96] | 65 | #else |
---|
| 66 | #include <tgmath.h> // C99 type-generic math, so sin(float) => sinf |
---|
| 67 | #endif |
---|
[2e44ac7] | 68 | // MSVC doesn't support C99, so no need for dllexport on C99 branch |
---|
| 69 | #define kernel |
---|
| 70 | #define SINCOS(angle,svar,cvar) do {const double _t_=angle; svar=sin(_t_);cvar=cos(_t_);} while (0) |
---|
[1557a1e] | 71 | # endif // !__cplusplus |
---|
[2e44ac7] | 72 | # define global |
---|
| 73 | # define local |
---|
| 74 | # define constant const |
---|
| 75 | // OpenCL powr(a,b) = C99 pow(a,b), b >= 0 |
---|
| 76 | // OpenCL pown(a,b) = C99 pow(a,b), b integer |
---|
| 77 | # define powr(a,b) pow(a,b) |
---|
| 78 | # define pown(a,b) pow(a,b) |
---|
[1557a1e] | 79 | #endif // !USE_OPENCL |
---|
[2e44ac7] | 80 | |
---|
[b966a96] | 81 | #if defined(NEED_EXPM1) |
---|
| 82 | static SAS_DOUBLE expm1(SAS_DOUBLE x_in) { |
---|
| 83 | double x = (double)x_in; // go back to float for single precision kernels |
---|
| 84 | // Adapted from the cephes math library. |
---|
| 85 | // Copyright 1984 - 1992 by Stephen L. Moshier |
---|
| 86 | if (x != x || x == 0.0) { |
---|
| 87 | return x; // NaN and +/- 0 |
---|
| 88 | } else if (x < -0.5 || x > 0.5) { |
---|
| 89 | return exp(x) - 1.0; |
---|
| 90 | } else { |
---|
| 91 | const double xsq = x*x; |
---|
| 92 | const double p = ((( |
---|
| 93 | +1.2617719307481059087798E-4)*xsq |
---|
| 94 | +3.0299440770744196129956E-2)*xsq |
---|
| 95 | +9.9999999999999999991025E-1); |
---|
| 96 | const double q = (((( |
---|
| 97 | +3.0019850513866445504159E-6)*xsq |
---|
| 98 | +2.5244834034968410419224E-3)*xsq |
---|
| 99 | +2.2726554820815502876593E-1)*xsq |
---|
| 100 | +2.0000000000000000000897E0); |
---|
| 101 | double r = x * p; |
---|
| 102 | r = r / (q - r); |
---|
| 103 | return r+r; |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | #endif |
---|
| 107 | |
---|
[2e44ac7] | 108 | // Standard mathematical constants: |
---|
| 109 | // M_E, M_LOG2E, M_LOG10E, M_LN2, M_LN10, M_PI, M_PI_2=pi/2, M_PI_4=pi/4, |
---|
| 110 | // M_1_PI=1/pi, M_2_PI=2/pi, M_2_SQRTPI=2/sqrt(pi), SQRT2, SQRT1_2=sqrt(1/2) |
---|
| 111 | // OpenCL defines M_constant_F for float constants, and nothing if double |
---|
| 112 | // is not enabled on the card, which is why these constants may be missing |
---|
| 113 | #ifndef M_PI |
---|
| 114 | # define M_PI 3.141592653589793 |
---|
| 115 | #endif |
---|
| 116 | #ifndef M_PI_2 |
---|
| 117 | # define M_PI_2 1.570796326794897 |
---|
| 118 | #endif |
---|
| 119 | #ifndef M_PI_4 |
---|
| 120 | # define M_PI_4 0.7853981633974483 |
---|
| 121 | #endif |
---|
| 122 | #ifndef M_E |
---|
| 123 | # define M_E 2.718281828459045091 |
---|
| 124 | #endif |
---|
| 125 | |
---|
| 126 | // Non-standard function library |
---|
| 127 | // pi/180, used for converting between degrees and radians |
---|
| 128 | // 4/3 pi for computing sphere volumes |
---|
| 129 | // square and cube for computing squares and cubes |
---|
| 130 | #ifndef M_PI_180 |
---|
| 131 | # define M_PI_180 0.017453292519943295 |
---|
| 132 | #endif |
---|
| 133 | #ifndef M_4PI_3 |
---|
| 134 | # define M_4PI_3 4.18879020478639 |
---|
| 135 | #endif |
---|
[b966a96] | 136 | inline double square(double x) { return x*x; } |
---|
| 137 | inline double cube(double x) { return x*x*x; } |
---|
| 138 | inline double sinc(double x) { return x==0 ? 1.0 : sin(x)/x; } |
---|
[2e44ac7] | 139 | |
---|