Path: blob/main/contrib/arm-optimized-routines/math/aarch64/experimental/cbrtf_1u5.c
48375 views
/*1* Single-precision 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 "poly_scalar_f32.h"8#include "math_config.h"9#include "test_sig.h"10#include "test_defs.h"1112#define AbsMask 0x7fffffff13#define SignMask 0x8000000014#define TwoThirds 0x1.555556p-1f1516#define T(i) __cbrtf_data.table[i]1718/* Approximation for single-precision cbrt(x), using low-order polynomial and19one Newton iteration on a reduced interval. Greatest error is 1.5 ULP. This20is observed for every value where the mantissa is 0x1.81410e and the21exponent is a multiple of 3, for example:22cbrtf(0x1.81410ep+30) got 0x1.255d96p+1023want 0x1.255d92p+10. */24float25cbrtf (float x)26{27uint32_t ix = asuint (x);28uint32_t iax = ix & AbsMask;29uint32_t sign = ix & SignMask;3031if (unlikely (iax == 0 || iax == 0x7f800000))32return x;3334/* |x| = m * 2^e, where m is in [0.5, 1.0].35We can easily decompose x into m and e using frexpf. */36int e;37float m = frexpf (asfloat (iax), &e);3839/* p is a rough approximation for cbrt(m) in [0.5, 1.0]. The better this is,40the less accurate the next stage of the algorithm needs to be. An order-441polynomial is enough for one Newton iteration. */42float p = pairwise_poly_3_f32 (m, m * m, __cbrtf_data.poly);4344/* One iteration of Newton's method for iteratively approximating cbrt. */45float m_by_3 = m / 3;46float a = fmaf (TwoThirds, p, m_by_3 / (p * p));4748/* Assemble the result by the following:4950cbrt(x) = cbrt(m) * 2 ^ (e / 3).5152Let t = (2 ^ (e / 3)) / (2 ^ round(e / 3)).5354Then we know t = 2 ^ (i / 3), where i is the remainder from e / 3.55i is an integer in [-2, 2], so t can be looked up in the table T.56Hence the result is assembled as:5758cbrt(x) = cbrt(m) * t * 2 ^ round(e / 3) * sign.59Which can be done easily using ldexpf. */60return asfloat (asuint (ldexpf (a * T (2 + e % 3), e / 3)) | sign);61}6263TEST_SIG (S, F, 1, cbrt, -10.0, 10.0)64TEST_ULP (cbrtf, 1.03)65TEST_SYM_INTERVAL (cbrtf, 0, inf, 1000000)666768