diff --git a/ostd/limits.hh b/ostd/limits.hh new file mode 100644 index 0000000..f11481b --- /dev/null +++ b/ostd/limits.hh @@ -0,0 +1,103 @@ +/* Type limits for OctaSTD. + * + * This file is part of OctaSTD. See COPYING.md for futher information. + */ + +#ifndef OSTD_LIMITS_HH +#define OSTD_LIMITS_HH + +#include +#include + +#include "ostd/types.hh" +#include "ostd/type_traits.hh" + +namespace ostd { + +namespace detail { + template, bool F = IsFloatingPoint> + struct NumericLimitsBase { + static constexpr T minv = T{}; + static constexpr T maxv = T{}; + static constexpr T lowv = T{}; + static constexpr bool is_signed = false; + static constexpr bool is_integer = false; + }; + + /* signed integer types */ + template> + struct IntegerLimitsBase { + using UT = MakeUnsigned; + static constexpr T minv = -(~(1ULL << (sizeof(T) * CHAR_BIT - 1))) - 1; + static constexpr T maxv = T(UT(~UT(0)) >> 1); + static constexpr T lowv = minv; + + static constexpr bool is_signed = true; + static constexpr bool is_integer = true; + }; + + /* unsigned integer types */ + template + struct IntegerLimitsBase { + static constexpr T minv = T(0); + static constexpr T maxv = ~T(0); + static constexpr T lowv = minv; + + static constexpr bool is_signed = false; + static constexpr bool is_integer = true; + }; + + /* all integer types */ + template + struct NumericLimitsBase: IntegerLimitsBase {}; + + /* floating point types */ + template + struct FloatLimitsBase; + + template<> + struct FloatLimitsBase { + static constexpr float minv = FLT_MIN; + static constexpr float maxv = FLT_MAX; + static constexpr float lowv = -maxv; + }; + + template<> + struct FloatLimitsBase { + static constexpr double minv = DBL_MIN; + static constexpr double maxv = DBL_MAX; + static constexpr double lowv = -maxv; + }; + + template<> + struct FloatLimitsBase { + static constexpr long double minv = LDBL_MIN; + static constexpr long double maxv = LDBL_MAX; + static constexpr long double lowv = -maxv; + }; + + template + struct NumericLimitsBase: FloatLimitsBase { + static constexpr bool is_signed = true; + static constexpr bool is_integer = false; + }; +} + +template +static constexpr T NumericLimitMin = detail::NumericLimitsBase::minv; + +template +static constexpr T NumericLimitMax = detail::NumericLimitsBase::maxv; + +template +static constexpr T NumericLimitLowest = detail::NumericLimitsBase::lowv; + +template +static constexpr bool NumericLimitIsSigned = detail::NumericLimitsBase::is_signed; + +template +static constexpr bool NumericLimitIsInteger = detail::NumericLimitsBase::is_integer; + +} + +#endif diff --git a/ostd/string.hh b/ostd/string.hh index ba26a49..800446e 100644 --- a/ostd/string.hh +++ b/ostd/string.hh @@ -889,14 +889,15 @@ struct ToString { \ } \ }; -OSTD_TOSTR_NUM(sbyte, "%d") +OSTD_TOSTR_NUM(sbyte, "%hhd") +OSTD_TOSTR_NUM(short, "%hd") OSTD_TOSTR_NUM(int, "%d") -OSTD_TOSTR_NUM(int &, "%d") OSTD_TOSTR_NUM(long, "%ld") OSTD_TOSTR_NUM(float, "%f") OSTD_TOSTR_NUM(double, "%f") -OSTD_TOSTR_NUM(byte, "%u") +OSTD_TOSTR_NUM(byte, "%hhu") +OSTD_TOSTR_NUM(ushort, "%hu") OSTD_TOSTR_NUM(uint, "%u") OSTD_TOSTR_NUM(ulong, "%lu") OSTD_TOSTR_NUM(llong, "%lld")