forked from libtom/libtomfloat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmpf_exp.c
320 lines (287 loc) · 7.11 KB
/
mpf_exp.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
/* LibTomFloat, multiple-precision floating-point library
*
* LibTomFloat is a library that provides multiple-precision
* floating-point artihmetic as well as trigonometric functionality.
*
* This library requires the public domain LibTomMath to be installed.
*
* This library is free for all purposes without any express
* gurantee it works
*
* Tom St Denis, [email protected], http://float.libtomcrypt.org
*/
#include <tomfloat.h>
static int mpf_exp_newton(mp_float * a, mp_float * b);
/* compute b = e^a using e^x == \sum_{n=0}^{\infty} {1 \over n!}x^n */
int mpf_exp(mp_float * a, mp_float * b)
{
long n, oldeps, eps, loops, decexpo;
mp_float to, t, tx, ret, x0, one, two, nt, diff;
int err, sign, m, i;
sign = MP_ZPOS;
err = MP_OKAY;
// TODO: more checks & balances
if (mpf_iszero(a)) {
return mpf_const_d(a, 1);
}
oldeps = a->radix;
if (oldeps > 2 * MP_DIGIT_BIT) {
puts("not more than one time?");
return mpf_exp_newton(a,b);
}
decexpo = a->exp + a->radix;
if (decexpo > 0) {
decexpo = mpf_getdecimalexponent(decexpo) - 1;
// TODO: evaluate the cutoffs and guard digits more exactly
switch (decexpo) {
case 0:
case 1:
eps = oldeps + 20;
m = 1 << 4;
break;
case 2:
case 3:
eps = oldeps + 30;
m = 1 << 5;
break;
case 4:
eps = oldeps + 60;
m = 1 << 6;
break;
case 5:
eps = oldeps + 120;
m = 1 << 7;
break;
case 6:
eps = oldeps + 240;
m = 1 << 8;
break;
case 7:
eps = oldeps + 480;
m = 1 << 9;
break;
case 8:
eps = oldeps + 960;
m = 1 << 10;
break;
/*
This gives ~3.33561e434294481 for 1e9 instead of ~8.003e434294481
There seems to be a still undetected flaw (over/underflow?), maybe
even somewhere else.
case 9:
eps = oldeps + 2000;
m = 1 << 11;
break;
*/
default:
err = MP_RANGE;
break;
}
} else {
m = 1 << 4;
eps = oldeps + 20;
}
if ((err =
mpf_init_multi(eps, &to, &t, &tx, &ret, &x0, &one, &two, &nt,
&diff, NULL)) != MP_OKAY) {
return err;
}
if ((err = mpf_copy(a, &nt)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_normalize_to(&nt, eps)) != MP_OKAY) {
goto _ERR;
}
if (a->mantissa.sign == MP_NEG) {
sign = MP_NEG;
nt.mantissa.sign = MP_ZPOS;
}
if ((err = mpf_const_0(&ret)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_const_d(&to, 1)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_const_d(&tx, 1)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_const_d(&one, 1)) != MP_OKAY) {
goto _ERR;
}
n = 1;
// NOTE: calculate a bit more precisely
loops = eps + 10;
// argument reduction by 1/2^m
nt.exp -= m;
do {
// x0 = ret
if ((err = mpf_copy(&ret, &x0)) != MP_OKAY) {
goto _ERR;
}
// t = n++
if ((err = mpf_const_d(&t, n++)) != MP_OKAY) {
goto _ERR;
}
// t = 1/t
if ((err = mpf_inv(&t, &t)) != MP_OKAY) {
goto _ERR;
}
// to = t * to
if ((err = mpf_mul(&t, &to, &to)) != MP_OKAY) {
goto _ERR;
}
// tx = tx * nt
if ((err = mpf_mul(&tx, &nt, &tx)) != MP_OKAY) {
goto _ERR;
}
// t = to * tx
if ((err = mpf_mul(&to, &tx, &t)) != MP_OKAY) {
goto _ERR;
}
// ret = t + ret
if ((err = mpf_add(&t, &ret, &ret)) != MP_OKAY) {
goto _ERR;
}
if (loops-- == 0) {
fprintf(stderr, "exp did not converge in %ld rounds\n", eps + 10);
goto _ERR;
}
} while (mpf_cmp(&x0, &ret) != MP_EQ);
#ifdef DEBUG
fprintf(stderr, "loops = %ld\n", (eps + 10) - loops);
#endif
// reverse argument reduction
for (i = 0; i < m; i++) {
// to = ret^2
if ((err = mpf_sqr(&ret, &to)) != MP_OKAY) {
goto _ERR;
}
// ret = ret * 2
ret.exp += 1;
// ret = ret + to
if ((err = mpf_add(&ret, &to, &ret)) != MP_OKAY) {
goto _ERR;
}
}
// we have exp(z) - 1 now, add one unit
if ((err = mpf_add(&ret, &one, &ret)) != MP_OKAY) {
goto _ERR;
}
// exp(-z) = 1/exp(z)
if (sign == MP_NEG) {
if ((err = mpf_inv(&ret, &ret)) != MP_OKAY) {
goto _ERR;
}
}
if ((err = mpf_normalize_to(&ret, oldeps)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_copy(&ret, b)) != MP_OKAY) {
goto _ERR;
}
_ERR:
mpf_clear_multi(&to, &t, &tx, &ret, &x0, &one, &two, &nt, &diff, NULL);
return err;
}
// $x_{n+1} = x_n ( 1 + a - \log( x_n) )$
static int mpf_exp_newton(mp_float * a, mp_float * b){
long n, oldeps, eps, nloops, maxrounds, starteps, maxeps;
mp_float t, x0, xn, one, A, EPS;
int err, sign, m, i;
sign = MP_ZPOS;
err = MP_OKAY;
oldeps = a->radix;
eps = oldeps + 4 * MP_DIGIT_BIT;
if ((err = mpf_init(&A, oldeps)) != MP_OKAY) {
return err;
}
if ((err = mpf_copy(a, &A)) != MP_OKAY) {
mpf_clear(&A);
return err;
}
// get the seed with the series
if ((err = mpf_normalize_to(&A, 2 * MP_DIGIT_BIT)) != MP_OKAY) {
mpf_clear(&A);
return err;
}
mpf_exp(&A,&A);
maxrounds = A.radix;
nloops = 0L;
if ((err = mpf_init(&EPS, oldeps)) != MP_OKAY) {
mpf_clear(&A);
return err;
}
if ((err = mpf_const_eps(&EPS)) != MP_OKAY) {
mpf_clear(&A);
mpf_clear(&EPS);
return err;
}
oldeps = a->radix;
starteps = 2 * MP_DIGIT_BIT;
maxeps = oldeps + MP_DIGIT_BIT;
if ((err =
mpf_init_multi(starteps, &EPS, &t, &x0, &xn,&one, NULL)) != MP_OKAY) {
return err;
}
if ((err = mpf_normalize_to(&A, starteps)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_copy(&A, &xn)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_const_d(&one, 1L)) != MP_OKAY) {
goto _ERR;
}
// $x_{n+1} = x_n ( 1 + a - \log( x_n) )$
do {
if ((err = mpf_copy(&xn, &x0)) != MP_OKAY) {
goto _ERR;
}
starteps = starteps * 2;
if (starteps > maxeps) {
// do one round with full precision
starteps = maxeps;
}
if ((err =
mpf_normalize_to_multi(starteps, &one, &x0, &xn, &A, &t,
&EPS,NULL)) != MP_OKAY) {
goto _ERR;
}
// t = log(x_n)
if ((err = mpf_ln(&xn, &t)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_copy(a, &A)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_normalize_to(&A, starteps)) != MP_OKAY) {
goto _ERR;
}
// t = a - t
if ((err = mpf_sub(&A, &t, &t)) != MP_OKAY) {
goto _ERR;
}
// t = 1 + t
if ((err = mpf_add(&t, &one, &t)) != MP_OKAY) {
goto _ERR;
}
// xn = xn * t
if ((err = mpf_mul(&xn, &t, &xn)) != MP_OKAY) {
goto _ERR;
}
nloops++;
if (nloops >= maxrounds) {
// it might be a bug elsewhere, please report
fprintf(stderr, "mpf_sqrt did not converge in %ld rounds\n",
nloops);
return MP_RANGE;
}
} while (mpf_cmp(&x0, &xn) != MP_EQ);
if ((err = mpf_normalize_to(&xn, oldeps)) != MP_OKAY) {
goto _ERR;
}
mpf_exch(&xn, b);
_ERR:
mpf_clear_multi(&t, &x0, &xn, &one, NULL);
return err;
}