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 | |
parent | 0776012d0942537b1ddfef13cd37f8bfb125f501 (diff) |
Add numeric libraryv0.3.0
-rw-r--r-- | asl/base/BUILD.bazel | 2 | ||||
-rw-r--r-- | asl/base/bit.hpp | 8 | ||||
-rw-r--r-- | asl/base/bit_tests.cpp | 9 | ||||
-rw-r--r-- | asl/base/float.hpp | 3 | ||||
-rw-r--r-- | asl/base/numeric.hpp | 43 | ||||
-rw-r--r-- | asl/base/numeric_tests.cpp | 16 | ||||
-rw-r--r-- | asl/base/utility.hpp | 12 | ||||
-rw-r--r-- | asl/containers/hash_set.hpp | 1 | ||||
-rw-r--r-- | asl/formatting/format_float.cpp | 1 |
9 files changed, 66 insertions, 29 deletions
diff --git a/asl/base/BUILD.bazel b/asl/base/BUILD.bazel index 9157ad0..7870ad9 100644 --- a/asl/base/BUILD.bazel +++ b/asl/base/BUILD.bazel @@ -18,6 +18,7 @@ cc_library( "functional.hpp", "integers.hpp", "meta.hpp", + "numeric.hpp", "utility.hpp", ], srcs = [ @@ -44,5 +45,6 @@ cc_library( "functional", "integers", "meta", + "numeric", "utility", ]] diff --git a/asl/base/bit.hpp b/asl/base/bit.hpp index f2e3f61..5056383 100644 --- a/asl/base/bit.hpp +++ b/asl/base/bit.hpp @@ -15,14 +15,6 @@ constexpr bool has_single_bit(is_unsigned_integer auto x) return x != 0 && ((x - 1) & x) == 0; } -// @Todo Move this to numeric library -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)); -} - constexpr int popcount(uint8_t v) { v = v - ((v >> 1) & 0x55); diff --git a/asl/base/bit_tests.cpp b/asl/base/bit_tests.cpp index 3b95c2f..48cddcd 100644 --- a/asl/base/bit_tests.cpp +++ b/asl/base/bit_tests.cpp @@ -17,15 +17,6 @@ ASL_TEST(has_single_bit) ASL_TEST_EXPECT(!asl::has_single_bit(341U)); } -ASL_TEST(is_pow2) -{ - ASL_TEST_EXPECT(asl::is_pow2(4)); - ASL_TEST_EXPECT(asl::is_pow2(65536)); - ASL_TEST_EXPECT(!asl::is_pow2(6)); - ASL_TEST_EXPECT(!asl::is_pow2(1978)); - ASL_TEST_EXPECT(!asl::is_pow2(0)); -} - ASL_TEST(popcount) // NOLINT(*-cognitive-complexity) { ASL_TEST_EXPECT(asl::popcount(uint8_t{0}) == 0); diff --git a/asl/base/float.hpp b/asl/base/float.hpp index bd5e3c4..89a9f06 100644 --- a/asl/base/float.hpp +++ b/asl/base/float.hpp @@ -6,6 +6,9 @@ #include "asl/base/meta.hpp" +using float32_t = float; +using float64_t = double; + namespace asl { 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 + diff --git a/asl/base/numeric_tests.cpp b/asl/base/numeric_tests.cpp new file mode 100644 index 0000000..cfbc1ac --- /dev/null +++ b/asl/base/numeric_tests.cpp @@ -0,0 +1,16 @@ +// Copyright 2025 Steven Le Rouzic +// +// SPDX-License-Identifier: BSD-3-Clause + +#include "asl/base/numeric.hpp" + +#include "asl/testing/testing.hpp" + +ASL_TEST(is_pow2) +{ + ASL_TEST_EXPECT(asl::is_pow2(4)); + ASL_TEST_EXPECT(asl::is_pow2(65536)); + ASL_TEST_EXPECT(!asl::is_pow2(6)); + ASL_TEST_EXPECT(!asl::is_pow2(1978)); + ASL_TEST_EXPECT(!asl::is_pow2(0)); +} diff --git a/asl/base/utility.hpp b/asl/base/utility.hpp index 77ccc6b..f371ab4 100644 --- a/asl/base/utility.hpp +++ b/asl/base/utility.hpp @@ -69,18 +69,6 @@ constexpr U bit_cast(T value) requires (sizeof(T) == sizeof(U)) return __builtin_bit_cast(U, value); } -template<typename T> -constexpr T min(T a, T b) -{ - return (a <= b) ? a : b; -} - -template<typename T> -constexpr T max(T a, T b) -{ - return (a >= b) ? a : b; -} - // NOLINTBEGIN(*-macro-parentheses) #define ASL_DELETE_COPY(T) \ T(const T&) = delete; \ diff --git a/asl/containers/hash_set.hpp b/asl/containers/hash_set.hpp index 809b402..42c4cb8 100644 --- a/asl/containers/hash_set.hpp +++ b/asl/containers/hash_set.hpp @@ -8,6 +8,7 @@ #include "asl/base/utility.hpp" #include "asl/base/meta.hpp" #include "asl/base/bit.hpp" +#include "asl/base/numeric.hpp" #include "asl/memory/allocator.hpp" #include "asl/memory/memory.hpp" #include "asl/types/maybe_uninit.hpp" diff --git a/asl/formatting/format_float.cpp b/asl/formatting/format_float.cpp index 3802ff9..1d0375a 100644 --- a/asl/formatting/format_float.cpp +++ b/asl/formatting/format_float.cpp @@ -4,6 +4,7 @@ #include "asl/formatting/format.hpp" #include "asl/base/float.hpp" +#include "asl/base/numeric.hpp" #define JKJ_STD_REPLACEMENT_NAMESPACE_DEFINED 0 #define JKJ_STATIC_DATA_SECTION_DEFINED 0 |