Path: blob/main/contrib/arm-optimized-routines/math/aarch64/cospi_3u5.c
48255 views
/*1* Double-precision scalar cospi function.2*3* Copyright (c) 2023-2024, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#include "mathlib.h"8#include "math_config.h"9#include "test_sig.h"10#include "test_defs.h"11#include "poly_scalar_f64.h"1213/* Taylor series coefficents for sin(pi * x).14C2 coefficient (orginally ~=5.16771278) has been split into two parts:15C2_hi = 4, C2_lo = C2 - C2_hi (~=1.16771278)16This change in magnitude reduces floating point rounding errors.17C2_hi is then reintroduced after the polynomial approxmation. */18static const double poly[]19= { 0x1.921fb54442d184p1, -0x1.2aef39896f94bp0, 0x1.466bc6775ab16p1,20-0x1.32d2cce62dc33p-1, 0x1.507834891188ep-4, -0x1.e30750a28c88ep-8,210x1.e8f48308acda4p-12, -0x1.6fc0032b3c29fp-16, 0x1.af86ae521260bp-21,22-0x1.012a9870eeb7dp-25 };2324#define Shift 0x1.8p+522526/* Approximation for scalar double-precision cospi(x).27Maximum error: 3.13 ULP:28cospi(0x1.160b129300112p-21) got 0x1.fffffffffd16bp-129want 0x1.fffffffffd16ep-1. */30double31arm_math_cospi (double x)32{33if (isinf (x) || isnan (x))34return __math_invalid (x);3536double ax = asdouble (asuint64 (x) & ~0x8000000000000000);3738/* Edge cases for when cospif should be exactly 1. (Integers)390x1p53 is the limit for single precision to store any decimal places. */40if (ax >= 0x1p53)41return 1;4243/* If x is an integer, return +- 1, based upon if x is odd. */44uint64_t m = (uint64_t) ax;45if (m == ax)46return (m & 1) ? -1 : 1;4748/* For very small inputs, squaring r causes underflow.49Values below this threshold can be approximated via50cospi(x) ~= 1. */51if (ax < 0x1p-63)52return 1;5354/* Any non-integer values >= 0x1x51 will be int +0.5.55These values should return exactly 0. */56if (ax >= 0x1p51)57return 0;5859/* n = rint(|x|). */60double n = ax + Shift;61uint64_t sign = asuint64 (n) << 63;62n = n - Shift;6364/* We know that cospi(x) = sinpi(0.5 - x)65range reduction and offset into sinpi range -1/2 .. 1/266r = 0.5 - |x - rint(x)|. */67double r = 0.5 - fabs (ax - n);6869/* y = sin(r). */70double r2 = r * r;71double y = horner_9_f64 (r2, poly);72y = y * r;7374/* Reintroduce C2_hi. */75y = fma (-4 * r2, r, y);7677/* As all values are reduced to -1/2 .. 1/2, the result of cos(x) always be78positive, therefore, the sign must be introduced based upon if x rounds to79odd or even. */80return asdouble (asuint64 (y) ^ sign);81}8283#if WANT_EXPERIMENTAL_MATH84double85cospi (double x)86{87return arm_math_cospi (x);88}89#endif9091#if WANT_TRIGPI_TESTS92TEST_ULP (arm_math_cospi, 2.63)93TEST_SYM_INTERVAL (arm_math_cospi, 0, 0x1p-63, 5000)94TEST_SYM_INTERVAL (arm_math_cospi, 0x1p-63, 0.5, 10000)95TEST_SYM_INTERVAL (arm_math_cospi, 0.5, 0x1p51f, 10000)96TEST_SYM_INTERVAL (arm_math_cospi, 0x1p51f, inf, 10000)97#endif9899100