annotate libquadmath/math/x2y2m1q.c @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 04ced10e8804
children
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.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
2 Copyright (C) 2012-2018 Free Software Foundation, Inc.
111
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
kono
parents:
diff changeset
21 /* Calculate X + Y exactly and store the result in *HI + *LO. It is
kono
parents:
diff changeset
22 given that |X| >= |Y| and the values are small enough that no
kono
parents:
diff changeset
23 overflow occurs. */
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 static inline void
kono
parents:
diff changeset
26 add_split (__float128 *hi, __float128 *lo, __float128 x, __float128 y)
kono
parents:
diff changeset
27 {
kono
parents:
diff changeset
28 /* Apply Dekker's algorithm. */
kono
parents:
diff changeset
29 *hi = x + y;
kono
parents:
diff changeset
30 *lo = (x - *hi) + y;
kono
parents:
diff changeset
31 }
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 /* Compare absolute values of floating-point values pointed to by P
kono
parents:
diff changeset
34 and Q for qsort. */
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 static int
kono
parents:
diff changeset
37 compare (const void *p, const void *q)
kono
parents:
diff changeset
38 {
kono
parents:
diff changeset
39 __float128 pld = fabsq (*(const __float128 *) p);
kono
parents:
diff changeset
40 __float128 qld = fabsq (*(const __float128 *) q);
kono
parents:
diff changeset
41 if (pld < qld)
kono
parents:
diff changeset
42 return -1;
kono
parents:
diff changeset
43 else if (pld == qld)
kono
parents:
diff changeset
44 return 0;
kono
parents:
diff changeset
45 else
kono
parents:
diff changeset
46 return 1;
kono
parents:
diff changeset
47 }
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 /* Return X^2 + Y^2 - 1, computed without large cancellation error.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
50 It is given that 1 > X >= Y >= epsilon / 2, and that X^2 + Y^2 >=
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
51 0.5. */
111
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 __float128
kono
parents:
diff changeset
54 __quadmath_x2y2m1q (__float128 x, __float128 y)
kono
parents:
diff changeset
55 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
56 __float128 vals[5];
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
57 SET_RESTORE_ROUNDF128 (FE_TONEAREST);
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
58 mul_splitq (&vals[1], &vals[0], x, x);
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
59 mul_splitq (&vals[3], &vals[2], y, y);
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
60 vals[4] = -1;
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
61 qsort (vals, 5, sizeof (__float128), compare);
111
kono
parents:
diff changeset
62 /* Add up the values so that each element of VALS has absolute value
kono
parents:
diff changeset
63 at most equal to the last set bit of the next nonzero
kono
parents:
diff changeset
64 element. */
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
65 for (size_t i = 0; i <= 3; i++)
111
kono
parents:
diff changeset
66 {
kono
parents:
diff changeset
67 add_split (&vals[i + 1], &vals[i], vals[i + 1], vals[i]);
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
68 qsort (vals + i + 1, 4 - i, sizeof (__float128), compare);
111
kono
parents:
diff changeset
69 }
kono
parents:
diff changeset
70 /* Now any error from this addition will be small. */
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 111
diff changeset
71 return vals[4] + vals[3] + vals[2] + vals[1] + vals[0];
111
kono
parents:
diff changeset
72 }