10 case RN: return MPFR_RNDN;
11 case RZ: return MPFR_RNDZ;
12 case RD: return MPFR_RNDD;
13 case RU: return MPFR_RNDU;
20 mpfr_out_str(stdout, 10, 0, x, MPFR_RNDN);
34 #if LDBL_MANT_DIG == 64
37 mpfr_set_emin(-16444);
43 round x into y considering x is already rounded (t = up or down)
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
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)
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);
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);
75 return mpfr_set(y, x, r);
78 static int adjust(mpfr_t mr, mpfr_t my, int t, int r)
81 //printf("adj %d\n", t);
83 t = adjust_round(mr, my, t, r);
84 //printf("rnd %d\n", t);
86 t = mpfr_subnormalize(mr, t, r);
87 //printf("sub %d\n", t);
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);
100 //static int eflags(mpfr_t mr, mpfr_t my, int t)
101 static int eflags(int naninput)
105 if (mpfr_inexflag_p())
107 // if (mpfr_underflow_p() && (t || mpfr_cmp(mr, my) != 0))
108 if (mpfr_underflow_p() && i)
110 if (mpfr_overflow_p())
114 if (!naninput && (mpfr_nanflag_p() || mpfr_erangeflag_p()))
119 static void genf(struct t *p, mpfr_t my, int t, int r)
121 MPFR_DECL_INIT(mr, 24);
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));
128 if (!isfinite(p->y)) {
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);
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);
142 static int mpf1(struct t *p, int (*fmp)(mpfr_t, const mpfr_t, mpfr_rnd_t))
146 MPFR_DECL_INIT(mx, 24);
147 MPFR_DECL_INIT(my, 128);
151 mpfr_set_flt(mx, p->x, MPFR_RNDN);
155 if ((p->e & INEXACT) && nextafterf(p->y, 0) == 0) {
156 mpfr_set_emin(-(1<<20));
158 mpfr_mul_2si(my, my, 149, MPFR_RNDN);
159 p->dy = scalbnl(p->y, 149) - mpfr_get_ld(my, r);
165 static int mpf2(struct t *p, int (*fmp)(mpfr_t, const mpfr_t, const mpfr_t, mpfr_rnd_t))
169 MPFR_DECL_INIT(mx, 24);
170 MPFR_DECL_INIT(mx2, 24);
171 MPFR_DECL_INIT(my, 128);
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);
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);
189 static void gend(struct t *p, mpfr_t my, int t, int r)
191 MPFR_DECL_INIT(mr, 53);
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));
198 if (!isfinite(p->y)) {
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);
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);
212 static int mpd1(struct t *p, int (*fmp)(mpfr_t, const mpfr_t, mpfr_rnd_t))
216 MPFR_DECL_INIT(mx, 53);
217 MPFR_DECL_INIT(my, 128);
221 mpfr_set_d(mx, p->x, MPFR_RNDN);
223 //printf("underflow: %d\n", mpfr_underflow_p());
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));
230 mpfr_mul_2si(my, my, 1074, MPFR_RNDN);
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);
239 static int mpd2(struct t *p, int (*fmp)(mpfr_t, const mpfr_t, const mpfr_t, mpfr_rnd_t))
243 MPFR_DECL_INIT(mx, 53);
244 MPFR_DECL_INIT(mx2, 53);
245 MPFR_DECL_INIT(my, 128);
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);
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);
263 #if LDBL_MANT_DIG == 64
264 static void genl(struct t *p, mpfr_t my, int t, int r)
266 MPFR_DECL_INIT(mr, 64);
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));
273 if (!isfinite(p->y)) {
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);
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);
288 static int mpl1(struct t *p, int (*fmp)(mpfr_t, const mpfr_t, mpfr_rnd_t))
290 #if LDBL_MANT_DIG == 53
292 #elif LDBL_MANT_DIG == 64
295 MPFR_DECL_INIT(mx, 64);
296 MPFR_DECL_INIT(my, 128);
300 mpfr_set_ld(mx, p->x, MPFR_RNDN);
304 if ((p->e & INEXACT) && nextafterl(p->y, 0) == 0) {
305 mpfr_set_emin(-(1<<20));
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);
317 static int mpl2(struct t *p, int (*fmp)(mpfr_t, const mpfr_t, const mpfr_t, mpfr_rnd_t))
319 #if LDBL_MANT_DIG == 53
321 #elif LDBL_MANT_DIG == 64
324 MPFR_DECL_INIT(mx, 64);
325 MPFR_DECL_INIT(mx2, 64);
326 MPFR_DECL_INIT(my, 128);
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);
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);
348 static int mplgamma_sign;
349 static int wrap_lgamma(mpfr_t my, const mpfr_t mx, mpfr_rnd_t r)
351 return mpfr_lgamma(my, &mplgamma_sign, mx, r);
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)
356 return mpfr_remquo(my, &mpremquo_q, mx, mx2, r);
358 static int mpbessel_n;
359 static int wrap_jn(mpfr_t my, const mpfr_t mx, mpfr_rnd_t r)
361 return mpfr_jn(my, mpbessel_n, mx, r);
363 static int wrap_yn(mpfr_t my, const mpfr_t mx, mpfr_rnd_t r)
365 return mpfr_yn(my, mpbessel_n, mx, r);
367 static int wrap_ceil(mpfr_t my, const mpfr_t mx, mpfr_rnd_t r)
369 return mpfr_ceil(my, mx);
371 static int wrap_floor(mpfr_t my, const mpfr_t mx, mpfr_rnd_t r)
373 return mpfr_floor(my, mx);
375 static int wrap_round(mpfr_t my, const mpfr_t mx, mpfr_rnd_t r)
377 return mpfr_round(my, mx);
379 static int wrap_trunc(mpfr_t my, const mpfr_t mx, mpfr_rnd_t r)
381 return mpfr_trunc(my, mx);
383 static int wrap_nearbyint(mpfr_t my, const mpfr_t mx, mpfr_rnd_t r)
385 int i = mpfr_rint(my, mx, r);
386 mpfr_clear_inexflag();
389 static int wrap_pow10(mpfr_t my, const mpfr_t mx, mpfr_rnd_t r)
391 return mpfr_ui_pow(my, 10, mx, r);
394 int mpacos(struct t *t) { return mpd1(t, mpfr_acos); }
395 int mpacosf(struct t *t) { return mpf1(t, mpfr_acos); }
396 int mpacosl(struct t *t) { return mpl1(t, mpfr_acos); }
397 int mpacosh(struct t *t) { return mpd1(t, mpfr_acosh); }
398 int mpacoshf(struct t *t) { return mpf1(t, mpfr_acosh); }
399 int mpacoshl(struct t *t) { return mpl1(t, mpfr_acosh); }
400 int mpasin(struct t *t) { return mpd1(t, mpfr_asin); }
401 int mpasinf(struct t *t) { return mpf1(t, mpfr_asin); }
402 int mpasinl(struct t *t) { return mpl1(t, mpfr_asin); }
403 int mpasinh(struct t *t) { return mpd1(t, mpfr_asinh); }
404 int mpasinhf(struct t *t) { return mpf1(t, mpfr_asinh); }
405 int mpasinhl(struct t *t) { return mpl1(t, mpfr_asinh); }
406 int mpatan(struct t *t) { return mpd1(t, mpfr_atan); }
407 int mpatanf(struct t *t) { return mpf1(t, mpfr_atan); }
408 int mpatanl(struct t *t) { return mpl1(t, mpfr_atan); }
409 int mpatan2(struct t *t) { return mpd2(t, mpfr_atan2); }
410 int mpatan2f(struct t *t) { return mpf2(t, mpfr_atan2); }
411 int mpatan2l(struct t *t) { return mpl2(t, mpfr_atan2); }
412 int mpatanh(struct t *t) { return mpd1(t, mpfr_atanh); }
413 int mpatanhf(struct t *t) { return mpf1(t, mpfr_atanh); }
414 int mpatanhl(struct t *t) { return mpl1(t, mpfr_atanh); }
415 int mpcbrt(struct t *t) { return mpd1(t, mpfr_cbrt); }
416 int mpcbrtf(struct t *t) { return mpf1(t, mpfr_cbrt); }
417 int mpcbrtl(struct t *t) { return mpl1(t, mpfr_cbrt); }
418 int mpceil(struct t *t) { return mpd1(t, wrap_ceil); }
419 int mpceilf(struct t *t) { return mpf1(t, wrap_ceil); }
420 int mpceill(struct t *t) { return mpl1(t, wrap_ceil); }
421 int mpcopysign(struct t *t) { return mpd2(t, mpfr_copysign); }
422 int mpcopysignf(struct t *t) { return mpf2(t, mpfr_copysign); }
423 int mpcopysignl(struct t *t) { return mpl2(t, mpfr_copysign); }
424 int mpcos(struct t *t) { return mpd1(t, mpfr_cos); }
425 int mpcosf(struct t *t) { return mpf1(t, mpfr_cos); }
426 int mpcosl(struct t *t) { return mpl1(t, mpfr_cos); }
427 int mpcosh(struct t *t) { return mpd1(t, mpfr_cosh); }
428 int mpcoshf(struct t *t) { return mpf1(t, mpfr_cosh); }
429 int mpcoshl(struct t *t) { return mpl1(t, mpfr_cosh); }
430 int mperf(struct t *t) { return mpd1(t, mpfr_erf); }
431 int mperff(struct t *t) { return mpf1(t, mpfr_erf); }
432 int mperfl(struct t *t) { return mpl1(t, mpfr_erf); }
433 int mperfc(struct t *t) { return mpd1(t, mpfr_erfc); }
434 int mperfcf(struct t *t) { return mpf1(t, mpfr_erfc); }
435 int mperfcl(struct t *t) { return mpl1(t, mpfr_erfc); }
436 int mpexp(struct t *t) { return mpd1(t, mpfr_exp); }
437 int mpexpf(struct t *t) { return mpf1(t, mpfr_exp); }
438 int mpexpl(struct t *t) { return mpl1(t, mpfr_exp); }
439 int mpexp2(struct t *t) { return mpd1(t, mpfr_exp2); }
440 int mpexp2f(struct t *t) { return mpf1(t, mpfr_exp2); }
441 int mpexp2l(struct t *t) { return mpl1(t, mpfr_exp2); }
442 int mpexpm1(struct t *t) { return mpd1(t, mpfr_expm1); }
443 int mpexpm1f(struct t *t) { return mpf1(t, mpfr_expm1); }
444 int mpexpm1l(struct t *t) { return mpl1(t, mpfr_expm1); }
445 int mpfabs(struct t *t) { return mpd1(t, mpfr_abs); }
446 int mpfabsf(struct t *t) { return mpf1(t, mpfr_abs); }
447 int mpfabsl(struct t *t) { return mpl1(t, mpfr_abs); }
448 int mpfdim(struct t *t) { return mpd2(t, mpfr_dim); }
449 int mpfdimf(struct t *t) { return mpf2(t, mpfr_dim); }
450 int mpfdiml(struct t *t) { return mpl2(t, mpfr_dim); }
451 int mpfloor(struct t *t) { return mpd1(t, wrap_floor); }
452 int mpfloorf(struct t *t) { return mpf1(t, wrap_floor); }
453 int mpfloorl(struct t *t) { return mpl1(t, wrap_floor); }
454 int mpfmax(struct t *t) { return mpd2(t, mpfr_max); }
455 int mpfmaxf(struct t *t) { return mpf2(t, mpfr_max); }
456 int mpfmaxl(struct t *t) { return mpl2(t, mpfr_max); }
457 int mpfmin(struct t *t) { return mpd2(t, mpfr_min); }
458 int mpfminf(struct t *t) { return mpf2(t, mpfr_min); }
459 int mpfminl(struct t *t) { return mpl2(t, mpfr_min); }
460 int mpfmod(struct t *t) { return mpd2(t, mpfr_fmod); }
461 int mpfmodf(struct t *t) { return mpf2(t, mpfr_fmod); }
462 int mpfmodl(struct t *t) { return mpl2(t, mpfr_fmod); }
463 int mphypot(struct t *t) { return mpd2(t, mpfr_hypot); }
464 int mphypotf(struct t *t) { return mpf2(t, mpfr_hypot); }
465 int mphypotl(struct t *t) { return mpl2(t, mpfr_hypot); }
466 int mplgamma(struct t *t) { return mpd1(t, wrap_lgamma) || (t->i = mplgamma_sign, 0); }
467 int mplgammaf(struct t *t) { return mpf1(t, wrap_lgamma) || (t->i = mplgamma_sign, 0); }
468 int mplgammal(struct t *t) { return mpl1(t, wrap_lgamma) || (t->i = mplgamma_sign, 0); }
469 int mplog(struct t *t) { return mpd1(t, mpfr_log); }
470 int mplogf(struct t *t) { return mpf1(t, mpfr_log); }
471 int mplogl(struct t *t) { return mpl1(t, mpfr_log); }
472 int mplog10(struct t *t) { return mpd1(t, mpfr_log10); }
473 int mplog10f(struct t *t) { return mpf1(t, mpfr_log10); }
474 int mplog10l(struct t *t) { return mpl1(t, mpfr_log10); }
475 int mplog1p(struct t *t) { return mpd1(t, mpfr_log1p); }
476 int mplog1pf(struct t *t) { return mpf1(t, mpfr_log1p); }
477 int mplog1pl(struct t *t) { return mpl1(t, mpfr_log1p); }
478 int mplog2(struct t *t) { return mpd1(t, mpfr_log2); }
479 int mplog2f(struct t *t) { return mpf1(t, mpfr_log2); }
480 int mplog2l(struct t *t) { return mpl1(t, mpfr_log2); }
481 int mplogb(struct t *t)
483 MPFR_DECL_INIT(mx, 53);
500 mpfr_set_d(mx, t->x, MPFR_RNDN);
501 t->y = mpfr_get_exp(mx) - 1;
504 int mplogbf(struct t *t)
506 MPFR_DECL_INIT(mx, 24);
523 mpfr_set_flt(mx, t->x, MPFR_RNDN);
524 t->y = mpfr_get_exp(mx) - 1;
527 int mplogbl(struct t *t)
529 MPFR_DECL_INIT(mx, 64);
546 mpfr_set_ld(mx, t->x, MPFR_RNDN);
547 t->y = mpfr_get_exp(mx) - 1;
550 int mpnearbyint(struct t *t) { return mpd1(t, wrap_nearbyint) || (t->e&=~INEXACT, 0); }
551 int mpnearbyintf(struct t *t) { return mpf1(t, wrap_nearbyint) || (t->e&=~INEXACT, 0); }
552 int mpnearbyintl(struct t *t) { return mpl1(t, wrap_nearbyint) || (t->e&=~INEXACT, 0); }
553 // TODO: hard to implement with mpfr
554 int mpnextafter(struct t *t)
556 feclearexcept(FE_ALL_EXCEPT);
557 t->y = nextafter(t->x, t->x2);
562 int mpnextafterf(struct t *t)
564 feclearexcept(FE_ALL_EXCEPT);
565 t->y = nextafterf(t->x, t->x2);
570 int mpnextafterl(struct t *t)
572 feclearexcept(FE_ALL_EXCEPT);
573 t->y = nextafterl(t->x, t->x2);
578 int mpnexttoward(struct t *t)
580 feclearexcept(FE_ALL_EXCEPT);
581 t->y = nexttoward(t->x, t->x2);
586 int mpnexttowardf(struct t *t)
588 feclearexcept(FE_ALL_EXCEPT);
589 t->y = nexttowardf(t->x, t->x2);
594 int mpnexttowardl(struct t *t) { return mpnextafterl(t); }
595 int mppow(struct t *t) { return mpd2(t, mpfr_pow); }
596 int mppowf(struct t *t) { return mpf2(t, mpfr_pow); }
597 int mppowl(struct t *t) { return mpl2(t, mpfr_pow); }
598 int mpremainder(struct t *t) { return mpd2(t, mpfr_remainder); }
599 int mpremainderf(struct t *t) { return mpf2(t, mpfr_remainder); }
600 int mpremainderl(struct t *t) { return mpl2(t, mpfr_remainder); }
601 int mprint(struct t *t) { return mpd1(t, mpfr_rint); }
602 int mprintf(struct t *t) { return mpf1(t, mpfr_rint); }
603 int mprintl(struct t *t) { return mpl1(t, mpfr_rint); }
604 int mpround(struct t *t) { return mpd1(t, wrap_round); }
605 int mproundf(struct t *t) { return mpf1(t, wrap_round); }
606 int mproundl(struct t *t) { return mpl1(t, wrap_round); }
607 int mpsin(struct t *t) { return mpd1(t, mpfr_sin); }
608 int mpsinf(struct t *t) { return mpf1(t, mpfr_sin); }
609 int mpsinl(struct t *t) { return mpl1(t, mpfr_sin); }
610 int mpsinh(struct t *t) { return mpd1(t, mpfr_sinh); }
611 int mpsinhf(struct t *t) { return mpf1(t, mpfr_sinh); }
612 int mpsinhl(struct t *t) { return mpl1(t, mpfr_sinh); }
613 int mpsqrt(struct t *t) { return mpd1(t, mpfr_sqrt); }
614 int mpsqrtf(struct t *t) { return mpf1(t, mpfr_sqrt); }
615 int mpsqrtl(struct t *t) { return mpl1(t, mpfr_sqrt); }
616 int mptan(struct t *t) { return mpd1(t, mpfr_tan); }
617 int mptanf(struct t *t) { return mpf1(t, mpfr_tan); }
618 int mptanl(struct t *t) { return mpl1(t, mpfr_tan); }
619 int mptanh(struct t *t) { return mpd1(t, mpfr_tanh); }
620 int mptanhf(struct t *t) { return mpf1(t, mpfr_tanh); }
621 int mptanhl(struct t *t) { return mpl1(t, mpfr_tanh); }
622 // TODO: tgamma(2) raises wrong flags
623 int mptgamma(struct t *t) { return mpd1(t, mpfr_gamma); }
624 int mptgammaf(struct t *t) { return mpf1(t, mpfr_gamma); }
625 int mptgammal(struct t *t) { return mpl1(t, mpfr_gamma); }
626 int mptrunc(struct t *t) { return mpd1(t, wrap_trunc); }
627 int mptruncf(struct t *t) { return mpf1(t, wrap_trunc); }
628 int mptruncl(struct t *t) { return mpl1(t, wrap_trunc); }
629 int mpj0(struct t *t) { return mpd1(t, mpfr_j0); }
630 int mpj1(struct t *t) { return mpd1(t, mpfr_j1); }
631 int mpy0(struct t *t) { return mpd1(t, mpfr_y0); }
632 int mpy1(struct t *t) { return mpd1(t, mpfr_y1); }
633 // TODO: non standard functions
634 int mpscalb(struct t *t)
637 t->y = scalb(t->x, t->x2);
642 int mpscalbf(struct t *t)
645 t->y = scalbf(t->x, t->x2);
650 int mpj0f(struct t *t) { return mpf1(t, mpfr_j0); }
651 int mpj0l(struct t *t) { return mpl1(t, mpfr_j0); }
652 int mpj1f(struct t *t) { return mpf1(t, mpfr_j1); }
653 int mpj1l(struct t *t) { return mpl1(t, mpfr_j1); }
654 int mpy0f(struct t *t) { return mpf1(t, mpfr_y0); }
655 int mpy0l(struct t *t) { return mpl1(t, mpfr_y0); }
656 int mpy1f(struct t *t) { return mpf1(t, mpfr_y1); }
657 int mpy1l(struct t *t) { return mpl1(t, mpfr_y1); }
658 int mpexp10(struct t *t) { return mpd1(t, wrap_pow10); }
659 int mpexp10f(struct t *t) { return mpf1(t, wrap_pow10); }
660 int mpexp10l(struct t *t) { return mpl1(t, wrap_pow10); }
661 int mppow10(struct t *t) { return mpd1(t, wrap_pow10); }
662 int mppow10f(struct t *t) { return mpf1(t, wrap_pow10); }
663 int mppow10l(struct t *t) { return mpl1(t, wrap_pow10); }
665 int mpfrexp(struct t *t)
669 MPFR_DECL_INIT(mx, 53);
675 mpfr_set_d(mx, t->x, MPFR_RNDN);
676 k = mpfr_frexp(&e, mx, mx, t->r);
677 mpfr_subnormalize(mx, k, t->r);
678 t->y = mpfr_get_d(mx, MPFR_RNDN);
680 t->e = eflags(isnan(t->x));
684 int mpfrexpf(struct t *t)
688 MPFR_DECL_INIT(mx, 24);
694 mpfr_set_flt(mx, t->x, MPFR_RNDN);
695 k = mpfr_frexp(&e, mx, mx, t->r);
696 mpfr_subnormalize(mx, k, t->r);
697 t->y = mpfr_get_flt(mx, MPFR_RNDN);
699 t->e = eflags(isnan(t->x));
703 int mpfrexpl(struct t *t)
707 MPFR_DECL_INIT(mx, 64);
713 mpfr_set_ld(mx, t->x, MPFR_RNDN);
714 k = mpfr_frexp(&e, mx, mx, t->r);
715 mpfr_subnormalize(mx, k, t->r);
716 t->y = mpfr_get_ld(mx, MPFR_RNDN);
718 t->e = eflags(isnan(t->x));
722 int mpldexp(struct t *t)
725 MPFR_DECL_INIT(mx, 53);
731 mpfr_set_d(mx, t->x, MPFR_RNDN);
732 k = mpfr_mul_2si(mx, mx, t->i, t->r);
733 mpfr_subnormalize(mx, k, t->r);
734 t->y = mpfr_get_d(mx, MPFR_RNDN);
735 t->e = eflags(isnan(t->x));
739 int mpldexpf(struct t *t)
742 MPFR_DECL_INIT(mx, 24);
748 mpfr_set_flt(mx, t->x, MPFR_RNDN);
749 k = mpfr_mul_2si(mx, mx, t->i, t->r);
750 mpfr_subnormalize(mx, k, t->r);
751 t->y = mpfr_get_flt(mx, MPFR_RNDN);
752 t->e = eflags(isnan(t->x));
756 int mpldexpl(struct t *t)
759 MPFR_DECL_INIT(mx, 64);
765 mpfr_set_ld(mx, t->x, MPFR_RNDN);
766 k = mpfr_mul_2si(mx, mx, t->i, t->r);
767 mpfr_subnormalize(mx, k, t->r);
768 t->y = mpfr_get_ld(mx, MPFR_RNDN);
769 t->e = eflags(isnan(t->x));
773 int mpscalbn(struct t *t) { return mpldexp(t); }
774 int mpscalbnf(struct t *t) { return mpldexpf(t); }
775 int mpscalbnl(struct t *t) { return mpldexpl(t); }
776 int mpscalbln(struct t *t) { return mpldexp(t); }
777 int mpscalblnf(struct t *t) { return mpldexpf(t); }
778 int mpscalblnl(struct t *t) { return mpldexpl(t); }
780 int mplgamma_r(struct t *t) { return mplgamma(t); }
781 int mplgammaf_r(struct t *t) { return mplgammaf(t); }
782 int mplgammal_r(struct t *t) { return mplgammal(t); }
784 int mpilogb(struct t *t)
786 MPFR_DECL_INIT(mx, 53);
788 mpfr_set_d(mx, t->x, MPFR_RNDN);
789 t->i = mpfr_get_exp(mx) - 1;
791 if (isinf(t->x) || isnan(t->x) || t->x == 0)
795 int mpilogbf(struct t *t)
797 MPFR_DECL_INIT(mx, 24);
799 mpfr_set_flt(mx, t->x, MPFR_RNDN);
800 t->i = mpfr_get_exp(mx) - 1;
802 if (isinf(t->x) || isnan(t->x) || t->x == 0)
806 int mpilogbl(struct t *t)
808 MPFR_DECL_INIT(mx, 64);
810 mpfr_set_ld(mx, t->x, MPFR_RNDN);
811 t->i = mpfr_get_exp(mx) - 1;
813 if (isinf(t->x) || isnan(t->x) || t->x == 0)
818 // TODO: ll* is hard to do with mpfr
820 int mp##n(struct t *t) \
824 t->e = getexcept(); \
841 int mpmodf(struct t *t)
845 r = mpd1(t, wrap_trunc);
851 r = mpd1(t, mpfr_frac);
856 int mpmodff(struct t *t)
860 r = mpf1(t, wrap_trunc);
866 r = mpf1(t, mpfr_frac);
871 int mpmodfl(struct t *t)
875 r = mpl1(t, wrap_trunc);
881 r = mpl1(t, mpfr_frac);
886 int mpsincos(struct t *t)
890 r = mpd1(t, mpfr_cos);
896 r = mpd1(t, mpfr_sin);
901 int mpsincosf(struct t *t)
905 r = mpf1(t, mpfr_cos);
911 r = mpf1(t, mpfr_sin);
916 int mpsincosl(struct t *t)
920 r = mpl1(t, mpfr_cos);
926 r = mpl1(t, mpfr_sin);
931 int mpremquo(struct t *t) { return mpd2(t, wrap_remquo) || (t->i = mpremquo_q, 0); }
932 int mpremquof(struct t *t) { return mpf2(t, wrap_remquo) || (t->i = mpremquo_q, 0); }
933 int mpremquol(struct t *t) { return mpl2(t, wrap_remquo) || (t->i = mpremquo_q, 0); }
935 int mpfma(struct t *t)
939 MPFR_DECL_INIT(mx, 53);
940 MPFR_DECL_INIT(mx2, 53);
941 MPFR_DECL_INIT(mx3, 53);
942 MPFR_DECL_INIT(my, 128);
946 mpfr_set_d(mx, t->x, MPFR_RNDN);
947 mpfr_set_d(mx2, t->x2, MPFR_RNDN);
948 mpfr_set_d(mx3, t->x3, MPFR_RNDN);
949 tn = mpfr_fma(my, mx, mx2, mx3, r);
951 if ((t->e & INEXACT) && nextafter(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, 1074, MPFR_RNDN);
955 t->dy = scalbnl(t->y, 1074) - mpfr_get_ld(my, r);
956 mpfr_set_emin(-1073);
961 int mpfmaf(struct t *t)
965 MPFR_DECL_INIT(mx, 24);
966 MPFR_DECL_INIT(mx2, 24);
967 MPFR_DECL_INIT(mx3, 24);
968 MPFR_DECL_INIT(my, 128);
972 mpfr_set_flt(mx, t->x, MPFR_RNDN);
973 mpfr_set_flt(mx2, t->x2, MPFR_RNDN);
974 mpfr_set_flt(mx3, t->x3, MPFR_RNDN);
975 tn = mpfr_fma(my, mx, mx2, mx3, r);
977 if ((t->e & INEXACT) && nextafterf(t->y, 0) == 0) {
978 mpfr_set_emin(-(1<<20));
979 tn = mpfr_fma(my, mx, mx2, mx3, r);
980 mpfr_mul_2si(my, my, 149, MPFR_RNDN);
981 t->dy = scalbnl(t->y, 149) - mpfr_get_ld(my, r);
987 int mpfmal(struct t *t)
989 #if LDBL_MANT_DIG == 53
991 #elif LDBL_MANT_DIG == 64
994 MPFR_DECL_INIT(mx, 64);
995 MPFR_DECL_INIT(mx2, 64);
996 MPFR_DECL_INIT(mx3, 64);
997 MPFR_DECL_INIT(my, 128);
1001 mpfr_set_ld(mx, t->x, MPFR_RNDN);
1002 mpfr_set_ld(mx2, t->x2, MPFR_RNDN);
1003 mpfr_set_ld(mx3, t->x3, MPFR_RNDN);
1004 tn = mpfr_fma(my, mx, mx2, mx3, r);
1006 if ((t->e & INEXACT) && nextafterl(t->y, 0) == 0) {
1007 mpfr_set_emin(-(1<<20));
1008 tn = mpfr_fma(my, mx, mx2, mx3, r);
1009 mpfr_mul_2si(my, my, 16445, MPFR_RNDN);
1010 t->dy = scalbnl(t->y, 16445) - mpfr_get_ld(my, r);
1011 mpfr_set_emin(-16444);
1019 int mpjn(struct t *t) { mpbessel_n = t->i; return mpd1(t, wrap_jn); }
1020 int mpjnf(struct t *t) { mpbessel_n = t->i; return mpf1(t, wrap_jn); }
1021 int mpjnl(struct t *t) { mpbessel_n = t->i; return mpl1(t, wrap_jn); }
1022 int mpyn(struct t *t) { mpbessel_n = t->i; return mpd1(t, wrap_yn); }
1023 int mpynf(struct t *t) { mpbessel_n = t->i; return mpf1(t, wrap_yn); }
1024 int mpynl(struct t *t) { mpbessel_n = t->i; return mpl1(t, wrap_yn); }