Path: blob/main/contrib/arm-optimized-routines/math/aarch64/advsimd/exp10f.c
48375 views
/*1* Single-precision vector 10^x function.2*3* Copyright (c) 2023-2024, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#define _GNU_SOURCE8#include "v_math.h"9#include "test_sig.h"10#include "test_defs.h"11#include "v_poly_f32.h"1213#define ScaleBound 192.0f1415static const struct data16{17float32x4_t c0, c1, c3;18float log10_2_high, log10_2_low, c2, c4;19float32x4_t inv_log10_2, special_bound;20uint32x4_t exponent_bias, special_offset, special_bias;21#if !WANT_SIMD_EXCEPT22float32x4_t scale_thresh;23#endif24} data = {25/* Coefficients generated using Remez algorithm with minimisation of relative26error.27rel error: 0x1.89dafa3p-2428abs error: 0x1.167d55p-23 in [-log10(2)/2, log10(2)/2]29maxerr: 1.85943 +0.5 ulp. */30.c0 = V4 (0x1.26bb16p+1f),31.c1 = V4 (0x1.5350d2p+1f),32.c2 = 0x1.04744ap+1f,33.c3 = V4 (0x1.2d8176p+0f),34.c4 = 0x1.12b41ap-1f,35.inv_log10_2 = V4 (0x1.a934fp+1),36.log10_2_high = 0x1.344136p-2,37.log10_2_low = 0x1.ec10cp-27,38/* rint (log2 (2^127 / (1 + sqrt (2)))). */39.special_bound = V4 (126.0f),40.exponent_bias = V4 (0x3f800000),41.special_offset = V4 (0x82000000),42.special_bias = V4 (0x7f000000),43#if !WANT_SIMD_EXCEPT44.scale_thresh = V4 (ScaleBound)45#endif46};4748#if WANT_SIMD_EXCEPT4950# define SpecialBound 38.0f /* rint(log10(2^127)). */51# define TinyBound v_u32 (0x20000000) /* asuint (0x1p-63). */52# define BigBound v_u32 (0x42180000) /* asuint (SpecialBound). */53# define Thres v_u32 (0x22180000) /* BigBound - TinyBound. */5455static float32x4_t VPCS_ATTR NOINLINE56special_case (float32x4_t x, float32x4_t y, uint32x4_t cmp)57{58/* If fenv exceptions are to be triggered correctly, fall back to the scalar59routine to special lanes. */60return v_call_f32 (exp10f, x, y, cmp);61}6263#else6465# define SpecialBound 126.0f6667static float32x4_t VPCS_ATTR NOINLINE68special_case (float32x4_t poly, float32x4_t n, uint32x4_t e, uint32x4_t cmp1,69float32x4_t scale, const struct data *d)70{71/* 2^n may overflow, break it up into s1*s2. */72uint32x4_t b = vandq_u32 (vclezq_f32 (n), d->special_offset);73float32x4_t s1 = vreinterpretq_f32_u32 (vaddq_u32 (b, d->special_bias));74float32x4_t s2 = vreinterpretq_f32_u32 (vsubq_u32 (e, b));75uint32x4_t cmp2 = vcagtq_f32 (n, d->scale_thresh);76float32x4_t r2 = vmulq_f32 (s1, s1);77float32x4_t r1 = vmulq_f32 (vfmaq_f32 (s2, poly, s2), s1);78/* Similar to r1 but avoids double rounding in the subnormal range. */79float32x4_t r0 = vfmaq_f32 (scale, poly, scale);80float32x4_t r = vbslq_f32 (cmp1, r1, r0);81return vbslq_f32 (cmp2, r2, r);82}8384#endif8586/* Fast vector implementation of single-precision exp10.87Algorithm is accurate to 2.36 ULP.88_ZGVnN4v_exp10f(0x1.be2b36p+1) got 0x1.7e79c4p+1189want 0x1.7e79cp+11. */90float32x4_t VPCS_ATTR NOINLINE V_NAME_F1 (exp10) (float32x4_t x)91{92const struct data *d = ptr_barrier (&data);93#if WANT_SIMD_EXCEPT94/* asuint(x) - TinyBound >= BigBound - TinyBound. */95uint32x4_t cmp = vcgeq_u32 (96vsubq_u32 (vreinterpretq_u32_f32 (vabsq_f32 (x)), TinyBound), Thres);97float32x4_t xm = x;98/* If any lanes are special, mask them with 1 and retain a copy of x to allow99special case handler to fix special lanes later. This is only necessary if100fenv exceptions are to be triggered correctly. */101if (unlikely (v_any_u32 (cmp)))102x = v_zerofy_f32 (x, cmp);103#endif104105/* exp10(x) = 2^n * 10^r = 2^n * (1 + poly (r)),106with poly(r) in [1/sqrt(2), sqrt(2)] and107x = r + n * log10 (2), with r in [-log10(2)/2, log10(2)/2]. */108float32x4_t log10_2_c24 = vld1q_f32 (&d->log10_2_high);109float32x4_t n = vrndaq_f32 (vmulq_f32 (x, d->inv_log10_2));110float32x4_t r = vfmsq_laneq_f32 (x, n, log10_2_c24, 0);111r = vfmaq_laneq_f32 (r, n, log10_2_c24, 1);112uint32x4_t e = vshlq_n_u32 (vreinterpretq_u32_s32 (vcvtaq_s32_f32 (n)), 23);113114float32x4_t scale = vreinterpretq_f32_u32 (vaddq_u32 (e, d->exponent_bias));115116#if !WANT_SIMD_EXCEPT117uint32x4_t cmp = vcagtq_f32 (n, d->special_bound);118#endif119120float32x4_t r2 = vmulq_f32 (r, r);121float32x4_t p12 = vfmaq_laneq_f32 (d->c1, r, log10_2_c24, 2);122float32x4_t p34 = vfmaq_laneq_f32 (d->c3, r, log10_2_c24, 3);123float32x4_t p14 = vfmaq_f32 (p12, r2, p34);124float32x4_t poly = vfmaq_f32 (vmulq_f32 (r, d->c0), p14, r2);125126if (unlikely (v_any_u32 (cmp)))127#if WANT_SIMD_EXCEPT128return special_case (xm, vfmaq_f32 (scale, poly, scale), cmp);129#else130return special_case (poly, n, e, cmp, scale, d);131#endif132133return vfmaq_f32 (scale, poly, scale);134}135136HALF_WIDTH_ALIAS_F1 (exp10)137138#if WANT_EXP10_TESTS139TEST_SIG (S, F, 1, exp10, -9.9, 9.9)140TEST_SIG (V, F, 1, exp10, -9.9, 9.9)141TEST_ULP (V_NAME_F1 (exp10), 1.86)142TEST_DISABLE_FENV_IF_NOT (V_NAME_F1 (exp10), WANT_SIMD_EXCEPT)143TEST_SYM_INTERVAL (V_NAME_F1 (exp10), 0, SpecialBound, 5000)144TEST_SYM_INTERVAL (V_NAME_F1 (exp10), SpecialBound, ScaleBound, 5000)145TEST_SYM_INTERVAL (V_NAME_F1 (exp10), ScaleBound, inf, 10000)146#endif147148149