source: trunk/HeuristicLab.ExtLibs/HeuristicLab.NativeInterpreter/0.1/NativeInterpreter-0.1/src/vector_operations.h @ 16269

Last change on this file since 16269 was 16269, checked in by bburlacu, 21 months ago

#2958: Add C++ source code

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