[16269] | 1 | #ifndef VECTOR_OPERATIONS_H
|
---|
| 2 | #define VECTOR_OPERATIONS_H
|
---|
| 3 |
|
---|
[16274] | 4 | #define _USE_MATH_DEFINES
|
---|
| 5 | #include <cmath>
|
---|
[16269] | 6 | #include <cstring>
|
---|
| 7 |
|
---|
| 8 | #ifdef USE_VDT
|
---|
| 9 | #include "vdt/vdtMath.h"
|
---|
| 10 | #include "vdt/stdwrap.h"
|
---|
| 11 | #define hl_exp vdt::fast_exp
|
---|
| 12 | #define hl_log vdt::fast_log
|
---|
| 13 | #define hl_sin vdt::fast_sin
|
---|
| 14 | #define hl_cos vdt::fast_cos
|
---|
| 15 | #define hl_tan vdt::fast_tan
|
---|
| 16 | #define hl_sqrt vdt::fast_sqrt
|
---|
| 17 | #define hl_pow vdt::fast_pow
|
---|
| 18 | #define hl_round vdt::fast_round
|
---|
| 19 | #else
|
---|
| 20 | #define hl_exp std::exp
|
---|
| 21 | #define hl_log std::log
|
---|
| 22 | #define hl_sin std::sin
|
---|
| 23 | #define hl_cos std::cos
|
---|
| 24 | #define hl_tan std::tan
|
---|
| 25 | #define hl_sqrt std::sqrt
|
---|
| 26 | #define hl_pow std::pow
|
---|
| 27 | #define hl_round std::round
|
---|
| 28 | #endif
|
---|
| 29 |
|
---|
[16274] | 30 | constexpr int BATCHSIZE = 64;
|
---|
[16269] | 31 |
|
---|
[16274] | 32 | #define FOR(i) for(int i = 0; i < BATCHSIZE; ++i)
|
---|
[16269] | 33 |
|
---|
| 34 | // When auto-vectorizing without __restrict,
|
---|
| 35 | // gcc and clang check for overlap (with a bunch of integer code)
|
---|
| 36 | // before running the vectorized loop
|
---|
| 37 |
|
---|
| 38 | // vector - vector operations
|
---|
[16274] | 39 | inline void load(double* __restrict a, double const * __restrict b) noexcept { std::memcpy(a, b, BATCHSIZE * sizeof(double)); }
|
---|
| 40 | inline void add(double* __restrict a, double const * __restrict b) noexcept { FOR(i) a[i] += b[i]; }
|
---|
| 41 | inline void sub(double* __restrict a, double const * __restrict b) noexcept { FOR(i) a[i] -= b[i]; }
|
---|
| 42 | inline void mul(double* __restrict a, double const * __restrict b) noexcept { FOR(i) a[i] *= b[i]; }
|
---|
| 43 | inline void div(double* __restrict a, double const * __restrict b) noexcept { FOR(i) a[i] /= b[i]; }
|
---|
| 44 | inline void exp(double* __restrict a, double const * __restrict b) noexcept { FOR(i) a[i] = hl_exp(b[i]); }
|
---|
| 45 | inline void log(double* __restrict a, double const * __restrict b) noexcept { FOR(i) a[i] = hl_log(b[i]); }
|
---|
| 46 | inline void sin(double* __restrict a, double const * __restrict b) noexcept { FOR(i) a[i] = hl_sin(b[i]); }
|
---|
| 47 | inline void cos(double* __restrict a, double const * __restrict b) noexcept { FOR(i) a[i] = hl_cos(b[i]); }
|
---|
| 48 | inline void tan(double* __restrict a, double const * __restrict b) noexcept { FOR(i) a[i] = hl_tan(b[i]); }
|
---|
| 49 | inline void sqrt(double* __restrict a, double const * __restrict b) noexcept { FOR(i) a[i] = hl_sqrt(b[i]); }
|
---|
| 50 | inline void pow(double* __restrict a, double const * __restrict b) noexcept { FOR(i) a[i] = hl_pow(a[i], hl_round(b[i])); };
|
---|
| 51 | inline void root(double* __restrict a, double const * __restrict b) noexcept { FOR(i) a[i] = hl_pow(a[i], 1. / hl_round(b[i])); };
|
---|
| 52 | inline void square(double* __restrict a, double const * __restrict b) noexcept { FOR(i) a[i] = hl_pow(b[i], 2.); };
|
---|
| 53 | inline void inv(double* __restrict a, double const * __restrict b) noexcept { FOR(i) a[i] = 1. / b[i]; }
|
---|
| 54 | inline void neg(double* __restrict a, double const * __restrict b) noexcept { FOR(i) a[i] = -b[i]; }
|
---|
[16351] | 55 | inline void abs(double* __restrict a, double const * __restrict b) noexcept { FOR(i) a[i] = std::fabs(b[i]); }
|
---|
[16349] | 56 | inline void analytical_quotient(double* __restrict a, double const * __restrict b) noexcept { FOR(i) a[i] /= hl_sqrt(b[i]*b[i] + 1.); }
|
---|
[16269] | 57 |
|
---|
| 58 | // vector - scalar operations
|
---|
| 59 | inline void load(double* __restrict a, double s) noexcept { FOR(i) a[i] = s; }
|
---|
| 60 | inline void add(double* __restrict a, double s) noexcept { FOR(i) a[i] += s; }
|
---|
| 61 | inline void sub(double* __restrict a, double s) noexcept { FOR(i) a[i] -= s; }
|
---|
| 62 | inline void mul(double* __restrict a, double s) noexcept { FOR(i) a[i] *= s; }
|
---|
| 63 | inline void div(double* __restrict a, double s) noexcept { FOR(i) a[i] /= s; }
|
---|
[16349] | 64 | inline void pow(double* __restrict dst, double const * __restrict src, double s) noexcept { FOR(i) dst[i] = hl_pow(src[i], s); }
|
---|
[16269] | 65 |
|
---|
| 66 | // vector operations
|
---|
| 67 | inline void neg(double* __restrict a) noexcept { FOR(i) a[i] = -a[i]; }
|
---|
[16274] | 68 | inline void inv(double* __restrict a) noexcept { FOR(i) a[i] = 1. / a[i]; }
|
---|
[16269] | 69 | inline void exp(double* __restrict a) noexcept { FOR(i) a[i] = hl_exp(a[i]); }
|
---|
| 70 | inline void log(double* __restrict a) noexcept { FOR(i) a[i] = hl_log(a[i]); }
|
---|
| 71 | inline void sin(double* __restrict a) noexcept { FOR(i) a[i] = hl_sin(a[i]); }
|
---|
| 72 | inline void cos(double* __restrict a) noexcept { FOR(i) a[i] = hl_cos(a[i]); }
|
---|
| 73 | inline void round(double* __restrict a) noexcept { FOR(i) a[i] = hl_round(a[i]); }
|
---|
[16274] | 74 | inline void square(double* __restrict a) noexcept { FOR(i) a[i] = hl_pow(a[i], 2.); }
|
---|
[16269] | 75 |
|
---|
| 76 | #undef FOR
|
---|
| 77 | #endif
|
---|