cb10c4182abb1bd6892e2c73bcb1007358339180
[libc-test] / src / math / gen / mp.c
1 #include <stdio.h>
2
3 #include <stdint.h>
4 #include <mpfr.h>
5 #include "gen.h"
6
7 static int rmap(int r)
8 {
9         switch (r) {
10         case RN: return MPFR_RNDN;
11         case RZ: return MPFR_RNDZ;
12         case RD: return MPFR_RNDD;
13         case RU: return MPFR_RNDU;
14         }
15         return -1;
16 }
17
18 void debug(mpfr_t x)
19 {
20         mpfr_out_str(stdout, 10, 0, x, MPFR_RNDN);
21         printf("\n");
22 }
23
24 void mpsetup()
25 {
26         mpfr_set_emin(-1073);
27         mpfr_set_emax(1024);
28 }
29 void mpsetupf()
30 {
31         mpfr_set_emin(-148);
32         mpfr_set_emax(128);
33 }
34 #if LDBL_MANT_DIG == 64
35 void mpsetupl()
36 {
37         mpfr_set_emin(-16444);
38         mpfr_set_emax(16384);
39 }
40 #endif
41
42 /*
43 round x into y considering x is already rounded (t = up or down)
44
45 only cases where adjustment is done:
46         x=...|1...0, t=up    -> x=nextbelow(x)
47         x=...|1...0, t=down  -> x=nextabove(x)
48 where | is the rounding point, ... is 0 or 1 bit patterns
49 */
50
51 // TODO: adjust(y, 0, 2, RN); when prec is 24 (0 vs 0x1p-149f), special case x=0
52 static int adjust_round(mpfr_t y, mpfr_t x, int t, int r)
53 {
54         mp_limb_t *p, *q;
55         unsigned xp, yp;
56         int t2;
57
58         xp = mpfr_get_prec(x);
59         yp = mpfr_get_prec(y);
60         if (yp >= xp || r != MPFR_RNDN || t == 0 || !mpfr_number_p(x) || mpfr_zero_p(x)) {
61                 t2 = mpfr_set(y, x, r);
62                 return t2 ? t2 : t;
63         }
64         p = x->_mpfr_d;
65         yp++;
66         q = p + (xp + mp_bits_per_limb - 1)/mp_bits_per_limb - (yp + mp_bits_per_limb - 1)/mp_bits_per_limb;
67         if ((*p & 1 << -xp%mp_bits_per_limb) || !(*q & 1 << -yp%mp_bits_per_limb)) {
68                 t2 = mpfr_set(y, x, r);
69                 return t2 ? t2 : t;
70         }
71         if (t > 0)
72                 mpfr_nextbelow(x);
73         else
74                 mpfr_nextabove(x);
75         return mpfr_set(y, x, r);
76 }
77
78 static int adjust(mpfr_t mr, mpfr_t my, int t, int r)
79 {
80 //      double d, dn, dp;
81 //printf("adj %d\n", t);
82 //debug(my);
83         t = adjust_round(mr, my, t, r);
84 //printf("rnd %d\n", t);
85 //debug(mr);
86         t = mpfr_subnormalize(mr, t, r);
87 //printf("sub %d\n", t);
88 //debug(mr);
89 //      d = mpfr_get_d(mr, r);
90 //      dn = nextafter(d, INFINITY);
91 //      dp = nextafter(d, -INFINITY);
92 //printf("c\n %.21e %a\n %.21e %a\n %.21e %a\n",d,d,dn,dn,dp,dp);
93 //      dn = nextafterf(d, INFINITY);
94 //      dp = nextafterf(d, -INFINITY);
95 //printf("cf\n %.21e %a\n %.21e %a\n %.21e %a\n",d,d,dn,dn,dp,dp);
96         return t;
97 }
98
99 // TODO
100 //static int eflags(mpfr_t mr, mpfr_t my, int t)
101 static int eflags(int naninput)
102 {
103         int i = 0;
104
105         if (mpfr_inexflag_p())
106                 i |= FE_INEXACT;
107 //      if (mpfr_underflow_p() && (t || mpfr_cmp(mr, my) != 0))
108         if (mpfr_underflow_p() && i)
109                 i |= FE_UNDERFLOW;
110         if (mpfr_overflow_p())
111                 i |= FE_OVERFLOW;
112         if (mpfr_divby0_p())
113                 i |= FE_DIVBYZERO;
114         if (!naninput && (mpfr_nanflag_p() || mpfr_erangeflag_p()))
115                 i |= FE_INVALID;
116         return i;
117 }
118
119 static void genf(struct t *p, mpfr_t my, int t, int r)
120 {
121         MPFR_DECL_INIT(mr, 24);
122         int i;
123
124         t = adjust(mr, my, t, r);
125         p->y = mpfr_get_flt(mr, r);
126         p->e = eflags(isnan(p->x) || isnan(p->x2));
127         i = eulpf(p->y);
128         if (!isfinite(p->y)) {
129                 p->dy = 0;
130         } else if (i < 0) {
131                 mpfr_div_2si(mr, mr, i, MPFR_RNDN);
132                 mpfr_div_2si(my, my, i, MPFR_RNDN);
133                 mpfr_sub(my, mr, my, MPFR_RNDN);
134                 p->dy = mpfr_get_flt(my, r);
135         } else {
136                 mpfr_sub(my, mr, my, MPFR_RNDN);
137                 mpfr_div_2si(my, my, i, MPFR_RNDN);
138                 p->dy = mpfr_get_flt(my, r);
139         }
140 }
141
142 static int mpf1(struct t *p, int (*fmp)(mpfr_t, const mpfr_t, mpfr_rnd_t))
143 {
144         int tn;
145         int r = rmap(p->r);
146         MPFR_DECL_INIT(mx, 24);
147         MPFR_DECL_INIT(my, 128);
148
149         mpsetupf();
150         mpfr_clear_flags();
151         mpfr_set_flt(mx, p->x, MPFR_RNDN);
152         tn = fmp(my, mx, r);
153         p->x2 = 0;
154         genf(p, my, tn, r);
155         if ((p->e & INEXACT) && nextafterf(p->y, 0) == 0) {
156                 mpfr_set_emin(-(1<<20));
157                 tn = fmp(my, mx, r);
158                 mpfr_mul_2si(my, my, 149, MPFR_RNDN);
159                 p->dy = scalbnl(p->y, 149) - mpfr_get_ld(my, r);
160                 mpfr_set_emin(-148);
161         }
162         return 0;
163 }
164
165 static int mpf2(struct t *p, int (*fmp)(mpfr_t, const mpfr_t, const mpfr_t, mpfr_rnd_t))
166 {
167         int tn;
168         int r = rmap(p->r);
169         MPFR_DECL_INIT(mx, 24);
170         MPFR_DECL_INIT(mx2, 24);
171         MPFR_DECL_INIT(my, 128);
172
173         mpsetupf();
174         mpfr_clear_flags();
175         mpfr_set_flt(mx, p->x, MPFR_RNDN);
176         mpfr_set_flt(mx2, p->x2, MPFR_RNDN);
177         tn = fmp(my, mx, mx2, r);
178         genf(p, my, tn, r);
179         if ((p->e & INEXACT) && nextafterf(p->y, 0) == 0) {
180                 mpfr_set_emin(-(1<<20));
181                 tn = fmp(my, mx, mx2, r);
182                 mpfr_mul_2si(my, my, 149, MPFR_RNDN);
183                 p->dy = scalbnl(p->y, 149) - mpfr_get_ld(my, r);
184                 mpfr_set_emin(-148);
185         }
186         return 0;
187 }
188
189 static void gend(struct t *p, mpfr_t my, int t, int r)
190 {
191         MPFR_DECL_INIT(mr, 53);
192         int i;
193
194         t = adjust(mr, my, t, r);
195         p->y = mpfr_get_d(mr, r);
196         p->e = eflags(isnan(p->x) || isnan(p->x2));
197         i = eulp(p->y);
198         if (!isfinite(p->y)) {
199                 p->dy = 0;
200         } else if (i < 0) {
201                 mpfr_div_2si(mr, mr, i, MPFR_RNDN);
202                 mpfr_div_2si(my, my, i, MPFR_RNDN);
203                 mpfr_sub(my, mr, my, MPFR_RNDN);
204                 p->dy = mpfr_get_flt(my, r);
205         } else {
206                 mpfr_sub(my, mr, my, MPFR_RNDN);
207                 mpfr_div_2si(my, my, i, MPFR_RNDN);
208                 p->dy = mpfr_get_flt(my, r);
209         }
210 }
211
212 static int mpd1(struct t *p, int (*fmp)(mpfr_t, const mpfr_t, mpfr_rnd_t))
213 {
214         int tn;
215         int r = rmap(p->r);
216         MPFR_DECL_INIT(mx, 53);
217         MPFR_DECL_INIT(my, 128);
218
219         mpsetup();
220         mpfr_clear_flags();
221         mpfr_set_d(mx, p->x, MPFR_RNDN);
222         tn = fmp(my, mx, r);
223 //printf("underflow: %d\n", mpfr_underflow_p());
224         p->x2 = 0;
225         gend(p, my, tn, r);
226 //printf("dy: %a  %.3f\n", p->dy, p->dy);
227         if ((p->e & INEXACT) && nextafter(p->y, 0) == 0) {
228                 mpfr_set_emin(-(1<<20));
229                 tn = fmp(my, mx, r);
230                 mpfr_mul_2si(my, my, 1074, MPFR_RNDN);
231 //debug(my);
232                 p->dy = scalbnl(p->y, 1074) - mpfr_get_ld(my, r);
233 //printf("dy: %a  %.3f\n", p->dy, p->dy);
234                 mpfr_set_emin(-1073);
235         }
236         return 0;
237 }
238
239 static int mpd2(struct t *p, int (*fmp)(mpfr_t, const mpfr_t, const mpfr_t, mpfr_rnd_t))
240 {
241         int tn;
242         int r = rmap(p->r);
243         MPFR_DECL_INIT(mx, 53);
244         MPFR_DECL_INIT(mx2, 53);
245         MPFR_DECL_INIT(my, 128);
246
247         mpsetup();
248         mpfr_clear_flags();
249         mpfr_set_d(mx, p->x, MPFR_RNDN);
250         mpfr_set_d(mx2, p->x2, MPFR_RNDN);
251         tn = fmp(my, mx, mx2, r);
252         gend(p, my, tn, r);
253         if ((p->e & INEXACT) && nextafter(p->y, 0) == 0) {
254                 mpfr_set_emin(-(1<<20));
255                 tn = fmp(my, mx, mx2, r);
256                 mpfr_mul_2si(my, my, 1074, MPFR_RNDN);
257                 p->dy = scalbnl(p->y, 1074) - mpfr_get_ld(my, r);
258                 mpfr_set_emin(-1073);
259         }
260         return 0;
261 }
262
263 #if LDBL_MANT_DIG == 64
264 static void genl(struct t *p, mpfr_t my, int t, int r)
265 {
266         MPFR_DECL_INIT(mr, 64);
267         int i;
268
269         t = adjust(mr, my, t, r);
270         p->y = mpfr_get_ld(mr, r);
271         p->e = eflags(isnan(p->x) || isnan(p->x2));
272         i = eulpl(p->y);
273         if (!isfinite(p->y)) {
274                 p->dy = 0;
275         } else if (i < 0) {
276                 mpfr_div_2si(mr, mr, i, MPFR_RNDN);
277                 mpfr_div_2si(my, my, i, MPFR_RNDN);
278                 mpfr_sub(my, mr, my, MPFR_RNDN);
279                 p->dy = mpfr_get_flt(my, r);
280         } else {
281                 mpfr_sub(my, mr, my, MPFR_RNDN);
282                 mpfr_div_2si(my, my, i, MPFR_RNDN);
283                 p->dy = mpfr_get_flt(my, r);
284         }
285 }
286 #endif
287
288 static int mpl1(struct t *p, int (*fmp)(mpfr_t, const mpfr_t, mpfr_rnd_t))
289 {
290 #if LDBL_MANT_DIG == 53
291         return mpd1(p, fmp);
292 #elif LDBL_MANT_DIG == 64
293         int tn;
294         int r = rmap(p->r);
295         MPFR_DECL_INIT(mx, 64);
296         MPFR_DECL_INIT(my, 128);
297
298         mpsetupl();
299         mpfr_clear_flags();
300         mpfr_set_ld(mx, p->x, MPFR_RNDN);
301         tn = fmp(my, mx, r);
302         p->x2 = 0;
303         genl(p, my, tn, r);
304         if ((p->e & INEXACT) && nextafterl(p->y, 0) == 0) {
305                 mpfr_set_emin(-(1<<20));
306                 tn = fmp(my, mx, r);
307                 mpfr_mul_2si(my, my, 16445, MPFR_RNDN);
308                 p->dy = scalbnl(p->y, 16445) - mpfr_get_ld(my, r);
309                 mpfr_set_emin(-16444);
310         }
311         return 0;
312 #else
313         return -1;
314 #endif
315 }
316
317 static int mpl2(struct t *p, int (*fmp)(mpfr_t, const mpfr_t, const mpfr_t, mpfr_rnd_t))
318 {
319 #if LDBL_MANT_DIG == 53
320         return mpd2(p, fmp);
321 #elif LDBL_MANT_DIG == 64
322         int tn;
323         int r = rmap(p->r);
324         MPFR_DECL_INIT(mx, 64);
325         MPFR_DECL_INIT(mx2, 64);
326         MPFR_DECL_INIT(my, 128);
327
328         mpsetupl();
329         mpfr_clear_flags();
330         mpfr_set_ld(mx, p->x, MPFR_RNDN);
331         mpfr_set_ld(mx2, p->x2, MPFR_RNDN);
332         tn = fmp(my, mx, mx2, r);
333         genl(p, my, tn, r);
334         if ((p->e & INEXACT) && nextafterl(p->y, 0) == 0) {
335                 mpfr_set_emin(-(1<<20));
336                 tn = fmp(my, mx, mx2, r);
337                 mpfr_mul_2si(my, my, 16445, MPFR_RNDN);
338                 p->dy = scalbnl(p->y, 16445) - mpfr_get_ld(my, r);
339                 mpfr_set_emin(-16444);
340         }
341         return 0;
342 #else
343         return -1;
344 #endif
345 }
346
347 // TODO
348 static int mplgamma_sign;
349 static int wrap_lgamma(mpfr_t my, const mpfr_t mx, mpfr_rnd_t r)
350 {
351         return mpfr_lgamma(my, &mplgamma_sign, mx, r);
352 }
353 static long mpremquo_q;
354 static int wrap_remquo(mpfr_t my, const mpfr_t mx, const mpfr_t mx2, mpfr_rnd_t r)
355 {
356         return mpfr_remquo(my, &mpremquo_q, mx, mx2, r);
357 }
358 static int wrap_ceil(mpfr_t my, const mpfr_t mx, mpfr_rnd_t r)
359 {
360         return mpfr_ceil(my, mx);
361 }
362 static int wrap_floor(mpfr_t my, const mpfr_t mx, mpfr_rnd_t r)
363 {
364         return mpfr_floor(my, mx);
365 }
366 static int wrap_round(mpfr_t my, const mpfr_t mx, mpfr_rnd_t r)
367 {
368         return mpfr_round(my, mx);
369 }
370 static int wrap_trunc(mpfr_t my, const mpfr_t mx, mpfr_rnd_t r)
371 {
372         return mpfr_trunc(my, mx);
373 }
374 static int wrap_nearbyint(mpfr_t my, const mpfr_t mx, mpfr_rnd_t r)
375 {
376         int i = mpfr_rint(my, mx, r);
377         mpfr_clear_inexflag();
378         return i;
379 }
380 static int wrap_pow10(mpfr_t my, const mpfr_t mx, mpfr_rnd_t r)
381 {
382         return mpfr_ui_pow(my, 10, mx, r);
383 }
384
385 int mpacos(struct t *t) { return mpd1(t, mpfr_acos); }
386 int mpacosf(struct t *t) { return mpf1(t, mpfr_acos); }
387 int mpacosl(struct t *t) { return mpl1(t, mpfr_acos); }
388 int mpacosh(struct t *t) { return mpd1(t, mpfr_acosh); }
389 int mpacoshf(struct t *t) { return mpf1(t, mpfr_acosh); }
390 int mpacoshl(struct t *t) { return mpl1(t, mpfr_acosh); }
391 int mpasin(struct t *t) { return mpd1(t, mpfr_asin); }
392 int mpasinf(struct t *t) { return mpf1(t, mpfr_asin); }
393 int mpasinl(struct t *t) { return mpl1(t, mpfr_asin); }
394 int mpasinh(struct t *t) { return mpd1(t, mpfr_asinh); }
395 int mpasinhf(struct t *t) { return mpf1(t, mpfr_asinh); }
396 int mpasinhl(struct t *t) { return mpl1(t, mpfr_asinh); }
397 int mpatan(struct t *t) { return mpd1(t, mpfr_atan); }
398 int mpatanf(struct t *t) { return mpf1(t, mpfr_atan); }
399 int mpatanl(struct t *t) { return mpl1(t, mpfr_atan); }
400 int mpatan2(struct t *t) { return mpd2(t, mpfr_atan2); }
401 int mpatan2f(struct t *t) { return mpf2(t, mpfr_atan2); }
402 int mpatan2l(struct t *t) { return mpl2(t, mpfr_atan2); }
403 int mpatanh(struct t *t) { return mpd1(t, mpfr_atanh); }
404 int mpatanhf(struct t *t) { return mpf1(t, mpfr_atanh); }
405 int mpatanhl(struct t *t) { return mpl1(t, mpfr_atanh); }
406 int mpcbrt(struct t *t) { return mpd1(t, mpfr_cbrt); }
407 int mpcbrtf(struct t *t) { return mpf1(t, mpfr_cbrt); }
408 int mpcbrtl(struct t *t) { return mpl1(t, mpfr_cbrt); }
409 int mpceil(struct t *t) { return mpd1(t, wrap_ceil); }
410 int mpceilf(struct t *t) { return mpf1(t, wrap_ceil); }
411 int mpceill(struct t *t) { return mpl1(t, wrap_ceil); }
412 int mpcopysign(struct t *t) { return mpd2(t, mpfr_copysign); }
413 int mpcopysignf(struct t *t) { return mpf2(t, mpfr_copysign); }
414 int mpcopysignl(struct t *t) { return mpl2(t, mpfr_copysign); }
415 int mpcos(struct t *t) { return mpd1(t, mpfr_cos); }
416 int mpcosf(struct t *t) { return mpf1(t, mpfr_cos); }
417 int mpcosl(struct t *t) { return mpl1(t, mpfr_cos); }
418 int mpcosh(struct t *t) { return mpd1(t, mpfr_cosh); }
419 int mpcoshf(struct t *t) { return mpf1(t, mpfr_cosh); }
420 int mpcoshl(struct t *t) { return mpl1(t, mpfr_cosh); }
421 int mperf(struct t *t) { return mpd1(t, mpfr_erf); }
422 int mperff(struct t *t) { return mpf1(t, mpfr_erf); }
423 int mperfl(struct t *t) { return mpl1(t, mpfr_erf); }
424 int mperfc(struct t *t) { return mpd1(t, mpfr_erfc); }
425 int mperfcf(struct t *t) { return mpf1(t, mpfr_erfc); }
426 int mperfcl(struct t *t) { return mpl1(t, mpfr_erfc); }
427 int mpexp(struct t *t) { return mpd1(t, mpfr_exp); }
428 int mpexpf(struct t *t) { return mpf1(t, mpfr_exp); }
429 int mpexpl(struct t *t) { return mpl1(t, mpfr_exp); }
430 int mpexp2(struct t *t) { return mpd1(t, mpfr_exp2); }
431 int mpexp2f(struct t *t) { return mpf1(t, mpfr_exp2); }
432 int mpexp2l(struct t *t) { return mpl1(t, mpfr_exp2); }
433 int mpexpm1(struct t *t) { return mpd1(t, mpfr_expm1); }
434 int mpexpm1f(struct t *t) { return mpf1(t, mpfr_expm1); }
435 int mpexpm1l(struct t *t) { return mpl1(t, mpfr_expm1); }
436 int mpfabs(struct t *t) { return mpd1(t, mpfr_abs); }
437 int mpfabsf(struct t *t) { return mpf1(t, mpfr_abs); }
438 int mpfabsl(struct t *t) { return mpl1(t, mpfr_abs); }
439 int mpfdim(struct t *t) { return mpd2(t, mpfr_dim); }
440 int mpfdimf(struct t *t) { return mpf2(t, mpfr_dim); }
441 int mpfdiml(struct t *t) { return mpl2(t, mpfr_dim); }
442 int mpfloor(struct t *t) { return mpd1(t, wrap_floor); }
443 int mpfloorf(struct t *t) { return mpf1(t, wrap_floor); }
444 int mpfloorl(struct t *t) { return mpl1(t, wrap_floor); }
445 int mpfmax(struct t *t) { return mpd2(t, mpfr_max); }
446 int mpfmaxf(struct t *t) { return mpf2(t, mpfr_max); }
447 int mpfmaxl(struct t *t) { return mpl2(t, mpfr_max); }
448 int mpfmin(struct t *t) { return mpd2(t, mpfr_min); }
449 int mpfminf(struct t *t) { return mpf2(t, mpfr_min); }
450 int mpfminl(struct t *t) { return mpl2(t, mpfr_min); }
451 int mpfmod(struct t *t) { return mpd2(t, mpfr_fmod); }
452 int mpfmodf(struct t *t) { return mpf2(t, mpfr_fmod); }
453 int mpfmodl(struct t *t) { return mpl2(t, mpfr_fmod); }
454 int mphypot(struct t *t) { return mpd2(t, mpfr_hypot); }
455 int mphypotf(struct t *t) { return mpf2(t, mpfr_hypot); }
456 int mphypotl(struct t *t) { return mpl2(t, mpfr_hypot); }
457 int mplgamma(struct t *t) { return mpd1(t, wrap_lgamma) || (t->i = mplgamma_sign, 0); }
458 int mplgammaf(struct t *t) { return mpf1(t, wrap_lgamma) || (t->i = mplgamma_sign, 0); }
459 int mplgammal(struct t *t) { return mpl1(t, wrap_lgamma) || (t->i = mplgamma_sign, 0); }
460 int mplog(struct t *t) { return mpd1(t, mpfr_log); }
461 int mplogf(struct t *t) { return mpf1(t, mpfr_log); }
462 int mplogl(struct t *t) { return mpl1(t, mpfr_log); }
463 int mplog10(struct t *t) { return mpd1(t, mpfr_log10); }
464 int mplog10f(struct t *t) { return mpf1(t, mpfr_log10); }
465 int mplog10l(struct t *t) { return mpl1(t, mpfr_log10); }
466 int mplog1p(struct t *t) { return mpd1(t, mpfr_log1p); }
467 int mplog1pf(struct t *t) { return mpf1(t, mpfr_log1p); }
468 int mplog1pl(struct t *t) { return mpl1(t, mpfr_log1p); }
469 int mplog2(struct t *t) { return mpd1(t, mpfr_log2); }
470 int mplog2f(struct t *t) { return mpf1(t, mpfr_log2); }
471 int mplog2l(struct t *t) { return mpl1(t, mpfr_log2); }
472 int mplogb(struct t *t)
473 {
474         MPFR_DECL_INIT(mx, 53);
475
476         mpfr_set_d(mx, t->x, MPFR_RNDN);
477         t->y = mpfr_get_exp(mx) - 1;
478         t->dy = 0;
479         t->e = 0;
480         return 0;
481 }
482 int mplogbf(struct t *t)
483 {
484         MPFR_DECL_INIT(mx, 24);
485
486         mpfr_set_flt(mx, t->x, MPFR_RNDN);
487         t->y = mpfr_get_exp(mx) - 1;
488         t->dy = 0;
489         t->e = 0;
490         return 0;
491 }
492 int mplogbl(struct t *t)
493 {
494         MPFR_DECL_INIT(mx, 64);
495
496         mpfr_set_ld(mx, t->x, MPFR_RNDN);
497         t->y = mpfr_get_exp(mx) - 1;
498         t->dy = 0;
499         t->e = 0;
500         return 0;
501 }
502 int mpnearbyint(struct t *t) { return mpd1(t, wrap_nearbyint) || (t->e&=~INEXACT, 0); }
503 int mpnearbyintf(struct t *t) { return mpf1(t, wrap_nearbyint) || (t->e&=~INEXACT, 0); }
504 int mpnearbyintl(struct t *t) { return mpl1(t, wrap_nearbyint) || (t->e&=~INEXACT, 0); }
505 // TODO: hard to implement with mpfr
506 int mpnextafter(struct t *t)
507 {
508         feclearexcept(FE_ALL_EXCEPT);
509         t->y = nextafter(t->x, t->x2);
510         t->e = getexcept();
511         t->dy = 0;
512         return 0;
513 }
514 int mpnextafterf(struct t *t)
515 {
516         feclearexcept(FE_ALL_EXCEPT);
517         t->y = nextafterf(t->x, t->x2);
518         t->e = getexcept();
519         t->dy = 0;
520         return 0;
521 }
522 int mpnextafterl(struct t *t)
523 {
524         feclearexcept(FE_ALL_EXCEPT);
525         t->y = nextafterl(t->x, t->x2);
526         t->e = getexcept();
527         t->dy = 0;
528         return 0;
529 }
530 int mpnexttoward(struct t *t)
531 {
532         feclearexcept(FE_ALL_EXCEPT);
533         t->y = nexttoward(t->x, t->x2);
534         t->e = getexcept();
535         t->dy = 0;
536         return 0;
537 }
538 int mpnexttowardf(struct t *t)
539 {
540         feclearexcept(FE_ALL_EXCEPT);
541         t->y = nexttowardf(t->x, t->x2);
542         t->e = getexcept();
543         t->dy = 0;
544         return 0;
545 }
546 int mpnexttowardl(struct t *t) { return mpnextafterl(t); }
547 int mppow(struct t *t) { return mpd2(t, mpfr_pow); }
548 int mppowf(struct t *t) { return mpf2(t, mpfr_pow); }
549 int mppowl(struct t *t) { return mpl2(t, mpfr_pow); }
550 int mpremainder(struct t *t) { return mpd2(t, mpfr_remainder); }
551 int mpremainderf(struct t *t) { return mpf2(t, mpfr_remainder); }
552 int mpremainderl(struct t *t) { return mpl2(t, mpfr_remainder); }
553 int mprint(struct t *t) { return mpd1(t, mpfr_rint); }
554 int mprintf(struct t *t) { return mpf1(t, mpfr_rint); }
555 int mprintl(struct t *t) { return mpl1(t, mpfr_rint); }
556 int mpround(struct t *t) { return mpd1(t, wrap_round); }
557 int mproundf(struct t *t) { return mpf1(t, wrap_round); }
558 int mproundl(struct t *t) { return mpl1(t, wrap_round); }
559 int mpsin(struct t *t) { return mpd1(t, mpfr_sin); }
560 int mpsinf(struct t *t) { return mpf1(t, mpfr_sin); }
561 int mpsinl(struct t *t) { return mpl1(t, mpfr_sin); }
562 int mpsinh(struct t *t) { return mpd1(t, mpfr_sinh); }
563 int mpsinhf(struct t *t) { return mpf1(t, mpfr_sinh); }
564 int mpsinhl(struct t *t) { return mpl1(t, mpfr_sinh); }
565 int mpsqrt(struct t *t) { return mpd1(t, mpfr_sqrt); }
566 int mpsqrtf(struct t *t) { return mpf1(t, mpfr_sqrt); }
567 int mpsqrtl(struct t *t) { return mpl1(t, mpfr_sqrt); }
568 int mptan(struct t *t) { return mpd1(t, mpfr_tan); }
569 int mptanf(struct t *t) { return mpf1(t, mpfr_tan); }
570 int mptanl(struct t *t) { return mpl1(t, mpfr_tan); }
571 int mptanh(struct t *t) { return mpd1(t, mpfr_tanh); }
572 int mptanhf(struct t *t) { return mpf1(t, mpfr_tanh); }
573 int mptanhl(struct t *t) { return mpl1(t, mpfr_tanh); }
574 int mptgamma(struct t *t) { return mpd1(t, mpfr_gamma); }
575 int mptgammaf(struct t *t) { return mpf1(t, mpfr_gamma); }
576 int mptgammal(struct t *t) { return mpl1(t, mpfr_gamma); }
577 int mptrunc(struct t *t) { return mpd1(t, wrap_trunc); }
578 int mptruncf(struct t *t) { return mpf1(t, wrap_trunc); }
579 int mptruncl(struct t *t) { return mpl1(t, wrap_trunc); }
580 int mpj0(struct t *t) { return mpd1(t, mpfr_j0); }
581 int mpj1(struct t *t) { return mpd1(t, mpfr_j1); }
582 int mpy0(struct t *t) { return mpd1(t, mpfr_y0); }
583 int mpy1(struct t *t) { return mpd1(t, mpfr_y1); }
584 // TODO: non standard functions
585 int mpscalb(struct t *t)
586 {
587         setupfenv(t->r);
588         t->y = scalb(t->x, t->x2);
589         t->e = getexcept();
590         t->dy = 0; // wrong
591         return 0;
592 }
593 int mpscalbf(struct t *t)
594 {
595         setupfenv(t->r);
596         t->y = scalbf(t->x, t->x2);
597         t->e = getexcept();
598         t->dy = 0; // wrong
599         return 0;
600 }
601 int mpj0f(struct t *t) { return mpf1(t, mpfr_j0); }
602 int mpj0l(struct t *t) { return mpl1(t, mpfr_j0); }
603 int mpj1f(struct t *t) { return mpf1(t, mpfr_j1); }
604 int mpj1l(struct t *t) { return mpl1(t, mpfr_j1); }
605 int mpy0f(struct t *t) { return mpf1(t, mpfr_y0); }
606 int mpy0l(struct t *t) { return mpl1(t, mpfr_y0); }
607 int mpy1f(struct t *t) { return mpf1(t, mpfr_y1); }
608 int mpy1l(struct t *t) { return mpl1(t, mpfr_y1); }
609 int mpexp10(struct t *t) { return mpd1(t, wrap_pow10); }
610 int mpexp10f(struct t *t) { return mpf1(t, wrap_pow10); }
611 int mpexp10l(struct t *t) { return mpl1(t, wrap_pow10); }
612 int mppow10(struct t *t) { return mpd1(t, wrap_pow10); }
613 int mppow10f(struct t *t) { return mpf1(t, wrap_pow10); }
614 int mppow10l(struct t *t) { return mpl1(t, wrap_pow10); }
615
616 int mpfrexp(struct t *t)
617 {
618         mpfr_exp_t e;
619         int k;
620         MPFR_DECL_INIT(mx, 53);
621
622         t->dy = 0;
623         t->y = 0;
624         mpsetup();
625         mpfr_clear_flags();
626         mpfr_set_d(mx, t->x, MPFR_RNDN);
627         k = mpfr_frexp(&e, mx, mx, t->r);
628         mpfr_subnormalize(mx, k, t->r);
629         t->y = mpfr_get_d(mx, MPFR_RNDN);
630         t->i = e;
631         t->e = eflags(isnan(t->x));
632         return 0;
633 }
634
635 int mpfrexpf(struct t *t)
636 {
637         mpfr_exp_t e;
638         int k;
639         MPFR_DECL_INIT(mx, 24);
640
641         t->dy = 0;
642         t->y = 0;
643         mpsetup();
644         mpfr_clear_flags();
645         mpfr_set_flt(mx, t->x, MPFR_RNDN);
646         k = mpfr_frexp(&e, mx, mx, t->r);
647         mpfr_subnormalize(mx, k, t->r);
648         t->y = mpfr_get_flt(mx, MPFR_RNDN);
649         t->i = e;
650         t->e = eflags(isnan(t->x));
651         return 0;
652 }
653
654 int mpfrexpl(struct t *t)
655 {
656         mpfr_exp_t e;
657         int k;
658         MPFR_DECL_INIT(mx, 64);
659
660         t->dy = 0;
661         t->y = 0;
662         mpsetup();
663         mpfr_clear_flags();
664         mpfr_set_ld(mx, t->x, MPFR_RNDN);
665         k = mpfr_frexp(&e, mx, mx, t->r);
666         mpfr_subnormalize(mx, k, t->r);
667         t->y = mpfr_get_ld(mx, MPFR_RNDN);
668         t->i = e;
669         t->e = eflags(isnan(t->x));
670         return 0;
671 }
672
673 int mpldexp(struct t *t)
674 {
675         int k;
676         MPFR_DECL_INIT(mx, 53);
677
678         t->dy = 0;
679         t->y = 0;
680         mpsetup();
681         mpfr_clear_flags();
682         mpfr_set_d(mx, t->x, MPFR_RNDN);
683         k = mpfr_mul_2si(mx, mx, t->i, t->r);
684         mpfr_subnormalize(mx, k, t->r);
685         t->y = mpfr_get_d(mx, MPFR_RNDN);
686         t->e = eflags(isnan(t->x));
687         return 0;
688 }
689
690 int mpldexpf(struct t *t)
691 {
692         int k;
693         MPFR_DECL_INIT(mx, 24);
694
695         t->dy = 0;
696         t->y = 0;
697         mpsetup();
698         mpfr_clear_flags();
699         mpfr_set_flt(mx, t->x, MPFR_RNDN);
700         k = mpfr_mul_2si(mx, mx, t->i, t->r);
701         mpfr_subnormalize(mx, k, t->r);
702         t->y = mpfr_get_flt(mx, MPFR_RNDN);
703         t->e = eflags(isnan(t->x));
704         return 0;
705 }
706
707 int mpldexpl(struct t *t)
708 {
709         int k;
710         MPFR_DECL_INIT(mx, 64);
711
712         t->dy = 0;
713         t->y = 0;
714         mpsetup();
715         mpfr_clear_flags();
716         mpfr_set_ld(mx, t->x, MPFR_RNDN);
717         k = mpfr_mul_2si(mx, mx, t->i, t->r);
718         mpfr_subnormalize(mx, k, t->r);
719         t->y = mpfr_get_ld(mx, MPFR_RNDN);
720         t->e = eflags(isnan(t->x));
721         return 0;
722 }
723
724 int mpscalbn(struct t *t) { return mpldexp(t); }
725 int mpscalbnf(struct t *t) { return mpldexpf(t); }
726 int mpscalbnl(struct t *t) { return mpldexpl(t); }
727 int mpscalbln(struct t *t) { return mpldexp(t); }
728 int mpscalblnf(struct t *t) { return mpldexpf(t); }
729 int mpscalblnl(struct t *t) { return mpldexpl(t); }
730
731 int mplgamma_r(struct t *t) { return mplgamma(t); }
732 int mplgammaf_r(struct t *t) { return mplgammaf(t); }
733 int mplgammal_r(struct t *t) { return mplgammal(t); }
734
735 int mpilogb(struct t *t)
736 {
737         MPFR_DECL_INIT(mx, 53);
738
739         mpfr_set_d(mx, t->x, MPFR_RNDN);
740         t->i = mpfr_get_exp(mx) - 1;
741         t->e = 0;
742         return 0;
743 }
744 int mpilogbf(struct t *t)
745 {
746         MPFR_DECL_INIT(mx, 24);
747
748         mpfr_set_flt(mx, t->x, MPFR_RNDN);
749         t->i = mpfr_get_exp(mx) - 1;
750         t->e = 0;
751         return 0;
752 }
753 int mpilogbl(struct t *t)
754 {
755         MPFR_DECL_INIT(mx, 64);
756
757         mpfr_set_ld(mx, t->x, MPFR_RNDN);
758         t->i = mpfr_get_exp(mx) - 1;
759         t->e = 0;
760         return 0;
761 }
762
763 // TODO: ll* is hard to do with mpfr
764 #define mp_f_i(n) \
765 int mp##n(struct t *t) \
766 { \
767         setupfenv(t->r); \
768         t->i = n(t->x); \
769         t->e = getexcept(); \
770         return 0; \
771 }
772
773 mp_f_i(llrint)
774 mp_f_i(llrintf)
775 mp_f_i(llrintl)
776 mp_f_i(lrint)
777 mp_f_i(lrintf)
778 mp_f_i(lrintl)
779 mp_f_i(llround)
780 mp_f_i(llroundf)
781 mp_f_i(llroundl)
782 mp_f_i(lround)
783 mp_f_i(lroundf)
784 mp_f_i(lroundl)
785
786 int mpmodf(struct t *t)
787 {
788         int e, r;
789
790         r = mpd1(t, wrap_trunc);
791         if (r)
792                 return r;
793         t->y2 = t->y;
794         t->dy2 = t->dy;
795         e = t->e;
796         r = mpd1(t, mpfr_frac);
797         t->e |= e;
798         return r;
799 }
800
801 int mpmodff(struct t *t)
802 {
803         int e, r;
804
805         r = mpf1(t, wrap_trunc);
806         if (r)
807                 return r;
808         t->y2 = t->y;
809         t->dy2 = t->dy;
810         e = t->e;
811         r = mpf1(t, mpfr_frac);
812         t->e |= e;
813         return r;
814 }
815
816 int mpmodfl(struct t *t)
817 {
818         int e, r;
819
820         r = mpl1(t, wrap_trunc);
821         if (r)
822                 return r;
823         t->y2 = t->y;
824         t->dy2 = t->dy;
825         e = t->e;
826         r = mpl1(t, mpfr_frac);
827         t->e |= e;
828         return r;
829 }
830
831 int mpsincos(struct t *t)
832 {
833         int e, r;
834
835         r = mpd1(t, mpfr_cos);
836         if (r)
837                 return r;
838         t->y2 = t->y;
839         t->dy2 = t->dy;
840         e = t->e;
841         r = mpd1(t, mpfr_sin);
842         t->e |= e;
843         return r;
844 }
845
846 int mpsincosf(struct t *t)
847 {
848         int e, r;
849
850         r = mpf1(t, mpfr_cos);
851         if (r)
852                 return r;
853         t->y2 = t->y;
854         t->dy2 = t->dy;
855         e = t->e;
856         r = mpf1(t, mpfr_sin);
857         t->e |= e;
858         return r;
859 }
860
861 int mpsincosl(struct t *t)
862 {
863         int e, r;
864
865         r = mpl1(t, mpfr_cos);
866         if (r)
867                 return r;
868         t->y2 = t->y;
869         t->dy2 = t->dy;
870         e = t->e;
871         r = mpl1(t, mpfr_sin);
872         t->e |= e;
873         return r;
874 }
875
876 int mpremquo(struct t *t) { return mpd2(t, wrap_remquo) || (t->i = mpremquo_q, 0); }
877 int mpremquof(struct t *t) { return mpf2(t, wrap_remquo) || (t->i = mpremquo_q, 0); }
878 int mpremquol(struct t *t) { return mpl2(t, wrap_remquo) || (t->i = mpremquo_q, 0); }
879
880 int mpfma(struct t *t)
881 {
882         int tn;
883         int r = rmap(t->r);
884         MPFR_DECL_INIT(mx, 53);
885         MPFR_DECL_INIT(mx2, 53);
886         MPFR_DECL_INIT(mx3, 53);
887         MPFR_DECL_INIT(my, 128);
888
889         mpsetup();
890         mpfr_clear_flags();
891         mpfr_set_d(mx, t->x, MPFR_RNDN);
892         mpfr_set_d(mx2, t->x2, MPFR_RNDN);
893         mpfr_set_d(mx3, t->x3, MPFR_RNDN);
894         tn = mpfr_fma(my, mx, mx2, mx3, r);
895         gend(t, my, tn, r);
896         if ((t->e & INEXACT) && nextafter(t->y, 0) == 0) {
897                 mpfr_set_emin(-(1<<20));
898                 tn = mpfr_fma(my, mx, mx2, mx3, r);
899                 mpfr_mul_2si(my, my, 1074, MPFR_RNDN);
900                 t->dy = scalbnl(t->y, 1074) - mpfr_get_ld(my, r);
901                 mpfr_set_emin(-1073);
902         }
903         return 0;
904 }
905
906 int mpfmaf(struct t *t)
907 {
908         int tn;
909         int r = rmap(t->r);
910         MPFR_DECL_INIT(mx, 24);
911         MPFR_DECL_INIT(mx2, 24);
912         MPFR_DECL_INIT(mx3, 24);
913         MPFR_DECL_INIT(my, 128);
914
915         mpsetupf();
916         mpfr_clear_flags();
917         mpfr_set_flt(mx, t->x, MPFR_RNDN);
918         mpfr_set_flt(mx2, t->x2, MPFR_RNDN);
919         mpfr_set_flt(mx3, t->x3, MPFR_RNDN);
920         tn = mpfr_fma(my, mx, mx2, mx3, r);
921         genf(t, my, tn, r);
922         if ((t->e & INEXACT) && nextafterf(t->y, 0) == 0) {
923                 mpfr_set_emin(-(1<<20));
924                 tn = mpfr_fma(my, mx, mx2, mx3, r);
925                 mpfr_mul_2si(my, my, 149, MPFR_RNDN);
926                 t->dy = scalbnl(t->y, 149) - mpfr_get_ld(my, r);
927                 mpfr_set_emin(-148);
928         }
929         return 0;
930 }
931
932 int mpfmal(struct t *t)
933 {
934 #if LDBL_MANT_DIG == 53
935         return mpfma(t);
936 #elif LDBL_MANT_DIG == 64
937         int tn;
938         int r = rmap(t->r);
939         MPFR_DECL_INIT(mx, 64);
940         MPFR_DECL_INIT(mx2, 64);
941         MPFR_DECL_INIT(mx3, 64);
942         MPFR_DECL_INIT(my, 128);
943
944         mpsetupl();
945         mpfr_clear_flags();
946         mpfr_set_ld(mx, t->x, MPFR_RNDN);
947         mpfr_set_ld(mx2, t->x2, MPFR_RNDN);
948         mpfr_set_ld(mx3, t->x3, MPFR_RNDN);
949         tn = mpfr_fma(my, mx, mx2, mx3, r);
950         genl(t, my, tn, r);
951         if ((t->e & INEXACT) && nextafterl(t->y, 0) == 0) {
952                 mpfr_set_emin(-(1<<20));
953                 tn = mpfr_fma(my, mx, mx2, mx3, r);
954                 mpfr_mul_2si(my, my, 16445, MPFR_RNDN);
955                 t->dy = scalbnl(t->y, 16445) - mpfr_get_ld(my, r);
956                 mpfr_set_emin(-16444);
957         }
958         return 0;
959 #else
960         return -1;
961 #endif
962 }
963