10720e2fea8a5a84b5cc9b975e16793c787dcfa3
[libm] / src / math / fmodl.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/e_fmodl.c */
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 "libm.h"
14
15 #if LD64
16 long double fmodl(long double x, long double y)
17 {
18         return fmod(x, y);
19 }
20 #elif LD80 || LD128
21
22 #define BIAS (LDBL_MAX_EXP - 1)
23
24 #if LDBL_MANL_SIZE > 32
25 typedef uint64_t manl_t;
26 #else
27 typedef uint32_t manl_t;
28 #endif
29
30 #if LDBL_MANH_SIZE > 32
31 typedef uint64_t manh_t;
32 #else
33 typedef uint32_t manh_t;
34 #endif
35
36 /*
37  * These macros add and remove an explicit integer bit in front of the
38  * fractional mantissa, if the architecture doesn't have such a bit by
39  * default already.
40  */
41 #ifdef LDBL_IMPLICIT_NBIT
42 #define SET_NBIT(hx)    ((hx) | (1ULL << LDBL_MANH_SIZE))
43 #define HFRAC_BITS      LDBL_MANH_SIZE
44 #else
45 #define SET_NBIT(hx)    (hx)
46 #define HFRAC_BITS      (LDBL_MANH_SIZE - 1)
47 #endif
48
49 #define MANL_SHIFT      (LDBL_MANL_SIZE - 1)
50
51 static const long double one = 1.0, Zero[] = {0.0, -0.0,};
52
53 /*
54  * fmodl(x,y)
55  * Return x mod y in exact arithmetic
56  * Method: shift and subtract
57  *
58  * Assumptions:
59  * - The low part of the mantissa fits in a manl_t exactly.
60  * - The high part of the mantissa fits in an int64_t with enough room
61  *   for an explicit integer bit in front of the fractional bits.
62  */
63 long double 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 (x*y)/(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)))  /* |x|<|y| return x or x-y */
86                         return x;
87                 if (ux.bits.manh==uy.bits.manh && ux.bits.manl==uy.bits.manl)
88                         return Zero[sx];  /* |x| = |y| return x*0 */
89         }
90
91         /* determine ix = ilogb(x) */
92         if (ux.bits.exp == 0) {  /* subnormal x */
93                 ux.e *= 0x1.0p512;
94                 ix = ux.bits.exp - (BIAS + 512);
95         } else {
96                 ix = ux.bits.exp - BIAS;
97         }
98
99         /* determine iy = ilogb(y) */
100         if (uy.bits.exp == 0) {  /* subnormal y */
101                 uy.e *= 0x1.0p512;
102                 iy = uy.bits.exp - (BIAS + 512);
103         } else {
104                 iy = uy.bits.exp - BIAS;
105         }
106
107         /* set up {hx,lx}, {hy,ly} and align y to x */
108         hx = SET_NBIT(ux.bits.manh);
109         hy = SET_NBIT(uy.bits.manh);
110         lx = ux.bits.manl;
111         ly = uy.bits.manl;
112
113         /* fix point fmod */
114         n = ix - iy;
115
116         while (n--) {
117                 hz = hx-hy;
118                 lz = lx-ly;
119                 if (lx < ly)
120                         hz -= 1;
121                 if (hz < 0) {
122                         hx = hx+hx+(lx>>MANL_SHIFT);
123                         lx = lx+lx;
124                 } else {
125                         if ((hz|lz)==0)   /* return sign(x)*0 */
126                                 return Zero[sx];
127                         hx = hz+hz+(lz>>MANL_SHIFT);
128                         lx = lz+lz;
129                 }
130         }
131         hz = hx-hy;
132         lz = lx-ly;
133         if (lx < ly)
134                 hz -= 1;
135         if (hz >= 0) {
136                 hx = hz;
137                 lx = lz;
138         }
139
140         /* convert back to floating value and restore the sign */
141         if ((hx|lx) == 0)   /* return sign(x)*0 */
142                 return Zero[sx];
143         while (hx < (1ULL<<HFRAC_BITS)) {  /* normalize x */
144                 hx = hx+hx+(lx>>MANL_SHIFT);
145                 lx = lx+lx;
146                 iy -= 1;
147         }
148         ux.bits.manh = hx; /* The mantissa is truncated here if needed. */
149         ux.bits.manl = lx;
150         if (iy < LDBL_MIN_EXP) {
151                 ux.bits.exp = iy + (BIAS + 512);
152                 ux.e *= 0x1p-512;
153         } else {
154                 ux.bits.exp = iy + BIAS;
155         }
156         x = ux.e * one;   /* create necessary signal */
157         return x;         /* exact output */
158 }
159 #endif