From 6813b819c92815fe04bf208e8b188277af641b6e Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Tue, 5 Mar 2024 14:12:30 +0100 Subject: add lightweight alternative to std:numeric_limits --- compat/limits.cpp | 31 +++++++++++++++++++++++++++++++ compat/limits.hpp | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 compat/limits.cpp 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 +#include + +namespace floormat { + +namespace { + +static_assert(limits::min == -2147483648); +static_assert(limits::max == 2147483647); +static_assert(limits::max == (1 << FLT_MANT_DIG) && limits::min == (-1 << FLT_MANT_DIG)); +static_assert(limits::max == (1LL << DBL_MANT_DIG) && limits::min == (-1LL << DBL_MANT_DIG)); +static_assert(limits::min == 0 && limits::max == (uint64_t)-1); + +static_assert(limits::max == INT8_MAX); +static_assert(limits::max == INT16_MAX); +static_assert(limits::max == INT32_MAX); +static_assert(limits::max == INT64_MAX); +static_assert(limits::max == UINT8_MAX); +static_assert(limits::max == UINT16_MAX); +static_assert(limits::max == UINT32_MAX); +static_assert(limits::max == UINT64_MAX); +static_assert(limits::max == 16777216.f); +static_assert(limits::min == -16777216.f); +static_assert(limits::max == 9007199254740992.); +static_assert(limits::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 struct limits; + +template +requires (std::is_signed_v && std::is_integral_v) +struct limits +{ + using Type = T; + static constexpr T min{-int64_t{1} << sizeof(T)*8-1}, max{-(min+1)}; +}; + +template +requires (std::is_unsigned_v) +struct limits +{ + using Type = T; + static_assert(std::is_integral_v); + static constexpr T min{0}, max{T(-1)}; +}; + +template<> struct limits +{ + using Type = float; + static constexpr float max{1 << 24}, min{-max}; + using integer_type = int32_t; +}; + +template<> struct limits +{ + using Type = double; + static constexpr double max = double{uint64_t{1} << 53}, min{-max}; + using integer_type = int64_t; +}; + +template requires std::is_arithmetic_v using int_max = typename limits::max;; +template requires std::is_arithmetic_v using int_min = typename limits::min;; + +} // namespace floormat -- cgit v1.2.3