summaryrefslogtreecommitdiffhomepage
path: root/compat
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2024-03-05 14:12:30 +0100
committerStanislaw Halik <sthalik@misaki.pl>2024-03-05 14:12:30 +0100
commit6813b819c92815fe04bf208e8b188277af641b6e (patch)
treee187af8b42468a8429d95ff8e2c4193a0fa64846 /compat
parent6b7090ee5f0e1f154a8ae52d7e7119d4082892cb (diff)
add lightweight alternative to std:numeric_limits
Diffstat (limited to 'compat')
-rw-r--r--compat/limits.cpp31
-rw-r--r--compat/limits.hpp40
2 files changed, 70 insertions, 1 deletions
diff --git a/compat/limits.cpp b/compat/limits.cpp
new file mode 100644
index 00000000..9a1c6314
--- /dev/null
+++ b/compat/limits.cpp
@@ -0,0 +1,31 @@
+#include "limits.hpp"
+#include <climits>
+#include <cfloat>
+
+namespace floormat {
+
+namespace {
+
+static_assert(limits<int32_t>::min == -2147483648);
+static_assert(limits<int32_t>::max == 2147483647);
+static_assert(limits<float>::max == (1 << FLT_MANT_DIG) && limits<float>::min == (-1 << FLT_MANT_DIG));
+static_assert(limits<double>::max == (1LL << DBL_MANT_DIG) && limits<double>::min == (-1LL << DBL_MANT_DIG));
+static_assert(limits<uint64_t>::min == 0 && limits<uint64_t>::max == (uint64_t)-1);
+
+static_assert(limits<int8_t >::max == INT8_MAX);
+static_assert(limits<int16_t>::max == INT16_MAX);
+static_assert(limits<int32_t>::max == INT32_MAX);
+static_assert(limits<int64_t>::max == INT64_MAX);
+static_assert(limits<uint8_t >::max == UINT8_MAX);
+static_assert(limits<uint16_t>::max == UINT16_MAX);
+static_assert(limits<uint32_t>::max == UINT32_MAX);
+static_assert(limits<uint64_t>::max == UINT64_MAX);
+static_assert(limits<float>::max == 16777216.f);
+static_assert(limits<float>::min == -16777216.f);
+static_assert(limits<double>::max == 9007199254740992.);
+static_assert(limits<double>::min == -9007199254740992.);
+
+} // namespace
+
+
+} // namespace floormat
diff --git a/compat/limits.hpp b/compat/limits.hpp
index ec1b67a8..0de74afa 100644
--- a/compat/limits.hpp
+++ b/compat/limits.hpp
@@ -1,3 +1,41 @@
#pragma once
-// add things from numeric_limits for {u,}ints and floats/doubles/long doubles
+namespace floormat {
+
+template<typename T> struct limits;
+
+template<typename T>
+requires (std::is_signed_v<T> && std::is_integral_v<T>)
+struct limits<T>
+{
+ using Type = T;
+ static constexpr T min{-int64_t{1} << sizeof(T)*8-1}, max{-(min+1)};
+};
+
+template<typename T>
+requires (std::is_unsigned_v<T>)
+struct limits<T>
+{
+ using Type = T;
+ static_assert(std::is_integral_v<T>);
+ static constexpr T min{0}, max{T(-1)};
+};
+
+template<> struct limits<float>
+{
+ using Type = float;
+ static constexpr float max{1 << 24}, min{-max};
+ using integer_type = int32_t;
+};
+
+template<> struct limits<double>
+{
+ using Type = double;
+ static constexpr double max = double{uint64_t{1} << 53}, min{-max};
+ using integer_type = int64_t;
+};
+
+template<typename T> requires std::is_arithmetic_v<T> using int_max = typename limits<T>::max;;
+template<typename T> requires std::is_arithmetic_v<T> using int_min = typename limits<T>::min;;
+
+} // namespace floormat