source: sasmodels/sasmodels/kernel_header.c @ 5cf3c33

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

be explicit about int32 in C interface

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