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