]>
Commit | Line | Data |
---|---|---|
875c76c7 | 1 | /* Single-precision log2 function. |
2b778ceb | 2 | Copyright (C) 2017-2021 Free Software Foundation, Inc. |
875c76c7 | 3 | This file is part of the GNU C Library. |
e7fd8a39 | 4 | |
875c76c7 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. | |
e7fd8a39 | 14 | |
875c76c7 SN |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see | |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
e7fd8a39 | 18 | |
1ed0291c | 19 | #include <math.h> |
875c76c7 | 20 | #include <stdint.h> |
220622dd | 21 | #include <libm-alias-finite.h> |
24b6515d | 22 | #include <libm-alias-float.h> |
875c76c7 SN |
23 | #include "math_config.h" |
24 | ||
25 | /* | |
26 | LOG2F_TABLE_BITS = 4 | |
27 | LOG2F_POLY_ORDER = 4 | |
e7fd8a39 | 28 | |
875c76c7 SN |
29 | ULP error: 0.752 (nearest rounding.) |
30 | Relative error: 1.9 * 2^-26 (before rounding.) | |
31 | */ | |
e7fd8a39 | 32 | |
875c76c7 SN |
33 | #define N (1 << LOG2F_TABLE_BITS) |
34 | #define T __log2f_data.tab | |
35 | #define A __log2f_data.poly | |
36 | #define OFF 0x3f330000 | |
e7fd8a39 | 37 | |
0ac5ae23 | 38 | float |
bd4430c2 | 39 | __log2f (float x) |
e7fd8a39 | 40 | { |
875c76c7 SN |
41 | /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ |
42 | double_t z, r, r2, p, y, y0, invc, logc; | |
43 | uint32_t ix, iz, top, tmp; | |
44 | int k, i; | |
45 | ||
46 | ix = asuint (x); | |
47 | #if WANT_ROUNDING | |
48 | /* Fix sign of zero with downward rounding when x==1. */ | |
49 | if (__glibc_unlikely (ix == 0x3f800000)) | |
50 | return 0; | |
51 | #endif | |
52 | if (__glibc_unlikely (ix - 0x00800000 >= 0x7f800000 - 0x00800000)) | |
53 | { | |
54 | /* x < 0x1p-126 or inf or nan. */ | |
55 | if (ix * 2 == 0) | |
56 | return __math_divzerof (1); | |
57 | if (ix == 0x7f800000) /* log2(inf) == inf. */ | |
58 | return x; | |
59 | if ((ix & 0x80000000) || ix * 2 >= 0xff000000) | |
60 | return __math_invalidf (x); | |
61 | /* x is subnormal, normalize it. */ | |
62 | ix = asuint (x * 0x1p23f); | |
63 | ix -= 23 << 23; | |
64 | } | |
65 | ||
66 | /* x = 2^k z; where z is in range [OFF,2*OFF] and exact. | |
67 | The range is split into N subintervals. | |
68 | The ith subinterval contains z and c is near its center. */ | |
69 | tmp = ix - OFF; | |
70 | i = (tmp >> (23 - LOG2F_TABLE_BITS)) % N; | |
71 | top = tmp & 0xff800000; | |
72 | iz = ix - top; | |
73 | k = (int32_t) tmp >> 23; /* arithmetic shift */ | |
74 | invc = T[i].invc; | |
75 | logc = T[i].logc; | |
76 | z = (double_t) asfloat (iz); | |
e7fd8a39 | 77 | |
875c76c7 SN |
78 | /* log2(x) = log1p(z/c-1)/ln2 + log2(c) + k */ |
79 | r = z * invc - 1; | |
80 | y0 = logc + (double_t) k; | |
e7fd8a39 | 81 | |
875c76c7 SN |
82 | /* Pipelined polynomial evaluation to approximate log1p(r)/ln2. */ |
83 | r2 = r * r; | |
84 | y = A[1] * r + A[2]; | |
85 | y = A[0] * r2 + y; | |
86 | p = A[3] * r + y0; | |
87 | y = y * r2 + p; | |
88 | return (float) y; | |
e7fd8a39 | 89 | } |
bd4430c2 SN |
90 | #ifndef __log2f |
91 | strong_alias (__log2f, __ieee754_log2f) | |
220622dd | 92 | libm_alias_finite (__ieee754_log2f, __log2f) |
bd4430c2 | 93 | versioned_symbol (libm, __log2f, log2f, GLIBC_2_27); |
24b6515d | 94 | libm_alias_float_other (__log2, log2) |
bd4430c2 | 95 | #endif |