2 * ====================================================
3 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 * Developed at SunSoft, a Sun Microsystems, Inc. business.
6 * Permission to use, copy, modify, and distribute this
7 * software is freely granted, provided that this notice
9 * ====================================================
17 #include "math_private.h"
19 #define BIAS (LDBL_MAX_EXP - 1)
21 #if LDBL_MANL_SIZE > 32
22 typedef uint64_t manl_t;
24 typedef uint32_t manl_t;
27 #if LDBL_MANH_SIZE > 32
28 typedef uint64_t manh_t;
30 typedef uint32_t manh_t;
34 * These macros add and remove an explicit integer bit in front of the
35 * fractional mantissa, if the architecture doesn't have such a bit by
38 #ifdef LDBL_IMPLICIT_NBIT
39 #define SET_NBIT(hx) ((hx) | (1ULL << LDBL_MANH_SIZE))
40 #define HFRAC_BITS LDBL_MANH_SIZE
42 #define SET_NBIT(hx) (hx)
43 #define HFRAC_BITS (LDBL_MANH_SIZE - 1)
46 #define MANL_SHIFT (LDBL_MANL_SIZE - 1)
48 static const long double Zero[] = {0.0L, -0.0L};
51 * Return the IEEE remainder and set *quo to the last n bits of the
52 * quotient, rounded to the nearest integer. We choose n=31 because
53 * we wind up computing all the integer bits of the quotient anyway as
54 * a side-effect of computing the remainder by the shift and subtract
55 * method. In practice, this is far more bits than are needed to use
56 * remquo in reduction algorithms.
59 * - The low part of the mantissa fits in a manl_t exactly.
60 * - The high part of the mantissa fits in an int64_t with enough room
61 * for an explicit integer bit in front of the fractional bits.
64 remquol(long double x, long double y, int *quo)
66 union IEEEl2bits ux, uy;
67 int64_t hx,hz; /* We need a carry bit even if LDBL_MANH_SIZE is 32. */
75 sxy = sx ^ uy.bits.sign;
76 ux.bits.sign = 0; /* |x| */
77 uy.bits.sign = 0; /* |y| */
79 /* purge off exception values */
80 if((uy.bits.exp|uy.bits.manh|uy.bits.manl)==0 || /* y=0 */
81 (ux.bits.exp == BIAS + LDBL_MAX_EXP) || /* or x not finite */
82 (uy.bits.exp == BIAS + LDBL_MAX_EXP &&
83 ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)!=0)) /* or y is NaN */
84 return nan_mix_op(x, y, *)/nan_mix_op(x, y, *);
85 if(ux.bits.exp<=uy.bits.exp) {
86 if((ux.bits.exp<uy.bits.exp) ||
87 (ux.bits.manh<=uy.bits.manh &&
88 (ux.bits.manh<uy.bits.manh ||
89 ux.bits.manl<uy.bits.manl))) {
91 goto fixup; /* |x|<|y| return x or x-y */
93 if(ux.bits.manh==uy.bits.manh && ux.bits.manl==uy.bits.manl) {
94 *quo = (sxy ? -1 : 1);
95 return Zero[sx]; /* |x|=|y| return x*0*/
99 /* determine ix = ilogb(x) */
100 if(ux.bits.exp == 0) { /* subnormal x */
102 ix = ux.bits.exp - (BIAS + 512);
104 ix = ux.bits.exp - BIAS;
107 /* determine iy = ilogb(y) */
108 if(uy.bits.exp == 0) { /* subnormal y */
110 iy = uy.bits.exp - (BIAS + 512);
112 iy = uy.bits.exp - BIAS;
115 /* set up {hx,lx}, {hy,ly} and align y to x */
116 hx = SET_NBIT(ux.bits.manh);
117 hy = SET_NBIT(uy.bits.manh);
125 hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
126 if(hz<0){hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;}
127 else {hx = hz+hz+(lz>>MANL_SHIFT); lx = lz+lz; q++;}
130 hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
131 if(hz>=0) {hx=hz;lx=lz;q++;}
133 /* convert back to floating value and restore the sign */
134 if((hx|lx)==0) { /* return sign(x)*0 */
136 *quo = (sxy ? -q : q);
139 while(hx<(1ULL<<HFRAC_BITS)) { /* normalize x */
140 hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;
143 ux.bits.manh = hx; /* The integer bit is truncated here if needed. */
145 if (iy < LDBL_MIN_EXP) {
146 ux.bits.exp = iy + (BIAS + 512);
149 ux.bits.exp = iy + BIAS;
154 if (y < LDBL_MIN * 2) {
155 if (x+x>y || (x+x==y && (q & 1))) {
159 } else if (x>0.5*y || (x==0.5*y && (q & 1))) {
167 *quo = (sxy ? -q : q);