From 3c54243ac7894d65b8f0ab69721a4d410f4348af Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 14 Feb 2024 22:34:42 +0200 Subject: [PATCH] BAN: Implement some numerical limits --- BAN/include/BAN/Limits.h | 71 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 BAN/include/BAN/Limits.h diff --git a/BAN/include/BAN/Limits.h b/BAN/include/BAN/Limits.h new file mode 100644 index 0000000000..cd0a393f8d --- /dev/null +++ b/BAN/include/BAN/Limits.h @@ -0,0 +1,71 @@ +#pragma once + +#include + +#include + +namespace BAN +{ + + template + class numeric_limits + { + public: + numeric_limits() = delete; + + static constexpr T max() requires(is_signed_v && is_integral_v) + { + if constexpr(sizeof(T) == sizeof(int8_t)) + return __INT8_MAX__; + if constexpr(sizeof(T) == sizeof(int16_t)) + return __INT16_MAX__; + if constexpr(sizeof(T) == sizeof(int32_t)) + return __INT32_MAX__; + if constexpr(sizeof(T) == sizeof(int64_t)) + return __INT64_MAX__; + } + + static constexpr T max() requires(is_unsigned_v && is_integral_v) + { + if constexpr(sizeof(T) == sizeof(uint8_t)) + return __UINT8_MAX__; + if constexpr(sizeof(T) == sizeof(uint16_t)) + return __UINT16_MAX__; + if constexpr(sizeof(T) == sizeof(uint32_t)) + return __UINT32_MAX__; + if constexpr(sizeof(T) == sizeof(uint64_t)) + return __UINT64_MAX__; + } + + static constexpr T max() requires(is_floating_point_v) + { + if constexpr(sizeof(T) == sizeof(float)) + return __FLT_MAX__; + if constexpr(sizeof(T) == sizeof(double)) + return __DBL_MAX__; + if constexpr(sizeof(T) == sizeof(long double)) + return __LDBL_MAX__; + } + + static constexpr T infinity() requires(is_floating_point_v) + { + if constexpr(sizeof(T) == sizeof(float)) + return __FLT_MAX__; + if constexpr(sizeof(T) == sizeof(double)) + return __DBL_MAX__; + if constexpr(sizeof(T) == sizeof(long double)) + return __LDBL_MAX__; + } + + static constexpr T nan() requires(is_floating_point_v) + { + if constexpr(sizeof(T) == sizeof(float)) + return __builtin_nanf(""); + if constexpr(sizeof(T) == sizeof(double)) + return __builtin_nan(""); + if constexpr(sizeof(T) == sizeof(long double)) + return __builtin_nanl(""); + } + }; + +}