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