1 /* 2 * Double-precision log(x) function. 3 * 4 * Copyright (c) 2018-2024, Arm Limited. 5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception 6 */ 7 8 #include <float.h> 9 #include <math.h> 10 #include <stdint.h> 11 #include "math_config.h" 12 #include "test_defs.h" 13 #include "test_sig.h" 14 15 #define T __log_data.tab 16 #define T2 __log_data.tab2 17 #define B __log_data.poly1 18 #define A __log_data.poly 19 #define Ln2hi __log_data.ln2hi 20 #define Ln2lo __log_data.ln2lo 21 #define N (1 << LOG_TABLE_BITS) 22 #define OFF 0x3fe6000000000000 23 24 /* Top 16 bits of a double. */ 25 static inline uint32_t 26 top16 (double x) 27 { 28 return asuint64 (x) >> 48; 29 } 30 31 double 32 log (double x) 33 { 34 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ 35 double_t w, z, r, r2, r3, y, invc, logc, kd, hi, lo; 36 uint64_t ix, iz, tmp; 37 uint32_t top; 38 int k, i; 39 40 ix = asuint64 (x); 41 top = top16 (x); 42 43 #if LOG_POLY1_ORDER == 10 || LOG_POLY1_ORDER == 11 44 # define LO asuint64 (1.0 - 0x1p-5) 45 # define HI asuint64 (1.0 + 0x1.1p-5) 46 #elif LOG_POLY1_ORDER == 12 47 # define LO asuint64 (1.0 - 0x1p-4) 48 # define HI asuint64 (1.0 + 0x1.09p-4) 49 #endif 50 if (unlikely (ix - LO < HI - LO)) 51 { 52 /* Handle close to 1.0 inputs separately. */ 53 /* Fix sign of zero with downward rounding when x==1. */ 54 if (WANT_ROUNDING && unlikely (ix == asuint64 (1.0))) 55 return 0; 56 r = x - 1.0; 57 r2 = r * r; 58 r3 = r * r2; 59 #if LOG_POLY1_ORDER == 10 60 /* Worst-case error is around 0.516 ULP. */ 61 y = r3 * (B[1] + r * B[2] + r2 * B[3] 62 + r3 * (B[4] + r * B[5] + r2 * B[6] + r3 * (B[7] + r * B[8]))); 63 w = B[0] * r2; /* B[0] == -0.5. */ 64 hi = r + w; 65 y += r - hi + w; 66 y += hi; 67 #elif LOG_POLY1_ORDER == 11 68 /* Worst-case error is around 0.516 ULP. */ 69 y = r3 * (B[1] + r * B[2] 70 + r2 * (B[3] + r * B[4] + r2 * B[5] 71 + r3 * (B[6] + r * B[7] + r2 * B[8] + r3 * B[9]))); 72 w = B[0] * r2; /* B[0] == -0.5. */ 73 hi = r + w; 74 y += r - hi + w; 75 y += hi; 76 #elif LOG_POLY1_ORDER == 12 77 y = r3 * (B[1] + r * B[2] + r2 * B[3] 78 + r3 * (B[4] + r * B[5] + r2 * B[6] 79 + r3 * (B[7] + r * B[8] + r2 * B[9] + r3 * B[10]))); 80 # if N <= 64 81 /* Worst-case error is around 0.532 ULP. */ 82 w = B[0] * r2; /* B[0] == -0.5. */ 83 hi = r + w; 84 y += r - hi + w; 85 y += hi; 86 # else 87 /* Worst-case error is around 0.507 ULP. */ 88 w = r * 0x1p27; 89 double_t rhi = r + w - w; 90 double_t rlo = r - rhi; 91 w = rhi * rhi * B[0]; /* B[0] == -0.5. */ 92 hi = r + w; 93 lo = r - hi + w; 94 lo += B[0] * rlo * (rhi + r); 95 y += lo; 96 y += hi; 97 # endif 98 #endif 99 return eval_as_double (y); 100 } 101 if (unlikely (top - 0x0010 >= 0x7ff0 - 0x0010)) 102 { 103 /* x < 0x1p-1022 or inf or nan. */ 104 if (ix * 2 == 0) 105 return __math_divzero (1); 106 if (ix == asuint64 (INFINITY)) /* log(inf) == inf. */ 107 return x; 108 if ((top & 0x8000) || (top & 0x7ff0) == 0x7ff0) 109 return __math_invalid (x); 110 /* x is subnormal, normalize it. */ 111 ix = asuint64 (x * 0x1p52); 112 ix -= 52ULL << 52; 113 } 114 115 /* x = 2^k z; where z is in range [OFF,2*OFF) and exact. 116 The range is split into N subintervals. 117 The ith subinterval contains z and c is near its center. */ 118 tmp = ix - OFF; 119 i = (tmp >> (52 - LOG_TABLE_BITS)) % N; 120 k = (int64_t) tmp >> 52; /* arithmetic shift */ 121 iz = ix - (tmp & 0xfffULL << 52); 122 invc = T[i].invc; 123 logc = T[i].logc; 124 z = asdouble (iz); 125 126 /* log(x) = log1p(z/c-1) + log(c) + k*Ln2. */ 127 /* r ~= z/c - 1, |r| < 1/(2*N). */ 128 #if HAVE_FAST_FMA 129 /* rounding error: 0x1p-55/N. */ 130 r = fma (z, invc, -1.0); 131 #else 132 /* rounding error: 0x1p-55/N + 0x1p-66. */ 133 r = (z - T2[i].chi - T2[i].clo) * invc; 134 #endif 135 kd = (double_t) k; 136 137 /* hi + lo = r + log(c) + k*Ln2. */ 138 w = kd * Ln2hi + logc; 139 hi = w + r; 140 lo = w - hi + r + kd * Ln2lo; 141 142 /* log(x) = lo + (log1p(r) - r) + hi. */ 143 r2 = r * r; /* rounding error: 0x1p-54/N^2. */ 144 /* Worst case error if |y| > 0x1p-5: 145 0.5 + 4.13/N + abs-poly-error*2^57 ULP (+ 0.002 ULP without fma) 146 Worst case error if |y| > 0x1p-4: 147 0.5 + 2.06/N + abs-poly-error*2^56 ULP (+ 0.001 ULP without fma). */ 148 #if LOG_POLY_ORDER == 6 149 y = lo + r2 * A[0] + r * r2 * (A[1] + r * A[2] + r2 * (A[3] + r * A[4])) + hi; 150 #elif LOG_POLY_ORDER == 7 151 y = lo 152 + r2 * (A[0] + r * A[1] + r2 * (A[2] + r * A[3]) 153 + r2 * r2 * (A[4] + r * A[5])) 154 + hi; 155 #endif 156 return eval_as_double (y); 157 } 158 #if USE_GLIBC_ABI 159 strong_alias (log, __log_finite) 160 hidden_alias (log, __ieee754_log) 161 # if LDBL_MANT_DIG == 53 162 long double logl (long double x) { return log (x); } 163 # endif 164 #endif 165 166 TEST_SIG (S, D, 1, log, 0.01, 11.1) 167 TEST_ULP (log, 0.02) 168 TEST_ULP_NONNEAREST (log, 0.5) 169 TEST_INTERVAL (log, 0, 0xffff000000000000, 10000) 170 TEST_INTERVAL (log, 0x1p-4, 0x1p4, 400000) 171 TEST_INTERVAL (log, 0, inf, 400000) 172