-
Notifications
You must be signed in to change notification settings - Fork 14
/
atan-pentium.c
280 lines (217 loc) · 7.03 KB
/
atan-pentium.c
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
*
THIS CODE IS BROKEN. DO NOT USE.
this function computes atan correctly rounded to the nearest,
using experimental techniques based on double-extended arithmetic
It used to work, honest, but changes in the crlibm framework have
broken it. It should be merged some day with atan-itanium, but this is
not high on the agenda.
*
* Author : Nicolas Gast, Florent de Dinechin
*
To have it replace the crlibm atan, do:
gcc -DHAVE_CONFIG_H -I. -fPIC -O2 -c atan-pentium.c; mv atan-pentium.o atan_fast.o; make
*/
#include <stdio.h>
#include <stdlib.h>
#include <crlibm.h>
#include <crlibm_private.h>
#include <double-extended.h>
#define debug 0 /*Warning : turning debugging on seems to change the final result */
#define DEBUG 0
#define NICOLASTEST 0
#ifdef HAVE_FENV_H
#include <fenv.h>
#endif
/* The following seems perfectly harmless */
#ifdef FENV_H
#pragma STDC FENV_ACCESS ON
#endif
#include "atan-pentium.h"
#include <fpu_control.h>
/* Dummy functions to compile OK */
extern double atan_rd(double x) {return 0;}
extern double atan_ru(double x) {return 0;}
extern double atan_rz(double x) {return 0;}
extern double atan_rn(double x) {
db_number x_db;
unsigned int hx;
double sign;
double u;
double comp;
double atanhi, atanlo, atanlo_u;
long double Xred;
long double Xred2;
long double q;
long double atan;
long double eps;
int i;
if(x>=0)
sign = 1;
else
{sign = -1;
x=-x;}
x_db.d = x;
hx = x_db.i[HI] & 0x7FFFFFFF;
/* Filter cases */
if ( hx >= 0x43500000) /* x >= 2^54 */
{
if ( (x_db.i[LO] == 0) && (hx & 0x000fffff) == 0x00080000)
return x+x; /* NaN */
else
return sign*HALFPI.d; /* atan(x) = Pi/2 */
}
else
if ( hx < 0x3E400000 )
{return sign*x;} /* x<2^-27 then atan(x) =~ x */
DOUBLE_EXTENDED_MODE;
if (x > MIN_REDUCTION_NEEDED) /* test if reduction is necessary : */
{
/* 1) Argument reduction : */
/* compute i so that a[i] < x < a[i+1] */
if (x>arctan_table[61][A])
i=61;
else {
i=31;
if (x < arctan_table[i][A]) i-= 16;
else i+=16;
if (x < arctan_table[i][A]) i-= 8;
else i+= 8;
if (x < arctan_table[i][A]) i-= 4;
else i+= 4;
if (x < arctan_table[i][A]) i-= 2;
else i+= 2;
if (x < arctan_table[i][A]) i-= 1;
else i+= 1;
if (x < arctan_table[i][A]) i-= 1;
}
Xred = (x - arctan_table[i][B] )/(1.0L + x * arctan_table[i][B] );
Xred2 = Xred*Xred;
/* Polynomial evaluation */
q = Xred2*(coef_poly[0][0]+Xred2*
(coef_poly[1][0]+Xred2*
(coef_poly[2][0]+Xred2*
(coef_poly[3][0]))));
/* reconstruction : atan(x) = atan(b[i]) + atan(x) */
atan = arctan_table[i][ATAN_BHI] + (Xred + q*Xred);
atanhi = (double) atan;
atanlo = atan-atanhi;
}
else
// no reduction needed
{
Xred2 = x*x;
/* Polynomial evaluation */
q = Xred2*(coef_poly[0][0]+Xred2*
(coef_poly[1][0]+Xred2*
(coef_poly[2][0]+Xred2*
(coef_poly[3][0]))));
atan = q*x + x;
}
/* We do not use the macro of double-extended.h because we want to
compute in parallel the rounding test and the product sign*atan */
#if 1
DE_TEST_AND_RETURN_RN(sign*atan, ACCURATE_TO_62_BITS);
#else
{
db_ext_number _z; double _yd, retval;
int _lo;
int _mask=ACCURATE_TO_62_BITS;
_z.d = atan; retval= sign*atan;
_yd = (double) atan;
_lo = _z.i[DE_MANTISSA_LO] &(_mask);
if((_lo!=(0x3ff&(_mask))) && (_lo!= (0x400&(_mask)))) {
BACK_TO_DOUBLE_MODE;
return retval;
}
}
#endif
{
/*Second step, double-double */
long double tmphi, tmplo;
long double x0hi, x0lo;
long double xmBihi, xmBilo;
long double Xredhi, Xredlo;
long double Xred2;
long double qhi,qlo; /* q = polynomial */
long double q;
long double Xred2hi,Xred2lo;
long double atanhi,atanlo;
int j;
#if EVAL_PERF
crlibm_second_step_taken++;
#endif
if (x > MIN_REDUCTION_NEEDED) /* test if reduction is necessary : */
{
/* 1) Argument reduction : */
if (i==61)
{
Add12_ext( &xmBihi , &xmBilo , x , -arctan_table[61][B]);
}
else
{
xmBihi = x-arctan_table[i][B];
xmBilo = 0.0;
}
Mul12_ext(&tmphi,&tmplo, x, arctan_table[i][B]);
if (x > 1)
Add22_ext(&x0hi,&x0lo,tmphi,tmplo, 1.0,0.0);
else {Add22_ext( &x0hi , &x0lo , 1.0,0.0,tmphi,tmplo);}
Div22_ext( Xredhi, Xredlo, xmBihi , xmBilo , x0hi,x0lo);
Xred2 = Xredhi*Xredhi;
Mul22_ext( &Xred2hi,&Xred2lo,Xredhi,Xredlo,Xredhi, Xredlo);
/*poly eval */
q = (coef_poly[4][0]+Xred2*
(coef_poly[5][0]+Xred2*
(coef_poly[6][0]+Xred2*
(coef_poly[7][0]+
(Xred2*coef_poly[8][0])))));
Mul12_ext( &qhi, &qlo, q, Xred2);
for(j=3;j>=0;j--)
{
Add22_ext(&qhi,&qlo, coef_poly[j][0], coef_poly[j][1], qhi,qlo);
Mul22_ext(&qhi,&qlo, qhi,qlo, Xred2hi,Xred2lo);
}
Mul22_ext(&qhi,&qlo, Xredhi,Xredlo, qhi,qlo);
Add22_ext(&qhi,&qlo, Xredhi,Xredlo, qhi,qlo);
/* reconstruction : atan(x) = atan(b[i]) + atan(x) */
Add22_ext(&atanhi,&atanlo, arctan_table[i][ATAN_BHI], arctan_table[i][ATAN_BLO], qhi,qlo);
}
else
// no reduction needed
{
/* Polynomial evaluation */
Mul12_ext( &Xred2hi,&Xred2lo,x,x);
/*poly eval */
q = Xred2hi*(coef_poly[5][0]+Xred2hi*
(coef_poly[6][0]+Xred2hi*
(coef_poly[7][0]+Xred2hi*
(coef_poly[8][0]))));
Add12_ext(&qhi,&qlo, coef_poly[4][0],q);
#if debug
printf(" xred2 = %1.50Le + %1.50Le\n", Xred2hi, Xred2lo);
printf(" qhi+qlo0= %1.50Le + %1.50Le\n",qhi, qlo);
#endif
Mul22_ext(&qhi,&qlo, qhi,qlo, Xred2hi,Xred2lo);
for(j=3;j>=0;j--)
{
Add22_ext(&qhi,&qlo, coef_poly[j][0], coef_poly[j][1], qhi,qlo);
Mul22_ext(&qhi,&qlo, qhi,qlo, Xred2hi,Xred2lo);
}
Mul22_ext (&qhi,&qlo, x,0, qhi,qlo);
#if debug
printf(" qhi+qlo = %1.50Le + %1.50Le\n",qhi, qlo);
#endif
/* The sequence in the TOMS paper */
Add12_ext (&atanhi,&atanlo,x,qhi);
atanlo += qlo;
}
#if debug
printf(" %1.50Le + %1.50Le\n",atanhi, atanlo);
printf(" %1.50Le\n",atanhi + atanlo);
#endif
BACK_TO_DOUBLE_MODE;
return sign*((double) (atanhi+atanlo));
}
}