1 /* Compute full X * Y for double type.
2 Copyright (C) 2013-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
24 /* Calculate X * Y exactly and store the result in *HI + *LO. It is
25 given that the values are small enough that no overflow occurs and
26 large enough (or zero) that no underflow occurs. */
29 mul_split (double *hi
, double *lo
, double x
, double y
)
32 /* Fast built-in fused multiply-add. */
34 *lo
= __builtin_fma (x
, y
, -*hi
);
36 /* Apply Dekker's algorithm. */
38 # define C ((1 << (DBL_MANT_DIG + 1) / 2) + 1)
46 *lo
= (((x1
* y1
- *hi
) + x1
* y2
) + x2
* y1
) + x2
* y2
;
50 /* Add a + b exactly, such that *hi + *lo = a + b.
51 Assumes |a| >= |b| and rounding to nearest. */
53 fast_two_sum (double *hi
, double *lo
, double a
, double b
)
58 e
= *hi
- a
; /* exact */
59 *lo
= b
- e
; /* exact */
60 /* Now *hi + *lo = a + b exactly. */
63 /* Multiplication of two floating-point expansions: *hi + *lo is an
64 approximation of (h1+l1)*(h2+l2), assuming |l1| <= 1/2*ulp(h1)
65 and |l2| <= 1/2*ulp(h2) and rounding to nearest. */
67 mul_expansion (double *hi
, double *lo
, double h1
, double l1
,
72 mul_split (hi
, lo
, h1
, h2
);
73 r
= h1
* l2
+ h2
* l1
;
74 /* Now add r to (hi,lo) using fast two-sum, where we know |r| < |hi|. */
75 fast_two_sum (hi
, &e
, *hi
, r
);
79 /* Calculate X / Y and store the approximate result in *HI + *LO. It is
80 assumed that Y is not zero, that no overflow nor underflow occurs, and
81 rounding is to nearest. */
83 div_split (double *hi
, double *lo
, double x
, double y
)
88 mul_split (&a
, &b
, *hi
, y
);
89 /* a + b = hi*y, which should be near x. */
90 a
= x
- a
; /* huge cancellation */
92 /* Now x ~ hi*y + a thus x/y ~ hi + a/y. */
96 /* Division of two floating-point expansions: *hi + *lo is an
97 approximation of (h1+l1)/(h2+l2), assuming |l1| <= 1/2*ulp(h1)
98 and |l2| <= 1/2*ulp(h2), h2+l2 is not zero, and rounding to nearest. */
100 div_expansion (double *hi
, double *lo
, double h1
, double l1
,
101 double h2
, double l2
)
105 div_split (hi
, lo
, h1
, h2
);
106 /* (h1+l1)/(h2+l2) ~ h1/h2 + (l1*h2 - l2*h1)/h2^2 */
107 r
= (l1
* h2
- l2
* h1
) / (h2
* h2
);
108 /* Now add r to (hi,lo) using fast two-sum, where we know |r| < |hi|. */
109 fast_two_sum (hi
, &e
, *hi
, r
);
111 /* Renormalize since |lo| might be larger than 0.5 ulp(hi). */
112 fast_two_sum (hi
, lo
, *hi
, *lo
);
115 #endif /* _MUL_SPLIT_H */