source: sasmodels/sasmodels/kernel_header.c @ b3796fa

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

Allow use of cephes erf() even if erf() is available in the math library

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