Path: blob/main/contrib/arm-optimized-routines/math/aarch64/experimental/cbrt_2u.c
48378 views
/*1* Double-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 "math_config.h"8#include "test_sig.h"9#include "test_defs.h"1011TEST_SIG (S, D, 1, cbrt, -10.0, 10.0)1213#define AbsMask 0x7fffffffffffffff14#define TwoThirds 0x1.5555555555555p-11516#define C(i) __cbrt_data.poly[i]17#define T(i) __cbrt_data.table[i]1819/* Approximation for double-precision cbrt(x), using low-order polynomial and20two Newton iterations. Greatest observed error is 1.79 ULP. Errors repeat21according to the exponent, for instance an error observed for double value22m * 2^e will be observed for any input m * 2^(e + 3*i), where i is an23integer.24cbrt(0x1.fffff403f0bc6p+1) got 0x1.965fe72821e9bp+025want 0x1.965fe72821e99p+0. */26double27cbrt (double x)28{29uint64_t ix = asuint64 (x);30uint64_t iax = ix & AbsMask;31uint64_t sign = ix & ~AbsMask;3233if (unlikely (iax == 0 || iax == 0x7ff0000000000000))34return x;3536/* |x| = m * 2^e, where m is in [0.5, 1.0].37We can easily decompose x into m and e using frexp. */38int e;39double m = frexp (asdouble (iax), &e);4041/* Calculate rough approximation for cbrt(m) in [0.5, 1.0], starting point42for Newton iterations. */43double p_01 = fma (C (1), m, C (0));44double p_23 = fma (C (3), m, C (2));45double p = fma (p_23, m * m, p_01);4647/* Two iterations of Newton's method for iteratively approximating cbrt. */48double m_by_3 = m / 3;49double a = fma (TwoThirds, p, m_by_3 / (p * p));50a = fma (TwoThirds, a, m_by_3 / (a * a));5152/* Assemble the result by the following:5354cbrt(x) = cbrt(m) * 2 ^ (e / 3).5556Let t = (2 ^ (e / 3)) / (2 ^ round(e / 3)).5758Then we know t = 2 ^ (i / 3), where i is the remainder from e / 3.59i is an integer in [-2, 2], so t can be looked up in the table T.60Hence the result is assembled as:6162cbrt(x) = cbrt(m) * t * 2 ^ round(e / 3) * sign.63Which can be done easily using ldexp. */64return asdouble (asuint64 (ldexp (a * T (2 + e % 3), e / 3)) | sign);65}6667TEST_ULP (cbrt, 1.30)68TEST_SYM_INTERVAL (cbrt, 0, inf, 1000000)697071