Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
finite.hpp
Go to the documentation of this file.
1/**
2 * @file finite.hpp
3 * @brief Finiteness of a float, read from its bits, and a fallback for a value that is not finite
4 * @ingroup math
5 *
6 * The exponent's bits answer with an integer compare, with no FPU compare.
7 */
8
9#ifndef SBL_MATH_FINITE_HPP_
10#define SBL_MATH_FINITE_HPP_
11
12#include <cstdint>
13
15
16namespace sbl::math {
17
18/** @brief A single's exponent field: all ones in infinities and NaNs. */
19inline constexpr uint32_t FLOAT_EXPONENT_BITS = 0x7F800000u;
20
21/**
22 * @brief std::isfinite, read from the exponent's bits.
23 *
24 * @param x Any float
25 * @return false for an infinity or a NaN, true otherwise
26 */
27inline bool is_finite(float x) { return (magnitude_bits(x) & FLOAT_EXPONENT_BITS) != FLOAT_EXPONENT_BITS; }
28
29/**
30 * @brief x, or `fallback` when x is an infinity or a NaN.
31 *
32 * @param x Any float
33 * @param fallback Value returned for a non-finite x
34 * @return x or fallback
35 */
36inline float finite_or(float x, float fallback) { return is_finite(x) ? x : fallback; }
37
38} // namespace sbl::math
39
40#endif // SBL_MATH_FINITE_HPP_
A float's magnitude as its bit pattern, for compares without the FPU.
Domain-neutral math: constants, clamp, interpolation.
Definition clamp.hpp:13
constexpr uint32_t FLOAT_EXPONENT_BITS
A single's exponent field: all ones in infinities and NaNs.
Definition finite.hpp:19
float finite_or(float x, float fallback)
x, or fallback when x is an infinity or a NaN.
Definition finite.hpp:36
bool is_finite(float x)
std::isfinite, read from the exponent's bits.
Definition finite.hpp:27
uint32_t magnitude_bits(float x)
x's bits with the sign cleared.
Definition magnitude.hpp:29