Path: blob/main/contrib/arm-optimized-routines/math/aarch64/advsimd/cbrtf.c
48375 views
/*1* Single-precision vector cbrt(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"10#include "v_poly_f32.h"1112const static struct data13{14float32x4_t poly[4], one_third;15float table[5];16} data = {17.poly = { /* Very rough approximation of cbrt(x) in [0.5, 1], generated with18FPMinimax. */19V4 (0x1.c14e96p-2), V4 (0x1.dd2d3p-1), V4 (-0x1.08e81ap-1),20V4 (0x1.2c74c2p-3) },21.table = { /* table[i] = 2^((i - 2) / 3). */220x1.428a3p-1, 0x1.965feap-1, 0x1p0, 0x1.428a3p0, 0x1.965feap0 },23.one_third = V4 (0x1.555556p-2f),24};2526#define SignMask v_u32 (0x80000000)27#define SmallestNormal v_u32 (0x00800000)28#define Thresh vdup_n_u16 (0x7f00) /* asuint(INFINITY) - SmallestNormal. */29#define MantissaMask v_u32 (0x007fffff)30#define HalfExp v_u32 (0x3f000000)3132static float32x4_t VPCS_ATTR NOINLINE33special_case (float32x4_t x, float32x4_t y, uint16x4_t special)34{35return v_call_f32 (cbrtf, x, y, vmovl_u16 (special));36}3738static inline float32x4_t39shifted_lookup (const float *table, int32x4_t i)40{41return (float32x4_t){ table[i[0] + 2], table[i[1] + 2], table[i[2] + 2],42table[i[3] + 2] };43}4445/* Approximation for vector single-precision cbrt(x) using Newton iteration46with initial guess obtained by a low-order polynomial. Greatest error47is 1.64 ULP. This is observed for every value where the mantissa is480x1.85a2aa and the exponent is a multiple of 3, for example:49_ZGVnN4v_cbrtf(0x1.85a2aap+3) got 0x1.267936p+150want 0x1.267932p+1. */51float32x4_t VPCS_ATTR NOINLINE V_NAME_F1 (cbrt) (float32x4_t x)52{53const struct data *d = ptr_barrier (&data);54uint32x4_t iax = vreinterpretq_u32_f32 (vabsq_f32 (x));5556/* Subnormal, +/-0 and special values. */57uint16x4_t special = vcge_u16 (vsubhn_u32 (iax, SmallestNormal), Thresh);5859/* Decompose |x| into m * 2^e, where m is in [0.5, 1.0]. This is a vector60version of frexpf, which gets subnormal values wrong - these have to be61special-cased as a result. */62float32x4_t m = vbslq_f32 (MantissaMask, x, v_f32 (0.5));63int32x4_t e64= vsubq_s32 (vreinterpretq_s32_u32 (vshrq_n_u32 (iax, 23)), v_s32 (126));6566/* p is a rough approximation for cbrt(m) in [0.5, 1.0]. The better this is,67the less accurate the next stage of the algorithm needs to be. An order-468polynomial is enough for one Newton iteration. */69float32x4_t p = v_pairwise_poly_3_f32 (m, vmulq_f32 (m, m), d->poly);7071float32x4_t one_third = d->one_third;72float32x4_t two_thirds = vaddq_f32 (one_third, one_third);7374/* One iteration of Newton's method for iteratively approximating cbrt. */75float32x4_t m_by_3 = vmulq_f32 (m, one_third);76float32x4_t a77= vfmaq_f32 (vdivq_f32 (m_by_3, vmulq_f32 (p, p)), two_thirds, p);7879/* Assemble the result by the following:8081cbrt(x) = cbrt(m) * 2 ^ (e / 3).8283We can get 2 ^ round(e / 3) using ldexp and integer divide, but since e is84not necessarily a multiple of 3 we lose some information.8586Let q = 2 ^ round(e / 3), then t = 2 ^ (e / 3) / q.8788Then we know t = 2 ^ (i / 3), where i is the remainder from e / 3, which89is an integer in [-2, 2], and can be looked up in the table T. Hence the90result is assembled as:9192cbrt(x) = cbrt(m) * t * 2 ^ round(e / 3) * sign. */93float32x4_t ef = vmulq_f32 (vcvtq_f32_s32 (e), one_third);94int32x4_t ey = vcvtq_s32_f32 (ef);95int32x4_t em3 = vsubq_s32 (e, vmulq_s32 (ey, v_s32 (3)));9697float32x4_t my = shifted_lookup (d->table, em3);98my = vmulq_f32 (my, a);99100/* Vector version of ldexpf. */101float32x4_t y102= vreinterpretq_f32_s32 (vshlq_n_s32 (vaddq_s32 (ey, v_s32 (127)), 23));103y = vmulq_f32 (y, my);104105if (unlikely (v_any_u16h (special)))106return special_case (x, vbslq_f32 (SignMask, x, y), special);107108/* Copy sign. */109return vbslq_f32 (SignMask, x, y);110}111112HALF_WIDTH_ALIAS_F1 (cbrt)113114TEST_SIG (V, F, 1, cbrt, -10.0, 10.0)115TEST_ULP (V_NAME_F1 (cbrt), 1.15)116TEST_SYM_INTERVAL (V_NAME_F1 (cbrt), 0, inf, 1000000)117118119