Debian Codegraph @

src/basic/math-util.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <math.h> /* IWYU pragma: export */
5
6 #include "basic-forward.h"
7
8 /* On some optimization level, iszero(x) is converted to (x == 0.0), and emits warning -Wfloat-equal.
9 * The argument must be a floating point, i.e. one of float, double, or long double. */
10 #define iszero_safe(x) (fpclassify(x) == FP_ZERO)
11
12 /* To avoid x == y and triggering compile warning -Wfloat-equal. This returns false if one of the argument is
13 * NaN or infinity. One of the argument must be a floating point.
14 *
15 * The volatile temporaries force a memory roundtrip, truncating any excess precision (e.g. x87's
16 * 80-bit register width for double arithmetic) down to the declared type. -fexcess-precision=standard
17 * doesn't fully cover this on x87 — a function return value carried in ST(0) can still arrive at the
18 * caller in 80-bit precision (see gcc PR#323), so a value that should compare equal to a
19 * same-magnitude literal picks up extra mantissa bits and doesn't. The memory store-and-reload is
20 * the one operation guaranteed to truncate. The temporaries inherit the type of the subtraction
21 * expression so the macro stays generic over float / double / long double rather than silently
22 * truncating wider arguments. */
23 #define fp_equal(x, y) \
24 ({ \
25 volatile __typeof__((x) - (y)) _fp_x = (x); \
26 volatile __typeof__((x) - (y)) _fp_y = (y); \
27 iszero_safe(_fp_x - _fp_y); \
28 })
29
30 /* 10^n. Exact for |n| ≤ 22; otherwise multiplies and may accumulate rounding error. Saturates to
31 * 0.0 or +Inf outside binary64's exponent range; large |n| is capped internally so untrusted
32 * inputs can't cause unbounded work. */
33 double xexp10i(int n);