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	SET_NBIT(hx)	((hx) | (1ULL << LDBL_MANH_SIZE))
25 #define	HFRAC_BITS	EXT_FRACHBITS
26 #else
27 #define	SET_NBIT(hx)	(hx)
28 #define	HFRAC_BITS	(EXT_FRACHBITS - 1)
29 #endif
30 
31 #define	MANL_SHIFT	(EXT_FRACLBITS - 1)
32 
33 static const long double Zero[] = {0.0L, -0.0L};
34 
35 /*
36  * Return the IEEE remainder and set *quo to the last n bits of the
37  * quotient, rounded to the nearest integer.  We choose n=31 because
38  * we wind up computing all the integer bits of the quotient anyway as
39  * a side-effect of computing the remainder by the shift and subtract
40  * method.  In practice, this is far more bits than are needed to use
41  * remquo in reduction algorithms.
42  *
43  * Assumptions:
44  * - The low part of the mantissa fits in a manl_t exactly.
45  * - The high part of the mantissa fits in an int64_t with enough room
46  *   for an explicit integer bit in front of the fractional bits.
47  */
48 long double
remquol(long double x,long double y,int * quo)49 remquol(long double x, long double y, int *quo)
50 {
51 	int64_t hx,hz;	/* We need a carry bit even if LDBL_MANH_SIZE is 32. */
52 	uint32_t hy;
53 	uint32_t lx,ly,lz;
54 	uint32_t esx, esy;
55 	int ix,iy,n,q,sx,sxy;
56 
57 	GET_LDOUBLE_WORDS(esx,hx,lx,x);
58 	GET_LDOUBLE_WORDS(esy,hy,ly,y);
59 	sx = esx & 0x8000;
60 	sxy = sx ^ (esy & 0x8000);
61 	esx &= 0x7fff;				/* |x| */
62 	esy &= 0x7fff;				/* |y| */
63 	SET_LDOUBLE_EXP(x,esx);
64 	SET_LDOUBLE_EXP(y,esy);
65 
66     /* purge off exception values */
67 	if((esy|hy|ly)==0 ||			/* y=0 */
68 	   (esx == BIAS + LDBL_MAX_EXP) ||	/* or x not finite */
69 	   (esy == BIAS + LDBL_MAX_EXP &&
70 	    ((hy&~LDBL_NBIT)|ly)!=0)) {		/* or y is NaN */
71             *quo = 0;
72 	    return (x*y)/(x*y);
73         }
74 	if(esx<=esy) {
75 	    if((esx<esy) ||
76 	       (hx<=hy &&
77 		(hx<hy ||
78 		 lx<ly))) {
79 		q = 0;
80 		goto fixup;			/* |x|<|y| return x or x-y */
81 	    }
82 	    if(hx==hy && lx==ly) {
83 		*quo = 1;
84 		return Zero[sx!=0];		/* |x|=|y| return x*0*/
85 	    }
86 	}
87 
88     /* determine ix = ilogb(x) */
89 	if(esx == 0) {				/* subnormal x */
90 	    x *= 0x1.0p512L;
91 	    GET_LDOUBLE_WORDS(esx,hx,lx,x);
92 	    ix = esx - (BIAS + 512);
93 	} else {
94 	    ix = esx - BIAS;
95 	}
96 
97     /* determine iy = ilogb(y) */
98 	if(esy == 0) {				/* subnormal y */
99 	    y *= 0x1.0p512L;
100 	    GET_LDOUBLE_WORDS(esy,hy,ly,y);
101 	    iy = esy - (BIAS + 512);
102 	} else {
103 	    iy = esy - BIAS;
104 	}
105 
106     /* set up {hx,lx}, {hy,ly} and align y to x */
107 	hx = SET_NBIT(hx);
108 	lx = SET_NBIT(lx);
109 
110     /* fix point fmod */
111 	n = ix - iy;
112 	q = 0;
113 
114 	while(n--) {
115 	    hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
116 	    if(hz<0){hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;}
117 	    else {hx = hz+hz+(lz>>MANL_SHIFT); lx = lz+lz; q++;}
118 	    q <<= 1;
119 	}
120 	hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
121 	if(hz>=0) {hx=hz;lx=lz;q++;}
122 
123     /* convert back to floating value and restore the sign */
124 	if((hx|lx)==0) {			/* return sign(x)*0 */
125 	    *quo = (sxy ? -q : q);
126 	    return Zero[sx!=0];
127 	}
128 	while(hx<(1LL<<HFRAC_BITS)) {	/* normalize x */
129 	    hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;
130 	    iy -= 1;
131 	}
132 	if (iy < LDBL_MIN_EXP) {
133 	    esx = (iy + BIAS + 512) & 0x7fff;
134 	    SET_LDOUBLE_WORDS(x,esx,hx,lx);
135 	    x *= 0x1p-512L;
136 	    GET_LDOUBLE_WORDS(esx,hx,lx,x);
137 	} else {
138 	    esx = (iy + BIAS) & 0x7fff;
139 	}
140 	SET_LDOUBLE_WORDS(x,esx,hx,lx);
141 fixup:
142 	y = fabsl(y);
143 	if (y < LDBL_MIN * 2) {
144 	    if (x+x>y || (x+x==y && (q & 1))) {
145 		q++;
146 		x-=y;
147 	    }
148 	} else if (x>0.5L*y || (x==0.5L*y && (q & 1))) {
149 	    q++;
150 	    x-=y;
151 	}
152 
153 	GET_LDOUBLE_EXP(esx,x);
154 	esx ^= sx;
155 	SET_LDOUBLE_EXP(x,esx);
156 
157 	q &= 0x7fffffff;
158 	*quo = (sxy ? -q : q);
159 	return x;
160 }
161