-
Notifications
You must be signed in to change notification settings - Fork 1
/
sinf.hpp
169 lines (116 loc) · 4.81 KB
/
sinf.hpp
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#ifndef VXL_SINF_HPP
# define VXL_SINF_HPP
# pragma once
#include <cmath>
#include "vector.hpp"
namespace vxl
{
namespace detail
{
namespace constantsf
{
inline constexpr auto FOPI = 1.27323954473516f;
inline constexpr auto DP1 = 0.78515625f;
inline constexpr auto DP2 = 2.4187564849853515625e-4f;
inline constexpr auto DP3 = 3.77489497744594108e-8f;
}
}
template <unsigned N>
inline constexpr vector<float, N> sin(vector<float, N> xx) noexcept
{
using int_value_type = typename vector_traits<float, N>::int_value_type;
using int_vector_type = typename vector_traits<float, N>::int_vector_type;
auto& x(xx.ref());
auto& xi((int_vector_type&)(x));
auto sign(int_vector_type(x) & cvector<int_value_type, N>(1 << 31));
xi &= cvector<int_value_type, N>(~(1 << 31));
auto j(convert<int_value_type, N>(
cvector<float, N>(detail::constantsf::FOPI) * x));
j += j & cvector<int_value_type, N>(1);
auto y(convert<float, N>(j));
sign ^= (j & cvector<int_value_type, N>(4)) <<
cvector<int_value_type, N>(29);
x = ((x - y * cvector<float, N>(detail::constantsf::DP1)) -
y * cvector<float, N>(detail::constantsf::DP2)) -
y * cvector<float, N>(detail::constantsf::DP3);
auto const z(x * x);
auto const y1(((cvector<float, N>(2.443315711809948e-5f) * z -
cvector<float, N>(1.388731625493765e-3f)) * z +
cvector<float, N>(4.166664568298827e-2f)) * z * z -
cvector<float, N>(.5f) * z + cvector<float, N>(1.f));
auto const y2((((cvector<float, N>(-1.9515295891e-4f) * z +
cvector<float, N>(8.3321608736e-3f)) * z -
cvector<float, N>(1.6666654611e-1f)) * z * x) + x);
auto r(select(y1, y2, int_vector_type(cvector<int_value_type, N>(2) == j)));
(int_vector_type&)(r) ^= sign;
return {r};
}
template <unsigned N>
inline constexpr vector<float, N> cos(vector<float, N> xx) noexcept
{
using int_value_type = typename vector_traits<float, N>::int_value_type;
using int_vector_type = typename vector_traits<float, N>::int_vector_type;
auto& x(xx.ref());
auto& xi((int_vector_type&)(x));
xi &= cvector<int_value_type, N>(~(1 << 31));
auto j(convert<int_value_type, N>(
cvector<float, N>(detail::constantsf::FOPI) * x));
j += j & cvector<int_value_type, N>(1);
auto y(convert<float, N>(j));
auto const sign((~(j - cvector<int_value_type, N>(2)) &
cvector<int_value_type, N>(4)) << cvector<int_value_type, N>(29));
x = ((x - y * cvector<float, N>(detail::constantsf::DP1)) -
y * cvector<float, N>(detail::constantsf::DP2)) -
y * cvector<float, N>(detail::constantsf::DP3);
auto const z(x * x);
auto const y1(((cvector<float, N>(2.443315711809948e-5f) * z -
cvector<float, N>(1.388731625493765e-3f)) * z +
cvector<float, N>(4.166664568298827e-2f)) * z * z -
cvector<float, N>(.5f) * z + cvector<float, N>(1.f));
auto const y2((((cvector<float, N>(-1.9515295891e-4f) * z +
cvector<float, N>(8.3321608736e-3f)) * z -
cvector<float, N>(1.6666654611e-1f)) * z * x) + x);
auto r(select(y2, y1, int_vector_type(cvector<int_value_type, N>(2) == j)));
(int_vector_type&)(r) ^= sign;
return {r};
}
template <unsigned N>
inline constexpr std::pair<vector<float, N>, vector<float, N> >
//__attribute__ ((noinline))
sincos(vector<float, N> xx) noexcept
{
using int_value_type = typename vector_traits<float, N>::int_value_type;
using int_vector_type = typename vector_traits<float, N>::int_vector_type;
auto& x(xx.ref());
auto& xi((int_vector_type&)(x));
auto sign_sin(xi & cvector<int_value_type, N>(1 << 31));
xi &= cvector<int_value_type, N>(~(1 << 31));
auto j(convert<int_value_type, N>(
cvector<float, N>(detail::constantsf::FOPI) * x));
j += j & cvector<int_value_type, N>(1);
auto y(convert<float, N>(j));
sign_sin ^= (j & cvector<int_value_type, N>(4)) <<
cvector<int_value_type, N>(29);
// shift bit at index 2 into 1, if it is set and bit at index 1 is not set
auto const sign_cos((~(j - cvector<int_value_type, N>(2)) &
cvector<int_value_type, N>(4)) << cvector<int_value_type, N>(29));
x = ((x - y * cvector<float, N>(detail::constantsf::DP1)) -
y * cvector<float, N>(detail::constantsf::DP2)) -
y * cvector<float, N>(detail::constantsf::DP3);
auto const z(x * x);
auto const y1(((cvector<float, N>(2.443315711809948e-5f) * z -
cvector<float, N>(1.388731625493765e-3f)) * z +
cvector<float, N>(4.166664568298827e-2f)) * z * z -
cvector<float, N>(.5f) * z + cvector<float, N>(1.f));
auto const y2((((cvector<float, N>(-1.9515295891e-4f) * z +
cvector<float, N>(8.3321608736e-3f)) * z -
cvector<float, N>(1.6666654611e-1f)) * z * x) + x);
int_vector_type const sel(cvector<int_value_type, N>(2) == j);
auto p1(select(y1, y2, sel));
(int_vector_type&)(p1) ^= sign_sin;
auto p2(select(y2, y1, sel));
(int_vector_type&)(p2) ^= sign_cos;
return {{p1}, {p2}};
}
}
#endif // VXL_SINF_HPP