1570af302Sopenharmony_ci/* 2570af302Sopenharmony_ci * Double-precision log(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 "log_data.h" 12570af302Sopenharmony_ci 13570af302Sopenharmony_ci#define T __log_data.tab 14570af302Sopenharmony_ci#define T2 __log_data.tab2 15570af302Sopenharmony_ci#define B __log_data.poly1 16570af302Sopenharmony_ci#define A __log_data.poly 17570af302Sopenharmony_ci#define Ln2hi __log_data.ln2hi 18570af302Sopenharmony_ci#define Ln2lo __log_data.ln2lo 19570af302Sopenharmony_ci#define N (1 << LOG_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 log(double x) 29570af302Sopenharmony_ci{ 30570af302Sopenharmony_ci double_t w, z, r, r2, r3, y, invc, logc, kd, hi, lo; 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 - 0x1p-4) 38570af302Sopenharmony_ci#define HI asuint64(1.0 + 0x1.09p-4) 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 r2 = r * r; 46570af302Sopenharmony_ci r3 = r * r2; 47570af302Sopenharmony_ci y = r3 * 48570af302Sopenharmony_ci (B[1] + r * B[2] + r2 * B[3] + 49570af302Sopenharmony_ci r3 * (B[4] + r * B[5] + r2 * B[6] + 50570af302Sopenharmony_ci r3 * (B[7] + r * B[8] + r2 * B[9] + r3 * B[10]))); 51570af302Sopenharmony_ci /* Worst-case error is around 0.507 ULP. */ 52570af302Sopenharmony_ci w = r * 0x1p27; 53570af302Sopenharmony_ci double_t rhi = r + w - w; 54570af302Sopenharmony_ci double_t rlo = r - rhi; 55570af302Sopenharmony_ci w = rhi * rhi * B[0]; /* B[0] == -0.5. */ 56570af302Sopenharmony_ci hi = r + w; 57570af302Sopenharmony_ci lo = r - hi + w; 58570af302Sopenharmony_ci lo += B[0] * rlo * (rhi + r); 59570af302Sopenharmony_ci y += lo; 60570af302Sopenharmony_ci y += hi; 61570af302Sopenharmony_ci return eval_as_double(y); 62570af302Sopenharmony_ci } 63570af302Sopenharmony_ci if (predict_false(top - 0x0010 >= 0x7ff0 - 0x0010)) { 64570af302Sopenharmony_ci /* x < 0x1p-1022 or inf or nan. */ 65570af302Sopenharmony_ci if (ix * 2 == 0) 66570af302Sopenharmony_ci return __math_divzero(1); 67570af302Sopenharmony_ci if (ix == asuint64(INFINITY)) /* log(inf) == inf. */ 68570af302Sopenharmony_ci return x; 69570af302Sopenharmony_ci if ((top & 0x8000) || (top & 0x7ff0) == 0x7ff0) 70570af302Sopenharmony_ci return __math_invalid(x); 71570af302Sopenharmony_ci /* x is subnormal, normalize it. */ 72570af302Sopenharmony_ci ix = asuint64(x * 0x1p52); 73570af302Sopenharmony_ci ix -= 52ULL << 52; 74570af302Sopenharmony_ci } 75570af302Sopenharmony_ci 76570af302Sopenharmony_ci /* x = 2^k z; where z is in range [OFF,2*OFF) and exact. 77570af302Sopenharmony_ci The range is split into N subintervals. 78570af302Sopenharmony_ci The ith subinterval contains z and c is near its center. */ 79570af302Sopenharmony_ci tmp = ix - OFF; 80570af302Sopenharmony_ci i = (tmp >> (52 - LOG_TABLE_BITS)) % N; 81570af302Sopenharmony_ci k = (int64_t)tmp >> 52; /* arithmetic shift */ 82570af302Sopenharmony_ci iz = ix - (tmp & 0xfffULL << 52); 83570af302Sopenharmony_ci invc = T[i].invc; 84570af302Sopenharmony_ci logc = T[i].logc; 85570af302Sopenharmony_ci z = asdouble(iz); 86570af302Sopenharmony_ci 87570af302Sopenharmony_ci /* log(x) = log1p(z/c-1) + log(c) + k*Ln2. */ 88570af302Sopenharmony_ci /* r ~= z/c - 1, |r| < 1/(2*N). */ 89570af302Sopenharmony_ci#if __FP_FAST_FMA 90570af302Sopenharmony_ci /* rounding error: 0x1p-55/N. */ 91570af302Sopenharmony_ci r = __builtin_fma(z, invc, -1.0); 92570af302Sopenharmony_ci#else 93570af302Sopenharmony_ci /* rounding error: 0x1p-55/N + 0x1p-66. */ 94570af302Sopenharmony_ci r = (z - T2[i].chi - T2[i].clo) * invc; 95570af302Sopenharmony_ci#endif 96570af302Sopenharmony_ci kd = (double_t)k; 97570af302Sopenharmony_ci 98570af302Sopenharmony_ci /* hi + lo = r + log(c) + k*Ln2. */ 99570af302Sopenharmony_ci w = kd * Ln2hi + logc; 100570af302Sopenharmony_ci hi = w + r; 101570af302Sopenharmony_ci lo = w - hi + r + kd * Ln2lo; 102570af302Sopenharmony_ci 103570af302Sopenharmony_ci /* log(x) = lo + (log1p(r) - r) + hi. */ 104570af302Sopenharmony_ci r2 = r * r; /* rounding error: 0x1p-54/N^2. */ 105570af302Sopenharmony_ci /* Worst case error if |y| > 0x1p-5: 106570af302Sopenharmony_ci 0.5 + 4.13/N + abs-poly-error*2^57 ULP (+ 0.002 ULP without fma) 107570af302Sopenharmony_ci Worst case error if |y| > 0x1p-4: 108570af302Sopenharmony_ci 0.5 + 2.06/N + abs-poly-error*2^56 ULP (+ 0.001 ULP without fma). */ 109570af302Sopenharmony_ci y = lo + r2 * A[0] + 110570af302Sopenharmony_ci r * r2 * (A[1] + r * A[2] + r2 * (A[3] + r * A[4])) + hi; 111570af302Sopenharmony_ci return eval_as_double(y); 112570af302Sopenharmony_ci} 113