AuroraRuntime/Include/auROXTL/auNumericLimits.hpp

107 lines
4.3 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: auNumericLimits.hpp
Date: 2022-2-19
Author: Reece
***/
#pragma once
template<typename T, T Min, T Max, T Epsilon, bool IsSigned = false, bool IsInteger = false>
struct _AuNonStdLimits
{
using Type = T;
static const bool is_signed = IsSigned;
static const bool is_integer = IsInteger;
static constexpr Type min()
{
return Min;
}
static constexpr Type max()
{
return Max;
}
static constexpr Type epsilon()
{
return Epsilon;
}
};
template<class T>
struct AuNumericLimits;
#define _AU_LIMITS_ADD(bits) \
\
template<> \
struct AuNumericLimits<AuUInt ## bits> : _AuNonStdLimits<AuUInt ## bits, 0, static_cast<AuUInt ## bits>(AuInt ## bits(-1)), 1, false, true> \
{ \
\
}; \
\
template<> \
struct AuNumericLimits<AuInt ## bits> : _AuNonStdLimits<AuInt ## bits, AuInt ## bits(AuUInt ## bits(0) - AuInt ## bits(AuConstPow<AuUInt ## bits>(2, bits - 1) - 1)) - 1, AuInt ## bits(AuConstPow<AuUInt ## bits>(2, bits - 1) - 1), 1, true, true> \
{ \
\
};
_AU_LIMITS_ADD(8)
_AU_LIMITS_ADD(16)
_AU_LIMITS_ADD(32)
_AU_LIMITS_ADD(64)
#undef _AU_LIMITS_ADD
#if defined(AURORA_COMPILER_MSVC)
template<>
struct AuNumericLimits<long> : AuNumericLimits<int>
{};
template<>
struct AuNumericLimits<unsigned long> : AuNumericLimits<unsigned int>
{};
#endif
// TODO: clang x86 and x64 will need different hacks for [unsigned] long long
template<>
struct AuNumericLimits<bool> : _AuNonStdLimits<bool, false, true, true>
{};
template<>
struct AuNumericLimits<char16_t> : AuNumericLimits<AuUInt16>
{};
template<>
struct AuNumericLimits<char32_t> : AuNumericLimits<AuUInt32>
{};
template<>
struct AuNumericLimits<float> : _AuNonStdLimits<float,
float(-340282346638528859811704183484516925440.0000000000000000),
float(340282346638528859811704183484516925440.0000000000000000),
float(0.0000000596)>
{};
template<>
struct AuNumericLimits<double> : _AuNonStdLimits<double,
double(0), // specifying these arguments, even below the limit, will crash msvcs frontend
double(1),
double(0.000000000000000111)>
{
static double max()
{
return 1.7976931348623157e+308;
}
static double min()
{
return -2.2250738585072014e-308;
}
};