From 549a58731f332e8056eb3b35f6110b92b3c25350 Mon Sep 17 00:00:00 2001 From: Charles Pigott Date: Tue, 20 Jul 2021 08:19:00 +0100 Subject: Codechange: Remove (unused) ability to specify min/max of OverflowSafeInt --- src/core/overflowsafe_type.hpp | 45 ++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 21 deletions(-) (limited to 'src/core') diff --git a/src/core/overflowsafe_type.hpp b/src/core/overflowsafe_type.hpp index 3f2948551..01400df0a 100644 --- a/src/core/overflowsafe_type.hpp +++ b/src/core/overflowsafe_type.hpp @@ -12,6 +12,8 @@ #include "math_func.hpp" +#include + #ifdef __has_builtin # if __has_builtin(__builtin_add_overflow) && __has_builtin(__builtin_sub_overflow) && __has_builtin(__builtin_mul_overflow) # define HAS_OVERFLOW_BUILTINS @@ -23,13 +25,14 @@ * you multiply the maximum value with 2, or add 2, or subtract something from * the minimum value, etc. * @param T the type these integers are stored with. - * @param T_MAX the maximum value for the integers. - * @param T_MIN the minimum value for the integers. */ -template +template class OverflowSafeInt { private: + static constexpr T T_MAX = std::numeric_limits::max(); + static constexpr T T_MIN = std::numeric_limits::min(); + /** The non-overflow safe backend to store the value in. */ T m_value; public: @@ -175,31 +178,31 @@ public: }; /* Sometimes we got int64 operator OverflowSafeInt instead of vice versa. Handle that properly */ -template inline OverflowSafeInt operator + (int64 a, OverflowSafeInt b) { return b + a; } -template inline OverflowSafeInt operator - (int64 a, OverflowSafeInt b) { return -b + a; } -template inline OverflowSafeInt operator * (int64 a, OverflowSafeInt b) { return b * a; } -template inline OverflowSafeInt operator / (int64 a, OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } +template inline OverflowSafeInt operator + (int64 a, OverflowSafeInt b) { return b + a; } +template inline OverflowSafeInt operator - (int64 a, OverflowSafeInt b) { return -b + a; } +template inline OverflowSafeInt operator * (int64 a, OverflowSafeInt b) { return b * a; } +template inline OverflowSafeInt operator / (int64 a, OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } /* Sometimes we got int operator OverflowSafeInt instead of vice versa. Handle that properly */ -template inline OverflowSafeInt operator + (int a, OverflowSafeInt b) { return b + a; } -template inline OverflowSafeInt operator - (int a, OverflowSafeInt b) { return -b + a; } -template inline OverflowSafeInt operator * (int a, OverflowSafeInt b) { return b * a; } -template inline OverflowSafeInt operator / (int a, OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } +template inline OverflowSafeInt operator + (int a, OverflowSafeInt b) { return b + a; } +template inline OverflowSafeInt operator - (int a, OverflowSafeInt b) { return -b + a; } +template inline OverflowSafeInt operator * (int a, OverflowSafeInt b) { return b * a; } +template inline OverflowSafeInt operator / (int a, OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } /* Sometimes we got uint operator OverflowSafeInt instead of vice versa. Handle that properly */ -template inline OverflowSafeInt operator + (uint a, OverflowSafeInt b) { return b + a; } -template inline OverflowSafeInt operator - (uint a, OverflowSafeInt b) { return -b + a; } -template inline OverflowSafeInt operator * (uint a, OverflowSafeInt b) { return b * a; } -template inline OverflowSafeInt operator / (uint a, OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } +template inline OverflowSafeInt operator + (uint a, OverflowSafeInt b) { return b + a; } +template inline OverflowSafeInt operator - (uint a, OverflowSafeInt b) { return -b + a; } +template inline OverflowSafeInt operator * (uint a, OverflowSafeInt b) { return b * a; } +template inline OverflowSafeInt operator / (uint a, OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } /* Sometimes we got byte operator OverflowSafeInt instead of vice versa. Handle that properly */ -template inline OverflowSafeInt operator + (byte a, OverflowSafeInt b) { return b + (uint)a; } -template inline OverflowSafeInt operator - (byte a, OverflowSafeInt b) { return -b + (uint)a; } -template inline OverflowSafeInt operator * (byte a, OverflowSafeInt b) { return b * (uint)a; } -template inline OverflowSafeInt operator / (byte a, OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } +template inline OverflowSafeInt operator + (byte a, OverflowSafeInt b) { return b + (uint)a; } +template inline OverflowSafeInt operator - (byte a, OverflowSafeInt b) { return -b + (uint)a; } +template inline OverflowSafeInt operator * (byte a, OverflowSafeInt b) { return b * (uint)a; } +template inline OverflowSafeInt operator / (byte a, OverflowSafeInt b) { return (OverflowSafeInt)a / (int)b; } -typedef OverflowSafeInt OverflowSafeInt64; -typedef OverflowSafeInt OverflowSafeInt32; +typedef OverflowSafeInt OverflowSafeInt64; +typedef OverflowSafeInt OverflowSafeInt32; #undef HAS_OVERFLOW_BUILTINS -- cgit v1.2.3-70-g09d2