131914882SAlex Richardson /*
231914882SAlex Richardson * Double-precision log2(x) function.
331914882SAlex Richardson *
4*f3087befSAndrew Turner * Copyright (c) 2018-2024, Arm Limited.
5072a4ba8SAndrew Turner * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
631914882SAlex Richardson */
731914882SAlex Richardson
831914882SAlex Richardson #include <float.h>
931914882SAlex Richardson #include <math.h>
1031914882SAlex Richardson #include <stdint.h>
1131914882SAlex Richardson #include "math_config.h"
12*f3087befSAndrew Turner #include "test_defs.h"
13*f3087befSAndrew Turner #include "test_sig.h"
1431914882SAlex Richardson
1531914882SAlex Richardson #define T __log2_data.tab
1631914882SAlex Richardson #define T2 __log2_data.tab2
1731914882SAlex Richardson #define B __log2_data.poly1
1831914882SAlex Richardson #define A __log2_data.poly
1931914882SAlex Richardson #define InvLn2hi __log2_data.invln2hi
2031914882SAlex Richardson #define InvLn2lo __log2_data.invln2lo
2131914882SAlex Richardson #define N (1 << LOG2_TABLE_BITS)
2231914882SAlex Richardson #define OFF 0x3fe6000000000000
2331914882SAlex Richardson
2431914882SAlex Richardson /* Top 16 bits of a double. */
2531914882SAlex Richardson static inline uint32_t
top16(double x)2631914882SAlex Richardson top16 (double x)
2731914882SAlex Richardson {
2831914882SAlex Richardson return asuint64 (x) >> 48;
2931914882SAlex Richardson }
3031914882SAlex Richardson
3131914882SAlex Richardson double
log2(double x)3231914882SAlex Richardson log2 (double x)
3331914882SAlex Richardson {
3431914882SAlex Richardson /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
3531914882SAlex Richardson double_t z, r, r2, r4, y, invc, logc, kd, hi, lo, t1, t2, t3, p;
3631914882SAlex Richardson uint64_t ix, iz, tmp;
3731914882SAlex Richardson uint32_t top;
3831914882SAlex Richardson int k, i;
3931914882SAlex Richardson
4031914882SAlex Richardson ix = asuint64 (x);
4131914882SAlex Richardson top = top16 (x);
4231914882SAlex Richardson
4331914882SAlex Richardson #if LOG2_POLY1_ORDER == 11
4431914882SAlex Richardson # define LO asuint64 (1.0 - 0x1.5b51p-5)
4531914882SAlex Richardson # define HI asuint64 (1.0 + 0x1.6ab2p-5)
4631914882SAlex Richardson #endif
4731914882SAlex Richardson if (unlikely (ix - LO < HI - LO))
4831914882SAlex Richardson {
4931914882SAlex Richardson /* Handle close to 1.0 inputs separately. */
5031914882SAlex Richardson /* Fix sign of zero with downward rounding when x==1. */
5131914882SAlex Richardson if (WANT_ROUNDING && unlikely (ix == asuint64 (1.0)))
5231914882SAlex Richardson return 0;
5331914882SAlex Richardson r = x - 1.0;
5431914882SAlex Richardson #if HAVE_FAST_FMA
5531914882SAlex Richardson hi = r * InvLn2hi;
5631914882SAlex Richardson lo = r * InvLn2lo + fma (r, InvLn2hi, -hi);
5731914882SAlex Richardson #else
5831914882SAlex Richardson double_t rhi, rlo;
5931914882SAlex Richardson rhi = asdouble (asuint64 (r) & -1ULL << 32);
6031914882SAlex Richardson rlo = r - rhi;
6131914882SAlex Richardson hi = rhi * InvLn2hi;
6231914882SAlex Richardson lo = rlo * InvLn2hi + r * InvLn2lo;
6331914882SAlex Richardson #endif
6431914882SAlex Richardson r2 = r * r; /* rounding error: 0x1p-62. */
6531914882SAlex Richardson r4 = r2 * r2;
6631914882SAlex Richardson #if LOG2_POLY1_ORDER == 11
6731914882SAlex Richardson /* Worst-case error is less than 0.54 ULP (0.55 ULP without fma). */
6831914882SAlex Richardson p = r2 * (B[0] + r * B[1]);
6931914882SAlex Richardson y = hi + p;
7031914882SAlex Richardson lo += hi - y + p;
7131914882SAlex Richardson lo += r4 * (B[2] + r * B[3] + r2 * (B[4] + r * B[5])
7231914882SAlex Richardson + r4 * (B[6] + r * B[7] + r2 * (B[8] + r * B[9])));
7331914882SAlex Richardson y += lo;
7431914882SAlex Richardson #endif
7531914882SAlex Richardson return eval_as_double (y);
7631914882SAlex Richardson }
7731914882SAlex Richardson if (unlikely (top - 0x0010 >= 0x7ff0 - 0x0010))
7831914882SAlex Richardson {
7931914882SAlex Richardson /* x < 0x1p-1022 or inf or nan. */
8031914882SAlex Richardson if (ix * 2 == 0)
8131914882SAlex Richardson return __math_divzero (1);
8231914882SAlex Richardson if (ix == asuint64 (INFINITY)) /* log(inf) == inf. */
8331914882SAlex Richardson return x;
8431914882SAlex Richardson if ((top & 0x8000) || (top & 0x7ff0) == 0x7ff0)
8531914882SAlex Richardson return __math_invalid (x);
8631914882SAlex Richardson /* x is subnormal, normalize it. */
8731914882SAlex Richardson ix = asuint64 (x * 0x1p52);
8831914882SAlex Richardson ix -= 52ULL << 52;
8931914882SAlex Richardson }
9031914882SAlex Richardson
9131914882SAlex Richardson /* x = 2^k z; where z is in range [OFF,2*OFF) and exact.
9231914882SAlex Richardson The range is split into N subintervals.
9331914882SAlex Richardson The ith subinterval contains z and c is near its center. */
9431914882SAlex Richardson tmp = ix - OFF;
9531914882SAlex Richardson i = (tmp >> (52 - LOG2_TABLE_BITS)) % N;
9631914882SAlex Richardson k = (int64_t) tmp >> 52; /* arithmetic shift */
9731914882SAlex Richardson iz = ix - (tmp & 0xfffULL << 52);
9831914882SAlex Richardson invc = T[i].invc;
9931914882SAlex Richardson logc = T[i].logc;
10031914882SAlex Richardson z = asdouble (iz);
10131914882SAlex Richardson kd = (double_t) k;
10231914882SAlex Richardson
10331914882SAlex Richardson /* log2(x) = log2(z/c) + log2(c) + k. */
10431914882SAlex Richardson /* r ~= z/c - 1, |r| < 1/(2*N). */
10531914882SAlex Richardson #if HAVE_FAST_FMA
10631914882SAlex Richardson /* rounding error: 0x1p-55/N. */
10731914882SAlex Richardson r = fma (z, invc, -1.0);
10831914882SAlex Richardson t1 = r * InvLn2hi;
10931914882SAlex Richardson t2 = r * InvLn2lo + fma (r, InvLn2hi, -t1);
11031914882SAlex Richardson #else
11131914882SAlex Richardson double_t rhi, rlo;
11231914882SAlex Richardson /* rounding error: 0x1p-55/N + 0x1p-65. */
11331914882SAlex Richardson r = (z - T2[i].chi - T2[i].clo) * invc;
11431914882SAlex Richardson rhi = asdouble (asuint64 (r) & -1ULL << 32);
11531914882SAlex Richardson rlo = r - rhi;
11631914882SAlex Richardson t1 = rhi * InvLn2hi;
11731914882SAlex Richardson t2 = rlo * InvLn2hi + r * InvLn2lo;
11831914882SAlex Richardson #endif
11931914882SAlex Richardson
12031914882SAlex Richardson /* hi + lo = r/ln2 + log2(c) + k. */
12131914882SAlex Richardson t3 = kd + logc;
12231914882SAlex Richardson hi = t3 + t1;
12331914882SAlex Richardson lo = t3 - hi + t1 + t2;
12431914882SAlex Richardson
12531914882SAlex Richardson /* log2(r+1) = r/ln2 + r^2*poly(r). */
12631914882SAlex Richardson /* Evaluation is optimized assuming superscalar pipelined execution. */
12731914882SAlex Richardson r2 = r * r; /* rounding error: 0x1p-54/N^2. */
12831914882SAlex Richardson r4 = r2 * r2;
12931914882SAlex Richardson #if LOG2_POLY_ORDER == 7
13031914882SAlex Richardson /* Worst-case error if |y| > 0x1p-4: 0.547 ULP (0.550 ULP without fma).
13131914882SAlex Richardson ~ 0.5 + 2/N/ln2 + abs-poly-error*0x1p56 ULP (+ 0.003 ULP without fma). */
13231914882SAlex Richardson p = A[0] + r * A[1] + r2 * (A[2] + r * A[3]) + r4 * (A[4] + r * A[5]);
13331914882SAlex Richardson y = lo + r2 * p + hi;
13431914882SAlex Richardson #endif
13531914882SAlex Richardson return eval_as_double (y);
13631914882SAlex Richardson }
13731914882SAlex Richardson #if USE_GLIBC_ABI
strong_alias(log2,__log2_finite)13831914882SAlex Richardson strong_alias (log2, __log2_finite)
13931914882SAlex Richardson hidden_alias (log2, __ieee754_log2)
14031914882SAlex Richardson # if LDBL_MANT_DIG == 53
14131914882SAlex Richardson long double log2l (long double x) { return log2 (x); }
14231914882SAlex Richardson # endif
14331914882SAlex Richardson #endif
144*f3087befSAndrew Turner
145*f3087befSAndrew Turner TEST_SIG (S, D, 1, log2, 0.01, 11.1)
146*f3087befSAndrew Turner TEST_ULP (log2, 0.05)
147*f3087befSAndrew Turner TEST_ULP_NONNEAREST (log2, 0.5)
148*f3087befSAndrew Turner TEST_INTERVAL (log2, 0, 0xffff000000000000, 10000)
149*f3087befSAndrew Turner TEST_INTERVAL (log2, 0x1p-4, 0x1p4, 40000)
150*f3087befSAndrew Turner TEST_INTERVAL (log2, 0, inf, 40000)
151