xref: /netbsd-src/external/gpl3/gcc.old/dist/libquadmath/math/x2y2m1q.c (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1*627f7eb2Smrg /* Compute x^2 + y^2 - 1, without large cancellation error.
2*627f7eb2Smrg    Copyright (C) 2012-2018 Free Software Foundation, Inc.
3*627f7eb2Smrg    This file is part of the GNU C Library.
4*627f7eb2Smrg 
5*627f7eb2Smrg    The GNU C Library is free software; you can redistribute it and/or
6*627f7eb2Smrg    modify it under the terms of the GNU Lesser General Public
7*627f7eb2Smrg    License as published by the Free Software Foundation; either
8*627f7eb2Smrg    version 2.1 of the License, or (at your option) any later version.
9*627f7eb2Smrg 
10*627f7eb2Smrg    The GNU C Library is distributed in the hope that it will be useful,
11*627f7eb2Smrg    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*627f7eb2Smrg    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13*627f7eb2Smrg    Lesser General Public License for more details.
14*627f7eb2Smrg 
15*627f7eb2Smrg    You should have received a copy of the GNU Lesser General Public
16*627f7eb2Smrg    License along with the GNU C Library; if not, see
17*627f7eb2Smrg    <http://www.gnu.org/licenses/>.  */
18*627f7eb2Smrg 
19*627f7eb2Smrg #include "quadmath-imp.h"
20*627f7eb2Smrg 
21*627f7eb2Smrg /* Calculate X + Y exactly and store the result in *HI + *LO.  It is
22*627f7eb2Smrg    given that |X| >= |Y| and the values are small enough that no
23*627f7eb2Smrg    overflow occurs.  */
24*627f7eb2Smrg 
25*627f7eb2Smrg static inline void
add_split(__float128 * hi,__float128 * lo,__float128 x,__float128 y)26*627f7eb2Smrg add_split (__float128 *hi, __float128 *lo, __float128 x, __float128 y)
27*627f7eb2Smrg {
28*627f7eb2Smrg   /* Apply Dekker's algorithm.  */
29*627f7eb2Smrg   *hi = x + y;
30*627f7eb2Smrg   *lo = (x - *hi) + y;
31*627f7eb2Smrg }
32*627f7eb2Smrg 
33*627f7eb2Smrg /* Compare absolute values of floating-point values pointed to by P
34*627f7eb2Smrg    and Q for qsort.  */
35*627f7eb2Smrg 
36*627f7eb2Smrg static int
compare(const void * p,const void * q)37*627f7eb2Smrg compare (const void *p, const void *q)
38*627f7eb2Smrg {
39*627f7eb2Smrg   __float128 pld = fabsq (*(const __float128 *) p);
40*627f7eb2Smrg   __float128 qld = fabsq (*(const __float128 *) q);
41*627f7eb2Smrg   if (pld < qld)
42*627f7eb2Smrg     return -1;
43*627f7eb2Smrg   else if (pld == qld)
44*627f7eb2Smrg     return 0;
45*627f7eb2Smrg   else
46*627f7eb2Smrg     return 1;
47*627f7eb2Smrg }
48*627f7eb2Smrg 
49*627f7eb2Smrg /* Return X^2 + Y^2 - 1, computed without large cancellation error.
50*627f7eb2Smrg    It is given that 1 > X >= Y >= epsilon / 2, and that X^2 + Y^2 >=
51*627f7eb2Smrg    0.5.  */
52*627f7eb2Smrg 
53*627f7eb2Smrg __float128
__quadmath_x2y2m1q(__float128 x,__float128 y)54*627f7eb2Smrg __quadmath_x2y2m1q (__float128 x, __float128 y)
55*627f7eb2Smrg {
56*627f7eb2Smrg   __float128 vals[5];
57*627f7eb2Smrg   SET_RESTORE_ROUNDF128 (FE_TONEAREST);
58*627f7eb2Smrg   mul_splitq (&vals[1], &vals[0], x, x);
59*627f7eb2Smrg   mul_splitq (&vals[3], &vals[2], y, y);
60*627f7eb2Smrg   vals[4] = -1;
61*627f7eb2Smrg   qsort (vals, 5, sizeof (__float128), compare);
62*627f7eb2Smrg   /* Add up the values so that each element of VALS has absolute value
63*627f7eb2Smrg      at most equal to the last set bit of the next nonzero
64*627f7eb2Smrg      element.  */
65*627f7eb2Smrg   for (size_t i = 0; i <= 3; i++)
66*627f7eb2Smrg     {
67*627f7eb2Smrg       add_split (&vals[i + 1], &vals[i], vals[i + 1], vals[i]);
68*627f7eb2Smrg       qsort (vals + i + 1, 4 - i, sizeof (__float128), compare);
69*627f7eb2Smrg     }
70*627f7eb2Smrg   /* Now any error from this addition will be small.  */
71*627f7eb2Smrg   return vals[4] + vals[3] + vals[2] + vals[1] + vals[0];
72*627f7eb2Smrg }
73