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