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