blob: e8fc7ea37ccc0ae5062bc341474cf4f0eae9c26c [file] [log] [blame]
/*
* Copyright (c) 2014 Advanced Micro Devices, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "math64.h"
__attribute__((overloadable)) double
acospi(double x)
{
// Computes arccos(x).
// The argument is first reduced by noting that arccos(x)
// is invalid for abs(x) > 1. For denormal and small
// arguments arccos(x) = pi/2 to machine accuracy.
// Remaining argument ranges are handled as follows.
// For abs(x) <= 0.5 use
// arccos(x) = pi/2 - arcsin(x)
// = pi/2 - (x + x^3*R(x^2))
// where R(x^2) is a rational minimax approximation to
// (arcsin(x) - x)/x^3.
// For abs(x) > 0.5 exploit the identity:
// arccos(x) = pi - 2*arcsin(sqrt(1-x)/2)
// together with the above rational approximation, and
// reconstruct the terms carefully.
const double pi = 0x1.921fb54442d18p+1;
const double piby2_tail = 6.12323399573676603587e-17; /* 0x3c91a62633145c07 */
double y = fabs(x);
int xneg = as_int2(x).hi < 0;
int xexp = (as_int2(y).hi >> 20) - EXPBIAS_DP64;
// abs(x) >= 0.5
int transform = xexp >= -1;
// Transform y into the range [0,0.5)
double r1 = 0.5 * (1.0 - y);
double s = sqrt(r1);
double r = y * y;
r = transform ? r1 : r;
y = transform ? s : y;
// Use a rational approximation for [0.0, 0.5]
double un = fma(r,
fma(r,
fma(r,
fma(r,
fma(r, 0.0000482901920344786991880522822991,
0.00109242697235074662306043804220),
-0.0549989809235685841612020091328),
0.275558175256937652532686256258),
-0.445017216867635649900123110649),
0.227485835556935010735943483075);
double ud = fma(r,
fma(r,
fma(r,
fma(r, 0.105869422087204370341222318533,
-0.943639137032492685763471240072),
2.76568859157270989520376345954),
-3.28431505720958658909889444194),
1.36491501334161032038194214209);
double u = r * MATH_DIVIDE(un, ud);
// Reconstruct acos carefully in transformed region
double res1 = fma(-2.0, MATH_DIVIDE(s + fma(y, u, -piby2_tail), pi), 1.0);
double s1 = as_double(as_ulong(s) & 0xffffffff00000000UL);
double c = MATH_DIVIDE(fma(-s1, s1, r), s + s1);
double res2 = MATH_DIVIDE(fma(2.0, s1, fma(2.0, c, 2.0 * y * u)), pi);
res1 = xneg ? res1 : res2;
res2 = 0.5 - fma(x, u, x) / pi;
res1 = transform ? res1 : res2;
const double qnan = as_double(QNANBITPATT_DP64);
res2 = x == 1.0 ? 0.0 : qnan;
res2 = x == -1.0 ? 1.0 : res2;
res1 = xexp >= 0 ? res2 : res1;
res1 = xexp < -56 ? 0.5 : res1;
return res1;
}