1570af302Sopenharmony_ci/* 2570af302Sopenharmony_ci * Double-precision log2(x) function. 3570af302Sopenharmony_ci * 4570af302Sopenharmony_ci * Copyright (c) 2018, Arm Limited. 5570af302Sopenharmony_ci * SPDX-License-Identifier: MIT 6570af302Sopenharmony_ci */ 7570af302Sopenharmony_ci 8570af302Sopenharmony_ci#include <math.h> 9570af302Sopenharmony_ci#include <stdint.h> 10570af302Sopenharmony_ci#include "libm.h" 11570af302Sopenharmony_ci#include "log2_data.h" 12570af302Sopenharmony_ci 13570af302Sopenharmony_ci#define T __log2_data.tab 14570af302Sopenharmony_ci#define T2 __log2_data.tab2 15570af302Sopenharmony_ci#define B __log2_data.poly1 16570af302Sopenharmony_ci#define A __log2_data.poly 17570af302Sopenharmony_ci#define InvLn2hi __log2_data.invln2hi 18570af302Sopenharmony_ci#define InvLn2lo __log2_data.invln2lo 19570af302Sopenharmony_ci#define N (1 << LOG2_TABLE_BITS) 20570af302Sopenharmony_ci#define OFF 0x3fe6000000000000 21570af302Sopenharmony_ci 22570af302Sopenharmony_ci/* Top 16 bits of a double. */ 23570af302Sopenharmony_cistatic inline uint32_t top16(double x) 24570af302Sopenharmony_ci{ 25570af302Sopenharmony_ci return asuint64(x) >> 48; 26570af302Sopenharmony_ci} 27570af302Sopenharmony_ci 28570af302Sopenharmony_cidouble log2(double x) 29570af302Sopenharmony_ci{ 30570af302Sopenharmony_ci double_t z, r, r2, r4, y, invc, logc, kd, hi, lo, t1, t2, t3, p; 31570af302Sopenharmony_ci uint64_t ix, iz, tmp; 32570af302Sopenharmony_ci uint32_t top; 33570af302Sopenharmony_ci int k, i; 34570af302Sopenharmony_ci 35570af302Sopenharmony_ci ix = asuint64(x); 36570af302Sopenharmony_ci top = top16(x); 37570af302Sopenharmony_ci#define LO asuint64(1.0 - 0x1.5b51p-5) 38570af302Sopenharmony_ci#define HI asuint64(1.0 + 0x1.6ab2p-5) 39570af302Sopenharmony_ci if (predict_false(ix - LO < HI - LO)) { 40570af302Sopenharmony_ci /* Handle close to 1.0 inputs separately. */ 41570af302Sopenharmony_ci /* Fix sign of zero with downward rounding when x==1. */ 42570af302Sopenharmony_ci if (WANT_ROUNDING && predict_false(ix == asuint64(1.0))) 43570af302Sopenharmony_ci return 0; 44570af302Sopenharmony_ci r = x - 1.0; 45570af302Sopenharmony_ci#if __FP_FAST_FMA 46570af302Sopenharmony_ci hi = r * InvLn2hi; 47570af302Sopenharmony_ci lo = r * InvLn2lo + __builtin_fma(r, InvLn2hi, -hi); 48570af302Sopenharmony_ci#else 49570af302Sopenharmony_ci double_t rhi, rlo; 50570af302Sopenharmony_ci rhi = asdouble(asuint64(r) & -1ULL << 32); 51570af302Sopenharmony_ci rlo = r - rhi; 52570af302Sopenharmony_ci hi = rhi * InvLn2hi; 53570af302Sopenharmony_ci lo = rlo * InvLn2hi + r * InvLn2lo; 54570af302Sopenharmony_ci#endif 55570af302Sopenharmony_ci r2 = r * r; /* rounding error: 0x1p-62. */ 56570af302Sopenharmony_ci r4 = r2 * r2; 57570af302Sopenharmony_ci /* Worst-case error is less than 0.54 ULP (0.55 ULP without fma). */ 58570af302Sopenharmony_ci p = r2 * (B[0] + r * B[1]); 59570af302Sopenharmony_ci y = hi + p; 60570af302Sopenharmony_ci lo += hi - y + p; 61570af302Sopenharmony_ci lo += r4 * (B[2] + r * B[3] + r2 * (B[4] + r * B[5]) + 62570af302Sopenharmony_ci r4 * (B[6] + r * B[7] + r2 * (B[8] + r * B[9]))); 63570af302Sopenharmony_ci y += lo; 64570af302Sopenharmony_ci return eval_as_double(y); 65570af302Sopenharmony_ci } 66570af302Sopenharmony_ci if (predict_false(top - 0x0010 >= 0x7ff0 - 0x0010)) { 67570af302Sopenharmony_ci /* x < 0x1p-1022 or inf or nan. */ 68570af302Sopenharmony_ci if (ix * 2 == 0) 69570af302Sopenharmony_ci return __math_divzero(1); 70570af302Sopenharmony_ci if (ix == asuint64(INFINITY)) /* log(inf) == inf. */ 71570af302Sopenharmony_ci return x; 72570af302Sopenharmony_ci if ((top & 0x8000) || (top & 0x7ff0) == 0x7ff0) 73570af302Sopenharmony_ci return __math_invalid(x); 74570af302Sopenharmony_ci /* x is subnormal, normalize it. */ 75570af302Sopenharmony_ci ix = asuint64(x * 0x1p52); 76570af302Sopenharmony_ci ix -= 52ULL << 52; 77570af302Sopenharmony_ci } 78570af302Sopenharmony_ci 79570af302Sopenharmony_ci /* x = 2^k z; where z is in range [OFF,2*OFF) and exact. 80570af302Sopenharmony_ci The range is split into N subintervals. 81570af302Sopenharmony_ci The ith subinterval contains z and c is near its center. */ 82570af302Sopenharmony_ci tmp = ix - OFF; 83570af302Sopenharmony_ci i = (tmp >> (52 - LOG2_TABLE_BITS)) % N; 84570af302Sopenharmony_ci k = (int64_t)tmp >> 52; /* arithmetic shift */ 85570af302Sopenharmony_ci iz = ix - (tmp & 0xfffULL << 52); 86570af302Sopenharmony_ci invc = T[i].invc; 87570af302Sopenharmony_ci logc = T[i].logc; 88570af302Sopenharmony_ci z = asdouble(iz); 89570af302Sopenharmony_ci kd = (double_t)k; 90570af302Sopenharmony_ci 91570af302Sopenharmony_ci /* log2(x) = log2(z/c) + log2(c) + k. */ 92570af302Sopenharmony_ci /* r ~= z/c - 1, |r| < 1/(2*N). */ 93570af302Sopenharmony_ci#if __FP_FAST_FMA 94570af302Sopenharmony_ci /* rounding error: 0x1p-55/N. */ 95570af302Sopenharmony_ci r = __builtin_fma(z, invc, -1.0); 96570af302Sopenharmony_ci t1 = r * InvLn2hi; 97570af302Sopenharmony_ci t2 = r * InvLn2lo + __builtin_fma(r, InvLn2hi, -t1); 98570af302Sopenharmony_ci#else 99570af302Sopenharmony_ci double_t rhi, rlo; 100570af302Sopenharmony_ci /* rounding error: 0x1p-55/N + 0x1p-65. */ 101570af302Sopenharmony_ci r = (z - T2[i].chi - T2[i].clo) * invc; 102570af302Sopenharmony_ci rhi = asdouble(asuint64(r) & -1ULL << 32); 103570af302Sopenharmony_ci rlo = r - rhi; 104570af302Sopenharmony_ci t1 = rhi * InvLn2hi; 105570af302Sopenharmony_ci t2 = rlo * InvLn2hi + r * InvLn2lo; 106570af302Sopenharmony_ci#endif 107570af302Sopenharmony_ci 108570af302Sopenharmony_ci /* hi + lo = r/ln2 + log2(c) + k. */ 109570af302Sopenharmony_ci t3 = kd + logc; 110570af302Sopenharmony_ci hi = t3 + t1; 111570af302Sopenharmony_ci lo = t3 - hi + t1 + t2; 112570af302Sopenharmony_ci 113570af302Sopenharmony_ci /* log2(r+1) = r/ln2 + r^2*poly(r). */ 114570af302Sopenharmony_ci /* Evaluation is optimized assuming superscalar pipelined execution. */ 115570af302Sopenharmony_ci r2 = r * r; /* rounding error: 0x1p-54/N^2. */ 116570af302Sopenharmony_ci r4 = r2 * r2; 117570af302Sopenharmony_ci /* Worst-case error if |y| > 0x1p-4: 0.547 ULP (0.550 ULP without fma). 118570af302Sopenharmony_ci ~ 0.5 + 2/N/ln2 + abs-poly-error*0x1p56 ULP (+ 0.003 ULP without fma). */ 119570af302Sopenharmony_ci p = A[0] + r * A[1] + r2 * (A[2] + r * A[3]) + r4 * (A[4] + r * A[5]); 120570af302Sopenharmony_ci y = lo + r2 * p + hi; 121570af302Sopenharmony_ci return eval_as_double(y); 122570af302Sopenharmony_ci} 123