32 #ifndef VSMC_MATH_VMATH_HPP 33 #define VSMC_MATH_VMATH_HPP 40 #elif VSMC_USE_ACCELERATE 41 #include <Accelerate/Accelerate.h> 46 #define VSMC_DEFINE_MATH_VMATH_VML_1(func, name) \ 47 inline void name(std::size_t n, const float *a, float *y) \ 49 internal::size_check<MKL_INT>(n, #name); \ 50 ::vs##func(static_cast<MKL_INT>(n), a, y); \ 52 inline void name(std::size_t n, const double *a, double *y) \ 54 internal::size_check<MKL_INT>(n, #name); \ 55 ::vd##func(static_cast<MKL_INT>(n), a, y); \ 58 #define VSMC_DEFINE_MATH_VMATH_VML_2(func, name) \ 59 inline void name(std::size_t n, const float *a, const float *b, float *y) \ 61 internal::size_check<MKL_INT>(n, #name); \ 62 ::vs##func(static_cast<MKL_INT>(n), a, b, y); \ 65 std::size_t n, const double *a, const double *b, double *y) \ 67 internal::size_check<MKL_INT>(n, #name); \ 68 ::vd##func(static_cast<MKL_INT>(n), a, b, y); \ 80 float beta_a,
float beta_b,
float mu_a,
float mu_b,
float *y)
82 internal::size_check<MKL_INT>(n,
"linear_frac");
84 static_cast<MKL_INT>(n), a, b, beta_a, beta_b, mu_a, mu_b, y);
86 inline void linear_frac(std::size_t n,
const double *a,
const double *b,
87 double beta_a,
double beta_b,
double mu_a,
double mu_b,
double *y)
89 internal::size_check<MKL_INT>(n,
"linear_frac");
91 static_cast<MKL_INT>(n), a, b, beta_a, beta_b, mu_a, mu_b, y);
103 inline
void pow(
std::
size_t n, const
float *a,
float b,
float *y)
105 internal::size_check<MKL_INT>(n,
"pow");
106 ::vsPowx(static_cast<MKL_INT>(n), a, b, y);
108 inline void pow(std::size_t n,
const double *a,
double b,
double *y)
110 internal::size_check<MKL_INT>(n,
"pow");
111 ::vdPowx(static_cast<MKL_INT>(n), a, b, y);
123 inline
void sincos(
std::
size_t n, const
float *a,
float *y,
float *z)
125 internal::size_check<MKL_INT>(n,
"sincos");
126 ::vsSinCos(static_cast<MKL_INT>(n), a, y, z);
128 inline void sincos(std::size_t n,
const double *a,
double *y,
double *z)
130 internal::size_check<MKL_INT>(n,
"sincos");
131 ::vdSinCos(static_cast<MKL_INT>(n), a, y, z);
159 inline
void modf(
std::
size_t n, const
float *a,
float *y,
float *z)
161 internal::size_check<MKL_INT>(n,
"modf");
162 ::vsModf(static_cast<MKL_INT>(n), a, y, z);
164 inline void modf(std::size_t n,
const double *a,
double *y,
double *z)
166 internal::size_check<MKL_INT>(n,
"modf");
167 ::vdModf(static_cast<MKL_INT>(n), a, y, z);
172 #elif VSMC_USE_ACCELERATE 174 #define VSMC_DEFINE_MATH_VMATH_VFORCE_1(func, name) \ 175 inline void name(std::size_t n, const float *a, float *y) \ 177 internal::size_check<int>(n, #name); \ 178 int m = static_cast<int>(n); \ 179 ::vv##func##f(y, a, &m); \ 181 inline void name(std::size_t n, const double *a, double *y) \ 183 internal::size_check<int>(n, #name); \ 184 int m = static_cast<int>(n); \ 185 ::vv##func(y, a, &m); \ 188 #define VSMC_DEFINE_MATH_VMATH_VFORCE_2(func, name) \ 189 inline void name(std::size_t n, const float *a, const float *b, float *y) \ 191 internal::size_check<int>(n, #name); \ 192 int m = static_cast<int>(n); \ 193 ::vv##func##f(y, a, b, &m); \ 196 std::size_t n, const double *a, const double *b, double *y) \ 198 internal::size_check<int>(n, #name); \ 199 int m = static_cast<int>(n); \ 200 ::vv##func(y, a, b, &m); \ 206 VSMC_DEFINE_MATH_VMATH_VFORCE_1(fabs,
abs)
208 VSMC_DEFINE_MATH_VMATH_VFORCE_2(
div,
div)
209 VSMC_DEFINE_MATH_VMATH_VFORCE_1(
sqrt,
sqrt)
210 VSMC_DEFINE_MATH_VMATH_VFORCE_1(
cbrt,
cbrt)
211 VSMC_DEFINE_MATH_VMATH_VFORCE_2(
pow,
pow)
213 VSMC_DEFINE_MATH_VMATH_VFORCE_1(
exp,
exp)
215 VSMC_DEFINE_MATH_VMATH_VFORCE_1(
log,
log)
219 VSMC_DEFINE_MATH_VMATH_VFORCE_1(
cos,
cos)
220 VSMC_DEFINE_MATH_VMATH_VFORCE_1(
sin,
sin)
221 inline void sincos(std::size_t n,
const float *a,
float *y,
float *z)
223 internal::size_check<int>(n,
"sincos");
224 const int m =
static_cast<int>(n);
225 ::vvsincosf(y, z, a, &m);
227 inline void sincos(std::size_t n,
const double *a,
double *y,
double *z)
229 internal::size_check<int>(n,
"sincos");
230 const int m =
static_cast<int>(n);
231 ::vvsincos(y, z, a, &m);
233 VSMC_DEFINE_MATH_VMATH_VFORCE_1(
tan,
tan)
234 VSMC_DEFINE_MATH_VMATH_VFORCE_1(
acos,
acos)
235 VSMC_DEFINE_MATH_VMATH_VFORCE_1(
asin,
asin)
236 VSMC_DEFINE_MATH_VMATH_VFORCE_1(
atan,
atan)
239 VSMC_DEFINE_MATH_VMATH_VFORCE_1(
cosh,
cosh)
240 VSMC_DEFINE_MATH_VMATH_VFORCE_1(
sinh,
sinh)
241 VSMC_DEFINE_MATH_VMATH_VFORCE_1(
tanh,
tanh)
247 VSMC_DEFINE_MATH_VMATH_VFORCE_1(
ceil,
ceil)
251 #endif // VSMC_USE_MKL_VML 253 #define VSMC_DEFINE_MATH_VMATH_1(func, name) \ 254 template <typename T> \ 255 inline void name(std::size_t n, const T *a, T *y) \ 257 for (std::size_t i = 0; i != n; ++i) \ 261 #define VSMC_DEFINE_MATH_VMATH_2(func, name) \ 262 template <typename T> \ 263 inline void name(std::size_t n, const T *a, const T *b, T *y) \ 265 for (std::size_t i = 0; i != n; ++i) \ 266 y[i] = func(a[i], b[i]); \ 269 #define VSMC_DEFINE_MATH_VMATH_B(op, name) \ 270 template <typename T> \ 271 inline void name(std::size_t n, const T *a, const T *b, T *y) \ 273 for (std::size_t i = 0; i != n; ++i) \ 274 y[i] = a[i] op b[i]; \ 277 #define VSMC_DEFINE_MATH_VMATH_VS(op, name) \ 278 template <typename T> \ 279 inline void name(std::size_t n, const T *a, T b, T *y) \ 281 for (std::size_t i = 0; i != n; ++i) \ 285 #define VSMC_DEFINE_MATH_VMATH_SV(op, name) \ 286 template <typename T> \ 287 inline void name(std::size_t n, T a, const T *b, T *y) \ 289 for (std::size_t i = 0; i != n; ++i) \ 313 VSMC_DEFINE_MATH_VMATH_VS(-,
sub)
319 template <typename T>
320 inline
void sqr(
std::
size_t n, const T *a, T *y)
322 for (std::size_t i = 0; i != n; ++i)
340 template <typename T>
342 T beta_b, T mu_a, T mu_b, T *y)
345 const std::size_t m = n / k;
346 const std::size_t l = n % k;
347 for (std::size_t i = 0; i != m; ++i, a += k, y += k) {
348 for (std::size_t j = 0; j != k; ++j)
349 y[j] = beta_a * a[j] + mu_a;
350 for (std::size_t j = 0; j != k; ++j)
351 y[j] /= beta_b * b[j] + mu_b;
353 for (std::size_t i = 0; i != l; ++i)
354 y[i] = beta_a * a[i] + mu_a;
355 for (std::size_t i = 0; i != l; ++i)
356 y[i] /= beta_b * b[i] + mu_b;
360 template <
typename T>
361 inline void fma(std::size_t n,
const T *a,
const T *b,
const T *c, T *y)
363 for (std::size_t i = 0; i != n; ++i)
364 y[i] = a[i] * b[i] + c[i];
368 template <
typename T>
369 inline void fma(std::size_t n,
const T *a,
const T *b, T c, T *y)
374 for (std::size_t i = 0; i != n; ++i)
375 y[i] = a[i] * b[i] + c;
380 template <
typename T>
381 inline void fma(std::size_t n,
const T *a, T b,
const T *c, T *y)
386 for (std::size_t i = 0; i != n; ++i)
387 y[i] = a[i] * b + c[i];
392 template <
typename T>
393 inline void fma(std::size_t n,
const T *a, T b, T c, T *y)
396 std::copy_n(a, n, y);
402 for (std::size_t i = 0; i != n; ++i)
408 template <
typename T>
409 inline void fma(std::size_t n, T a,
const T *b,
const T *c, T *y)
414 for (std::size_t i = 0; i != n; ++i)
415 y[i] = a * b[i] + c[i];
420 template <
typename T>
421 inline void fma(std::size_t n, T a,
const T *b, T c, T *y)
426 for (std::size_t i = 0; i != n; ++i)
432 template <
typename T>
433 inline void fma(std::size_t n, T a, T b,
const T *c, T *y)
445 template <
typename T>
446 inline void inv(std::size_t n,
const T *a, T *y)
448 for (std::size_t i = 0; i != n; ++i)
449 y[i] = static_cast<T>(1) / a[i];
465 template <typename T>
469 const std::size_t m = n / k;
470 const std::size_t l = n % k;
471 for (std::size_t i = 0; i != m; ++i, a += k, y += k) {
482 template <typename T>
487 const std::size_t m = n / k;
488 const std::size_t l = n % k;
489 for (std::size_t i = 0; i != m; ++i, a += k, y += k) {
498 template <
typename T>
499 inline void pow2o3(std::size_t n,
const T *a, T *y)
502 const std::size_t m = n / k;
503 const std::size_t l = n % k;
504 for (std::size_t i = 0; i != m; ++i, a += k, y += k) {
513 template <
typename T>
514 inline void pow3o2(std::size_t n,
const T *a, T *y)
517 const std::size_t m = n / k;
518 const std::size_t l = n % k;
519 for (std::size_t i = 0; i != m; ++i, a += k, y += k) {
521 for (std::size_t j = 0; j != k; ++j)
522 y[j] = y[j] * y[j] * y[j];
525 for (std::size_t i = 0; i != l; ++i)
526 y[i] = y[i] * y[i] * y[i];
532 template <typename T>
534 inline
void pow(
std::
size_t n, const T *a, T b, T *y)
536 for (std::size_t i = 0; i != n; ++i)
556 template <typename T>
560 const std::size_t m = n / k;
561 const std::size_t l = n % k;
562 for (std::size_t i = 0; i != m; ++i, a += k, y += k) {
563 mul(k, const_ln_10<T>(), a, y);
566 mul(l, const_ln_10<T>(), a, y);
599 template <typename T>
600 inline
void sincos(
std::
size_t n, const T *a, T *y, T *z)
603 const std::size_t m = n / k;
604 const std::size_t l = n % k;
605 for (std::size_t i = 0; i != m; ++i, a += k, y += k, z += k) {
668 template <typename T>
672 const std::size_t m = n / k;
673 const std::size_t l = n % k;
674 for (std::size_t i = 0; i != m; ++i, a += k, y += k) {
675 mul(k, static_cast<T>(1) / const_sqrt_2<T>(), a, y);
677 fma(k, static_cast<T>(0.5), static_cast<T>(0.5), y, y);
679 mul(l, static_cast<T>(1) / const_sqrt_2<T>(), a, y);
681 fma(l, static_cast<T>(0.5), static_cast<T>(0.5), y, y);
704 template <typename T>
705 inline
void modf(
std::
size_t n, const T *a, T *y, T *z)
707 for (std::size_t i = 0; i != n; ++i, ++a, ++y, ++z)
715 #endif // VSMC_MATH_VMATH_HPP void cdfnorm(std::size_t n, const float *a, float *y)
void trunc(std::size_t n, const float *a, float *y)
void erfcinv(std::size_t n, const float *a, float *y)
void cbrt(std::size_t n, const T *a, T *y)
For , compute .
#define VSMC_DEFINE_MATH_VMATH_1(func, name)
void mul(std::size_t n, const float *a, const float *b, float *y)
void log1p(std::size_t n, const float *a, float *y)
void acosh(std::size_t n, const float *a, float *y)
void sqrt(std::size_t n, const float *a, float *y)
void modf(std::size_t n, const T *a, T *y, T *z)
For , compute integeral and fraction parts.
void sinh(std::size_t n, const float *a, float *y)
void invsqrt(std::size_t n, const float *a, float *y)
void pow3o2(std::size_t n, const float *a, float *y)
void pow2o3(std::size_t n, const float *a, float *y)
void asin(std::size_t n, const float *a, float *y)
void tan(std::size_t n, const T *a, T *y)
For , compute .
void cdfnorminv(std::size_t n, const float *a, float *y)
void exp2(std::size_t n, const T *a, T *y)
For , compute .
void log2(std::size_t n, const T *a, T *y)
For , compute .
void tanh(std::size_t n, const float *a, float *y)
void erfinv(std::size_t n, const float *a, float *y)
void linear_frac(std::size_t n, const float *a, const float *b, float beta_a, float beta_b, float mu_a, float mu_b, float *y)
#define VSMC_DEFINE_MATH_VMATH_2(func, name)
#define VSMC_DEFINE_MATH_VMATH_SV(op, name)
void invcbrt(std::size_t n, const float *a, float *y)
void erf(std::size_t n, const float *a, float *y)
void expm1(std::size_t n, const float *a, float *y)
void pow(std::size_t n, const float *a, const float *b, float *y)
void cos(std::size_t n, const float *a, float *y)
void lgamma(std::size_t n, const float *a, float *y)
void exp(std::size_t n, const float *a, float *y)
void atanh(std::size_t n, const float *a, float *y)
void inv(std::size_t n, const float *a, float *y)
void asinh(std::size_t n, const float *a, float *y)
#define VSMC_DEFINE_MATH_VMATH_VML_2(func, name)
void atan2(std::size_t n, const float *a, const float *b, float *y)
void ceil(std::size_t n, const float *a, float *y)
void round(std::size_t n, const float *a, float *y)
void div(std::size_t n, const float *a, const float *b, float *y)
void sub(std::size_t n, const float *a, const float *b, float *y)
void fma(std::size_t n, const T *a, const T *b, const T *c, T *y)
For , compute .
void tgamma(std::size_t n, const T *a, T *y)
For , compute .
void sincos(std::size_t n, const float *a, float *y, float *z)
void sin(std::size_t n, const float *a, float *y)
void cosh(std::size_t n, const float *a, float *y)
void acos(std::size_t n, const float *a, float *y)
void add(std::size_t n, const float *a, const float *b, float *y)
void tgamm(std::size_t n, const float *a, float *y)
void atan(std::size_t n, const float *a, float *y)
#define VSMC_DEFINE_MATH_VMATH_VS(op, name)
void exp10(std::size_t n, const T *a, T *y)
For , compute .
void pow(std::size_t n, const T *a, T b, T *y)
For , compute .
void erfc(std::size_t n, const float *a, float *y)
void tan(std::size_t n, const float *a, float *y)
void hypot(std::size_t n, const T *a, const T *b, T *y)
For , compute .
void hypot(std::size_t n, const float *a, const float *b, float *y)
void cbrt(std::size_t n, const float *a, float *y)
void floor(std::size_t n, const float *a, float *y)
#define VSMC_DEFINE_MATH_VMATH_VML_1(func, name)
#define VSMC_DEFINE_MATH_VMATH_B(op, name)
void log(std::size_t n, const float *a, float *y)
void modf(std::size_t n, const float *a, float *y, float *z)
void lgamma(std::size_t n, const T *a, T *y)
For , compute .
void sqr(std::size_t n, const float *a, float *y)
void log10(std::size_t n, const float *a, float *y)
void abs(std::size_t n, const float *a, float *y)
void expm1(std::size_t n, const T *a, T *y)
For , compute .