Path: blob/main/contrib/arm-optimized-routines/math/aarch64/sve/cbrtf.c
48375 views
/*1* Single-precision SVE cbrt(x) function.2*3* Copyright (c) 2023-2024, 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"10#include "sv_poly_f32.h"1112const static struct data13{14float32_t poly[4];15float32_t table[5];16float32_t one_third, two_thirds;17} data = {18/* Very rough approximation of cbrt(x) in [0.5, 1], generated with FPMinimax.19*/20.poly = { 0x1.c14e96p-2, 0x1.dd2d3p-1, -0x1.08e81ap-1,210x1.2c74c2p-3, },22/* table[i] = 2^((i - 2) / 3). */23.table = { 0x1.428a3p-1, 0x1.965feap-1, 0x1p0, 0x1.428a3p0, 0x1.965feap0 },24.one_third = 0x1.555556p-2f,25.two_thirds = 0x1.555556p-1f,26};2728#define SmallestNormal 0x0080000029#define Thresh 0x7f000000 /* asuint(INFINITY) - SmallestNormal. */30#define MantissaMask 0x007fffff31#define HalfExp 0x3f0000003233static svfloat32_t NOINLINE34special_case (svfloat32_t x, svfloat32_t y, svbool_t special)35{36return sv_call_f32 (cbrtf, x, y, special);37}3839static inline svfloat32_t40shifted_lookup (const svbool_t pg, const float32_t *table, svint32_t i)41{42return svld1_gather_index (pg, table, svadd_x (pg, i, 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_ZGVsMxv_cbrtf (0x1.85a2aap+3) got 0x1.267936p+150want 0x1.267932p+1. */51svfloat32_t SV_NAME_F1 (cbrt) (svfloat32_t x, const svbool_t pg)52{53const struct data *d = ptr_barrier (&data);5455svfloat32_t ax = svabs_x (pg, x);56svuint32_t iax = svreinterpret_u32 (ax);57svuint32_t sign = sveor_x (pg, svreinterpret_u32 (x), iax);5859/* Subnormal, +/-0 and special values. */60svbool_t special = svcmpge (pg, svsub_x (pg, iax, SmallestNormal), Thresh);6162/* Decompose |x| into m * 2^e, where m is in [0.5, 1.0]. This is a vector63version of frexpf, which gets subnormal values wrong - these have to be64special-cased as a result. */65svfloat32_t m = svreinterpret_f32 (svorr_x (66pg, svand_x (pg, svreinterpret_u32 (x), MantissaMask), HalfExp));67svint32_t e = svsub_x (pg, svreinterpret_s32 (svlsr_x (pg, iax, 23)), 126);6869/* p is a rough approximation for cbrt(m) in [0.5, 1.0]. The better this is,70the less accurate the next stage of the algorithm needs to be. An order-471polynomial is enough for one Newton iteration. */72svfloat32_t p73= sv_pairwise_poly_3_f32_x (pg, m, svmul_x (pg, m, m), d->poly);7475/* One iteration of Newton's method for iteratively approximating cbrt. */76svfloat32_t m_by_3 = svmul_x (pg, m, d->one_third);77svfloat32_t a = svmla_x (pg, svdiv_x (pg, m_by_3, svmul_x (pg, p, p)), p,78d->two_thirds);7980/* Assemble the result by the following:8182cbrt(x) = cbrt(m) * 2 ^ (e / 3).8384We can get 2 ^ round(e / 3) using ldexp and integer divide, but since e is85not necessarily a multiple of 3 we lose some information.8687Let q = 2 ^ round(e / 3), then t = 2 ^ (e / 3) / q.8889Then we know t = 2 ^ (i / 3), where i is the remainder from e / 3, which90is an integer in [-2, 2], and can be looked up in the table T. Hence the91result is assembled as:9293cbrt(x) = cbrt(m) * t * 2 ^ round(e / 3) * sign. */94svfloat32_t ef = svmul_x (pg, svcvt_f32_x (pg, e), d->one_third);95svint32_t ey = svcvt_s32_x (pg, ef);96svint32_t em3 = svmls_x (pg, e, ey, 3);9798svfloat32_t my = shifted_lookup (pg, d->table, em3);99my = svmul_x (pg, my, a);100101/* Vector version of ldexpf. */102svfloat32_t y = svscale_x (pg, my, ey);103104if (unlikely (svptest_any (pg, special)))105return special_case (106x, svreinterpret_f32 (svorr_x (pg, svreinterpret_u32 (y), sign)),107special);108109/* Copy sign. */110return svreinterpret_f32 (svorr_x (pg, svreinterpret_u32 (y), sign));111}112113TEST_SIG (SV, F, 1, cbrt, -10.0, 10.0)114TEST_ULP (SV_NAME_F1 (cbrt), 1.15)115TEST_DISABLE_FENV (SV_NAME_F1 (cbrt))116TEST_SYM_INTERVAL (SV_NAME_F1 (cbrt), 0, inf, 1000000)117CLOSE_SVE_ATTR118119120