source: sasmodels/sasmodels/kernel_header.c @ 6a0d6aa

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

better line numbers for compiler errors; tinycc support (mostly)

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