|
Packit Service |
82fcde |
/* Compute cosine of argument.
|
|
Packit Service |
82fcde |
Copyright (C) 2017-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 <errno.h>
|
|
Packit Service |
82fcde |
#include <math.h>
|
|
Packit Service |
82fcde |
#include <math_private.h>
|
|
Packit Service |
82fcde |
#include <libm-alias-float.h>
|
|
Packit Service |
82fcde |
#include "s_sincosf.h"
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#ifndef COSF
|
|
Packit Service |
82fcde |
# define COSF_FUNC __cosf
|
|
Packit Service |
82fcde |
#else
|
|
Packit Service |
82fcde |
# define COSF_FUNC COSF
|
|
Packit Service |
82fcde |
#endif
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
float
|
|
Packit Service |
82fcde |
COSF_FUNC (float x)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
double theta = x;
|
|
Packit Service |
82fcde |
double abstheta = fabs (theta);
|
|
Packit Service |
82fcde |
if (isless (abstheta, M_PI_4))
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
double cx;
|
|
Packit Service |
82fcde |
if (abstheta >= 0x1p-5)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
const double theta2 = theta * theta;
|
|
Packit Service |
82fcde |
/* Chebyshev polynomial of the form for cos:
|
|
Packit Service |
82fcde |
* 1 + x^2 (C0 + x^2 (C1 + x^2 (C2 + x^2 (C3 + x^2 * C4)))). */
|
|
Packit Service |
82fcde |
cx = C3 + theta2 * C4;
|
|
Packit Service |
82fcde |
cx = C2 + theta2 * cx;
|
|
Packit Service |
82fcde |
cx = C1 + theta2 * cx;
|
|
Packit Service |
82fcde |
cx = C0 + theta2 * cx;
|
|
Packit Service |
82fcde |
cx = 1. + theta2 * cx;
|
|
Packit Service |
82fcde |
return cx;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else if (abstheta >= 0x1p-27)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
/* A simpler Chebyshev approximation is close enough for this range:
|
|
Packit Service |
82fcde |
* 1 + x^2 (CC0 + x^3 * CC1). */
|
|
Packit Service |
82fcde |
const double theta2 = theta * theta;
|
|
Packit Service |
82fcde |
cx = CC0 + theta * theta2 * CC1;
|
|
Packit Service |
82fcde |
cx = 1.0 + theta2 * cx;
|
|
Packit Service |
82fcde |
return cx;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
/* For small enough |theta|, this is close enough. */
|
|
Packit Service |
82fcde |
return 1.0 - abstheta;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else /* |theta| >= Pi/4. */
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
if (isless (abstheta, 9 * M_PI_4))
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
/* There are cases where FE_UPWARD rounding mode can
|
|
Packit Service |
82fcde |
produce a result of abstheta * inv_PI_4 == 9,
|
|
Packit Service |
82fcde |
where abstheta < 9pi/4, so the domain for
|
|
Packit Service |
82fcde |
pio2_table must go to 5 (9 / 2 + 1). */
|
|
Packit Service |
82fcde |
unsigned int n = (abstheta * inv_PI_4) + 1;
|
|
Packit Service |
82fcde |
theta = abstheta - pio2_table[n / 2];
|
|
Packit Service |
82fcde |
return reduced_cos (theta, n);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else if (isless (abstheta, INFINITY))
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
if (abstheta < 0x1p+23)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
unsigned int n = ((unsigned int) (abstheta * inv_PI_4)) + 1;
|
|
Packit Service |
82fcde |
double x = n / 2;
|
|
Packit Service |
82fcde |
theta = (abstheta - x * PI_2_hi) - x * PI_2_lo;
|
|
Packit Service |
82fcde |
/* Argument reduction needed. */
|
|
Packit Service |
82fcde |
return reduced_cos (theta, n);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else /* |theta| >= 2^23. */
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
x = fabsf (x);
|
|
Packit Service |
82fcde |
int exponent;
|
|
Packit Service |
82fcde |
GET_FLOAT_WORD (exponent, x);
|
|
Packit Service |
82fcde |
exponent = (exponent >> FLOAT_EXPONENT_SHIFT)
|
|
Packit Service |
82fcde |
- FLOAT_EXPONENT_BIAS;
|
|
Packit Service |
82fcde |
exponent += 3;
|
|
Packit Service |
82fcde |
exponent /= 28;
|
|
Packit Service |
82fcde |
double a = invpio4_table[exponent] * x;
|
|
Packit Service |
82fcde |
double b = invpio4_table[exponent + 1] * x;
|
|
Packit Service |
82fcde |
double c = invpio4_table[exponent + 2] * x;
|
|
Packit Service |
82fcde |
double d = invpio4_table[exponent + 3] * x;
|
|
Packit Service |
82fcde |
uint64_t l = a;
|
|
Packit Service |
82fcde |
l &= ~0x7;
|
|
Packit Service |
82fcde |
a -= l;
|
|
Packit Service |
82fcde |
double e = a + b;
|
|
Packit Service |
82fcde |
l = e;
|
|
Packit Service |
82fcde |
e = a - l;
|
|
Packit Service |
82fcde |
if (l & 1)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
e -= 1.0;
|
|
Packit Service |
82fcde |
e += b;
|
|
Packit Service |
82fcde |
e += c;
|
|
Packit Service |
82fcde |
e += d;
|
|
Packit Service |
82fcde |
e *= M_PI_4;
|
|
Packit Service |
82fcde |
return reduced_cos (e, l + 1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
e += b;
|
|
Packit Service |
82fcde |
e += c;
|
|
Packit Service |
82fcde |
e += d;
|
|
Packit Service |
82fcde |
if (e <= 1.0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
e *= M_PI_4;
|
|
Packit Service |
82fcde |
return reduced_cos (e, l + 1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
l++;
|
|
Packit Service |
82fcde |
e -= 2.0;
|
|
Packit Service |
82fcde |
e *= M_PI_4;
|
|
Packit Service |
82fcde |
return reduced_cos (e, l + 1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
int32_t ix;
|
|
Packit Service |
82fcde |
GET_FLOAT_WORD (ix, abstheta);
|
|
Packit Service |
82fcde |
/* cos(Inf or NaN) is NaN. */
|
|
Packit Service |
82fcde |
if (ix == 0x7f800000) /* Inf. */
|
|
Packit Service |
82fcde |
__set_errno (EDOM);
|
|
Packit Service |
82fcde |
return x - x;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#ifndef COSF
|
|
Packit Service |
82fcde |
libm_alias_float (__cos, cos)
|
|
Packit Service |
82fcde |
#endif
|