-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfast-sin.cpp
More file actions
152 lines (133 loc) · 3.88 KB
/
fast-sin.cpp
File metadata and controls
152 lines (133 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <math.h>
#include <stdio.h>
#include "utils.h"
#include "intrin-wrapper.h"
// Headers for intrinsics
#ifdef __SSE__
#include <xmmintrin.h>
#endif
#ifdef __SSE2__
#include <emmintrin.h>
#endif
#ifdef __AVX__
#include <immintrin.h>
#endif
// coefficients in the Taylor series expansion of sin(x)
static constexpr double c3 = -1/(((double)2)*3);
static constexpr double c5 = 1/(((double)2)*3*4*5);
static constexpr double c7 = -1/(((double)2)*3*4*5*6*7);
static constexpr double c9 = 1/(((double)2)*3*4*5*6*7*8*9);
static constexpr double c11 = -1/(((double)2)*3*4*5*6*7*8*9*10*11);
// sin(x) = x + c3*x^3 + c5*x^5 + c7*x^7 + x9*x^9 + c11*x^11
void sin4_reference(double* sinx, const double* x) {
for (long i = 0; i < 4; i++) sinx[i] = sin(x[i]);
}
void sin4_taylor(double* sinx, const double* x) {
for (int i = 0; i < 4; i++) {
double x1 = x[i];
double x2 = x1 * x1;
double x3 = x1 * x2;
double x5 = x3 * x2;
double x7 = x5 * x2;
double x9 = x7 * x2;
double x11 = x9 * x2;
double s = x1;
s += x3 * c3;
s += x5 * c5;
s += x7 * c7;
s += x9 * c9;
s += x11 * c11;
sinx[i] = s;
}
}
void sin4_intrin(double* sinx, const double* x) {
// The definition of intrinsic functions can be found at:
// https://software.intel.com/sites/landingpage/IntrinsicsGuide/#
#if defined(__AVX__)
__m256d x1, x2, x3;
x1 = _mm256_load_pd(x);
x2 = _mm256_mul_pd(x1, x1);
x3 = _mm256_mul_pd(x1, x2);
__m256d s = x1;
s = _mm256_add_pd(s, _mm256_mul_pd(x3 , _mm256_set1_pd(c3 )));
_mm256_store_pd(sinx, s);
#elif defined(__SSE2__)
constexpr int sse_length = 2;
for (int i = 0; i < 4; i+=sse_length) {
__m128d x1, x2, x3;
x1 = _mm_load_pd(x+i);
x2 = _mm_mul_pd(x1, x1);
x3 = _mm_mul_pd(x1, x2);
__m128 s = x1;
s = _mm_add_pd(s, _mm_mul_pd(x3 , _mm_set1_pd(c3 )));
_mm_store_pd(sinx+i, s);
}
#else
sin4_reference(sinx, x);
#endif
}
void sin4_vector(double* sinx, const double* x) {
// The Vec class is defined in the file intrin-wrapper.h
typedef Vec<double,4> Vec4;
Vec4 x1, x2, x3;
x1 = Vec4::LoadAligned(x);
x2 = x1 * x1;
x3 = x1 * x2;
Vec4 s = x1;
s += x3 * c3 ;
s.StoreAligned(sinx);
}
double err(double* x, double* y, long N) {
double error = 0;
for (long i = 0; i < N; i++) error = std::max(error, fabs(x[i]-y[i]));
return error;
}
int main() {
Timer tt;
long N = 1000000;
double* x = (double*) aligned_malloc(N*sizeof(double));
double* sinx_ref = (double*) aligned_malloc(N*sizeof(double));
double* sinx_taylor = (double*) aligned_malloc(N*sizeof(double));
double* sinx_intrin = (double*) aligned_malloc(N*sizeof(double));
double* sinx_vector = (double*) aligned_malloc(N*sizeof(double));
for (long i = 0; i < N; i++) {
x[i] = (drand48()-0.5) * M_PI/2; // [-pi/4,pi/4]
sinx_ref[i] = 0;
sinx_taylor[i] = 0;
sinx_intrin[i] = 0;
sinx_vector[i] = 0;
}
tt.tic();
for (long rep = 0; rep < 1000; rep++) {
for (long i = 0; i < N; i+=4) {
sin4_reference(sinx_ref+i, x+i);
}
}
printf("Reference time: %6.4f\n", tt.toc());
tt.tic();
for (long rep = 0; rep < 1000; rep++) {
for (long i = 0; i < N; i+=4) {
sin4_taylor(sinx_taylor+i, x+i);
}
}
printf("Taylor time: %6.4f Error: %e\n", tt.toc(), err(sinx_ref, sinx_taylor, N));
tt.tic();
for (long rep = 0; rep < 1000; rep++) {
for (long i = 0; i < N; i+=4) {
sin4_intrin(sinx_intrin+i, x+i);
}
}
printf("Intrin time: %6.4f Error: %e\n", tt.toc(), err(sinx_ref, sinx_intrin, N));
tt.tic();
for (long rep = 0; rep < 1000; rep++) {
for (long i = 0; i < N; i+=4) {
sin4_vector(sinx_vector+i, x+i);
}
}
printf("Vector time: %6.4f Error: %e\n", tt.toc(), err(sinx_ref, sinx_vector, N));
aligned_free(x);
aligned_free(sinx_ref);
aligned_free(sinx_taylor);
aligned_free(sinx_intrin);
aligned_free(sinx_vector);
}