Path: blob/main/contrib/arm-optimized-routines/math/aarch64/experimental/acosf_1u4.c
48375 views
/*1* Single-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 "poly_scalar_f32.h"8#include "math_config.h"9#include "test_sig.h"10#include "test_defs.h"1112#define AbsMask 0x7fffffff13#define Half 0x3f00000014#define One 0x3f80000015#define PiOver2f 0x1.921fb6p+0f16#define Pif 0x1.921fb6p+1f17#define Small 0x32800000 /* 2^-26. */18#define Small12 0x32819#define QNaN 0x7fc2021/* Fast implementation of single-precision acos(x) based on polynomial22approximation of single-precision asin(x).2324For x < Small, approximate acos(x) by pi/2 - x. Small = 2^-26 for correct25rounding.2627For |x| in [Small, 0.5], use the trigonometric identity2829acos(x) = pi/2 - asin(x)3031and use an order 4 polynomial P such that the final approximation of asin is32an odd polynomial: asin(x) ~ x + x^3 * P(x^2).3334The largest observed error in this region is 1.16 ulps,35acosf(0x1.ffbeccp-2) got 0x1.0c27f8p+0 want 0x1.0c27f6p+0.3637For |x| in [0.5, 1.0], use the following development of acos(x) near x = 13839acos(x) ~ pi/2 - 2 * sqrt(z) (1 + z * P(z))4041where z = (1-x)/2, z is near 0 when x approaches 1, and P contributes to the42approximation of asin near 0.4344The largest observed error in this region is 1.32 ulps,45acosf(0x1.15ba56p-1) got 0x1.feb33p-1 want 0x1.feb32ep-1.4647For x in [-1.0, -0.5], use this other identity to deduce the negative inputs48from their absolute value.4950acos(x) = pi - acos(-x)5152The largest observed error in this region is 1.28 ulps,53acosf(-0x1.002072p-1) got 0x1.0c1e84p+1 want 0x1.0c1e82p+1. */54float55acosf (float x)56{57uint32_t ix = asuint (x);58uint32_t ia = ix & AbsMask;59uint32_t ia12 = ia >> 20;60float ax = asfloat (ia);61uint32_t sign = ix & ~AbsMask;6263/* Special values and invalid range. */64if (unlikely (ia12 == QNaN))65return x;66if (ia > One)67return __math_invalidf (x);68if (ia12 < Small12)69return PiOver2f - x;7071/* Evaluate polynomial Q(|x|) = z + z * z2 * P(z2) with72z2 = x ^ 2 and z = |x| , if |x| < 0.573z2 = (1 - |x|) / 2 and z = sqrt(z2), if |x| >= 0.5. */74float z2 = ax < 0.5 ? x * x : fmaf (-0.5f, ax, 0.5f);75float z = ax < 0.5 ? ax : sqrtf (z2);7677/* Use a single polynomial approximation P for both intervals. */78float p = horner_4_f32 (z2, __asinf_poly);79/* Finalize polynomial: z + z * z2 * P(z2). */80p = fmaf (z * z2, p, z);8182/* acos(|x|) = pi/2 - sign(x) * Q(|x|), for |x| < 0.583= pi - 2 Q(|x|), for -1.0 < x <= -0.584= 2 Q(|x|) , for -0.5 < x < 0.0. */85if (ax < 0.5)86return PiOver2f - asfloat (asuint (p) | sign);8788return (x <= -0.5) ? fmaf (-2.0f, p, Pif) : 2.0f * p;89}9091TEST_SIG (S, F, 1, acos, -1.0, 1.0)92TEST_ULP (acosf, 0.82)93TEST_INTERVAL (acosf, 0, Small, 5000)94TEST_INTERVAL (acosf, Small, 0.5, 50000)95TEST_INTERVAL (acosf, 0.5, 1.0, 50000)96TEST_INTERVAL (acosf, 1.0, 0x1p11, 50000)97TEST_INTERVAL (acosf, 0x1p11, inf, 20000)98TEST_INTERVAL (acosf, -0, -inf, 20000)99100101