1 /* Single-precision log function.
2 Copyright (c) 2017 Arm Ltd. All rights reserved.
3
4 SPDX-License-Identifier: BSD-3-Clause
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 3. The name of the company may not be used to endorse or promote
15 products derived from this software without specific prior written
16 permission.
17
18 THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED
19 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
28
29 #include "fdlibm.h"
30 #if !__OBSOLETE_MATH_FLOAT
31
32 #include <math.h>
33 #include <stdint.h>
34 #include "math_config.h"
35
36 /*
37 LOGF_TABLE_BITS = 4
38 LOGF_POLY_ORDER = 4
39
40 ULP error: 0.818 (nearest rounding.)
41 Relative error: 1.957 * 2^-26 (before rounding.)
42 */
43
44 #define T __logf_data.tab
45 #define A __logf_data.poly
46 #define Ln2 __logf_data.ln2
47 #define N (1 << LOGF_TABLE_BITS)
48 #define OFF 0x3f330000
49
50 float
logf(float x)51 logf (float x)
52 {
53 /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */
54 double_t z, r, r2, y, y0, invc, logc;
55 uint32_t ix, iz, tmp;
56 int k, i;
57
58 ix = asuint (x);
59 #if WANT_ROUNDING
60 /* Fix sign of zero with downward rounding when x==1. */
61 if (__builtin_expect (ix == 0x3f800000, 0))
62 return 0;
63 #endif
64 if (__builtin_expect (ix - 0x00800000 >= 0x7f800000 - 0x00800000, 0))
65 {
66 /* x < 0x1p-126 or inf or nan. */
67 if (ix * 2 == 0)
68 return __math_divzerof (1);
69 if (ix == 0x7f800000) /* log(inf) == inf. */
70 return x;
71 if ((ix & 0x80000000) || ix * 2 >= 0xff000000)
72 return __math_invalidf (x);
73 /* x is subnormal, normalize it. */
74 ix = asuint (x * 0x1p23f);
75 ix -= (int32_t) 23 << 23;
76 }
77
78 /* x = 2^k z; where z is in range [OFF,2*OFF] and exact.
79 The range is split into N subintervals.
80 The ith subinterval contains z and c is near its center. */
81 tmp = ix - OFF;
82 i = (tmp >> (23 - LOGF_TABLE_BITS)) % N;
83 k = (int32_t) tmp >> 23; /* arithmetic shift */
84 iz = ix - (tmp & (uint32_t) 0x1ff << 23);
85 invc = T[i].invc;
86 logc = T[i].logc;
87 z = (double_t) asfloat (iz);
88
89 /* log(x) = log1p(z/c-1) + log(c) + k*Ln2 */
90 r = z * invc - 1;
91 y0 = logc + (double_t) k * Ln2;
92
93 /* Pipelined polynomial evaluation to approximate log1p(r). */
94 r2 = r * r;
95 y = A[1] * r + A[2];
96 y = A[0] * r2 + y;
97 y = y * r2 + (y0 + r);
98 return (float) y;
99 }
100 #endif /* !__OBSOLETE_MATH_FLOAT */
101