source: sasmodels/sasmodels/kernel_header.c @ 108e70e

core_shell_microgelsmagnetic_modelticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 108e70e was 108e70e, checked in by Paul Kienzle <pkienzle@…>, 6 years ago

use Iqac/Iqabc? for the new orientation interface but Iqxy for the old

  • Property mode set to 100644
File size: 8.3 KB
RevLine 
[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
[bb4b509]16   // Intel CPU on Mac gives strange values for erf(); on the verified
[e1d6983]17   // platforms (intel, nvidia, amd), the cephes erf() is significantly
18   // faster than that available in the native OpenCL.
[38ce0ab]19   #define NEED_ERF
[1557a1e]20   // OpenCL only has type generic math
[b3796fa]21   #define expf exp
[38ce0ab]22   #ifndef NEED_ERF
23   #  define erff erf
24   #  define erfcf erfc
25   #endif
[1557a1e]26#else // !USE_OPENCL
[b966a96]27// Use SAS_DOUBLE to force the use of double even for float kernels
28#  define SAS_DOUBLE dou ## ble
[2e44ac7]29#  ifdef __cplusplus
30      #include <cstdio>
31      #include <cmath>
32      using namespace std;
33      #if defined(_MSC_VER)
34         #include <limits>
35         #include <float.h>
36         #define kernel extern "C" __declspec( dllexport )
[b966a96]37         inline double trunc(double x) { return x>=0?floor(x):-floor(-x); }
38         inline double fmin(double x, double y) { return x>y ? y : x; }
39         inline double fmax(double x, double y) { return x<y ? y : x; }
40         #define isnan(x) _isnan(x)
41         #define isinf(x) (!_finite(x))
42         #define isfinite(x) _finite(x)
[2e44ac7]43         #define NAN (std::numeric_limits<double>::quiet_NaN()) // non-signalling NaN
[b966a96]44         #define INFINITY (std::numeric_limits<double>::infinity())
[1557a1e]45         #define NEED_ERF
[b966a96]46         #define NEED_EXPM1
47         #define NEED_TGAMMA
[2e44ac7]48     #else
49         #define kernel extern "C"
[5cf3c33]50         #include <cstdint>
[2e44ac7]51     #endif
[b966a96]52     inline void SINCOS(double angle, double &svar, double &cvar) { svar=sin(angle); cvar=cos(angle); }
[1557a1e]53#  else // !__cplusplus
[5cf3c33]54     #include <inttypes.h>  // C99 guarantees that int32_t types is here
[2e44ac7]55     #include <stdio.h>
[b966a96]56     #if defined(__TINYC__)
57         typedef int int32_t;
58         #include <math.h>
[bb4b509]59         // TODO: check isnan is correct
[b966a96]60         inline double _isnan(double x) { return x != x; } // hope this doesn't optimize away!
61         #undef isnan
62         #define isnan(x) _isnan(x)
63         // Defeat the double->float conversion since we don't have tgmath
64         inline SAS_DOUBLE trunc(SAS_DOUBLE x) { return x>=0?floor(x):-floor(-x); }
65         inline SAS_DOUBLE fmin(SAS_DOUBLE x, SAS_DOUBLE y) { return x>y ? y : x; }
66         inline SAS_DOUBLE fmax(SAS_DOUBLE x, SAS_DOUBLE y) { return x<y ? y : x; }
[1557a1e]67         #define NEED_ERF
[b966a96]68         #define NEED_EXPM1
69         #define NEED_TGAMMA
[edf06e1]70         // expf missing from windows?
71         #define expf exp
[b966a96]72     #else
73         #include <tgmath.h> // C99 type-generic math, so sin(float) => sinf
74     #endif
[2e44ac7]75     // MSVC doesn't support C99, so no need for dllexport on C99 branch
76     #define kernel
77     #define SINCOS(angle,svar,cvar) do {const double _t_=angle; svar=sin(_t_);cvar=cos(_t_);} while (0)
[1557a1e]78#  endif  // !__cplusplus
[2e44ac7]79#  define global
80#  define local
81#  define constant const
82// OpenCL powr(a,b) = C99 pow(a,b), b >= 0
83// OpenCL pown(a,b) = C99 pow(a,b), b integer
84#  define powr(a,b) pow(a,b)
85#  define pown(a,b) pow(a,b)
[1557a1e]86#endif // !USE_OPENCL
[2e44ac7]87
[b966a96]88#if defined(NEED_EXPM1)
[8698a0d]89   // TODO: precision is a half digit lower than numpy on mac in [1e-7, 0.5]
90   // Run "explore/precision.py sas_expm1" to see this (may have to fiddle
91   // the xrange for log to see the complete range).
[b966a96]92   static SAS_DOUBLE expm1(SAS_DOUBLE x_in) {
93      double x = (double)x_in;  // go back to float for single precision kernels
94      // Adapted from the cephes math library.
95      // Copyright 1984 - 1992 by Stephen L. Moshier
96      if (x != x || x == 0.0) {
97         return x; // NaN and +/- 0
98      } else if (x < -0.5 || x > 0.5) {
99         return exp(x) - 1.0;
100      } else {
101         const double xsq = x*x;
102         const double p = (((
103            +1.2617719307481059087798E-4)*xsq
104            +3.0299440770744196129956E-2)*xsq
105            +9.9999999999999999991025E-1);
106         const double q = ((((
107            +3.0019850513866445504159E-6)*xsq
108            +2.5244834034968410419224E-3)*xsq
109            +2.2726554820815502876593E-1)*xsq
110            +2.0000000000000000000897E0);
111         double r = x * p;
112         r =  r / (q - r);
113         return r+r;
114       }
115   }
116#endif
117
[2e44ac7]118// Standard mathematical constants:
119//   M_E, M_LOG2E, M_LOG10E, M_LN2, M_LN10, M_PI, M_PI_2=pi/2, M_PI_4=pi/4,
120//   M_1_PI=1/pi, M_2_PI=2/pi, M_2_SQRTPI=2/sqrt(pi), SQRT2, SQRT1_2=sqrt(1/2)
121// OpenCL defines M_constant_F for float constants, and nothing if double
122// is not enabled on the card, which is why these constants may be missing
123#ifndef M_PI
124#  define M_PI 3.141592653589793
125#endif
126#ifndef M_PI_2
127#  define M_PI_2 1.570796326794897
128#endif
129#ifndef M_PI_4
130#  define M_PI_4 0.7853981633974483
131#endif
132#ifndef M_E
133#  define M_E 2.718281828459045091
134#endif
[b3796fa]135#ifndef M_SQRT1_2
136#  define M_SQRT1_2 0.70710678118654746
137#endif
[2e44ac7]138
139// Non-standard function library
140// pi/180, used for converting between degrees and radians
141// 4/3 pi for computing sphere volumes
142// square and cube for computing squares and cubes
143#ifndef M_PI_180
144#  define M_PI_180 0.017453292519943295
145#endif
146#ifndef M_4PI_3
147#  define M_4PI_3 4.18879020478639
148#endif
[b966a96]149inline double square(double x) { return x*x; }
150inline double cube(double x) { return x*x*x; }
[1e7b0db0]151inline double sas_sinx_x(double x) { return x==0 ? 1.0 : sin(x)/x; }
[108e70e]152
153// CRUFT: support old style models with orientation received qx, qy and angles
154
155// To rotate from the canonical position to theta, phi, psi, first rotate by
156// psi about the major axis, oriented along z, which is a rotation in the
157// detector plane xy. Next rotate by theta about the y axis, aligning the major
158// axis in the xz plane. Finally, rotate by phi in the detector plane xy.
159// To compute the scattering, undo these rotations in reverse order:
160//     rotate in xy by -phi, rotate in xz by -theta, rotate in xy by -psi
161// The returned q is the length of the q vector and (xhat, yhat, zhat) is a unit
162// vector in the q direction.
163// To change between counterclockwise and clockwise rotation, change the
164// sign of phi and psi.
165
166#if 1
167//think cos(theta) should be sin(theta) in new coords, RKH 11Jan2017
168#define ORIENT_SYMMETRIC(qx, qy, theta, phi, q, sn, cn) do { \
169    SINCOS(phi*M_PI_180, sn, cn); \
170    q = sqrt(qx*qx + qy*qy); \
171    cn  = (q==0. ? 1.0 : (cn*qx + sn*qy)/q * sin(theta*M_PI_180));  \
172    sn = sqrt(1 - cn*cn); \
173    } while (0)
174#else
175// SasView 3.x definition of orientation
176#define ORIENT_SYMMETRIC(qx, qy, theta, phi, q, sn, cn) do { \
177    SINCOS(theta*M_PI_180, sn, cn); \
178    q = sqrt(qx*qx + qy*qy);\
179    cn = (q==0. ? 1.0 : (cn*cos(phi*M_PI_180)*qx + sn*qy)/q); \
180    sn = sqrt(1 - cn*cn); \
181    } while (0)
182#endif
183
184#if 1
185#define ORIENT_ASYMMETRIC(qx, qy, theta, phi, psi, q, xhat, yhat, zhat) do { \
186    q = sqrt(qx*qx + qy*qy); \
187    const double qxhat = qx/q; \
188    const double qyhat = qy/q; \
189    double sin_theta, cos_theta; \
190    double sin_phi, cos_phi; \
191    double sin_psi, cos_psi; \
192    SINCOS(theta*M_PI_180, sin_theta, cos_theta); \
193    SINCOS(phi*M_PI_180, sin_phi, cos_phi); \
194    SINCOS(psi*M_PI_180, sin_psi, cos_psi); \
195    xhat = qxhat*(-sin_phi*sin_psi + cos_theta*cos_phi*cos_psi) \
196         + qyhat*( cos_phi*sin_psi + cos_theta*sin_phi*cos_psi); \
197    yhat = qxhat*(-sin_phi*cos_psi - cos_theta*cos_phi*sin_psi) \
198         + qyhat*( cos_phi*cos_psi - cos_theta*sin_phi*sin_psi); \
199    zhat = qxhat*(-sin_theta*cos_phi) \
200         + qyhat*(-sin_theta*sin_phi); \
201    } while (0)
202#else
203// SasView 3.x definition of orientation
204#define ORIENT_ASYMMETRIC(qx, qy, theta, phi, psi, q, cos_alpha, cos_mu, cos_nu) do { \
205    q = sqrt(qx*qx + qy*qy); \
206    const double qxhat = qx/q; \
207    const double qyhat = qy/q; \
208    double sin_theta, cos_theta; \
209    double sin_phi, cos_phi; \
210    double sin_psi, cos_psi; \
211    SINCOS(theta*M_PI_180, sin_theta, cos_theta); \
212    SINCOS(phi*M_PI_180, sin_phi, cos_phi); \
213    SINCOS(psi*M_PI_180, sin_psi, cos_psi); \
214    cos_alpha = cos_theta*cos_phi*qxhat + sin_theta*qyhat; \
215    cos_mu = (-sin_theta*cos_psi*cos_phi - sin_psi*sin_phi)*qxhat + cos_theta*cos_psi*qyhat; \
216    cos_nu = (-cos_phi*sin_psi*sin_theta + sin_phi*cos_psi)*qxhat + sin_psi*cos_theta*qyhat; \
217    } while (0)
218#endif
Note: See TracBrowser for help on using the repository browser.