root/lib/msun/ld128/s_asinpil.c
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2026 Steven G. Kargl
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice unmodified, this list of conditions, and the following
 *    disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * src/s_asinpi.c for implemenation details.
 */

#include "math.h"
#include "math_private.h"

#define _CC     (0x1p57L + 1)
#define _ROOT   sqrtl

volatile static const double tiny = 1.e-300;
static const double half = 0.5, one = 1.;

/* Full precision high and low parts of 1 / pi. */
static const long double
invpihi =  3.18309886183790671537767526745028737e-01L,
invpilo = -1.28821588763206006125693864783127482e-35L;

/*
 * Prior to the leading multiplication by x^2, the rational approximation
 * has an absolute minimax error less than 2.86e-38 over the [0x1p-56,0.5]
 * domain (or log2(error) = -124.7).
 */
static inline long double
__r(long double xs)
{
        static const long double
            R0 =  5.30516476972984452562945877908381207e-02L,
            R1 = -2.60404681812983888677486288898051068e-01L,
            R2 =  5.43274368732204127849880302812989126e-01L,
            R3 = -6.27476039646895838848725721148947475e-01L,
            R4 =  4.37806223811161460670580937913479055e-01L,
            R5 = -1.88854683672201011042310131486279147e-01L,
            R6 =  4.94528645546233985859708428238164776e-02L,
            R7 = -7.38228286468784921885881430313681146e-03L,
            R8 =  5.47609957562826794808966842094709768e-04L,
            R9 = -1.44837427671490633843418618030301533e-05L,
            R10=  1.60087061374239702655150492329207605e-08L,
            S1 = -5.35851261206434695164324875722635942e+00L,
            S2 =  1.23839541267281630422432341920191869e+01L,
            S3 = -1.61473998442126533731585929134597693e+01L,
            S4 =  1.30442314991246752613457576698550221e+01L,
            S5 = -6.74685395302389922545194846871599697e+00L,
            S6 =  2.22958089278066716953022085768628547e+00L,
            S7 = -4.55313732093612723575589438727154778e-01L,
            S8 =  5.33391487207251214709627478847204628e-02L,
            S9 = -3.08343599417750507732125775680767303e-03L,
            S10=  6.12260957946655623349049151336307244e-05L;

        long double r, s;
        r = R5 + (R6 + (R7 + (R8 + (R9 + R10 * xs) * xs) *xs) * xs) * xs;
        r = R0 + (R1 + (R2 + (R3 + (R4 + r * xs) * xs) * xs) * xs) * xs;
        s = S5 + (S6 + (S7 + (S8 + (S9 + S10 * xs) * xs) *xs) * xs) * xs;
        s =  1 + (S1 + (S2 + (S3 + (S4 + r * xs) * xs) * xs) * xs) * xs;
        return (xs * (r / s));
}

long double
asinpil(long double x)
{
        long double ax, hi, lo, xh, xl, y, zh, zl;

        if (isnan(x) || isinf(x))
                return ((x - x) / (x - x));

        ax = fabsl(x);

        if (ax > 1)                             /* |x| > 1 */
                return ((x - x) / (x - x));


        if (ax <= 0.5) {                        /* |x| <= 0.5 */
                if (ax < 0x1p-57L) {            /* |x| < 0x1p-57 */
                        if (ax < 0x1p-16340L) { /* |x| < 0x1p-16340 */
                                if (ax == 0)
                                        return (x);
                                /* Scale for near subnormal. */
                                ax *= 0x1p114;
                                _XMUL(ax, 0, invpihi, invpilo, hi, lo);
                                y = (hi + lo) * 0x1p-114;
                        } else {
                                _XMUL(ax, 0, invpihi, invpilo, hi, lo);
                                y = hi + lo;
                        }
                } else {
                        y = __r(ax * ax);
                        _XADD(invpihi, invpilo, y, 0, xh, xl);
                        _XMUL(ax, 0, xh, xl, hi, lo);
                        y = hi + lo;
                }
        } else if (ax < 1) {            /* |x| < 1 */
                y = 1 - ax;
                x = __r(y / 2);
                _XADD(invpihi, invpilo, x, 0, xh, xl);
                _SQRT(2 * y, zh, zl);
                _XMUL(xh, xl, zh, zl, hi, lo);
                _XADD(half, 0, -hi, -lo, y, x);
        } else                                  /* |x| == 1 */
                y = half;

        return (x < 0 ? -y : y);
}

/*
 * See src/s_asinpi.c for implementation details.
 */

long double
acospil(long double x)
{
        long double ax, hi, lo, xh, xl, y, zh, zl;

        if (isnan(x) || isinf(x))
                return ((x - x) / (x - x));

        ax = fabsl(x);

        if (ax > 1)                             /* |x| > 1 */
                return ((x - x) / (x - x));

        if (ax <= 0.5) {                        /* |x| <= 0.5 */
                if (ax < 0x1p-55L) {            /* |x| < 0x1p-55 */
                        y = (ax == 0) ? half : (ax < 0x1p-113 ?
                            half - tiny : half - x * invpihi);
                } else {
                        y = __r(x * x);
                        _XADD(invpihi, invpilo, y, 0, xh, xl);
                        _XMUL(x, 0, xh, xl, hi, lo);
                        _XADD(half, 0, -hi, -lo, y, ax);
                }
        } else if (ax < 1) {            /* |x| < 1 */
                y = 1 - ax;
                ax = __r(y / 2);
                _XADD(invpihi, invpilo, ax, 0, xh, xl);
                _SQRT(2 * y, zh, zl);
                _XMUL(xh, xl, zh, zl, hi, lo);
                if (x < 0)
                        _XADD(one, 0, -hi, -lo, y, ax);
                else
                        y = hi + lo;
        } else                                  /* |x| == 1 */
                y = half;

        return (y);
}