Path: blob/main/contrib/arm-optimized-routines/math/aarch64/advsimd/log10.c
48375 views
/*1* Double-precision vector log10(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{13uint64x2_t off, sign_exp_mask, offset_lower_bound;14uint32x4_t special_bound;15double invln10, log10_2;16double c1, c3;17float64x2_t c0, c2, c4;18} data = {19/* Computed from log coefficients divided by log(10) then rounded to double20precision. */21.c0 = V2 (-0x1.bcb7b1526e506p-3),22.c1 = 0x1.287a7636be1d1p-3,23.c2 = V2 (-0x1.bcb7b158af938p-4),24.c3 = 0x1.63c78734e6d07p-4,25.c4 = V2 (-0x1.287461742fee4p-4),26.invln10 = 0x1.bcb7b1526e50ep-2,27.log10_2 = 0x1.34413509f79ffp-2,28.off = V2 (0x3fe6900900000000),29.sign_exp_mask = V2 (0xfff0000000000000),30/* Lower bound is 0x0010000000000000. For31optimised register use subnormals are detected after offset has been32subtracted, so lower bound - offset (which wraps around). */33.offset_lower_bound = V2 (0x0010000000000000 - 0x3fe6900900000000),34.special_bound = V4 (0x7fe00000), /* asuint64(inf) - 0x0010000000000000. */35};3637#define N (1 << V_LOG10_TABLE_BITS)38#define IndexMask (N - 1)3940struct entry41{42float64x2_t invc;43float64x2_t log10c;44};4546static inline struct entry47lookup (uint64x2_t i)48{49struct entry e;50uint64_t i051= (vgetq_lane_u64 (i, 0) >> (52 - V_LOG10_TABLE_BITS)) & IndexMask;52uint64_t i153= (vgetq_lane_u64 (i, 1) >> (52 - V_LOG10_TABLE_BITS)) & IndexMask;54float64x2_t e0 = vld1q_f64 (&__v_log10_data.table[i0].invc);55float64x2_t e1 = vld1q_f64 (&__v_log10_data.table[i1].invc);56e.invc = vuzp1q_f64 (e0, e1);57e.log10c = vuzp2q_f64 (e0, e1);58return e;59}6061static float64x2_t VPCS_ATTR NOINLINE62special_case (float64x2_t hi, uint64x2_t u_off, float64x2_t y, float64x2_t r2,63uint32x2_t special, const struct data *d)64{65float64x2_t x = vreinterpretq_f64_u64 (vaddq_u64 (u_off, d->off));66return v_call_f64 (log10, x, vfmaq_f64 (hi, y, r2), vmovl_u32 (special));67}6869/* Fast implementation of double-precision vector log1070is a slight modification of double-precision vector log.71Max ULP error: < 2.5 ulp (nearest rounding.)72Maximum measured at 2.46 ulp for x in [0.96, 0.97]73_ZGVnN2v_log10(0x1.13192407fcb46p+0) got 0x1.fff6be3cae4bbp-674want 0x1.fff6be3cae4b9p-6. */75float64x2_t VPCS_ATTR V_NAME_D1 (log10) (float64x2_t x)76{77const struct data *d = ptr_barrier (&data);7879/* To avoid having to mov x out of the way, keep u after offset has been80applied, and recover x by adding the offset back in the special-case81handler. */82uint64x2_t u = vreinterpretq_u64_f64 (x);83uint64x2_t u_off = vsubq_u64 (u, d->off);8485/* x = 2^k z; where z is in range [OFF,2*OFF) and exact.86The range is split into N subintervals.87The ith subinterval contains z and c is near its center. */88int64x2_t k = vshrq_n_s64 (vreinterpretq_s64_u64 (u_off), 52);89uint64x2_t iz = vsubq_u64 (u, vandq_u64 (u_off, d->sign_exp_mask));90float64x2_t z = vreinterpretq_f64_u64 (iz);9192struct entry e = lookup (u_off);9394uint32x2_t special = vcge_u32 (vsubhn_u64 (u_off, d->offset_lower_bound),95vget_low_u32 (d->special_bound));9697/* log10(x) = log1p(z/c-1)/log(10) + log10(c) + k*log10(2). */98float64x2_t r = vfmaq_f64 (v_f64 (-1.0), z, e.invc);99float64x2_t kd = vcvtq_f64_s64 (k);100101/* hi = r / log(10) + log10(c) + k*log10(2).102Constants in v_log10_data.c are computed (in extended precision) as103e.log10c := e.logc * invln10. */104float64x2_t cte = vld1q_f64 (&d->invln10);105float64x2_t hi = vfmaq_laneq_f64 (e.log10c, r, cte, 0);106107/* y = log10(1+r) + n * log10(2). */108hi = vfmaq_laneq_f64 (hi, kd, cte, 1);109110/* y = r2*(A0 + r*A1 + r2*(A2 + r*A3 + r2*A4)) + hi. */111float64x2_t r2 = vmulq_f64 (r, r);112float64x2_t odd_coeffs = vld1q_f64 (&d->c1);113float64x2_t y = vfmaq_laneq_f64 (d->c2, r, odd_coeffs, 1);114float64x2_t p = vfmaq_laneq_f64 (d->c0, r, odd_coeffs, 0);115y = vfmaq_f64 (y, d->c4, r2);116y = vfmaq_f64 (p, y, r2);117118if (unlikely (v_any_u32h (special)))119return special_case (hi, u_off, y, r2, special, d);120return vfmaq_f64 (hi, y, r2);121}122123TEST_SIG (V, D, 1, log10, 0.01, 11.1)124TEST_ULP (V_NAME_D1 (log10), 1.97)125TEST_INTERVAL (V_NAME_D1 (log10), -0.0, -inf, 1000)126TEST_INTERVAL (V_NAME_D1 (log10), 0, 0x1p-149, 1000)127TEST_INTERVAL (V_NAME_D1 (log10), 0x1p-149, 0x1p-126, 4000)128TEST_INTERVAL (V_NAME_D1 (log10), 0x1p-126, 0x1p-23, 50000)129TEST_INTERVAL (V_NAME_D1 (log10), 0x1p-23, 1.0, 50000)130TEST_INTERVAL (V_NAME_D1 (log10), 1.0, 100, 50000)131TEST_INTERVAL (V_NAME_D1 (log10), 100, inf, 50000)132133134