]>
Commit | Line | Data |
---|---|---|
bf27d397 SN |
1 | /* Single-precision log function. |
2 | Copyright (C) 2017 Free Software Foundation, Inc. | |
3 | This file is part of the GNU C Library. | |
f7eac6eb | 4 | |
bf27d397 SN |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with the GNU C Library; if not, see | |
17 | <http://www.gnu.org/licenses/>. */ | |
f7eac6eb | 18 | |
1ed0291c | 19 | #include <math.h> |
bf27d397 SN |
20 | #include <stdint.h> |
21 | #include "math_config.h" | |
f7eac6eb | 22 | |
bf27d397 SN |
23 | /* |
24 | LOGF_TABLE_BITS = 4 | |
25 | LOGF_POLY_ORDER = 4 | |
f7eac6eb | 26 | |
bf27d397 SN |
27 | ULP error: 0.818 (nearest rounding.) |
28 | Relative error: 1.957 * 2^-26 (before rounding.) | |
29 | */ | |
30 | ||
31 | #define T __logf_data.tab | |
32 | #define A __logf_data.poly | |
33 | #define Ln2 __logf_data.ln2 | |
34 | #define N (1 << LOGF_TABLE_BITS) | |
35 | #define OFF 0x3f330000 | |
f7eac6eb | 36 | |
0ac5ae23 | 37 | float |
bf27d397 | 38 | __ieee754_logf (float x) |
f7eac6eb | 39 | { |
bf27d397 SN |
40 | /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ |
41 | double_t z, r, r2, y, y0, invc, logc; | |
42 | uint32_t ix, iz, tmp; | |
43 | int k, i; | |
44 | ||
45 | ix = asuint (x); | |
46 | #if WANT_ROUNDING | |
47 | /* Fix sign of zero with downward rounding when x==1. */ | |
48 | if (__glibc_unlikely (ix == 0x3f800000)) | |
49 | return 0; | |
50 | #endif | |
51 | if (__glibc_unlikely (ix - 0x00800000 >= 0x7f800000 - 0x00800000)) | |
52 | { | |
53 | /* x < 0x1p-126 or inf or nan. */ | |
54 | if (ix * 2 == 0) | |
55 | return __math_divzerof (1); | |
56 | if (ix == 0x7f800000) /* log(inf) == inf. */ | |
57 | return x; | |
58 | if ((ix & 0x80000000) || ix * 2 >= 0xff000000) | |
59 | return __math_invalidf (x); | |
60 | /* x is subnormal, normalize it. */ | |
61 | ix = asuint (x * 0x1p23f); | |
62 | ix -= 23 << 23; | |
63 | } | |
64 | ||
65 | /* x = 2^k z; where z is in range [OFF,2*OFF] and exact. | |
66 | The range is split into N subintervals. | |
67 | The ith subinterval contains z and c is near its center. */ | |
68 | tmp = ix - OFF; | |
69 | i = (tmp >> (23 - LOGF_TABLE_BITS)) % N; | |
70 | k = (int32_t) tmp >> 23; /* arithmetic shift */ | |
71 | iz = ix - (tmp & 0x1ff << 23); | |
72 | invc = T[i].invc; | |
73 | logc = T[i].logc; | |
74 | z = (double_t) asfloat (iz); | |
f7eac6eb | 75 | |
bf27d397 SN |
76 | /* log(x) = log1p(z/c-1) + log(c) + k*Ln2 */ |
77 | r = z * invc - 1; | |
78 | y0 = logc + (double_t) k * Ln2; | |
f7eac6eb | 79 | |
bf27d397 SN |
80 | /* Pipelined polynomial evaluation to approximate log1p(r). */ |
81 | r2 = r * r; | |
82 | y = A[1] * r + A[2]; | |
83 | y = A[0] * r2 + y; | |
84 | y = y * r2 + (y0 + r); | |
85 | return (float) y; | |
f7eac6eb | 86 | } |
0ac5ae23 | 87 | strong_alias (__ieee754_logf, __logf_finite) |