Path: blob/main/contrib/arm-optimized-routines/math/aarch64/cospif_2u6.c
48255 views
/*1* Single-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"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 cospi(x) - cospif.23Maximum error: 2.64 ULP:24cospif(0x1.37e844p-4) got 0x1.f16b3p-125want 0x1.f16b2ap-1. */26float27arm_math_cospif (float x)28{29if (isinf (x) || isnan (x))30return __math_invalidf (x);3132float ax = asfloat (asuint (x) & ~0x80000000);3334/* Edge cases for when cospif should be exactly +/- 1. (Integers)350x1p23 is the limit for single precision to store any decimal places. */36if (ax >= 0x1p24f)37return 1;3839uint32_t m = roundf (ax);40if (m == ax)41return (m & 1) ? -1 : 1;4243/* Any non-integer values >= 0x1p22f will be int +0.5.44These values should return exactly 0. */45if (ax >= 0x1p22f)46return 0;4748/* For very small inputs, squaring r causes underflow.49Values below this threshold can be approximated via cospi(x) ~= 1 -50(pi*x). */51if (ax < 0x1p-31f)52return 1 - (C0 * x);5354/* n = rint(|x|). */55float n = ax + Shift;56uint32_t sign = asuint (n) << 31;57n = n - Shift;5859/* We know that cospi(x) = sinpi(0.5 - x)60range reduction and offset into sinpi range -1/2 .. 1/261r = 0.5 - |x - rint(x)|. */62float r = 0.5f - fabs (ax - n);6364/* y = sin(pi * r). */65float r2 = r * r;66float y = fmaf (C5, r2, C4);67y = fmaf (y, r2, C3);68y = fmaf (y, r2, C2);69y = fmaf (y, r2, C1);70y = fmaf (y, r2, C0);7172/* As all values are reduced to -1/2 .. 1/2, the result of cos(x) always be73positive, therefore, the sign must be introduced based upon if x rounds to74odd or even. */75return asfloat (asuint (y * r) ^ sign);76}7778#if WANT_EXPERIMENTAL_MATH79float80cospif (float x)81{82return arm_math_cospif (x);83}84#endif8586#if WANT_TRIGPI_TESTS87TEST_ULP (arm_math_cospif, 2.15)88TEST_SYM_INTERVAL (arm_math_cospif, 0, 0x1p-31, 5000)89TEST_SYM_INTERVAL (arm_math_cospif, 0x1p-31, 0.5, 10000)90TEST_SYM_INTERVAL (arm_math_cospif, 0.5, 0x1p22f, 10000)91TEST_SYM_INTERVAL (arm_math_cospif, 0x1p22f, inf, 10000)92#endif939495