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