xref: /openbsd-src/lib/libm/src/ld80/e_fmodl.c (revision beb15867ad58c144f0b15621aba82cb13678cd97)
1*49393c00Smartynas /* @(#)e_fmod.c 1.3 95/01/18 */
2*49393c00Smartynas /*-
3*49393c00Smartynas  * ====================================================
4*49393c00Smartynas  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5*49393c00Smartynas  *
6*49393c00Smartynas  * Developed at SunSoft, a Sun Microsystems, Inc. business.
7*49393c00Smartynas  * Permission to use, copy, modify, and distribute this
8*49393c00Smartynas  * software is freely granted, provided that this notice
9*49393c00Smartynas  * is preserved.
10*49393c00Smartynas  * ====================================================
11*49393c00Smartynas  */
12*49393c00Smartynas 
13*49393c00Smartynas #include <sys/types.h>
14*49393c00Smartynas #include <machine/ieee.h>
15*49393c00Smartynas 
16*49393c00Smartynas #include <float.h>
17*49393c00Smartynas #include <math.h>
18*49393c00Smartynas #include <stdint.h>
19*49393c00Smartynas 
20*49393c00Smartynas #include "math_private.h"
21*49393c00Smartynas 
22*49393c00Smartynas #define	BIAS (LDBL_MAX_EXP - 1)
23*49393c00Smartynas 
24*49393c00Smartynas /*
25*49393c00Smartynas  * These macros add and remove an explicit integer bit in front of the
26*49393c00Smartynas  * fractional mantissa, if the architecture doesn't have such a bit by
27*49393c00Smartynas  * default already.
28*49393c00Smartynas  */
29*49393c00Smartynas #ifdef LDBL_IMPLICIT_NBIT
30*49393c00Smartynas #define	LDBL_NBIT	0
31*49393c00Smartynas #define	SET_NBIT(hx)	((hx) | (1ULL << LDBL_MANH_SIZE))
32*49393c00Smartynas #define	HFRAC_BITS	EXT_FRACHBITS
33*49393c00Smartynas #else
34*49393c00Smartynas #define	LDBL_NBIT	0x80000000
35*49393c00Smartynas #define	SET_NBIT(hx)	(hx)
36*49393c00Smartynas #define	HFRAC_BITS	(EXT_FRACHBITS - 1)
37*49393c00Smartynas #endif
38*49393c00Smartynas 
39*49393c00Smartynas #define	MANL_SHIFT	(EXT_FRACLBITS - 1)
40*49393c00Smartynas 
41*49393c00Smartynas static const long double one = 1.0, Zero[] = {0.0, -0.0,};
42*49393c00Smartynas 
43*49393c00Smartynas /*
44*49393c00Smartynas  * fmodl(x,y)
45*49393c00Smartynas  * Return x mod y in exact arithmetic
46*49393c00Smartynas  * Method: shift and subtract
47*49393c00Smartynas  *
48*49393c00Smartynas  * Assumptions:
49*49393c00Smartynas  * - The low part of the mantissa fits in a manl_t exactly.
50*49393c00Smartynas  * - The high part of the mantissa fits in an int64_t with enough room
51*49393c00Smartynas  *   for an explicit integer bit in front of the fractional bits.
52*49393c00Smartynas  */
53*49393c00Smartynas long double
fmodl(long double x,long double y)54*49393c00Smartynas fmodl(long double x, long double y)
55*49393c00Smartynas {
56*49393c00Smartynas 	union {
57*49393c00Smartynas 		long double e;
58*49393c00Smartynas 		struct ieee_ext bits;
59*49393c00Smartynas 	} ux, uy;
60*49393c00Smartynas 	int64_t hx,hz;	/* We need a carry bit even if LDBL_MANH_SIZE is 32. */
61*49393c00Smartynas 	uint32_t hy;
62*49393c00Smartynas 	uint32_t lx,ly,lz;
63*49393c00Smartynas 	int ix,iy,n,sx;
64*49393c00Smartynas 
65*49393c00Smartynas 	ux.e = x;
66*49393c00Smartynas 	uy.e = y;
67*49393c00Smartynas 	sx = ux.bits.ext_sign;
68*49393c00Smartynas 
69*49393c00Smartynas     /* purge off exception values */
70*49393c00Smartynas 	if((uy.bits.ext_exp|uy.bits.ext_frach|uy.bits.ext_fracl)==0 || /* y=0 */
71*49393c00Smartynas 	   (ux.bits.ext_exp == BIAS + LDBL_MAX_EXP) ||	 /* or x not finite */
72*49393c00Smartynas 	   (uy.bits.ext_exp == BIAS + LDBL_MAX_EXP &&
73*49393c00Smartynas 	    ((uy.bits.ext_frach&~LDBL_NBIT)|uy.bits.ext_fracl)!=0)) /* or y is NaN */
74*49393c00Smartynas 	    return (x*y)/(x*y);
75*49393c00Smartynas 	if(ux.bits.ext_exp<=uy.bits.ext_exp) {
76*49393c00Smartynas 	    if((ux.bits.ext_exp<uy.bits.ext_exp) ||
77*49393c00Smartynas 	       (ux.bits.ext_frach<=uy.bits.ext_frach &&
78*49393c00Smartynas 		(ux.bits.ext_frach<uy.bits.ext_frach ||
79*49393c00Smartynas 		 ux.bits.ext_fracl<uy.bits.ext_fracl))) {
80*49393c00Smartynas 		return x;		/* |x|<|y| return x or x-y */
81*49393c00Smartynas 	    }
82*49393c00Smartynas 	    if(ux.bits.ext_frach==uy.bits.ext_frach &&
83*49393c00Smartynas 		ux.bits.ext_fracl==uy.bits.ext_fracl) {
84*49393c00Smartynas 		return Zero[sx];	/* |x|=|y| return x*0*/
85*49393c00Smartynas 	    }
86*49393c00Smartynas 	}
87*49393c00Smartynas 
88*49393c00Smartynas     /* determine ix = ilogb(x) */
89*49393c00Smartynas 	if(ux.bits.ext_exp == 0) {	/* subnormal x */
90*49393c00Smartynas 	    ux.e *= 0x1.0p512;
91*49393c00Smartynas 	    ix = ux.bits.ext_exp - (BIAS + 512);
92*49393c00Smartynas 	} else {
93*49393c00Smartynas 	    ix = ux.bits.ext_exp - BIAS;
94*49393c00Smartynas 	}
95*49393c00Smartynas 
96*49393c00Smartynas     /* determine iy = ilogb(y) */
97*49393c00Smartynas 	if(uy.bits.ext_exp == 0) {	/* subnormal y */
98*49393c00Smartynas 	    uy.e *= 0x1.0p512;
99*49393c00Smartynas 	    iy = uy.bits.ext_exp - (BIAS + 512);
100*49393c00Smartynas 	} else {
101*49393c00Smartynas 	    iy = uy.bits.ext_exp - BIAS;
102*49393c00Smartynas 	}
103*49393c00Smartynas 
104*49393c00Smartynas     /* set up {hx,lx}, {hy,ly} and align y to x */
105*49393c00Smartynas 	hx = SET_NBIT(ux.bits.ext_frach);
106*49393c00Smartynas 	hy = SET_NBIT(uy.bits.ext_frach);
107*49393c00Smartynas 	lx = ux.bits.ext_fracl;
108*49393c00Smartynas 	ly = uy.bits.ext_fracl;
109*49393c00Smartynas 
110*49393c00Smartynas     /* fix point fmod */
111*49393c00Smartynas 	n = ix - iy;
112*49393c00Smartynas 
113*49393c00Smartynas 	while(n--) {
114*49393c00Smartynas 	    hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
115*49393c00Smartynas 	    if(hz<0){hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;}
116*49393c00Smartynas 	    else {
117*49393c00Smartynas 		if ((hz|lz)==0)		/* return sign(x)*0 */
118*49393c00Smartynas 		    return Zero[sx];
119*49393c00Smartynas 		hx = hz+hz+(lz>>MANL_SHIFT); lx = lz+lz;
120*49393c00Smartynas 	    }
121*49393c00Smartynas 	}
122*49393c00Smartynas 	hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
123*49393c00Smartynas 	if(hz>=0) {hx=hz;lx=lz;}
124*49393c00Smartynas 
125*49393c00Smartynas     /* convert back to floating value and restore the sign */
126*49393c00Smartynas 	if((hx|lx)==0)			/* return sign(x)*0 */
127*49393c00Smartynas 	    return Zero[sx];
128*49393c00Smartynas 	while(hx<(1ULL<<HFRAC_BITS)) {	/* normalize x */
129*49393c00Smartynas 	    hx = hx+hx+(lx>>MANL_SHIFT); lx = lx+lx;
130*49393c00Smartynas 	    iy -= 1;
131*49393c00Smartynas 	}
132*49393c00Smartynas 	ux.bits.ext_frach = hx; /* The mantissa is truncated here if needed. */
133*49393c00Smartynas 	ux.bits.ext_fracl = lx;
134*49393c00Smartynas 	if (iy < LDBL_MIN_EXP) {
135*49393c00Smartynas 	    ux.bits.ext_exp = iy + (BIAS + 512);
136*49393c00Smartynas 	    ux.e *= 0x1p-512;
137*49393c00Smartynas 	} else {
138*49393c00Smartynas 	    ux.bits.ext_exp = iy + BIAS;
139*49393c00Smartynas 	}
140*49393c00Smartynas 	x = ux.e * one;		/* create necessary signal */
141*49393c00Smartynas 	return x;		/* exact output */
142*49393c00Smartynas }
143