131914882SAlex Richardson /* 231914882SAlex Richardson * Single-precision log function. 331914882SAlex Richardson * 4*f3087befSAndrew Turner * Copyright (c) 2017-2024, Arm Limited. 5072a4ba8SAndrew Turner * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception 631914882SAlex Richardson */ 731914882SAlex Richardson 831914882SAlex Richardson #include <math.h> 931914882SAlex Richardson #include <stdint.h> 1031914882SAlex Richardson #include "math_config.h" 11*f3087befSAndrew Turner #include "test_defs.h" 12*f3087befSAndrew Turner #include "test_sig.h" 1331914882SAlex Richardson 1431914882SAlex Richardson /* 1531914882SAlex Richardson LOGF_TABLE_BITS = 4 1631914882SAlex Richardson LOGF_POLY_ORDER = 4 1731914882SAlex Richardson 1831914882SAlex Richardson ULP error: 0.818 (nearest rounding.) 1931914882SAlex Richardson Relative error: 1.957 * 2^-26 (before rounding.) 2031914882SAlex Richardson */ 2131914882SAlex Richardson 2231914882SAlex Richardson #define T __logf_data.tab 2331914882SAlex Richardson #define A __logf_data.poly 2431914882SAlex Richardson #define Ln2 __logf_data.ln2 2531914882SAlex Richardson #define N (1 << LOGF_TABLE_BITS) 2631914882SAlex Richardson #define OFF 0x3f330000 2731914882SAlex Richardson 2831914882SAlex Richardson float 2931914882SAlex Richardson logf (float x) 3031914882SAlex Richardson { 3131914882SAlex Richardson /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ 3231914882SAlex Richardson double_t z, r, r2, y, y0, invc, logc; 3331914882SAlex Richardson uint32_t ix, iz, tmp; 3431914882SAlex Richardson int k, i; 3531914882SAlex Richardson 3631914882SAlex Richardson ix = asuint (x); 3731914882SAlex Richardson #if WANT_ROUNDING 3831914882SAlex Richardson /* Fix sign of zero with downward rounding when x==1. */ 3931914882SAlex Richardson if (unlikely (ix == 0x3f800000)) 4031914882SAlex Richardson return 0; 4131914882SAlex Richardson #endif 4231914882SAlex Richardson if (unlikely (ix - 0x00800000 >= 0x7f800000 - 0x00800000)) 4331914882SAlex Richardson { 4431914882SAlex Richardson /* x < 0x1p-126 or inf or nan. */ 4531914882SAlex Richardson if (ix * 2 == 0) 4631914882SAlex Richardson return __math_divzerof (1); 4731914882SAlex Richardson if (ix == 0x7f800000) /* log(inf) == inf. */ 4831914882SAlex Richardson return x; 4931914882SAlex Richardson if ((ix & 0x80000000) || ix * 2 >= 0xff000000) 5031914882SAlex Richardson return __math_invalidf (x); 5131914882SAlex Richardson /* x is subnormal, normalize it. */ 5231914882SAlex Richardson ix = asuint (x * 0x1p23f); 5331914882SAlex Richardson ix -= 23 << 23; 5431914882SAlex Richardson } 5531914882SAlex Richardson 5631914882SAlex Richardson /* x = 2^k z; where z is in range [OFF,2*OFF] and exact. 5731914882SAlex Richardson The range is split into N subintervals. 5831914882SAlex Richardson The ith subinterval contains z and c is near its center. */ 5931914882SAlex Richardson tmp = ix - OFF; 6031914882SAlex Richardson i = (tmp >> (23 - LOGF_TABLE_BITS)) % N; 6131914882SAlex Richardson k = (int32_t) tmp >> 23; /* arithmetic shift */ 62072a4ba8SAndrew Turner iz = ix - (tmp & 0xff800000); 6331914882SAlex Richardson invc = T[i].invc; 6431914882SAlex Richardson logc = T[i].logc; 6531914882SAlex Richardson z = (double_t) asfloat (iz); 6631914882SAlex Richardson 6731914882SAlex Richardson /* log(x) = log1p(z/c-1) + log(c) + k*Ln2 */ 6831914882SAlex Richardson r = z * invc - 1; 6931914882SAlex Richardson y0 = logc + (double_t) k * Ln2; 7031914882SAlex Richardson 7131914882SAlex Richardson /* Pipelined polynomial evaluation to approximate log1p(r). */ 7231914882SAlex Richardson r2 = r * r; 7331914882SAlex Richardson y = A[1] * r + A[2]; 7431914882SAlex Richardson y = A[0] * r2 + y; 7531914882SAlex Richardson y = y * r2 + (y0 + r); 7631914882SAlex Richardson return eval_as_float (y); 7731914882SAlex Richardson } 7831914882SAlex Richardson #if USE_GLIBC_ABI 7931914882SAlex Richardson strong_alias (logf, __logf_finite) 8031914882SAlex Richardson hidden_alias (logf, __ieee754_logf) 8131914882SAlex Richardson #endif 82*f3087befSAndrew Turner 83*f3087befSAndrew Turner TEST_SIG (S, F, 1, log, 0.01, 11.1) 84*f3087befSAndrew Turner TEST_ULP (logf, 0.32) 85*f3087befSAndrew Turner TEST_ULP_NONNEAREST (logf, 0.5) 86*f3087befSAndrew Turner TEST_INTERVAL (logf, 0, 0xffff0000, 10000) 87*f3087befSAndrew Turner TEST_INTERVAL (logf, 0x1p-4, 0x1p4, 500000) 88*f3087befSAndrew Turner TEST_INTERVAL (logf, 0, inf, 50000) 89