diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-04-03 23:18:51 +0200 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2025-04-03 23:27:39 +0200 |
commit | afbfd0e78176d47c495a29c795fbd3690fa0ded3 (patch) | |
tree | 9dd644166d5326f54de3492a4048e041c9b0ad66 /asl/base/numeric.hpp | |
parent | 0776012d0942537b1ddfef13cd37f8bfb125f501 (diff) |
Add numeric libraryv0.3.0
Diffstat (limited to 'asl/base/numeric.hpp')
-rw-r--r-- | asl/base/numeric.hpp | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/asl/base/numeric.hpp b/asl/base/numeric.hpp new file mode 100644 index 0000000..bbd229d --- /dev/null +++ b/asl/base/numeric.hpp @@ -0,0 +1,43 @@ +// Copyright 2025 Steven Le Rouzic +// +// SPDX-License-Identifier: BSD-3-Clause + +#pragma once + +#include "asl/base/integers.hpp" +#include "asl/base/bit.hpp" +#include "asl/base/meta.hpp" + +namespace asl +{ + +template<is_integer T> +constexpr bool is_pow2(T x) +{ + using unsigned_type = select_t<is_unsigned_integer<T>, T, as_unsigned_integer<T>>; + return x > 0 && has_single_bit(static_cast<unsigned_type>(x)); +} + +template<typename T> +concept is_numeric = is_integer<T> || is_floating_point<T>; + +template<is_numeric T> +constexpr T min(T a, T b) +{ + return (a <= b) ? a : b; +} + +template<is_numeric T> +constexpr T max(T a, T b) +{ + return (a >= b) ? a : b; +} + +template<is_numeric T> +constexpr T clamp(T x, T a, T b) +{ + return min(max(x, a), b); +} + +} // namespace asl + |