Path: blob/main/contrib/arm-optimized-routines/math/aarch64/sinpif_2u5.c
48255 views
/*1* Single-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#include "mathlib.h"8#include "math_config.h"9#include "test_sig.h"10#include "test_defs.h"1112/* Taylor series coefficents for sin(pi * x). */13#define C0 0x1.921fb6p1f14#define C1 -0x1.4abbcep2f15#define C2 0x1.466bc6p1f16#define C3 -0x1.32d2ccp-1f17#define C4 0x1.50783p-4f18#define C5 -0x1.e30750p-8f1920#define Shift 0x1.0p+23f2122/* Approximation for scalar single-precision sinpi(x) - sinpif.23Maximum error: 2.48 ULP:24sinpif(0x1.d062b6p-2) got 0x1.fa8c06p-125want 0x1.fa8c02p-1. */26float27arm_math_sinpif (float x)28{29if (isinf (x) || isnan (x))30return __math_invalidf (x);3132float r = asfloat (asuint (x) & ~0x80000000);33uint32_t sign = asuint (x) & 0x80000000;3435/* Edge cases for when sinpif should be exactly 0. (Integers)360x1p23 is the limit for single precision to store any decimal places. */37if (r >= 0x1p23f)38return asfloat (sign);3940int32_t m = roundf (r);41if (m == r)42return asfloat (sign);4344/* For very small inputs, squaring r causes underflow.45Values below this threshold can be approximated via sinpi(x) ~= pi*x. */46if (r < 0x1p-31f)47return C0 * x;4849/* Any non-integer values >= 0x1p22f will be int + 0.5.50These values should return exactly 1 or -1. */51if (r >= 0x1p22f)52{53uint32_t iy = ((m & 1) << 31) ^ asuint (-1.0f);54return asfloat (sign ^ iy);55}5657/* n = rint(|x|). */58float n = r + Shift;59sign ^= (asuint (n) << 31);60n = n - Shift;6162/* r = |x| - n (range reduction into -1/2 .. 1/2). */63r = r - n;6465/* y = sin(pi * r). */66float r2 = r * r;67float y = fmaf (C5, r2, C4);68y = fmaf (y, r2, C3);69y = fmaf (y, r2, C2);70y = fmaf (y, r2, C1);71y = fmaf (y, r2, C0);7273/* Copy sign of x to sin(|x|). */74return asfloat (asuint (y * r) ^ sign);75}7677#if WANT_EXPERIMENTAL_MATH78float79sinpif (float x)80{81return arm_math_sinpif (x);82}83#endif8485#if WANT_TRIGPI_TESTS86TEST_ULP (arm_math_sinpif, 1.99)87TEST_SYM_INTERVAL (arm_math_sinpif, 0, 0x1p-31, 5000)88TEST_SYM_INTERVAL (arm_math_sinpif, 0x1p-31, 0.5, 10000)89TEST_SYM_INTERVAL (arm_math_sinpif, 0.5, 0x1p22f, 10000)90TEST_SYM_INTERVAL (arm_math_sinpif, 0x1p22f, inf, 10000)91#endif929394