source: sasmodels/sasmodels/kernel_header.c @ 38ce0ab

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 38ce0ab was 38ce0ab, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

spherical_sld: using cephes erf function, OpenCL works with Intel CPU on Mac

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