Path: blob/main/contrib/arm-optimized-routines/math/aarch64/experimental/acos_2u.c
48375 views
/*1* Double-precision acos(x) function.2*3* Copyright (c) 2023-2024, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#include "math_config.h"8#include "poly_scalar_f64.h"9#include "test_sig.h"10#include "test_defs.h"1112#define AbsMask 0x7fffffffffffffff13#define Half 0x3fe000000000000014#define One 0x3ff000000000000015#define PiOver2 0x1.921fb54442d18p+016#define Pi 0x1.921fb54442d18p+117#define Small 0x3c90000000000000 /* 2^-53. */18#define Small16 0x3c9019#define QNaN 0x7ff82021/* Fast implementation of double-precision acos(x) based on polynomial22approximation of double-precision asin(x).2324For x < Small, approximate acos(x) by pi/2 - x. Small = 2^-53 for correct25rounding.2627For |x| in [Small, 0.5], use the trigonometric identity2829acos(x) = pi/2 - asin(x)3031and use an order 11 polynomial P such that the final approximation of asin32is an odd polynomial: asin(x) ~ x + x^3 * P(x^2).3334The largest observed error in this region is 1.18 ulps,35acos(0x1.fbab0a7c460f6p-2) got 0x1.0d54d1985c068p+036want 0x1.0d54d1985c069p+0.3738For |x| in [0.5, 1.0], use the following development of acos(x) near x = 13940acos(x) ~ pi/2 - 2 * sqrt(z) (1 + z * P(z))4142where z = (1-x)/2, z is near 0 when x approaches 1, and P contributes to the43approximation of asin near 0.4445The largest observed error in this region is 1.52 ulps,46acos(0x1.23d362722f591p-1) got 0x1.edbbedf8a7d6ep-147want 0x1.edbbedf8a7d6cp-1.4849For x in [-1.0, -0.5], use this other identity to deduce the negative inputs50from their absolute value: acos(x) = pi - acos(-x). */51double52acos (double x)53{54uint64_t ix = asuint64 (x);55uint64_t ia = ix & AbsMask;56uint64_t ia16 = ia >> 48;57double ax = asdouble (ia);58uint64_t sign = ix & ~AbsMask;5960/* Special values and invalid range. */61if (unlikely (ia16 == QNaN))62return x;63if (ia > One)64return __math_invalid (x);65if (ia16 < Small16)66return PiOver2 - x;6768/* Evaluate polynomial Q(|x|) = z + z * z2 * P(z2) with69z2 = x ^ 2 and z = |x| , if |x| < 0.570z2 = (1 - |x|) / 2 and z = sqrt(z2), if |x| >= 0.5. */71double z2 = ax < 0.5 ? x * x : fma (-0.5, ax, 0.5);72double z = ax < 0.5 ? ax : sqrt (z2);7374/* Use a single polynomial approximation P for both intervals. */75double z4 = z2 * z2;76double z8 = z4 * z4;77double z16 = z8 * z8;78double p = estrin_11_f64 (z2, z4, z8, z16, __asin_poly);7980/* Finalize polynomial: z + z * z2 * P(z2). */81p = fma (z * z2, p, z);8283/* acos(|x|) = pi/2 - sign(x) * Q(|x|), for |x| < 0.584= pi - 2 Q(|x|), for -1.0 < x <= -0.585= 2 Q(|x|) , for -0.5 < x < 0.0. */86if (ax < 0.5)87return PiOver2 - asdouble (asuint64 (p) | sign);8889return (x <= -0.5) ? fma (-2.0, p, Pi) : 2.0 * p;90}9192TEST_SIG (S, D, 1, acos, -1.0, 1.0)93TEST_ULP (acos, 1.02)94TEST_INTERVAL (acos, 0, Small, 5000)95TEST_INTERVAL (acos, Small, 0.5, 50000)96TEST_INTERVAL (acos, 0.5, 1.0, 50000)97TEST_INTERVAL (acos, 1.0, 0x1p11, 50000)98TEST_INTERVAL (acos, 0x1p11, inf, 20000)99TEST_INTERVAL (acos, -0, -inf, 20000)100101102