Path: blob/main/contrib/arm-optimized-routines/math/aarch64/experimental/log10_2u.c
48378 views
/*1* Double-precision log10(x) function.2*3* Copyright (c) 2020-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"1011/* Polynomial coefficients and lookup tables. */12#define T __log10_data.tab13#define T2 __log10_data.tab214#define B __log10_data.poly115#define A __log10_data.poly16#define Ln2hi __log10_data.ln2hi17#define Ln2lo __log10_data.ln2lo18#define InvLn10 __log10_data.invln1019#define N (1 << LOG10_TABLE_BITS)20#define OFF 0x3fe600000000000021#define LO asuint64 (1.0 - 0x1p-4)22#define HI asuint64 (1.0 + 0x1.09p-4)2324/* Top 16 bits of a double. */25static inline uint32_t26top16 (double x)27{28return asuint64 (x) >> 48;29}3031/* Fast and low accuracy implementation of log10.32The implementation is similar to that of math/log, except that:33- Polynomials are computed for log10(1+r) with r on same intervals as log.34- Lookup parameters are scaled (at runtime) to switch from base e to35base 10. Many errors above 1.59 ulp are observed across the whole range of36doubles. The greatest observed error is 1.61 ulp, at around 0.965:37log10(0x1.dc8710333a29bp-1) got -0x1.fee26884905a6p-638want -0x1.fee26884905a8p-6. */39double40log10 (double x)41{42/* double_t for better performance on targets with FLT_EVAL_METHOD==2. */43double_t w, z, r, r2, r3, y, invc, logc, kd, hi, lo;44uint64_t ix, iz, tmp;45uint32_t top;46int k, i;4748ix = asuint64 (x);49top = top16 (x);5051if (unlikely (ix - LO < HI - LO))52{53/* Handle close to 1.0 inputs separately. */54/* Fix sign of zero with downward rounding when x==1. */55if (WANT_ROUNDING && unlikely (ix == asuint64 (1.0)))56return 0;57r = x - 1.0;58r2 = r * r;59r3 = r * r2;60y = r361* (B[1] + r * B[2] + r2 * B[3]62+ r363* (B[4] + r * B[5] + r2 * B[6]64+ r3 * (B[7] + r * B[8] + r2 * B[9] + r3 * B[10])));65/* Worst-case error is around 0.507 ULP. */66w = r * 0x1p27;67double_t rhi = r + w - w;68double_t rlo = r - rhi;69w = rhi * rhi * B[0];70hi = r + w;71lo = r - hi + w;72lo += B[0] * rlo * (rhi + r);73y += lo;74y += hi;75/* Scale by 1/ln(10). Polynomial already contains scaling. */76y = y * InvLn10;7778return eval_as_double (y);79}80if (unlikely (top - 0x0010 >= 0x7ff0 - 0x0010))81{82/* x < 0x1p-1022 or inf or nan. */83if (ix * 2 == 0)84return __math_divzero (1);85if (ix == asuint64 (INFINITY)) /* log10(inf) == inf. */86return x;87if ((top & 0x8000) || (top & 0x7ff0) == 0x7ff0)88return __math_invalid (x);89/* x is subnormal, normalize it. */90ix = asuint64 (x * 0x1p52);91ix -= 52ULL << 52;92}9394/* x = 2^k z; where z is in range [OFF,2*OFF) and exact.95The range is split into N subintervals.96The ith subinterval contains z and c is near its center. */97tmp = ix - OFF;98i = (tmp >> (52 - LOG10_TABLE_BITS)) % N;99k = (int64_t) tmp >> 52; /* arithmetic shift. */100iz = ix - (tmp & 0xfffULL << 52);101invc = T[i].invc;102logc = T[i].logc;103z = asdouble (iz);104105/* log(x) = log1p(z/c-1) + log(c) + k*Ln2. */106/* r ~= z/c - 1, |r| < 1/(2*N). */107#if HAVE_FAST_FMA108/* rounding error: 0x1p-55/N. */109r = fma (z, invc, -1.0);110#else111/* rounding error: 0x1p-55/N + 0x1p-66. */112r = (z - T2[i].chi - T2[i].clo) * invc;113#endif114kd = (double_t) k;115116/* w = log(c) + k*Ln2hi. */117w = kd * Ln2hi + logc;118hi = w + r;119lo = w - hi + r + kd * Ln2lo;120121/* log10(x) = (w + r)/log(10) + (log10(1+r) - r/log(10)). */122r2 = r * r; /* rounding error: 0x1p-54/N^2. */123124/* Scale by 1/ln(10). Polynomial already contains scaling. */125y = lo + r2 * A[0] + r * r2 * (A[1] + r * A[2] + r2 * (A[3] + r * A[4]))126+ hi;127y = y * InvLn10;128129return eval_as_double (y);130}131132// clang-format off133#if USE_GLIBC_ABI134strong_alias (log10, __log10_finite)135hidden_alias (log10, __ieee754_log10)136#if LDBL_MANT_DIG == 53137long double138log10l (long double x)139{140return log10 (x);141}142#endif143#endif144// clang-format on145146TEST_SIG (S, D, 1, log10, 0.01, 11.1)147TEST_ULP (log10, 1.11)148TEST_INTERVAL (log10, 0, 0xffff000000000000, 10000)149TEST_INTERVAL (log10, 0x1p-4, 0x1p4, 40000)150TEST_INTERVAL (log10, 0, inf, 40000)151152153