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