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