forked from taschini/crlibm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_fast.c
360 lines (281 loc) · 9.18 KB
/
log_fast.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Correctly rounded logarithm
*
* Author : Daramy Catherine, Florent de Dinechin
*
* This file is part of the crlibm library developed by the Arenaire
* project at Ecole Normale Superieure de Lyon
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include "crlibm.h"
#include "crlibm_private.h"
#include "log_fast.h"
/* switches on various printfs. Default 0 */
#define DEBUG 0
/*
* 1) First reduction: exponent extraction
* E
* x = 2^ .(y) with 1 <= y < 2
*
* log(x) = E.log(2) + log(y) where:
* - log(2) is tabulated
* - log(y) need to be evaluated
*
*
* 2) Avoiding accuracy problem when E=-1 by testing
*
* if (ny >= sqrt(2)) then
* y = z/2; E = E+1;
* and,
* log(x) = (E+1).log(2) + log(y/2)
*
* so now: 11/16 <= sqrt(2)/2 <= y < sqrt(2) <= 23/16
*
*
* 3) Second reduction: tabular reduction
*
* The interval 1/sqrt(2) .. sqrt(2) is divided in 8 intervals.
* So, find the interval X_i where y is.
* And compute z = y - middle(X_i);
*
* 4) Computation:
*
* Polynomial evaluation of:
* - P(z) ~ log(z+middle(X_i))
*
* -4 -5
* with |z| < 2^ or 2^ depending the considered interval.
*
*
* 5) Reconstruction:
* log(x) = E.log(2) + P(z)
*
*/
/*
* Function used to evaluate log and pow functions
*/
void log_quick(double *pres_hi, double *pres_lo, int* prndcstindex, db_number * py, int E) {
double ln2_times_E_HI, ln2_times_E_LO, res_hi, res_lo;
double z, res, P_hi, P_lo;
int k, i;
res=(double)E;
if(E<0) E=-E;
/* find the interval including y.d */
i = ((((*py).i[HI] & 0x001F0000)>>16)-6) ;
if (i < 10)
i = i>>1;
else
i = ((i-1)>>1);
z = (*py).d - (middle[i]).d; /* (exact thanks to Sterbenz Lemma) */
/* Compute ln2_times_E = E*log(2) in double-double */
Add12( ln2_times_E_HI, ln2_times_E_LO, res*ln2hi.d, res*ln2lo.d);
/* Now begin the polynomial evaluation of log(1 + z) */
res = (Poly_h[i][DEGREE]).d;
for(k=DEGREE-1; k>1; k--){
res *= z;
res += (Poly_h[i][k]).d;
}
if(E <= EMIN_FASTPATH) {
/* Slow path */
if(E==0) {
*prndcstindex = 0 ;
/* In this case we start with a double-double multiplication to get enough relative accuracy */
Mul12(&P_hi, &P_lo, res, z);
Add22(&res_hi, &res_lo, (Poly_h[i][1]).d, (Poly_l[i][1]).d, P_hi, P_lo);
Mul22(&P_hi, &P_lo, res_hi, res_lo, z, 0.);
Add22(pres_hi, pres_lo, (Poly_h[i][0]).d, (Poly_l[i][0]).d, P_hi, P_lo);
}
else
{
if(E > EMIN_MEDIUMPATH)
*prndcstindex = 2;
else
*prndcstindex =1;
P_hi=res*z;
Add12(res_hi, res_lo, (Poly_h[i][1]).d, (Poly_l[i][1]).d + P_hi);
Mul22(&P_hi, &P_lo, res_hi, res_lo, z, 0.);
Add22(&res_hi, &res_lo, (Poly_h[i][0]).d, (Poly_l[i][0]).d, P_hi, P_lo);
/* Add E*log(2) */
Add22(pres_hi, pres_lo, ln2_times_E_HI, ln2_times_E_LO, res_hi, res_lo);
}
}
else { /* Fast path */
*prndcstindex = 3 ;
res = z*((Poly_h[i][1]).d + z*res);
#if 1
Add12(P_hi,P_lo, ln2_times_E_HI, (Poly_h[i][0]).d );
Add12(*pres_hi, *pres_lo, P_hi, (res + ((Poly_l[i][0]).d + (ln2_times_E_LO + P_lo))));
#else
Add12(*pres_hi, *pres_lo,
ln2_times_E_HI,
(Poly_h[i][0]).d + (res + ((Poly_l[i][0]).d + ln2_times_E_LO)));
#endif
}
}
/*************************************************************
*************************************************************
* ROUNDED TO NEAREST *
*************************************************************
*************************************************************/
double log_rn(double x){
db_number y;
double res_hi,res_lo,roundcst;
int E,rndcstindex;
E=0;
y.d=x;
/* Filter 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 = -52;
y.d *= two52.d; /* make x a normal number */
}
if (y.i[HI] >= 0x7ff00000){
return x+x; /* Inf or Nan */
}
/* reduce to y.d such that sqrt(2)/2 < y.d < sqrt(2) */
E += (y.i[HI]>>20)-1023; /* extract the exponent */
y.i[HI] = (y.i[HI] & 0x000fffff) | 0x3ff00000; /* do exponent = 0 */
if (y.d > SQRT_2){
y.d *= 0.5;
E++;
}
/* Call the actual computation */
log_quick(&res_hi, &res_lo, &rndcstindex, &y, E);
roundcst = rncst[rndcstindex];
/* Test for rounding to the nearest */
if(res_hi == (res_hi + (res_lo * roundcst)))
return res_hi;
else {
scs_t res;
#if DEBUG
printf("Going for Accurate Phase for x=%1.50e\n",x);
#endif
scs_log(res, y, E);
scs_get_d(&res_hi, res);
return res_hi;
}
}
/*************************************************************
*************************************************************
* ROUNDED TOWARD -INFINITY *
*************************************************************
*************************************************************/
double log_rd(double x){
db_number y;
double res_hi,res_lo,roundcst;
int E,rndcstindex;
scs_t res;
E=0;
y.d=x;
/* Filter 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 = -52;
y.d *= two52.d; /* make x as normal number = x's mantissa */
}
if (y.i[HI] >= 0x7ff00000){
return x+x; /* Inf or Nan */
}
/* The only double whose log is exactly a double */
if(x==1.0) return 0.0;
E += (y.i[HI]>>20)-1023; /* extract the exponent */
y.i[HI] = (y.i[HI] & 0x000fffff) | 0x3ff00000; /* do exponent = 0 */
if (y.d > SQRT_2){
y.d *= 0.5;
E++;
}
log_quick(&res_hi, &res_lo, &rndcstindex, &y, E);
roundcst = epsilon[rndcstindex];
TEST_AND_RETURN_RD(res_hi, res_lo, roundcst);
/* if the previous block didn't return a value, launch accurate phase */
#if DEBUG
printf("Going for Accurate Phase");
#endif
scs_log(res, y, E);
scs_get_d_minf(&res_hi, res);
return res_hi;
}
/*************************************************************
*************************************************************
* ROUNDED TOWARD +INFINITY *
*************************************************************
*************************************************************/
double log_ru(double x){
db_number y;
double res_hi,res_lo,roundcst;
int E,rndcstindex;
scs_t res;
E=0;
y.d=x;
/* Filter 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 = -52;
y.d *= two52.d; /* make x as normal number = x's mantissa */
}
if (y.i[HI] >= 0x7ff00000){
return x+x; /* Inf or Nan */
}
/* The only double whose log is exactly a double */
if(x==1.0) return 0.0;
E += (y.i[HI]>>20)-1023; /* extract the exponent */
y.i[HI] = (y.i[HI] & 0x000fffff) | 0x3ff00000; /* do exponent = 0 */
if (y.d > SQRT_2){
y.d *= 0.5;
E++;
}
log_quick(&res_hi, &res_lo, &rndcstindex, &y, E);
roundcst = epsilon[rndcstindex];
TEST_AND_RETURN_RU(res_hi, res_lo, roundcst);
/* if the previous block didn't return a value, launch accurate phase */
#if DEBUG
printf("Going for Accurate Phase");
#endif
scs_log(res, y, E);
scs_get_d_pinf(&res_hi, res);
return res_hi;
}
/*************************************************************
*************************************************************
* ROUNDED TOWARD ZERO *
*************************************************************
*************************************************************/
double log_rz(double x){
if(x>1)
return log_rd(x);
else
return log_ru(x);
}