-
Notifications
You must be signed in to change notification settings - Fork 18
/
asin.h
230 lines (184 loc) · 5.94 KB
/
asin.h
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* aasin.h
* The basic idea is to exploit Pade' polynomials.
* A lot of ideas were inspired by the cephes math library (by Stephen L. Moshier
* moshier@na-net.ornl.gov) as well as actual code.
* The Cephes library can be found here: http://www.netlib.org/cephes/
*
* Created on: Jun 23, 2012
* Author: Danilo Piparo, Thomas Hauth, Vincenzo Innocente
*/
/*
* VDT is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser Public License for more details.
*
* You should have received a copy of the GNU Lesser Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ASIN_H_
#define ASIN_H_
#include "vdtcore_common.h"
namespace vdt{
namespace details{
const double RX1asin = 2.967721961301243206100E-3;
const double RX2asin = -5.634242780008963776856E-1;
const double RX3asin = 6.968710824104713396794E0;
const double RX4asin = -2.556901049652824852289E1;
const double RX5asin = 2.853665548261061424989E1;
const double SX1asin = -2.194779531642920639778E1;
const double SX2asin = 1.470656354026814941758E2;
const double SX3asin = -3.838770957603691357202E2;
const double SX4asin = 3.424398657913078477438E2;
const double PX1asin = 4.253011369004428248960E-3;
const double PX2asin = -6.019598008014123785661E-1;
const double PX3asin = 5.444622390564711410273E0;
const double PX4asin = -1.626247967210700244449E1;
const double PX5asin = 1.956261983317594739197E1;
const double PX6asin = -8.198089802484824371615E0;
const double QX1asin = -1.474091372988853791896E1;
const double QX2asin = 7.049610280856842141659E1;
const double QX3asin = -1.471791292232726029859E2;
const double QX4asin = 1.395105614657485689735E2;
const double QX5asin = -4.918853881490881290097E1;
inline double getRX(const double x){
double rx = RX1asin;
rx*= x;
rx+= RX2asin;
rx*= x;
rx+= RX3asin;
rx*= x;
rx+= RX4asin;
rx*= x;
rx+= RX5asin;
return rx;
}
inline double getSX(const double x){
double sx = x;
sx+= SX1asin;
sx*= x;
sx+= SX2asin;
sx*= x;
sx+= SX3asin;
sx*= x;
sx+= SX4asin;
return sx;
}
inline double getPX(const double x){
double px = PX1asin;
px*= x;
px+= PX2asin;
px*= x;
px+= PX3asin;
px*= x;
px+= PX4asin;
px*= x;
px+= PX5asin;
px*= x;
px+= PX6asin;
return px;
}
inline double getQX(const double x){
double qx = x;
qx+= QX1asin;
qx*= x;
qx+= QX2asin;
qx*= x;
qx+= QX3asin;
qx*= x;
qx+= QX4asin;
qx*= x;
qx+= QX5asin;
return qx;
}
}
}
namespace vdt{
// asin double precision --------------------------------------------------------
/// Double Precision asin
inline double fast_asin(double x){
const uint64_t sign_mask = details::getSignMask(x);
x = std::fabs(x);
const double a = x;
double zz = 1.0 - a;
double px = details::getRX(zz);
double qx = details::getSX(zz);
const double p = zz * px/qx;
zz = std::sqrt(zz+zz);
double z = details::PIO4 - zz;
zz = zz * p - details::MOREBITS;
z -= zz;
z += details::PIO4;
if( a < 0.625 ){
zz = a * a;
px = details::getPX(zz);
qx = details::getQX(zz);
z = zz*px/qx;
z = a * z + a;
}
// Linear approx, not sooo needed but seable. Price is cheap though
double res = a < 1e-8? a : z ;
// Restore Sign
return details::dpORuint64(res,sign_mask);
}
//------------------------------------------------------------------------------
/// Single Precision asin
inline float fast_asinf(float x){
uint32_t flag=0;
const uint32_t sign_mask = details::getSignMask(x);
const float a = std::fabs(x);
float z;
if( a > 0.5f )
{
z = 0.5f * (1.0f - a);
x = sqrtf( z );
flag = 1;
}
else
{
x = a;
z = x * x;
}
z = (((( 4.2163199048E-2f * z
+ 2.4181311049E-2f) * z
+ 4.5470025998E-2f) * z
+ 7.4953002686E-2f) * z
+ 1.6666752422E-1f) * z * x
+ x;
// if( flag != 0 )
// {
// z = z + z;
// z = PIO2F - z;
// }
// No branch with the two coefficients
float tmp = z + z;
tmp = details::PIO2F - tmp;
// Linear approx, not sooo needed but seable. Price is cheap though
float res = a < 1e-4f? a : tmp * flag + (1-flag) * z ;
// Restore Sign
return details::spORuint32(res,sign_mask);
return( z );
}
//------------------------------------------------------------------------------
// The cos is in this file as well
inline double fast_acos( double x ){return details::PIO2 - fast_asin(x);}
//------------------------------------------------------------------------------
inline float fast_acosf( float x ){return details::PIO2F - fast_asinf(x);}
//------------------------------------------------------------------------------
// Vector signatures
void asinv(const uint32_t size, double const * __restrict__ iarray, double* __restrict__ oarray);
void fast_asinv(const uint32_t size, double const * __restrict__ iarray, double* __restrict__ oarray);
void asinfv(const uint32_t size, float const * __restrict__ iarray, float* __restrict__ oarray);
void fast_asinfv(const uint32_t size, float const * __restrict__ iarray, float* __restrict__ oarray);
void acosv(const uint32_t size, double const * __restrict__ iarray, double* __restrict__ oarray);
void fast_acosv(const uint32_t size, double const * __restrict__ iarray, double* __restrict__ oarray);
void acosfv(const uint32_t size, float const * __restrict__ iarray, float* __restrict__ oarray);
void fast_acosfv(const uint32_t size, float const * __restrict__ iarray, float* __restrict__ oarray);
} //vdt namespace
#endif /* ASIN_H_ */