Path: blob/main/contrib/arm-optimized-routines/math/aarch64/sve/exp10.c
48375 views
/*1* Double-precision SVE 10^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"1011#define SpecialBound 307.0 /* floor (log10 (2^1023)). */1213static const struct data14{15double c1, c3, c2, c4, c0;16double shift, log10_2, log2_10_hi, log2_10_lo, scale_thres, special_bound;17} data = {18/* Coefficients generated using Remez algorithm.19rel error: 0x1.9fcb9b3p-6020abs error: 0x1.a20d9598p-60 in [ -log10(2)/128, log10(2)/128 ]21max ulp err 0.52 +0.5. */22.c0 = 0x1.26bb1bbb55516p1,23.c1 = 0x1.53524c73cd32ap1,24.c2 = 0x1.0470591daeafbp1,25.c3 = 0x1.2bd77b1361ef6p0,26.c4 = 0x1.142b5d54e9621p-1,27/* 1.5*2^46+1023. This value is further explained below. */28.shift = 0x1.800000000ffc0p+46,29.log10_2 = 0x1.a934f0979a371p1, /* 1/log2(10). */30.log2_10_hi = 0x1.34413509f79ffp-2, /* log2(10). */31.log2_10_lo = -0x1.9dc1da994fd21p-59,32.scale_thres = 1280.0,33.special_bound = SpecialBound,34};3536#define SpecialOffset 0x6000000000000000 /* 0x1p513. */37/* SpecialBias1 + SpecialBias1 = asuint(1.0). */38#define SpecialBias1 0x7000000000000000 /* 0x1p769. */39#define SpecialBias2 0x3010000000000000 /* 0x1p-254. */4041/* Update of both special and non-special cases, if any special case is42detected. */43static inline svfloat64_t44special_case (svbool_t pg, svfloat64_t s, svfloat64_t y, svfloat64_t n,45const struct data *d)46{47/* s=2^n may overflow, break it up into s=s1*s2,48such that exp = s + s*y can be computed as s1*(s2+s2*y)49and s1*s1 overflows only if n>0. */5051/* If n<=0 then set b to 0x6, 0 otherwise. */52svbool_t p_sign = svcmple (pg, n, 0.0); /* n <= 0. */53svuint64_t b = svdup_u64_z (p_sign, SpecialOffset);5455/* Set s1 to generate overflow depending on sign of exponent n. */56svfloat64_t s1 = svreinterpret_f64 (svsubr_x (pg, b, SpecialBias1));57/* Offset s to avoid overflow in final result if n is below threshold. */58svfloat64_t s2 = svreinterpret_f64 (59svadd_x (pg, svsub_x (pg, svreinterpret_u64 (s), SpecialBias2), b));6061/* |n| > 1280 => 2^(n) overflows. */62svbool_t p_cmp = svacgt (pg, n, d->scale_thres);6364svfloat64_t r1 = svmul_x (svptrue_b64 (), s1, s1);65svfloat64_t r2 = svmla_x (pg, s2, s2, y);66svfloat64_t r0 = svmul_x (svptrue_b64 (), r2, s1);6768return svsel (p_cmp, r1, r0);69}7071/* Fast vector implementation of exp10 using FEXPA instruction.72Maximum measured error is 1.02 ulp.73SV_NAME_D1 (exp10)(-0x1.2862fec805e58p+2) got 0x1.885a89551d782p-1674want 0x1.885a89551d781p-16. */75svfloat64_t SV_NAME_D1 (exp10) (svfloat64_t x, svbool_t pg)76{77const struct data *d = ptr_barrier (&data);78svbool_t no_big_scale = svacle (pg, x, d->special_bound);79svbool_t special = svnot_z (pg, no_big_scale);8081/* n = round(x/(log10(2)/N)). */82svfloat64_t shift = sv_f64 (d->shift);83svfloat64_t z = svmla_x (pg, shift, x, d->log10_2);84svfloat64_t n = svsub_x (pg, z, shift);8586/* r = x - n*log10(2)/N. */87svfloat64_t log2_10 = svld1rq (svptrue_b64 (), &d->log2_10_hi);88svfloat64_t r = x;89r = svmls_lane (r, n, log2_10, 0);90r = svmls_lane (r, n, log2_10, 1);9192/* scale = 2^(n/N), computed using FEXPA. FEXPA does not propagate NaNs, so93for consistent NaN handling we have to manually propagate them. This94comes at significant performance cost. */95svuint64_t u = svreinterpret_u64 (z);96svfloat64_t scale = svexpa (u);97svfloat64_t c24 = svld1rq (svptrue_b64 (), &d->c2);98/* Approximate exp10(r) using polynomial. */99svfloat64_t r2 = svmul_x (svptrue_b64 (), r, r);100svfloat64_t p12 = svmla_lane (sv_f64 (d->c1), r, c24, 0);101svfloat64_t p34 = svmla_lane (sv_f64 (d->c3), r, c24, 1);102svfloat64_t p14 = svmla_x (pg, p12, p34, r2);103104svfloat64_t y = svmla_x (pg, svmul_x (svptrue_b64 (), r, d->c0), r2, p14);105106/* Assemble result as exp10(x) = 2^n * exp10(r). If |x| > SpecialBound107multiplication may overflow, so use special case routine. */108if (unlikely (svptest_any (pg, special)))109{110/* FEXPA zeroes the sign bit, however the sign is meaningful to the111special case function so needs to be copied.112e = sign bit of u << 46. */113svuint64_t e = svand_x (pg, svlsl_x (pg, u, 46), 0x8000000000000000);114/* Copy sign to scale. */115scale = svreinterpret_f64 (svadd_x (pg, e, svreinterpret_u64 (scale)));116return special_case (pg, scale, y, n, d);117}118119/* No special case. */120return svmla_x (pg, scale, scale, y);121}122123#if WANT_EXP10_TESTS124TEST_SIG (SV, D, 1, exp10, -9.9, 9.9)125TEST_ULP (SV_NAME_D1 (exp10), 0.52)126TEST_DISABLE_FENV (SV_NAME_D1 (exp10))127TEST_SYM_INTERVAL (SV_NAME_D1 (exp10), 0, SpecialBound, 10000)128TEST_SYM_INTERVAL (SV_NAME_D1 (exp10), SpecialBound, inf, 1000)129#endif130CLOSE_SVE_ATTR131132133