Path: blob/main/contrib/arm-optimized-routines/math/aarch64/sve/cosh.c
48375 views
/*1* Double-precision SVE cosh(x) function.2*3* Copyright (c) 2023-2025, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#include "sv_math.h"8#include "test_sig.h"9#include "test_defs.h"1011static const struct data12{13float64_t poly[3];14float64_t inv_ln2, ln2_hi, ln2_lo, shift, thres;15uint64_t special_bound;16} data = {17.poly = { 0x1.fffffffffffd4p-2, 0x1.5555571d6b68cp-3,180x1.5555576a59599p-5, },1920.inv_ln2 = 0x1.71547652b82fep8, /* N/ln2. */21/* -ln2/N. */22.ln2_hi = -0x1.62e42fefa39efp-9,23.ln2_lo = -0x1.abc9e3b39803f3p-64,24.shift = 0x1.8p+52,25.thres = 704.0,2627/* 0x1.6p9, above which exp overflows. */28.special_bound = 0x4086000000000000,29};3031static svfloat64_t NOINLINE32special_case (svfloat64_t x, svbool_t pg, svfloat64_t t, svbool_t special)33{34svfloat64_t half_t = svmul_x (svptrue_b64 (), t, 0.5);35svfloat64_t half_over_t = svdivr_x (pg, t, 0.5);36svfloat64_t y = svadd_x (pg, half_t, half_over_t);37return sv_call_f64 (cosh, x, y, special);38}3940/* Helper for approximating exp(x). Copied from sv_exp_tail, with no41special-case handling or tail. */42static inline svfloat64_t43exp_inline (svfloat64_t x, const svbool_t pg, const struct data *d)44{45/* Calculate exp(x). */46svfloat64_t z = svmla_x (pg, sv_f64 (d->shift), x, d->inv_ln2);47svfloat64_t n = svsub_x (pg, z, d->shift);4849svfloat64_t r = svmla_x (pg, x, n, d->ln2_hi);50r = svmla_x (pg, r, n, d->ln2_lo);5152svuint64_t u = svreinterpret_u64 (z);53svuint64_t e = svlsl_x (pg, u, 52 - V_EXP_TAIL_TABLE_BITS);54svuint64_t i = svand_x (svptrue_b64 (), u, 0xff);5556svfloat64_t y = svmla_x (pg, sv_f64 (d->poly[1]), r, d->poly[2]);57y = svmla_x (pg, sv_f64 (d->poly[0]), r, y);58y = svmla_x (pg, sv_f64 (1.0), r, y);59y = svmul_x (svptrue_b64 (), r, y);6061/* s = 2^(n/N). */62u = svld1_gather_index (pg, __v_exp_tail_data, i);63svfloat64_t s = svreinterpret_f64 (svadd_x (pg, u, e));6465return svmla_x (pg, s, s, y);66}6768/* Approximation for SVE double-precision cosh(x) using exp_inline.69cosh(x) = (exp(x) + exp(-x)) / 2.70The greatest observed error is in the scalar fall-back region, so is the71same as the scalar routine, 1.93 ULP:72_ZGVsMxv_cosh (0x1.628ad45039d2fp+9) got 0x1.fd774e958236dp+102173want 0x1.fd774e958236fp+1021.7475The greatest observed error in the non-special region is 1.54 ULP:76_ZGVsMxv_cosh (0x1.ba5651dd4486bp+2) got 0x1.f5e2bb8d5c98fp+877want 0x1.f5e2bb8d5c991p+8. */78svfloat64_t SV_NAME_D1 (cosh) (svfloat64_t x, const svbool_t pg)79{80const struct data *d = ptr_barrier (&data);8182svfloat64_t ax = svabs_x (pg, x);83svbool_t special = svcmpgt (pg, svreinterpret_u64 (ax), d->special_bound);8485/* Up to the point that exp overflows, we can use it to calculate cosh by86exp(|x|) / 2 + 1 / (2 * exp(|x|)). */87svfloat64_t t = exp_inline (ax, pg, d);8889/* Fall back to scalar for any special cases. */90if (unlikely (svptest_any (pg, special)))91return special_case (x, pg, t, special);9293svfloat64_t half_t = svmul_x (svptrue_b64 (), t, 0.5);94svfloat64_t half_over_t = svdivr_x (pg, t, 0.5);95return svadd_x (pg, half_t, half_over_t);96}9798TEST_SIG (SV, D, 1, cosh, -10.0, 10.0)99TEST_ULP (SV_NAME_D1 (cosh), 1.43)100TEST_DISABLE_FENV (SV_NAME_D1 (cosh))101TEST_SYM_INTERVAL (SV_NAME_D1 (cosh), 0, 0x1.6p9, 100000)102TEST_SYM_INTERVAL (SV_NAME_D1 (cosh), 0x1.6p9, inf, 1000)103CLOSE_SVE_ATTR104105