forked from libtom/libtomfloat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmpf_inv.c
289 lines (258 loc) · 6.8 KB
/
mpf_inv.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
/* 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>
#include <tommath.h>
/* compute 1/x */
int mpf_inv_newton(mp_float * a, mp_float * b)
{
int err, sign, sign2;
double d;
long expnt, oldeps, eps, nloops, maxrounds, rest;
mp_float frac, one, x0, xn, A, hn, EPS;
/* get sign of input */
sign = a->mantissa.sign;
/* force to positive */
a->mantissa.sign = MP_ZPOS;
oldeps = a->radix;
eps = oldeps + MP_DIGIT_BIT;
err = MP_OKAY;
if(mpf_iszero(a)){
// raise DivisionByZero
return MP_VAL;
}
if ((err = mpf_init_multi(eps, &frac, &one, &x0, &xn, &A, &hn,NULL)) != MP_OKAY) {
return err;
}
if ((err = mpf_frexp(a, &frac, &expnt)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_set_double(&frac, &d)) != MP_OKAY) {
goto _ERR;
}
d = 1.0 / d;
if ((err = mpf_get_double(d, &frac)) != MP_OKAY) {
goto _ERR;
}
expnt = -expnt + 1;
if ((err = mpf_ldexp(&frac, expnt, &frac)) != MP_OKAY) {
goto _ERR;
}
// TODO calculate guard digits more exactly and do it loop-wise
if ((err = mpf_normalize_to(&frac, eps)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_copy(&frac, &xn)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_const_d(&one, 1L)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_copy(a, &A)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_normalize_to(&A, eps)) != MP_OKAY) {
goto _ERR;
}
maxrounds = A.radix;
nloops = 0L;
if ((err = mpf_init(&EPS, oldeps)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_const_eps(&EPS)) != MP_OKAY) {
goto _ERR;
}
do {
if ((err = mpf_copy(&xn, &x0)) != MP_OKAY) {
goto _ERR;
}
// hn = 1 - (A * xn);
if ((err = mpf_mul(&A, &xn, &hn)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_sub(&one, &hn, &hn)) != MP_OKAY) {
goto _ERR;
}
sign2 = hn.mantissa.sign;
hn.mantissa.sign = MP_ZPOS;
// It makes more sense to compare after that limit is reached
if (hn.exp <= EPS.exp) {
if (mpf_cmp(&hn, &EPS) != MP_GT) {
break;
}
}
hn.mantissa.sign = sign2;
// xn = xn + (xn * hn);
if ((err = mpf_mul(&xn, &hn, &hn)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_add(&xn, &hn, &xn)) != MP_OKAY) {
goto _ERR;
}
nloops++;
if (nloops >= maxrounds) {
// it might be a bug elsewhere, please report
fprintf(stderr, "mpf_inv did not converge in %ld rounds", nloops);
return MP_RANGE;
}
// Comparing is not necessary here but might save an iteration
} while (mpf_cmp(&x0, &xn) != MP_EQ);
// } while (1);
if ((err = mpf_normalize_to(&xn, oldeps)) != MP_OKAY) {
goto _ERR;
}
mpf_exch(&xn, b);
/* now restore the signs */
a->mantissa.sign = sign;
b->mantissa.sign = sign;
_ERR:
mpf_clear_multi(&frac, &one, &x0, &xn, &A, &hn, &EPS ,NULL);
return err;
}
// not faster?
int mpf_inv(mp_float * a, mp_float * b)
{
int err, sign, sign2;
double d;
long expnt, oldeps, eps, nloops, maxrounds, starteps, maxeps;
mp_float frac, one, x0, xn, A, hn,hn2, EPS;
/* get sign of input */
sign = a->mantissa.sign;
/* force to positive */
a->mantissa.sign = MP_ZPOS;
oldeps = a->radix;
eps = oldeps + MP_DIGIT_BIT;
starteps = 64;//2 * MP_DIGIT_BIT;
maxeps = oldeps + MP_DIGIT_BIT;
err = MP_OKAY;
if(mpf_iszero(a)){
// raise DivisionByZero
return MP_VAL;
}
if ((err = mpf_init_multi(starteps, &frac, &one, &x0, &xn, &A, &hn,&hn2,NULL)) != MP_OKAY) {
return err;
}
if ((err = mpf_frexp(a, &frac, &expnt)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_set_double(&frac, &d)) != MP_OKAY) {
goto _ERR;
}
//fprintf(stderr,"\n1 %g\n",d);
d = 1.0 / d;
//fprintf(stderr,"2 %g\n",d);
if ((err = mpf_get_double(d, &frac)) != MP_OKAY) {
goto _ERR;
}
//fprintf(stderr,"3 %ld\n",expnt);
if ( d > 1.0)
expnt = -expnt ;
else
expnt = -expnt + 1;
//fprintf(stderr,"4 %ld\n",expnt);
if ((err = mpf_ldexp(&frac, expnt, &frac)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_normalize(&frac)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_copy(&frac, &xn)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_const_d(&one, 1L)) != 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; }
maxrounds = (long)(log(maxeps)/log(2)) + 5;//A.radix;
nloops = 0L;
if ((err = mpf_init(&EPS, oldeps)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_const_eps(&EPS)) != MP_OKAY) {
goto _ERR;
}
do {
starteps = starteps * 2;
if (starteps > maxeps) {
// do one round with full precision
// or...well...die from exhaustion
starteps = maxeps;
}
if ((err =
mpf_normalize_to_multi(starteps, &one, &x0, &xn, &A, &hn,&hn2,
&EPS, NULL)) != MP_OKAY) {
return err;
}
if ((err = mpf_copy(a, &A)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_normalize_to(&A, starteps)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_copy(&xn, &x0)) != MP_OKAY) {
goto _ERR;
}
// hn = 1 - (A * xn);
if ((err = mpf_mul(&A, &xn, &hn)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_sub(&one, &hn, &hn)) != MP_OKAY) {
goto _ERR;
}
sign2 = hn.mantissa.sign;
hn.mantissa.sign = MP_ZPOS;
// It makes more sense to compare after that limit is reached
//fprintf(stderr,"5 %ld %ld\n",hn.exp, EPS.exp);
if (mpf_iszero(&hn))
break;
if (hn.exp <= EPS.exp) {
if (mpf_cmp(&hn, &EPS) != MP_GT) {
break;
}
}
hn.mantissa.sign = sign2;
// xn = xn + xn( hn + hn^2)
if ((err = mpf_sqr(&hn, &hn2)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_add(&hn, &hn2, &hn)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_mul(&xn, &hn, &hn)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_add(&xn, &hn, &xn)) != MP_OKAY) {
goto _ERR;
}
nloops++;
if (nloops >= maxrounds) {
// it might be a bug elsewhere, please report
fprintf(stderr, "mpf_inv did not converge in %ld rounds, exp %ld\n", nloops,xn.exp);
return MP_RANGE;
}
// Comparing is not necessary here but might save an iteration
} while (mpf_cmp(&x0, &xn) != MP_EQ);
// } while (1);
if ((err = mpf_normalize_to(&xn, oldeps)) != MP_OKAY) {
goto _ERR;
}
mpf_exch(&xn, b);
/* now restore the signs */
a->mantissa.sign = sign;
b->mantissa.sign = sign;
_ERR:
mpf_clear_multi(&frac, &one, &x0, &xn, &A, &hn, &EPS ,NULL);
return err;
}