Path: blob/main/contrib/arm-optimized-routines/math/aarch64/advsimd/cosh.c
48375 views
/*1* Double-precision vector cosh(x) function.2*3* Copyright (c) 2022-2024, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#include "v_math.h"8#include "test_sig.h"9#include "test_defs.h"1011static const struct data12{13float64x2_t poly[3];14float64x2_t inv_ln2;15double ln2[2];16float64x2_t shift, thres;17uint64x2_t index_mask, special_bound;18} data = {19.poly = { V2 (0x1.fffffffffffd4p-2), V2 (0x1.5555571d6b68cp-3),20V2 (0x1.5555576a59599p-5), },2122.inv_ln2 = V2 (0x1.71547652b82fep8), /* N/ln2. */23/* -ln2/N. */24.ln2 = {-0x1.62e42fefa39efp-9, -0x1.abc9e3b39803f3p-64},25.shift = V2 (0x1.8p+52),26.thres = V2 (704.0),2728.index_mask = V2 (0xff),29/* 0x1.6p9, above which exp overflows. */30.special_bound = V2 (0x4086000000000000),31};3233static float64x2_t NOINLINE VPCS_ATTR34special_case (float64x2_t x, float64x2_t y, uint64x2_t special)35{36return v_call_f64 (cosh, x, y, special);37}3839/* Helper for approximating exp(x). Copied from v_exp_tail, with no40special-case handling or tail. */41static inline float64x2_t42exp_inline (float64x2_t x)43{44const struct data *d = ptr_barrier (&data);4546/* n = round(x/(ln2/N)). */47float64x2_t z = vfmaq_f64 (d->shift, x, d->inv_ln2);48uint64x2_t u = vreinterpretq_u64_f64 (z);49float64x2_t n = vsubq_f64 (z, d->shift);5051/* r = x - n*ln2/N. */52float64x2_t ln2 = vld1q_f64 (d->ln2);53float64x2_t r = vfmaq_laneq_f64 (x, n, ln2, 0);54r = vfmaq_laneq_f64 (r, n, ln2, 1);5556uint64x2_t e = vshlq_n_u64 (u, 52 - V_EXP_TAIL_TABLE_BITS);57uint64x2_t i = vandq_u64 (u, d->index_mask);5859/* y = tail + exp(r) - 1 ~= r + C1 r^2 + C2 r^3 + C3 r^4. */60float64x2_t y = vfmaq_f64 (d->poly[1], d->poly[2], r);61y = vfmaq_f64 (d->poly[0], y, r);62y = vmulq_f64 (vfmaq_f64 (v_f64 (1), y, r), r);6364/* s = 2^(n/N). */65u = v_lookup_u64 (__v_exp_tail_data, i);66float64x2_t s = vreinterpretq_f64_u64 (vaddq_u64 (u, e));6768return vfmaq_f64 (s, y, s);69}7071/* Approximation for vector double-precision cosh(x) using exp_inline.72cosh(x) = (exp(x) + exp(-x)) / 2.73The greatest observed error is in the scalar fall-back region, so is the74same as the scalar routine, 1.93 ULP:75_ZGVnN2v_cosh (0x1.628af341989dap+9) got 0x1.fdf28623ef921p+102176want 0x1.fdf28623ef923p+1021.7778The greatest observed error in the non-special region is 1.54 ULP:79_ZGVnN2v_cosh (0x1.8e205b6ecacf7p+2) got 0x1.f711dcb0c77afp+780want 0x1.f711dcb0c77b1p+7. */81float64x2_t VPCS_ATTR V_NAME_D1 (cosh) (float64x2_t x)82{83const struct data *d = ptr_barrier (&data);8485float64x2_t ax = vabsq_f64 (x);86uint64x2_t special87= vcgtq_u64 (vreinterpretq_u64_f64 (ax), d->special_bound);8889/* Up to the point that exp overflows, we can use it to calculate cosh by90exp(|x|) / 2 + 1 / (2 * exp(|x|)). */91float64x2_t t = exp_inline (ax);92float64x2_t half_t = vmulq_n_f64 (t, 0.5);93float64x2_t half_over_t = vdivq_f64 (v_f64 (0.5), t);9495/* Fall back to scalar for any special cases. */96if (unlikely (v_any_u64 (special)))97return special_case (x, vaddq_f64 (half_t, half_over_t), special);9899return vaddq_f64 (half_t, half_over_t);100}101102TEST_SIG (V, D, 1, cosh, -10.0, 10.0)103TEST_ULP (V_NAME_D1 (cosh), 1.43)104TEST_DISABLE_FENV_IF_NOT (V_NAME_D1 (cosh), WANT_SIMD_EXCEPT)105TEST_SYM_INTERVAL (V_NAME_D1 (cosh), 0, 0x1.6p9, 100000)106TEST_SYM_INTERVAL (V_NAME_D1 (cosh), 0x1.6p9, inf, 1000)107108109