1 /* origin: FreeBSD /usr/src/lib/msun/src/s_remquol.c */
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6 * Developed at SunSoft, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
10 * ====================================================
14 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
15 long double remquol(long double x, long double y, int *quo)
17 return remquo(x, y, quo);
19 #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
21 #define BIAS (LDBL_MAX_EXP - 1)
23 #if LDBL_MANL_SIZE > 32
24 typedef uint64_t manl_t;
26 typedef uint32_t manl_t;
29 #if LDBL_MANH_SIZE > 32
30 typedef uint64_t manh_t;
32 typedef uint32_t manh_t;
36 * These macros add and remove an explicit integer bit in front of the
37 * fractional mantissa, if the architecture doesn't have such a bit by
40 #ifdef LDBL_IMPLICIT_NBIT
41 #define SET_NBIT(hx) ((hx) | (1ULL << LDBL_MANH_SIZE))
42 #define HFRAC_BITS LDBL_MANH_SIZE
44 #define SET_NBIT(hx) (hx)
45 #define HFRAC_BITS (LDBL_MANH_SIZE - 1)
48 #define MANL_SHIFT (LDBL_MANL_SIZE - 1)
50 static const long double Zero[] = {0.0L, -0.0L};
53 * Return the IEEE remainder and set *quo to the last n bits of the
54 * quotient, rounded to the nearest integer. We choose n=31 because
55 * we wind up computing all the integer bits of the quotient anyway as
56 * a side-effect of computing the remainder by the shift and subtract
57 * method. In practice, this is far more bits than are needed to use
58 * remquo in reduction algorithms.
61 * - The low part of the mantissa fits in a manl_t exactly.
62 * - The high part of the mantissa fits in an int64_t with enough room
63 * for an explicit integer bit in front of the fractional bits.
65 long double remquol(long double x, long double y, int *quo)
67 union IEEEl2bits ux, uy;
68 int64_t hx,hz; /* We need a carry bit even if LDBL_MANH_SIZE is 32. */
76 sxy = sx ^ uy.bits.sign;
77 ux.bits.sign = 0; /* |x| */
78 uy.bits.sign = 0; /* |y| */
81 /* purge off exception values */
82 if ((uy.bits.exp|uy.bits.manh|uy.bits.manl)==0 || /* y=0 */
83 (ux.bits.exp == BIAS + LDBL_MAX_EXP) || /* or x not finite */
84 (uy.bits.exp == BIAS + LDBL_MAX_EXP &&
85 ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)!=0)) /* or y is NaN */
87 if (ux.bits.exp <= uy.bits.exp) {
88 if ((ux.bits.exp < uy.bits.exp) ||
89 (ux.bits.manh <= uy.bits.manh &&
90 (ux.bits.manh < uy.bits.manh ||
91 ux.bits.manl < uy.bits.manl))) {
93 goto fixup; /* |x|<|y| return x or x-y */
95 if (ux.bits.manh == uy.bits.manh && ux.bits.manl == uy.bits.manl) {
97 return Zero[sx]; /* |x|=|y| return x*0*/
101 /* determine ix = ilogb(x) */
102 if (ux.bits.exp == 0) { /* subnormal x */
104 ix = ux.bits.exp - (BIAS + 512);
106 ix = ux.bits.exp - BIAS;
109 /* determine iy = ilogb(y) */
110 if (uy.bits.exp == 0) { /* subnormal y */
112 iy = uy.bits.exp - (BIAS + 512);
114 iy = uy.bits.exp - BIAS;
117 /* set up {hx,lx}, {hy,ly} and align y to x */
118 hx = SET_NBIT(ux.bits.manh);
119 hy = SET_NBIT(uy.bits.manh);
133 hx = hx + hx + (lx>>MANL_SHIFT);
136 hx = hz + hz + (lz>>MANL_SHIFT);
152 /* convert back to floating value and restore the sign */
153 if ((hx|lx) == 0) { /* return sign(x)*0 */
157 while (hx < (1ULL<<HFRAC_BITS)) { /* normalize x */
158 hx = hx + hx + (lx>>MANL_SHIFT);
162 ux.bits.manh = hx; /* The integer bit is truncated here if needed. */
164 if (iy < LDBL_MIN_EXP) {
165 ux.bits.exp = iy + (BIAS + 512);
168 ux.bits.exp = iy + BIAS;
174 if (y < LDBL_MIN * 2) {
175 if (x + x > y || (x + x == y && (q & 1))) {
179 } else if (x > 0.5*y || (x == 0.5*y && (q & 1))) {