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 <sys/cdefs.h> 13 #include <float.h> 14 #include <stdint.h> 15 16 #include "fpmath.h" 17 #include "math.h" 18 #include "math_private.h" 19 20 #define BIAS (LDBL_MAX_EXP - 1) 21 22 #if LDBL_MANL_SIZE > 32 23 typedef uint64_t manl_t; 24 #else 25 typedef uint32_t manl_t; 26 #endif 27 28 #if LDBL_MANH_SIZE > 32 29 typedef uint64_t manh_t; 30 #else 31 typedef uint32_t manh_t; 32 #endif 33 34 /* 35 * These macros add and remove an explicit integer bit in front of the 36 * fractional mantissa, if the architecture doesn't have such a bit by 37 * default already. 38 */ 39 #ifdef LDBL_IMPLICIT_NBIT 40 #define SET_NBIT(hx) ((hx) | (1ULL << LDBL_MANH_SIZE)) 41 #define HFRAC_BITS LDBL_MANH_SIZE 42 #else 43 #define SET_NBIT(hx) (hx) 44 #define HFRAC_BITS (LDBL_MANH_SIZE - 1) 45 #endif 46 47 #define MANL_SHIFT (LDBL_MANL_SIZE - 1) 48 49 static const long double one = 1.0, Zero[] = {0.0, -0.0,}; 50 51 /* 52 * fmodl(x,y) 53 * Return x mod y in exact arithmetic 54 * Method: shift and subtract 55 * 56 * Assumptions: 57 * - The low part of the mantissa fits in a manl_t exactly. 58 * - The high part of the mantissa fits in an int64_t with enough room 59 * for an explicit integer bit in front of the fractional bits. 60 */ 61 long double 62 fmodl(long double x, long double y) 63 { 64 union IEEEl2bits ux, uy; 65 int64_t hx,hz; /* We need a carry bit even if LDBL_MANH_SIZE is 32. */ 66 manh_t hy; 67 manl_t lx,ly,lz; 68 int ix,iy,n,sx; 69 70 ux.e = x; 71 uy.e = y; 72 sx = ux.bits.sign; 73 74 /* purge off exception values */ 75 if((uy.bits.exp|uy.bits.manh|uy.bits.manl)==0 || /* y=0 */ 76 (ux.bits.exp == BIAS + LDBL_MAX_EXP) || /* or x not finite */ 77 (uy.bits.exp == BIAS + LDBL_MAX_EXP && 78 ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)!=0)) /* or y is NaN */ 79 return nan_mix_op(x, y, *)/nan_mix_op(x, y, *); 80 if(ux.bits.exp<=uy.bits.exp) { 81 if((ux.bits.exp<uy.bits.exp) || 82 (ux.bits.manh<=uy.bits.manh && 83 (ux.bits.manh<uy.bits.manh || 84 ux.bits.manl<uy.bits.manl))) { 85 return x; /* |x|<|y| return x or x-y */ 86 } 87 if(ux.bits.manh==uy.bits.manh && ux.bits.manl==uy.bits.manl) { 88 return Zero[sx]; /* |x|=|y| return x*0*/ 89 } 90 } 91 92 /* determine ix = ilogb(x) */ 93 if(ux.bits.exp == 0) { /* subnormal x */ 94 ux.e *= 0x1.0p512; 95 ix = ux.bits.exp - (BIAS + 512); 96 } else { 97 ix = ux.bits.exp - BIAS; 98 } 99 100 /* determine iy = ilogb(y) */ 101 if(uy.bits.exp == 0) { /* subnormal y */ 102 uy.e *= 0x1.0p512; 103 iy = uy.bits.exp - (BIAS + 512); 104 } else { 105 iy = uy.bits.exp - BIAS; 106 } 107 108 /* set up {hx,lx}, {hy,ly} and align y to x */ 109 hx = SET_NBIT(ux.bits.manh); 110 hy = SET_NBIT(uy.bits.manh); 111 lx = ux.bits.manl; 112 ly = uy.bits.manl; 113 114 /* fix point fmod */ 115 n = ix - iy; 116 117 while(n--) { 118 hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1; 119 if(hz<0){hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;} 120 else { 121 if ((hz|lz)==0) /* return sign(x)*0 */ 122 return Zero[sx]; 123 hx = hz+hz+(lz>>MANL_SHIFT); lx = lz+lz; 124 } 125 } 126 hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1; 127 if(hz>=0) {hx=hz;lx=lz;} 128 129 /* convert back to floating value and restore the sign */ 130 if((hx|lx)==0) /* return sign(x)*0 */ 131 return Zero[sx]; 132 while(hx<(1ULL<<HFRAC_BITS)) { /* normalize x */ 133 hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx; 134 iy -= 1; 135 } 136 ux.bits.manh = hx; /* The mantissa is truncated here if needed. */ 137 ux.bits.manl = lx; 138 if (iy < LDBL_MIN_EXP) { 139 ux.bits.exp = iy + (BIAS + 512); 140 ux.e *= 0x1p-512; 141 } else { 142 ux.bits.exp = iy + BIAS; 143 } 144 x = ux.e * one; /* create necessary signal */ 145 return x; /* exact output */ 146 } 147