1*412f47f9SXin Li /*
2*412f47f9SXin Li * Single-precision log function.
3*412f47f9SXin Li *
4*412f47f9SXin Li * Copyright (c) 2017-2023, Arm Limited.
5*412f47f9SXin Li * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*412f47f9SXin Li */
7*412f47f9SXin Li
8*412f47f9SXin Li #include <math.h>
9*412f47f9SXin Li #include <stdint.h>
10*412f47f9SXin Li #include "math_config.h"
11*412f47f9SXin Li
12*412f47f9SXin Li /*
13*412f47f9SXin Li LOGF_TABLE_BITS = 4
14*412f47f9SXin Li LOGF_POLY_ORDER = 4
15*412f47f9SXin Li
16*412f47f9SXin Li ULP error: 0.818 (nearest rounding.)
17*412f47f9SXin Li Relative error: 1.957 * 2^-26 (before rounding.)
18*412f47f9SXin Li */
19*412f47f9SXin Li
20*412f47f9SXin Li #define T __logf_data.tab
21*412f47f9SXin Li #define A __logf_data.poly
22*412f47f9SXin Li #define Ln2 __logf_data.ln2
23*412f47f9SXin Li #define N (1 << LOGF_TABLE_BITS)
24*412f47f9SXin Li #define OFF 0x3f330000
25*412f47f9SXin Li
26*412f47f9SXin Li float
logf(float x)27*412f47f9SXin Li logf (float x)
28*412f47f9SXin Li {
29*412f47f9SXin Li /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
30*412f47f9SXin Li double_t z, r, r2, y, y0, invc, logc;
31*412f47f9SXin Li uint32_t ix, iz, tmp;
32*412f47f9SXin Li int k, i;
33*412f47f9SXin Li
34*412f47f9SXin Li ix = asuint (x);
35*412f47f9SXin Li #if WANT_ROUNDING
36*412f47f9SXin Li /* Fix sign of zero with downward rounding when x==1. */
37*412f47f9SXin Li if (unlikely (ix == 0x3f800000))
38*412f47f9SXin Li return 0;
39*412f47f9SXin Li #endif
40*412f47f9SXin Li if (unlikely (ix - 0x00800000 >= 0x7f800000 - 0x00800000))
41*412f47f9SXin Li {
42*412f47f9SXin Li /* x < 0x1p-126 or inf or nan. */
43*412f47f9SXin Li if (ix * 2 == 0)
44*412f47f9SXin Li return __math_divzerof (1);
45*412f47f9SXin Li if (ix == 0x7f800000) /* log(inf) == inf. */
46*412f47f9SXin Li return x;
47*412f47f9SXin Li if ((ix & 0x80000000) || ix * 2 >= 0xff000000)
48*412f47f9SXin Li return __math_invalidf (x);
49*412f47f9SXin Li /* x is subnormal, normalize it. */
50*412f47f9SXin Li ix = asuint (x * 0x1p23f);
51*412f47f9SXin Li ix -= 23 << 23;
52*412f47f9SXin Li }
53*412f47f9SXin Li
54*412f47f9SXin Li /* x = 2^k z; where z is in range [OFF,2*OFF] and exact.
55*412f47f9SXin Li The range is split into N subintervals.
56*412f47f9SXin Li The ith subinterval contains z and c is near its center. */
57*412f47f9SXin Li tmp = ix - OFF;
58*412f47f9SXin Li i = (tmp >> (23 - LOGF_TABLE_BITS)) % N;
59*412f47f9SXin Li k = (int32_t) tmp >> 23; /* arithmetic shift */
60*412f47f9SXin Li iz = ix - (tmp & 0xff800000);
61*412f47f9SXin Li invc = T[i].invc;
62*412f47f9SXin Li logc = T[i].logc;
63*412f47f9SXin Li z = (double_t) asfloat (iz);
64*412f47f9SXin Li
65*412f47f9SXin Li /* log(x) = log1p(z/c-1) + log(c) + k*Ln2 */
66*412f47f9SXin Li r = z * invc - 1;
67*412f47f9SXin Li y0 = logc + (double_t) k * Ln2;
68*412f47f9SXin Li
69*412f47f9SXin Li /* Pipelined polynomial evaluation to approximate log1p(r). */
70*412f47f9SXin Li r2 = r * r;
71*412f47f9SXin Li y = A[1] * r + A[2];
72*412f47f9SXin Li y = A[0] * r2 + y;
73*412f47f9SXin Li y = y * r2 + (y0 + r);
74*412f47f9SXin Li return eval_as_float (y);
75*412f47f9SXin Li }
76*412f47f9SXin Li #if USE_GLIBC_ABI
77*412f47f9SXin Li strong_alias (logf, __logf_finite)
78*412f47f9SXin Li hidden_alias (logf, __ieee754_logf)
79*412f47f9SXin Li #endif
80