-
Notifications
You must be signed in to change notification settings - Fork 0
/
log-de2.c
280 lines (213 loc) · 7.45 KB
/
log-de2.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 function computes log, correctly rounded,
using experimental techniques based on double-extended arithmetic
THIS IS EXPERIMENTAL SOFTWARE
In particular it changes rounding modes all the time without warning
nor restoring.
*
* Author : Florent de Dinechin
* Florent.de.Dinechin at ens-lyon.fr
*
To have it replace the crlibm log, do:
on pentium,
gcc -DHAVE_CONFIG_H -I. -fPIC -O2 -c log-de2.c; mv log-de2.o log_fast.o; make
on itanium,
icc -I/users/fdedinex/local/IA64/include -mcpu=itanium2\
-Qoption,cpp,--extended_float_types -IPF_fp_speculationsafe -c log-de2.c;\
mv log-de2.o log_fast.o; make
*/
#include <stdio.h>
#include <stdlib.h>
#include "crlibm.h"
#include "crlibm_private.h"
#include "double-extended.h"
#include "log-de2.h"
double log_rn(double x) {
double wi;
#if defined(CRLIBM_TYPECPU_X86) || defined(CRLIBM_TYPECPU_AMD64)
db_number y;
int E, i, index, roundtestmask;
long double r, logirh, logirl, z, z2, z4, t,evenp,oddp, th, tl, eh,el,p1,p2,p3;
#else
long int E, i;
unsigned long int index, roundtestmask;
__fpreg xe, ye, r, logirh, logirl, z, z2, z4, t,evenp,oddp, th, tl, eh,el, c1,c2,c3,c4,c5,c6,p1,p2,p3;
#endif
E=0;
#if defined(CRLIBM_TYPECPU_X86) || defined(CRLIBM_TYPECPU_AMD64)
/* Pentium has few registers, so load coefficients directly from memory */
#define c6 c[0]
#define c5 c[1]
#define c4 c[2]
#define c3 c[3]
#define c2 c[4]
#define c1 c[5]
/* Special case hadling on a x86 */
y.d=x;
/* Filter special cases */
if (y.i[HI] < 0x00100000){ /* x < 2^(-1022) */
if (((y.i[HI] & 0x7fffffff)|y.i[LO])==0){
return -1.0/0.0;
} /* log(+/-0) = -Inf */
if (y.i[HI] < 0){
return (x-x)/0; /* log(-x) = Nan */
}
/* Subnormal number */
E = -64;
y.d *= two64; /* make x a normal number */
}
if (y.i[HI] >= 0x7ff00000){
return x+x; /* Inf or Nan */
}
DOUBLE_EXTENDED_MODE; /* This one should be overlapped with integer computation */
#define X_NEAR_1 (y.i[HI]>MINYFAST) && (y.i[HI]<MAXYFAST)
#else
/* prefetch coefficients */
c6=c[0]; c5=c[1]; c4=c[2]; c3=c[3]; c2=c[4]; c1=c[5];
/* Special case handling on the Itanium */
xe=x;
i = _Asm_getf(2/*_FR_D*/, xe);
/* Filter special cases */
//if (__builtin_expect( (i<0x0010000000000000ULL), (1+1==3))){ /* x < 2^(-1022) */
if (i<0x0010000000000000LL){ /* x < 2^(-1022) */
if ((i & 0x7fffffffffffffffULL)==0){
return -1.0/0.0;
} /* log(+/-0) = -Inf */
if (i<0){
return (x-x)/0; /* log(-x) = Nan */
}
/* Subnormal number */
xe *= two64; /* make x a normal number */
E = -64;
i = _Asm_getf(2/*_FR_D*/, xe); /* and update i */
}
//if (__builtin_expect( (i >= 0x7ff0000000000000ULL), (1+1==3) )){
if (i >= 0x7ff0000000000000ULL){
return x+x; /* Inf or Nan */
}
#define X_NEAR_1 __builtin_expect((i>(((uint64_t) MINYFAST)<<32)) && (i<(((uint64_t) MAXYFAST)<<32)), (1+1==3))
#endif
if(X_NEAR_1) {
roundtestmask=0x7fc;
z = x - 1 ; /* Sterbenz exact */
z2 = z*z;
evenp = c6; /* c6 */
oddp = c5; /* c5 */
evenp = c4 + z2 * evenp; /* c4 */
oddp = c3 + z2 * oddp; /* c3 */
evenp = c2 + z2 * evenp; /* c2 */
t = c1 + (z * evenp + z2 * oddp);
t = z*t;
//printf("z= %1.20e, t=%1.20e \n ", (double)z, (double)t);
}
else {
#if defined(CRLIBM_TYPECPU_X86) || defined(CRLIBM_TYPECPU_AMD64)
/* Extract exponent and mantissa */
E += (y.i[HI]>>20)-1023; /* extract the exponent */
index = (y.i[HI] & 0x000fffff);
y.i[HI] = index | 0x3ff00000; /* do exponent = 0 */
index = index >> (20-L);
/* now y.d holds 1+f, and E is the exponent */
logirh = argredtable[index].h;
r = (long double) (argredtable[index].r); /* approx to 1/y.d */
z = r*(long double)y.d - 1. ; /* even without an FMA, all exact */
if(E>12 || E<-12) { /* faster and less accurate polynomial evaluation */
roundtestmask=0x7fe;
p1 = logirh + z*c1; p2 = c2 + z*c3; p3 = c4 + z*c5; z2 = z*z;
p1 = p1 + z2*p2; p3 = p3 + z2*c6; z4=z2*z2;
t = p1 + z4*p3;
t = t + E*ln2h;
}
else {
roundtestmask=0x7f0;
p1 = c5 + z*c6; z2 = z*z;
p2 = c3 + z*c4; p3 = c1+z*c2;
t = p2 + z2*p1;
t = p3 + z2*t;
t = logirh + z*t;
t = t + E*ln2h;
}
#if 0
if(E>12 || E<-12)
roundtestmask=0x7fe;
else
roundtestmask=0x7f0;
p1 = c5 + z*c6; z2 = z*z;
p2 = c3 + z*c4; p3 = c1+z*c2;
t = p2 + z2*p1;
t = p3 + z2*t;
t = logirh + z*t;
#endif
#else /* Itanium here*/
/* Extract exponent and mantissa */
E += (i>>52)-1023;
//printf("\nE = %llx\n", E);
i = i & 0x000fffffffffffffULL; /* keep only mantissa */
ye = _Asm_setf(2/*_FR_D*/, i | 0x3ff0000000000000ULL ); /* exponent = 0*/
index = i >> (52-L);
//printf("\nindex= %lld\n", index);
//printf("\n ye = %1.20Le\n",(long double)ye);
/* now ye holds 1+f, and E is the exponent */
logirh = argredtable[index].h;
_Asm_frcpa(&r, 1.0L, ye, 1/*_SF1*/);
z = r*ye - 1. ; /* even without an FMA, all exact */
if(E>12 || E<-12) { /* faster and less accurate polynomial evaluation */
roundtestmask=0x7fe;
p1 = logirh + z*c1; p2 = c2 + z*c3; p3 = c4 + z*c5; z2 = z*z;
p1 = p1 + z2*p2; p3 = p3 + z2*c6; z4=z2*z2;
t = p1 + z4*p3;
t = t + E*ln2h;
}
else {
roundtestmask=0x7f0;
p1 = c5 + z*c6; z2 = z*z;
p2 = c3 + z*c4; p3 = c1+z*c2;
t = p2 + z2*p1;
t = p3 + z2*t;
t = logirh + z*t;
t = t + E*ln2h;
}
#endif
//printf(" x=%1.20Le\n r=%1.20Le\n z=%1.20Le\n logirh=%1.20Le\n ",(long double)xe, (long double)r,(long double)z, (long double)logirh);
/* Polynomial evaluation, unrolled to go through Gappa */
//printf("t=%1.20e \n ", (double)t);
}
#if 0 /* to time the first step only */
BACK_TO_DOUBLE_MODE; return (double)t;
#endif
/* To test the second step only, comment out the following line */
DE_TEST_AND_RETURN_RN(t, roundtestmask);
/* Accurate phase */
#if EVAL_PERF
crlibm_second_step_taken++;
#endif
t = c13h;
t = c12h + z*t;
t = c11h + z*t;
t = c10h + z*t;
t = c9h + z*t;
t = c8h + z*t;
//printf("\n t = %1.20Le\n", (long double)t);
Mul12_ext(&th, &tl, z, t);
Add22_ext(&th, &tl, c7h,c7l, th,tl);
FMA22_ext(&th, &tl, z,0, th,tl, c6h,c6l);
FMA22_ext(&th, &tl, z,0, th,tl, c5h,c5l);
FMA22_ext(&th, &tl, z,0, th,tl, c4h,c4l);
FMA22_ext(&th, &tl, z,0, th,tl, c3h,c3l);
FMA22_ext(&th, &tl, z,0, th,tl, c2h,c2l);
FMA22_ext(&th, &tl, z,0, th,tl, c1h,c1l);
if((X_NEAR_1)) {
Mul22_ext(&th, &tl, z,0, th,tl);
}
else{
FMA22_ext(&th, &tl, z,0, th,tl, logirh, argredtable[index].l);
/* the following is not a FMA22 (Eln2 > th+tl) */
Mul22_ext(&eh, &el, ln2h,ln2l, E, 0);
Add22_ext(&th, &tl, eh,el, th,tl);
}
BACK_TO_DOUBLE_MODE;
return (double) (th+tl); /* The exact sum of these double-extended is rounded to the nearest */
}
double log_ru(double x) { return x;};
double log_rd(double x) { return x;};
double log_rz(double x) { return x;};