2 Copyright (C) 2011-2018 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 #if ! defined USE_LONG_DOUBLE
27 #ifdef USE_LONG_DOUBLE
29 # define DOUBLE long double
30 # define MANT_DIG LDBL_MANT_DIG
31 # define L_(literal) literal##L
39 # define DOUBLE double
40 # define MANT_DIG DBL_MANT_DIG
41 # define L_(literal) literal
49 /* MSVC with option -fp:strict refuses to compile constant initializers that
50 contain floating-point operations. Pacify this compiler. */
52 # pragma fenv_access (off)
58 # define NAN (zero / zero)
60 # define NAN (L_(0.0) / L_(0.0))
63 /* To avoid rounding errors during the computation of x - q * y,
64 there are three possibilities:
65 - Use fma (- q, y, x).
66 - Have q be a single bit at a time, and compute x - q * y
67 through a subtraction.
68 - Have q be at most MANT_DIG/2 bits at a time, and compute
69 x - q * y by splitting y into two halves:
70 y = y1 * 2^(MANT_DIG/2) + y0
71 x - q * y = (x - q * y1 * 2^MANT_DIG/2) - q * y0.
72 The latter is probably the most efficient. */
74 /* Number of bits in a limb. */
75 #define LIMB_BITS (MANT_DIG / 2)
78 static const DOUBLE TWO_LIMB_BITS
=
79 /* Assume LIMB_BITS <= 3 * 31.
81 n = floor(n/3) + floor((n+1)/3) + floor((n+2)/3). */
82 (DOUBLE
) (1U << (LIMB_BITS
/ 3))
83 * (DOUBLE
) (1U << ((LIMB_BITS
+ 1) / 3))
84 * (DOUBLE
) (1U << ((LIMB_BITS
+ 2) / 3));
87 static const DOUBLE TWO_LIMB_BITS_INVERSE
=
88 /* Assume LIMB_BITS <= 3 * 31.
90 n = floor(n/3) + floor((n+1)/3) + floor((n+2)/3). */
91 L_(1.0) / ((DOUBLE
) (1U << (LIMB_BITS
/ 3))
92 * (DOUBLE
) (1U << ((LIMB_BITS
+ 1) / 3))
93 * (DOUBLE
) (1U << ((LIMB_BITS
+ 2) / 3)));
96 FMOD (DOUBLE x
, DOUBLE y
)
98 if (isfinite (x
) && isfinite (y
) && y
!= L_(0.0))
101 /* Return x, regardless of the sign of y. */
105 int negate
= ((!signbit (x
)) ^ (!signbit (y
)));
107 /* Take the absolute value of x and y. */
111 /* Trivial case that requires no computation. */
113 return (negate
? - x
: x
);
125 /* Write y = 2^yexp * (y1 * 2^-LIMB_BITS + y0 * 2^-(2*LIMB_BITS))
126 where y1 is an integer, 2^(LIMB_BITS-1) <= y1 < 2^LIMB_BITS,
127 y1 has at most LIMB_BITS bits,
128 0 <= y0 < 2^LIMB_BITS,
129 y0 has at most (MANT_DIG + 1) / 2 bits. */
130 ym
= FREXP (y
, &yexp
);
131 ym
= ym
* TWO_LIMB_BITS
;
133 y0
= (ym
- y1
) * TWO_LIMB_BITS
;
136 x = 2^(yexp+(k-3)*LIMB_BITS)
137 * (x2 * 2^(2*LIMB_BITS) + x1 * 2^LIMB_BITS + x0)
138 where x2, x1, x0 are each integers >= 0, < 2^LIMB_BITS. */
141 DOUBLE xm
= FREXP (x
, &xexp
);
142 /* Since we know x >= y, we know xexp >= yexp. */
144 /* Compute k = ceil(xexp / LIMB_BITS). */
145 k
= (xexp
+ LIMB_BITS
- 1) / LIMB_BITS
;
146 /* Note: (k - 1) * LIMB_BITS + 1 <= xexp <= k * LIMB_BITS. */
147 /* Note: 0.5 <= xm < 1.0. */
148 xm
= LDEXP (xm
, xexp
- (k
- 1) * LIMB_BITS
);
149 /* Note: Now xm < 2^(xexp - (k - 1) * LIMB_BITS) <= 2^LIMB_BITS
150 and xm >= 0.5 * 2^(xexp - (k - 1) * LIMB_BITS) >= 1.0
151 and xm has at most MANT_DIG <= 2*LIMB_BITS+1 bits. */
153 x1
= (xm
- x2
) * TWO_LIMB_BITS
;
154 /* Split off x0 from x1 later. */
157 /* Test whether [x2,x1,0] >= 2^LIMB_BITS * [y1,y0]. */
158 if (x2
> y1
|| (x2
== y1
&& x1
>= y0
))
160 /* Subtract 2^LIMB_BITS * [y1,y0] from [x2,x1,0]. */
165 if (!(x2
>= L_(1.0)))
172 /* Split off x0 from x1. */
174 DOUBLE x1int
= TRUNC (x1
);
175 x0
= TRUNC ((x1
- x1int
) * TWO_LIMB_BITS
);
181 /* Multiprecision division of the limb sequence [x2,x1,x0]
183 /* Here [x2,x1,x0] < 2^LIMB_BITS * [y1,y0]. */
184 /* The first guess takes into account only [x2,x1] and [y1].
186 By Knuth's theorem, we know that
187 q* = min (floor ([x2,x1] / [y1]), 2^LIMB_BITS - 1)
189 q = floor ([x2,x1,x0] / [y1,y0])
191 q* - 2 <= q <= q* + 1.
194 a) q* * y1 <= floor ([x2,x1] / [y1]) * y1 <= [x2,x1].
196 [x2,x1,x0] - q* * [y1,y0]
197 = 2^LIMB_BITS * ([x2,x1] - q* * [y1]) + x0 - q* * y0
203 [x2,x1,x0] > (q* - 2) * [y1,y0].
204 b) If q* = floor ([x2,x1] / [y1]), then
205 [x2,x1] < (q* + 1) * y1
207 [x2,x1,x0] - q* * [y1,y0]
208 = 2^LIMB_BITS * ([x2,x1] - q* * [y1]) + x0 - q* * y0
209 <= 2^LIMB_BITS * (y1 - 1) + x0 - q* * y0
210 <= 2^LIMB_BITS * (2^LIMB_BITS-2) + (2^LIMB_BITS-1) - 0
214 [x2,x1,x0] < (q* + 2) * [y1,y0].
219 In the other case, q* = 2^LIMB_BITS - 1. Then trivially
220 q < 2^LIMB_BITS = q* + 1.
222 We know that floor ([x2,x1] / [y1]) >= 2^LIMB_BITS if and
226 ? TWO_LIMB_BITS
- L_(1.0)
227 : TRUNC ((x2
* TWO_LIMB_BITS
+ x1
) / y1
));
231 [x2,x1,x0] - q* * [y1,y0]
232 = 2^LIMB_BITS * ([x2,x1] - q* * [y1]) + x0 - q* * y0. */
233 DOUBLE q_y1
= q
* y1
; /* exact, at most 2*LIMB_BITS bits */
234 DOUBLE q_y1_1
= TRUNC (q_y1
* TWO_LIMB_BITS_INVERSE
);
235 DOUBLE q_y1_0
= q_y1
- q_y1_1
* TWO_LIMB_BITS
;
236 DOUBLE q_y0
= q
* y0
; /* exact, at most MANT_DIG bits */
237 DOUBLE q_y0_1
= TRUNC (q_y0
* TWO_LIMB_BITS_INVERSE
);
238 DOUBLE q_y0_0
= q_y0
- q_y0_1
* TWO_LIMB_BITS
;
243 /* Move negative carry from x0 to x1 and from x1 to x2. */
253 if (x1
< L_(0.0)) /* not sure this can happen */
264 /* Move overflow from x0 to x1 and from x1 to x0. */
265 if (x0
>= TWO_LIMB_BITS
)
270 if (x1
>= TWO_LIMB_BITS
)
277 /* Reduce q by 1 again. */
280 /* Move overflow from x0 to x1 and from x1 to x0. */
281 if (x0
>= TWO_LIMB_BITS
)
286 if (x1
>= TWO_LIMB_BITS
)
292 /* Shouldn't happen, because we proved that
300 || (x1
== y1
&& x0
>= y0
))
302 /* Increase q by 1. */
305 /* Move negative carry from x0 to x1 and from x1 to x2. */
320 || (x1
== y1
&& x0
>= y0
))
321 /* Shouldn't happen, because we proved that
325 /* Here [x2,x1,x0] < [y1,y0]. */
328 #if (MANT_DIG + 1) / 2 > LIMB_BITS /* y0 can have a fractional bit */
330 x0
= (x0
- x1
) * TWO_LIMB_BITS
;
335 /* Here [x2,x1,x0] < 2^LIMB_BITS * [y1,y0]. */
340 * (x2 * 2^(2*LIMB_BITS) + x1 * 2^LIMB_BITS + x0). */
343 LDEXP ((x2
* TWO_LIMB_BITS
+ x1
) * TWO_LIMB_BITS
+ x0
,
344 yexp
- 3 * LIMB_BITS
);
345 return (negate
? - r
: r
);
352 if (ISNAN (x
) || ISNAN (y
))
353 return x
+ y
; /* NaN */
357 /* x infinite or y zero */