Path: blob/main/contrib/arm-optimized-routines/math/aarch64/sinpi_3u5.c
48255 views
/*1* Double-precision scalar sinpi function.2*3* Copyright (c) 2023-2024, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#define _GNU_SOURCE8#include <math.h>9#include "mathlib.h"10#include "math_config.h"11#include "test_sig.h"12#include "test_defs.h"13#include "poly_scalar_f64.h"1415/* Taylor series coefficents for sin(pi * x).16C2 coefficient (orginally ~=5.16771278) has been split into two parts:17C2_hi = 4, C2_lo = C2 - C2_hi (~=1.16771278)18This change in magnitude reduces floating point rounding errors.19C2_hi is then reintroduced after the polynomial approxmation. */20static const double poly[]21= { 0x1.921fb54442d184p1, -0x1.2aef39896f94bp0, 0x1.466bc6775ab16p1,22-0x1.32d2cce62dc33p-1, 0x1.507834891188ep-4, -0x1.e30750a28c88ep-8,230x1.e8f48308acda4p-12, -0x1.6fc0032b3c29fp-16, 0x1.af86ae521260bp-21,24-0x1.012a9870eeb7dp-25 };2526#define Shift 0x1.8p+5227/* TODO Store constant in structure for more efficient load. */28#define Pi 0x1.921fb54442d18p+12930/* Approximation for scalar double-precision sinpi(x).31Maximum error: 3.03 ULP:32sinpi(0x1.a90da2818f8b5p+7) got 0x1.fe358f255a4b3p-133want 0x1.fe358f255a4b6p-1. */34double35arm_math_sinpi (double x)36{37if (isinf (x) || isnan (x))38return __math_invalid (x);3940double r = asdouble (asuint64 (x) & ~0x8000000000000000);41uint64_t sign = asuint64 (x) & 0x8000000000000000;4243/* Edge cases for when sinpif should be exactly 0. (Integers)440x1p53 is the limit for single precision to store any decimal places. */45if (r >= 0x1p53)46return asdouble (sign);4748/* If x is an integer, return 0. */49uint64_t m = (uint64_t) r;50if (r == m)51return asdouble (sign);5253/* For very small inputs, squaring r causes underflow.54Values below this threshold can be approximated via sinpi(x) ≈ pi*x. */55if (r < 0x1p-63)56return Pi * x;5758/* Any non-integer values >= 0x1x51 will be int + 0.5.59These values should return exactly 1 or -1. */60if (r >= 0x1p51)61{62uint64_t iy = ((m & 1) << 63) ^ asuint64 (1.0);63return asdouble (sign ^ iy);64}6566/* n = rint(|x|). */67double n = r + Shift;68sign ^= (asuint64 (n) << 63);69n = n - Shift;7071/* r = |x| - n (range reduction into -1/2 .. 1/2). */72r = r - n;7374/* y = sin(r). */75double r2 = r * r;76double y = horner_9_f64 (r2, poly);77y = y * r;7879/* Reintroduce C2_hi. */80y = fma (-4 * r2, r, y);8182/* Copy sign of x to sin(|x|). */83return asdouble (asuint64 (y) ^ sign);84}8586#if WANT_EXPERIMENTAL_MATH87double88sinpi (double x)89{90return arm_math_sinpi (x);91}92#endif9394#if WANT_TRIGPI_TESTS95TEST_ULP (arm_math_sinpi, 2.53)96TEST_SYM_INTERVAL (arm_math_sinpi, 0, 0x1p-63, 5000)97TEST_SYM_INTERVAL (arm_math_sinpi, 0x1p-63, 0.5, 10000)98TEST_SYM_INTERVAL (arm_math_sinpi, 0.5, 0x1p51, 10000)99TEST_SYM_INTERVAL (arm_math_sinpi, 0x1p51, inf, 10000)100#endif101102103