summaryrefslogtreecommitdiffhomepage
path: root/compat/limits.hpp
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/limits.hpp
parent6b7090ee5f0e1f154a8ae52d7e7119d4082892cb (diff)
add lightweight alternative to std:numeric_limits
Diffstat (limited to 'compat/limits.hpp')
-rw-r--r--compat/limits.hpp40
1 files changed, 39 insertions, 1 deletions
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