Path: blob/main/contrib/arm-optimized-routines/math/aarch64/advsimd/asinh.c
48378 views
/*1* Double-precision vector asinh(x) function.2*3* Copyright (c) 2022-2024, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#include "test_defs.h"8#include "test_sig.h"9#include "v_math.h"1011const static struct data12{13uint64x2_t huge_bound, abs_mask, off, mask;14#if WANT_SIMD_EXCEPT15float64x2_t tiny_bound;16#endif17float64x2_t lc0, lc2;18double lc1, lc3, ln2, lc4;1920float64x2_t c0, c2, c4, c6, c8, c10, c12, c14, c16, c17;21double c1, c3, c5, c7, c9, c11, c13, c15;2223} data = {2425#if WANT_SIMD_EXCEPT26.tiny_bound = V2 (0x1p-26),27#endif28/* Even terms of polynomial s.t. asinh(x) is approximated by29asinh(x) ~= x + x^3 * (C0 + C1 * x + C2 * x^2 + C3 * x^3 + ...).30Generated using Remez, f = (asinh(sqrt(x)) - sqrt(x))/x^(3/2). */3132.c0 = V2 (-0x1.55555555554a7p-3),33.c1 = 0x1.3333333326c7p-4,34.c2 = V2 (-0x1.6db6db68332e6p-5),35.c3 = 0x1.f1c71b26fb40dp-6,36.c4 = V2 (-0x1.6e8b8b654a621p-6),37.c5 = 0x1.1c4daa9e67871p-6,38.c6 = V2 (-0x1.c9871d10885afp-7),39.c7 = 0x1.7a16e8d9d2ecfp-7,40.c8 = V2 (-0x1.3ddca533e9f54p-7),41.c9 = 0x1.0becef748dafcp-7,42.c10 = V2 (-0x1.b90c7099dd397p-8),43.c11 = 0x1.541f2bb1ffe51p-8,44.c12 = V2 (-0x1.d217026a669ecp-9),45.c13 = 0x1.0b5c7977aaf7p-9,46.c14 = V2 (-0x1.e0f37daef9127p-11),47.c15 = 0x1.388b5fe542a6p-12,48.c16 = V2 (-0x1.021a48685e287p-14),49.c17 = V2 (0x1.93d4ba83d34dap-18),5051.lc0 = V2 (-0x1.ffffffffffff7p-2),52.lc1 = 0x1.55555555170d4p-2,53.lc2 = V2 (-0x1.0000000399c27p-2),54.lc3 = 0x1.999b2e90e94cap-3,55.lc4 = -0x1.554e550bd501ep-3,56.ln2 = 0x1.62e42fefa39efp-1,5758.off = V2 (0x3fe6900900000000),59.huge_bound = V2 (0x5fe0000000000000),60.abs_mask = V2 (0x7fffffffffffffff),61.mask = V2 (0xfffULL << 52),62};6364static float64x2_t NOINLINE VPCS_ATTR65special_case (float64x2_t x, float64x2_t y, uint64x2_t abs_mask,66uint64x2_t special)67{68/* Copy sign. */69y = vbslq_f64 (abs_mask, y, x);70return v_call_f64 (asinh, x, y, special);71}7273#define N (1 << V_LOG_TABLE_BITS)74#define IndexMask (N - 1)7576struct entry77{78float64x2_t invc;79float64x2_t logc;80};8182static inline struct entry83lookup (uint64x2_t i)84{85/* Since N is a power of 2, n % N = n & (N - 1). */86struct entry e;87uint64_t i0 = (vgetq_lane_u64 (i, 0) >> (52 - V_LOG_TABLE_BITS)) & IndexMask;88uint64_t i1 = (vgetq_lane_u64 (i, 1) >> (52 - V_LOG_TABLE_BITS)) & IndexMask;89float64x2_t e0 = vld1q_f64 (&__v_log_data.table[i0].invc);90float64x2_t e1 = vld1q_f64 (&__v_log_data.table[i1].invc);91e.invc = vuzp1q_f64 (e0, e1);92e.logc = vuzp2q_f64 (e0, e1);93return e;94}9596static inline float64x2_t97log_inline (float64x2_t xm, const struct data *d)98{99100uint64x2_t u = vreinterpretq_u64_f64 (xm);101uint64x2_t u_off = vsubq_u64 (u, d->off);102103int64x2_t k = vshrq_n_s64 (vreinterpretq_s64_u64 (u_off), 52);104uint64x2_t iz = vsubq_u64 (u, vandq_u64 (u_off, d->mask));105float64x2_t z = vreinterpretq_f64_u64 (iz);106107struct entry e = lookup (u_off);108109/* log(x) = log1p(z/c-1) + log(c) + k*Ln2. */110float64x2_t r = vfmaq_f64 (v_f64 (-1.0), z, e.invc);111float64x2_t kd = vcvtq_f64_s64 (k);112113/* hi = r + log(c) + k*Ln2. */114float64x2_t ln2_and_lc4 = vld1q_f64 (&d->ln2);115float64x2_t hi = vfmaq_laneq_f64 (vaddq_f64 (e.logc, r), kd, ln2_and_lc4, 0);116117/* y = r2*(A0 + r*A1 + r2*(A2 + r*A3 + r2*A4)) + hi. */118float64x2_t odd_coeffs = vld1q_f64 (&d->lc1);119float64x2_t r2 = vmulq_f64 (r, r);120float64x2_t y = vfmaq_laneq_f64 (d->lc2, r, odd_coeffs, 1);121float64x2_t p = vfmaq_laneq_f64 (d->lc0, r, odd_coeffs, 0);122y = vfmaq_laneq_f64 (y, r2, ln2_and_lc4, 1);123y = vfmaq_f64 (p, r2, y);124return vfmaq_f64 (hi, y, r2);125}126127/* Double-precision implementation of vector asinh(x).128asinh is very sensitive around 1, so it is impractical to devise a single129low-cost algorithm which is sufficiently accurate on a wide range of input.130Instead we use two different algorithms:131asinh(x) = sign(x) * log(|x| + sqrt(x^2 + 1) if |x| >= 1132= sign(x) * (|x| + |x|^3 * P(x^2)) otherwise133where log(x) is an optimized log approximation, and P(x) is a polynomial134shared with the scalar routine. The greatest observed error 2.79 ULP, in135|x| >= 1:136_ZGVnN2v_asinh(0x1.2cd9d73ea76a6p+0) got 0x1.ffffd003219dap-1137want 0x1.ffffd003219ddp-1. */138VPCS_ATTR float64x2_t V_NAME_D1 (asinh) (float64x2_t x)139{140const struct data *d = ptr_barrier (&data);141float64x2_t ax = vabsq_f64 (x);142143uint64x2_t gt1 = vcgeq_f64 (ax, v_f64 (1));144145#if WANT_SIMD_EXCEPT146uint64x2_t iax = vreinterpretq_u64_f64 (ax);147uint64x2_t special = vcgeq_u64 (iax, (d->huge_bound));148uint64x2_t tiny = vcltq_f64 (ax, d->tiny_bound);149special = vorrq_u64 (special, tiny);150#else151uint64x2_t special = vcgeq_f64 (ax, vreinterpretq_f64_u64 (d->huge_bound));152#endif153154/* Option 1: |x| >= 1.155Compute asinh(x) according by asinh(x) = log(x + sqrt(x^2 + 1)).156If WANT_SIMD_EXCEPT is enabled, sidestep special values, which will157overflow, by setting special lanes to 1. These will be fixed later. */158float64x2_t option_1 = v_f64 (0);159if (likely (v_any_u64 (gt1)))160{161#if WANT_SIMD_EXCEPT162float64x2_t xm = v_zerofy_f64 (ax, special);163#else164float64x2_t xm = ax;165#endif166option_1 = log_inline (167vaddq_f64 (xm, vsqrtq_f64 (vfmaq_f64 (v_f64 (1), xm, xm))), d);168}169170/* Option 2: |x| < 1.171Compute asinh(x) using a polynomial.172If WANT_SIMD_EXCEPT is enabled, sidestep special lanes, which will173overflow, and tiny lanes, which will underflow, by setting them to 0. They174will be fixed later, either by selecting x or falling back to the scalar175special-case. The largest observed error in this region is 1.47 ULPs:176_ZGVnN2v_asinh(0x1.fdfcd00cc1e6ap-1) got 0x1.c1d6bf874019bp-1177want 0x1.c1d6bf874019cp-1. */178float64x2_t option_2 = v_f64 (0);179180if (likely (v_any_u64 (vceqzq_u64 (gt1))))181{182183#if WANT_SIMD_EXCEPT184ax = v_zerofy_f64 (ax, vorrq_u64 (tiny, gt1));185#endif186float64x2_t x2 = vmulq_f64 (ax, ax), z2 = vmulq_f64 (x2, x2);187/* Order-17 Pairwise Horner scheme. */188float64x2_t c13 = vld1q_f64 (&d->c1);189float64x2_t c57 = vld1q_f64 (&d->c5);190float64x2_t c911 = vld1q_f64 (&d->c9);191float64x2_t c1315 = vld1q_f64 (&d->c13);192193float64x2_t p01 = vfmaq_laneq_f64 (d->c0, x2, c13, 0);194float64x2_t p23 = vfmaq_laneq_f64 (d->c2, x2, c13, 1);195float64x2_t p45 = vfmaq_laneq_f64 (d->c4, x2, c57, 0);196float64x2_t p67 = vfmaq_laneq_f64 (d->c6, x2, c57, 1);197float64x2_t p89 = vfmaq_laneq_f64 (d->c8, x2, c911, 0);198float64x2_t p1011 = vfmaq_laneq_f64 (d->c10, x2, c911, 1);199float64x2_t p1213 = vfmaq_laneq_f64 (d->c12, x2, c1315, 0);200float64x2_t p1415 = vfmaq_laneq_f64 (d->c14, x2, c1315, 1);201float64x2_t p1617 = vfmaq_f64 (d->c16, x2, d->c17);202203float64x2_t p = vfmaq_f64 (p1415, z2, p1617);204p = vfmaq_f64 (p1213, z2, p);205p = vfmaq_f64 (p1011, z2, p);206p = vfmaq_f64 (p89, z2, p);207208p = vfmaq_f64 (p67, z2, p);209p = vfmaq_f64 (p45, z2, p);210211p = vfmaq_f64 (p23, z2, p);212213p = vfmaq_f64 (p01, z2, p);214option_2 = vfmaq_f64 (ax, p, vmulq_f64 (ax, x2));215#if WANT_SIMD_EXCEPT216option_2 = vbslq_f64 (tiny, x, option_2);217#endif218}219220/* Choose the right option for each lane. */221float64x2_t y = vbslq_f64 (gt1, option_1, option_2);222if (unlikely (v_any_u64 (special)))223{224return special_case (x, y, d->abs_mask, special);225}226/* Copy sign. */227return vbslq_f64 (d->abs_mask, y, x);228}229230TEST_SIG (V, D, 1, asinh, -10.0, 10.0)231TEST_ULP (V_NAME_D1 (asinh), 2.29)232TEST_DISABLE_FENV_IF_NOT (V_NAME_D1 (asinh), WANT_SIMD_EXCEPT)233TEST_SYM_INTERVAL (V_NAME_D1 (asinh), 0, 0x1p-26, 50000)234TEST_SYM_INTERVAL (V_NAME_D1 (asinh), 0x1p-26, 1, 50000)235TEST_SYM_INTERVAL (V_NAME_D1 (asinh), 1, 0x1p511, 50000)236TEST_SYM_INTERVAL (V_NAME_D1 (asinh), 0x1p511, inf, 40000)237/* Test vector asinh 3 times, with control lane < 1, > 1 and special.238Ensures the v_sel is choosing the right option in all cases. */239TEST_CONTROL_VALUE (V_NAME_D1 (asinh), 0.5)240TEST_CONTROL_VALUE (V_NAME_D1 (asinh), 2)241TEST_CONTROL_VALUE (V_NAME_D1 (asinh), 0x1p600)242243244