1 /*- 2 * ==================================================== 3 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 4 * 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 8 * is preserved. 9 * ==================================================== 10 */ 11 12 #include <float.h> 13 #include <stdint.h> 14 15 #include "fpmath.h" 16 #include "math.h" 17 #include "math_private.h" 18 19 #define BIAS (LDBL_MAX_EXP - 1) 20 21 #if LDBL_MANL_SIZE > 32 22 typedef uint64_t manl_t; 23 #else 24 typedef uint32_t manl_t; 25 #endif 26 27 #if LDBL_MANH_SIZE > 32 28 typedef uint64_t manh_t; 29 #else 30 typedef uint32_t manh_t; 31 #endif 32 33 /* 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 36 * default already. 37 */ 38 #ifdef LDBL_IMPLICIT_NBIT 39 #define SET_NBIT(hx) ((hx) | (1ULL << LDBL_MANH_SIZE)) 40 #define HFRAC_BITS LDBL_MANH_SIZE 41 #else 42 #define SET_NBIT(hx) (hx) 43 #define HFRAC_BITS (LDBL_MANH_SIZE - 1) 44 #endif 45 46 #define MANL_SHIFT (LDBL_MANL_SIZE - 1) 47 48 static const long double Zero[] = {0.0L, -0.0L}; 49 50 /* 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. 57 * 58 * Assumptions: 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. 62 */ 63 long double 64 remquol(long double x, long double y, int *quo) 65 { 66 union IEEEl2bits ux, uy; 67 int64_t hx,hz; /* We need a carry bit even if LDBL_MANH_SIZE is 32. */ 68 manh_t hy; 69 manl_t lx,ly,lz; 70 int ix,iy,n,q,sx,sxy; 71 72 ux.e = x; 73 uy.e = y; 74 sx = ux.bits.sign; 75 sxy = sx ^ uy.bits.sign; 76 ux.bits.sign = 0; /* |x| */ 77 uy.bits.sign = 0; /* |y| */ 78 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))) { 90 q = 0; 91 goto fixup; /* |x|<|y| return x or x-y */ 92 } 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*/ 96 } 97 } 98 99 /* determine ix = ilogb(x) */ 100 if(ux.bits.exp == 0) { /* subnormal x */ 101 ux.e *= 0x1.0p512; 102 ix = ux.bits.exp - (BIAS + 512); 103 } else { 104 ix = ux.bits.exp - BIAS; 105 } 106 107 /* determine iy = ilogb(y) */ 108 if(uy.bits.exp == 0) { /* subnormal y */ 109 uy.e *= 0x1.0p512; 110 iy = uy.bits.exp - (BIAS + 512); 111 } else { 112 iy = uy.bits.exp - BIAS; 113 } 114 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); 118 lx = ux.bits.manl; 119 ly = uy.bits.manl; 120 121 /* fix point fmod */ 122 n = ix - iy; 123 q = 0; 124 while(n--) { 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++;} 128 q <<= 1; 129 } 130 hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1; 131 if(hz>=0) {hx=hz;lx=lz;q++;} 132 133 /* convert back to floating value and restore the sign */ 134 if((hx|lx)==0) { /* return sign(x)*0 */ 135 q &= 0x7fffffff; 136 *quo = (sxy ? -q : q); 137 return Zero[sx]; 138 } 139 while(hx<(1ULL<<HFRAC_BITS)) { /* normalize x */ 140 hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx; 141 iy -= 1; 142 } 143 ux.bits.manh = hx; /* The integer bit is truncated here if needed. */ 144 ux.bits.manl = lx; 145 if (iy < LDBL_MIN_EXP) { 146 ux.bits.exp = iy + (BIAS + 512); 147 ux.e *= 0x1p-512; 148 } else { 149 ux.bits.exp = iy + BIAS; 150 } 151 fixup: 152 x = ux.e; /* |x| */ 153 y = fabsl(y); 154 if (y < LDBL_MIN * 2) { 155 if (x+x>y || (x+x==y && (q & 1))) { 156 q++; 157 x-=y; 158 } 159 } else if (x>0.5*y || (x==0.5*y && (q & 1))) { 160 q++; 161 x-=y; 162 } 163 ux.e = x; 164 ux.bits.sign ^= sx; 165 x = ux.e; 166 q &= 0x7fffffff; 167 *quo = (sxy ? -q : q); 168 return x; 169 } 170