tree-wide: Replace exp10() with our own impl
exp10() has a symbol version > 2.34 on latest glibc. To allow dropping our baseline required glibc runtime version to <= 2.34, let's add our own version to prevent pulling in the newer symbol from glibc.
This commit is contained in:
committed by
Daan De Meyer
parent
27e0bba966
commit
d52c8f2068
@@ -0,0 +1,24 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include "math-util.h"
|
||||
|
||||
double xexp10i(int n) {
|
||||
/* Powers of 10 up to 10^22 are exact in IEEE-754 binary64. */
|
||||
static const double table[] = {
|
||||
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11,
|
||||
1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22,
|
||||
};
|
||||
bool negative = n < 0;
|
||||
|
||||
/* Cast before negation so n == INT_MIN doesn't invoke signed-overflow UB. Unsigned negation
|
||||
* wraps to the magnitude we want. */
|
||||
unsigned k = negative ? -(unsigned) n : (unsigned) n;
|
||||
|
||||
/* 10^309 already overflows binary64 to +Inf; anything beyond just stays there. */
|
||||
k = MIN(k, 309u);
|
||||
double r = k < ELEMENTSOF(table) ? table[k] : table[ELEMENTSOF(table) - 1];
|
||||
for (unsigned i = ELEMENTSOF(table) - 1; i < k; i++)
|
||||
r *= 10.0;
|
||||
|
||||
return negative ? 1.0 / r : r;
|
||||
}
|
||||
@@ -12,3 +12,8 @@
|
||||
/* To avoid x == y and triggering compile warning -Wfloat-equal. This returns false if one of the argument is
|
||||
* NaN or infinity. One of the argument must be a floating point. */
|
||||
#define fp_equal(x, y) iszero_safe((x) - (y))
|
||||
|
||||
/* 10^n. Exact for |n| ≤ 22; otherwise multiplies and may accumulate rounding error. Saturates to
|
||||
* 0.0 or +Inf outside binary64's exponent range; large |n| is capped internally so untrusted
|
||||
* inputs can't cause unbounded work. */
|
||||
double xexp10i(int n);
|
||||
|
||||
@@ -64,6 +64,7 @@ basic_sources = files(
|
||||
'log.c',
|
||||
'log-context.c',
|
||||
'login-util.c',
|
||||
'math-util.c',
|
||||
'memfd-util.c',
|
||||
'memory-util.c',
|
||||
'mempool.c',
|
||||
|
||||
Reference in New Issue
Block a user