131914882SAlex Richardson /* 231914882SAlex Richardson * Single-precision log function. 331914882SAlex Richardson * 4*072a4ba8SAndrew Turner * Copyright (c) 2017-2023, Arm Limited. 5*072a4ba8SAndrew 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" 1131914882SAlex Richardson 1231914882SAlex Richardson /* 1331914882SAlex Richardson LOGF_TABLE_BITS = 4 1431914882SAlex Richardson LOGF_POLY_ORDER = 4 1531914882SAlex Richardson 1631914882SAlex Richardson ULP error: 0.818 (nearest rounding.) 1731914882SAlex Richardson Relative error: 1.957 * 2^-26 (before rounding.) 1831914882SAlex Richardson */ 1931914882SAlex Richardson 2031914882SAlex Richardson #define T __logf_data.tab 2131914882SAlex Richardson #define A __logf_data.poly 2231914882SAlex Richardson #define Ln2 __logf_data.ln2 2331914882SAlex Richardson #define N (1 << LOGF_TABLE_BITS) 2431914882SAlex Richardson #define OFF 0x3f330000 2531914882SAlex Richardson 2631914882SAlex Richardson float 2731914882SAlex Richardson logf (float x) 2831914882SAlex Richardson { 2931914882SAlex Richardson /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ 3031914882SAlex Richardson double_t z, r, r2, y, y0, invc, logc; 3131914882SAlex Richardson uint32_t ix, iz, tmp; 3231914882SAlex Richardson int k, i; 3331914882SAlex Richardson 3431914882SAlex Richardson ix = asuint (x); 3531914882SAlex Richardson #if WANT_ROUNDING 3631914882SAlex Richardson /* Fix sign of zero with downward rounding when x==1. */ 3731914882SAlex Richardson if (unlikely (ix == 0x3f800000)) 3831914882SAlex Richardson return 0; 3931914882SAlex Richardson #endif 4031914882SAlex Richardson if (unlikely (ix - 0x00800000 >= 0x7f800000 - 0x00800000)) 4131914882SAlex Richardson { 4231914882SAlex Richardson /* x < 0x1p-126 or inf or nan. */ 4331914882SAlex Richardson if (ix * 2 == 0) 4431914882SAlex Richardson return __math_divzerof (1); 4531914882SAlex Richardson if (ix == 0x7f800000) /* log(inf) == inf. */ 4631914882SAlex Richardson return x; 4731914882SAlex Richardson if ((ix & 0x80000000) || ix * 2 >= 0xff000000) 4831914882SAlex Richardson return __math_invalidf (x); 4931914882SAlex Richardson /* x is subnormal, normalize it. */ 5031914882SAlex Richardson ix = asuint (x * 0x1p23f); 5131914882SAlex Richardson ix -= 23 << 23; 5231914882SAlex Richardson } 5331914882SAlex Richardson 5431914882SAlex Richardson /* x = 2^k z; where z is in range [OFF,2*OFF] and exact. 5531914882SAlex Richardson The range is split into N subintervals. 5631914882SAlex Richardson The ith subinterval contains z and c is near its center. */ 5731914882SAlex Richardson tmp = ix - OFF; 5831914882SAlex Richardson i = (tmp >> (23 - LOGF_TABLE_BITS)) % N; 5931914882SAlex Richardson k = (int32_t) tmp >> 23; /* arithmetic shift */ 60*072a4ba8SAndrew Turner iz = ix - (tmp & 0xff800000); 6131914882SAlex Richardson invc = T[i].invc; 6231914882SAlex Richardson logc = T[i].logc; 6331914882SAlex Richardson z = (double_t) asfloat (iz); 6431914882SAlex Richardson 6531914882SAlex Richardson /* log(x) = log1p(z/c-1) + log(c) + k*Ln2 */ 6631914882SAlex Richardson r = z * invc - 1; 6731914882SAlex Richardson y0 = logc + (double_t) k * Ln2; 6831914882SAlex Richardson 6931914882SAlex Richardson /* Pipelined polynomial evaluation to approximate log1p(r). */ 7031914882SAlex Richardson r2 = r * r; 7131914882SAlex Richardson y = A[1] * r + A[2]; 7231914882SAlex Richardson y = A[0] * r2 + y; 7331914882SAlex Richardson y = y * r2 + (y0 + r); 7431914882SAlex Richardson return eval_as_float (y); 7531914882SAlex Richardson } 7631914882SAlex Richardson #if USE_GLIBC_ABI 7731914882SAlex Richardson strong_alias (logf, __logf_finite) 7831914882SAlex Richardson hidden_alias (logf, __ieee754_logf) 7931914882SAlex Richardson #endif 80