xref: /freebsd/lib/msun/src/s_remquol.c (revision 4d65a7c6951cea0333f1a0c1b32c38489cdfa6c5)
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 Zero[] = {0.0L, -0.0L};
50 
51 /*
52  * Return the IEEE remainder and set *quo to the last n bits of the
53  * quotient, rounded to the nearest integer.  We choose n=31 because
54  * we wind up computing all the integer bits of the quotient anyway as
55  * a side-effect of computing the remainder by the shift and subtract
56  * method.  In practice, this is far more bits than are needed to use
57  * remquo in reduction algorithms.
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 remquol(long double x, long double y, int *quo)
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,q,sx,sxy;
72 
73 	ux.e = x;
74 	uy.e = y;
75 	sx = ux.bits.sign;
76 	sxy = sx ^ uy.bits.sign;
77 	ux.bits.sign = 0;	/* |x| */
78 	uy.bits.sign = 0;	/* |y| */
79 
80     /* purge off exception values */
81 	if((uy.bits.exp|uy.bits.manh|uy.bits.manl)==0 || /* y=0 */
82 	   (ux.bits.exp == BIAS + LDBL_MAX_EXP) ||	 /* or x not finite */
83 	   (uy.bits.exp == BIAS + LDBL_MAX_EXP &&
84 	    ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)!=0)) /* or y is NaN */
85 	    return nan_mix_op(x, y, *)/nan_mix_op(x, y, *);
86 	if(ux.bits.exp<=uy.bits.exp) {
87 	    if((ux.bits.exp<uy.bits.exp) ||
88 	       (ux.bits.manh<=uy.bits.manh &&
89 		(ux.bits.manh<uy.bits.manh ||
90 		 ux.bits.manl<uy.bits.manl))) {
91 		q = 0;
92 		goto fixup;	/* |x|<|y| return x or x-y */
93 	    }
94 	    if(ux.bits.manh==uy.bits.manh && ux.bits.manl==uy.bits.manl) {
95 		*quo = (sxy ? -1 : 1);
96 		return Zero[sx];	/* |x|=|y| return x*0*/
97 	    }
98 	}
99 
100     /* determine ix = ilogb(x) */
101 	if(ux.bits.exp == 0) {	/* subnormal x */
102 	    ux.e *= 0x1.0p512;
103 	    ix = ux.bits.exp - (BIAS + 512);
104 	} else {
105 	    ix = ux.bits.exp - BIAS;
106 	}
107 
108     /* determine iy = ilogb(y) */
109 	if(uy.bits.exp == 0) {	/* subnormal y */
110 	    uy.e *= 0x1.0p512;
111 	    iy = uy.bits.exp - (BIAS + 512);
112 	} else {
113 	    iy = uy.bits.exp - BIAS;
114 	}
115 
116     /* set up {hx,lx}, {hy,ly} and align y to x */
117 	hx = SET_NBIT(ux.bits.manh);
118 	hy = SET_NBIT(uy.bits.manh);
119 	lx = ux.bits.manl;
120 	ly = uy.bits.manl;
121 
122     /* fix point fmod */
123 	n = ix - iy;
124 	q = 0;
125 	while(n--) {
126 	    hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
127 	    if(hz<0){hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;}
128 	    else {hx = hz+hz+(lz>>MANL_SHIFT); lx = lz+lz; q++;}
129 	    q <<= 1;
130 	}
131 	hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
132 	if(hz>=0) {hx=hz;lx=lz;q++;}
133 
134     /* convert back to floating value and restore the sign */
135 	if((hx|lx)==0) {			/* return sign(x)*0 */
136 	    q &= 0x7fffffff;
137 	    *quo = (sxy ? -q : q);
138 	    return Zero[sx];
139 	}
140 	while(hx<(1ULL<<HFRAC_BITS)) {	/* normalize x */
141 	    hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;
142 	    iy -= 1;
143 	}
144 	ux.bits.manh = hx; /* The integer bit is truncated here if needed. */
145 	ux.bits.manl = lx;
146 	if (iy < LDBL_MIN_EXP) {
147 	    ux.bits.exp = iy + (BIAS + 512);
148 	    ux.e *= 0x1p-512;
149 	} else {
150 	    ux.bits.exp = iy + BIAS;
151 	}
152 fixup:
153 	x = ux.e;		/* |x| */
154 	y = fabsl(y);
155 	if (y < LDBL_MIN * 2) {
156 	    if (x+x>y || (x+x==y && (q & 1))) {
157 		q++;
158 		x-=y;
159 	    }
160 	} else if (x>0.5*y || (x==0.5*y && (q & 1))) {
161 	    q++;
162 	    x-=y;
163 	}
164 	ux.e = x;
165 	ux.bits.sign ^= sx;
166 	x = ux.e;
167 	q &= 0x7fffffff;
168 	*quo = (sxy ? -q : q);
169 	return x;
170 }
171