annotate libquadmath/math/x2y2m1q.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Compute x^2 + y^2 - 1, without large cancellation error.
kono
parents:
diff changeset
2 Copyright (C) 2012 Free Software Foundation, Inc.
kono
parents:
diff changeset
3 This file is part of the GNU C Library.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 The GNU C Library is free software; you can redistribute it and/or
kono
parents:
diff changeset
6 modify it under the terms of the GNU Lesser General Public
kono
parents:
diff changeset
7 License as published by the Free Software Foundation; either
kono
parents:
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
kono
parents:
diff changeset
9
kono
parents:
diff changeset
10 The GNU C Library is distributed in the hope that it will be useful,
kono
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
kono
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
kono
parents:
diff changeset
13 Lesser General Public License for more details.
kono
parents:
diff changeset
14
kono
parents:
diff changeset
15 You should have received a copy of the GNU Lesser General Public
kono
parents:
diff changeset
16 License along with the GNU C Library; if not, see
kono
parents:
diff changeset
17 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
18
kono
parents:
diff changeset
19 #include "quadmath-imp.h"
kono
parents:
diff changeset
20 #include <stdlib.h>
kono
parents:
diff changeset
21
kono
parents:
diff changeset
22 /* Calculate X + Y exactly and store the result in *HI + *LO. It is
kono
parents:
diff changeset
23 given that |X| >= |Y| and the values are small enough that no
kono
parents:
diff changeset
24 overflow occurs. */
kono
parents:
diff changeset
25
kono
parents:
diff changeset
26 static inline void
kono
parents:
diff changeset
27 add_split (__float128 *hi, __float128 *lo, __float128 x, __float128 y)
kono
parents:
diff changeset
28 {
kono
parents:
diff changeset
29 /* Apply Dekker's algorithm. */
kono
parents:
diff changeset
30 *hi = x + y;
kono
parents:
diff changeset
31 *lo = (x - *hi) + y;
kono
parents:
diff changeset
32 }
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 /* Calculate X * Y exactly and store the result in *HI + *LO. It is
kono
parents:
diff changeset
35 given that the values are small enough that no overflow occurs and
kono
parents:
diff changeset
36 large enough (or zero) that no underflow occurs. */
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 static inline void
kono
parents:
diff changeset
39 mul_split (__float128 *hi, __float128 *lo, __float128 x, __float128 y)
kono
parents:
diff changeset
40 {
kono
parents:
diff changeset
41 /* Fast built-in fused multiply-add. */
kono
parents:
diff changeset
42 *hi = x * y;
kono
parents:
diff changeset
43 *lo = fmaq (x, y, -*hi);
kono
parents:
diff changeset
44 }
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 /* Compare absolute values of floating-point values pointed to by P
kono
parents:
diff changeset
47 and Q for qsort. */
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 static int
kono
parents:
diff changeset
50 compare (const void *p, const void *q)
kono
parents:
diff changeset
51 {
kono
parents:
diff changeset
52 __float128 pld = fabsq (*(const __float128 *) p);
kono
parents:
diff changeset
53 __float128 qld = fabsq (*(const __float128 *) q);
kono
parents:
diff changeset
54 if (pld < qld)
kono
parents:
diff changeset
55 return -1;
kono
parents:
diff changeset
56 else if (pld == qld)
kono
parents:
diff changeset
57 return 0;
kono
parents:
diff changeset
58 else
kono
parents:
diff changeset
59 return 1;
kono
parents:
diff changeset
60 }
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62 /* Return X^2 + Y^2 - 1, computed without large cancellation error.
kono
parents:
diff changeset
63 It is given that 1 > X >= Y >= epsilon / 2, and that either X >=
kono
parents:
diff changeset
64 0.75 or Y >= 0.5. */
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 __float128
kono
parents:
diff changeset
67 __quadmath_x2y2m1q (__float128 x, __float128 y)
kono
parents:
diff changeset
68 {
kono
parents:
diff changeset
69 __float128 vals[4];
kono
parents:
diff changeset
70 size_t i;
kono
parents:
diff changeset
71
kono
parents:
diff changeset
72 /* FIXME: SET_RESTORE_ROUNDL (FE_TONEAREST); */
kono
parents:
diff changeset
73 mul_split (&vals[1], &vals[0], x, x);
kono
parents:
diff changeset
74 mul_split (&vals[3], &vals[2], y, y);
kono
parents:
diff changeset
75 if (x >= 0.75Q)
kono
parents:
diff changeset
76 vals[1] -= 1.0Q;
kono
parents:
diff changeset
77 else
kono
parents:
diff changeset
78 {
kono
parents:
diff changeset
79 vals[1] -= 0.5Q;
kono
parents:
diff changeset
80 vals[3] -= 0.5Q;
kono
parents:
diff changeset
81 }
kono
parents:
diff changeset
82 qsort (vals, 4, sizeof (__float128), compare);
kono
parents:
diff changeset
83 /* Add up the values so that each element of VALS has absolute value
kono
parents:
diff changeset
84 at most equal to the last set bit of the next nonzero
kono
parents:
diff changeset
85 element. */
kono
parents:
diff changeset
86 for (i = 0; i <= 2; i++)
kono
parents:
diff changeset
87 {
kono
parents:
diff changeset
88 add_split (&vals[i + 1], &vals[i], vals[i + 1], vals[i]);
kono
parents:
diff changeset
89 qsort (vals + i + 1, 3 - i, sizeof (__float128), compare);
kono
parents:
diff changeset
90 }
kono
parents:
diff changeset
91 /* Now any error from this addition will be small. */
kono
parents:
diff changeset
92 return vals[3] + vals[2] + vals[1] + vals[0];
kono
parents:
diff changeset
93 }