|
Packit |
6c4009 |
/* Compute x^2 + y^2 - 1, without large cancellation error.
|
|
Packit |
6c4009 |
Copyright (C) 2012-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <math.h>
|
|
Packit |
6c4009 |
#include <math_private.h>
|
|
Packit |
6c4009 |
#include <mul_split.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Calculate X + Y exactly and store the result in *HI + *LO. It is
|
|
Packit |
6c4009 |
given that |X| >= |Y| and the values are small enough that no
|
|
Packit |
6c4009 |
overflow occurs. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static inline void
|
|
Packit |
6c4009 |
add_split (double *hi, double *lo, double x, double y)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Apply Dekker's algorithm. */
|
|
Packit |
6c4009 |
*hi = x + y;
|
|
Packit |
6c4009 |
*lo = (x - *hi) + y;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Compare absolute values of floating-point values pointed to by P
|
|
Packit |
6c4009 |
and Q for qsort. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
compare (const void *p, const void *q)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
double pd = fabs (*(const double *) p);
|
|
Packit |
6c4009 |
double qd = fabs (*(const double *) q);
|
|
Packit |
6c4009 |
if (pd < qd)
|
|
Packit |
6c4009 |
return -1;
|
|
Packit |
6c4009 |
else if (pd == qd)
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Return X^2 + Y^2 - 1, computed without large cancellation error.
|
|
Packit |
6c4009 |
It is given that 1 > X >= Y >= epsilon / 2, and that X^2 + Y^2 >=
|
|
Packit |
6c4009 |
0.5. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
long double
|
|
Packit |
6c4009 |
__x2y2m1l (long double x, long double y)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
double vals[13];
|
|
Packit |
6c4009 |
SET_RESTORE_ROUND (FE_TONEAREST);
|
|
Packit |
6c4009 |
union ibm_extended_long_double xu, yu;
|
|
Packit |
6c4009 |
xu.ld = x;
|
|
Packit |
6c4009 |
yu.ld = y;
|
|
Packit |
6c4009 |
if (fabs (xu.d[1].d) < 0x1p-500)
|
|
Packit |
6c4009 |
xu.d[1].d = 0.0;
|
|
Packit |
6c4009 |
if (fabs (yu.d[1].d) < 0x1p-500)
|
|
Packit |
6c4009 |
yu.d[1].d = 0.0;
|
|
Packit |
6c4009 |
mul_split (&vals[1], &vals[0], xu.d[0].d, xu.d[0].d);
|
|
Packit |
6c4009 |
mul_split (&vals[3], &vals[2], xu.d[0].d, xu.d[1].d);
|
|
Packit |
6c4009 |
vals[2] *= 2.0;
|
|
Packit |
6c4009 |
vals[3] *= 2.0;
|
|
Packit |
6c4009 |
mul_split (&vals[5], &vals[4], xu.d[1].d, xu.d[1].d);
|
|
Packit |
6c4009 |
mul_split (&vals[7], &vals[6], yu.d[0].d, yu.d[0].d);
|
|
Packit |
6c4009 |
mul_split (&vals[9], &vals[8], yu.d[0].d, yu.d[1].d);
|
|
Packit |
6c4009 |
vals[8] *= 2.0;
|
|
Packit |
6c4009 |
vals[9] *= 2.0;
|
|
Packit |
6c4009 |
mul_split (&vals[11], &vals[10], yu.d[1].d, yu.d[1].d);
|
|
Packit |
6c4009 |
vals[12] = -1.0;
|
|
Packit |
6c4009 |
qsort (vals, 13, sizeof (double), compare);
|
|
Packit |
6c4009 |
/* Add up the values so that each element of VALS has absolute value
|
|
Packit |
6c4009 |
at most equal to the last set bit of the next nonzero
|
|
Packit |
6c4009 |
element. */
|
|
Packit |
6c4009 |
for (size_t i = 0; i <= 11; i++)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
add_split (&vals[i + 1], &vals[i], vals[i + 1], vals[i]);
|
|
Packit |
6c4009 |
qsort (vals + i + 1, 12 - i, sizeof (double), compare);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
/* Now any error from this addition will be small. */
|
|
Packit |
6c4009 |
long double retval = (long double) vals[12];
|
|
Packit |
6c4009 |
for (size_t i = 11; i != (size_t) -1; i--)
|
|
Packit |
6c4009 |
retval += (long double) vals[i];
|
|
Packit |
6c4009 |
return retval;
|
|
Packit |
6c4009 |
}
|