From e780ba321897169ace90baa4b1d85190bc526065 Mon Sep 17 00:00:00 2001 From: Jonathan Bradley Date: Thu, 23 Jan 2025 13:55:37 -0500 Subject: pkmacros: add TypeSafeInt operators: *,/ --- pkmacros.h | 32 ++++++++++++++++++++++++++++++++ test/pkmacros.cpp | 18 ++++++++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/pkmacros.h b/pkmacros.h index eb8d6e7..583f6bc 100644 --- a/pkmacros.h +++ b/pkmacros.h @@ -62,6 +62,8 @@ constexpr TypeName TypeName_MAX = TypeName{TypeName_T_MAX}; \ TypeName operator+(const TypeName& a, const TypeName& b); \ TypeName operator-(const TypeName& a, const TypeName& b); \ + TypeName operator*(const TypeName& a, const TypeName& b); \ + TypeName operator/(const TypeName& a, const TypeName& b); \ TypeName operator&(const TypeName& a, const TypeName& b); \ TypeName operator|(const TypeName& a, const TypeName& b); \ TypeName operator^(const TypeName& a, const TypeName& b); \ @@ -73,6 +75,8 @@ TypeName operator>>(const TypeName& a, const TypeName& b); \ TypeName operator+=(TypeName& a, const TypeName& b); \ TypeName operator-=(TypeName& a, const TypeName& b); \ + TypeName operator*=(TypeName& a, const TypeName& b); \ + TypeName operator/=(TypeName& a, const TypeName& b); \ TypeName operator&=(TypeName& a, const TypeName& b); \ TypeName operator|=(TypeName& a, const TypeName& b); \ TypeName operator^=(TypeName& a, const TypeName& b); \ @@ -84,6 +88,12 @@ inline TypeName operator-(const TypeName& a, const TypeName& b) { \ return TypeName(static_cast(a) - static_cast(b)); \ } \ + inline TypeName operator*(const TypeName& a, const TypeName& b) { \ + return TypeName(static_cast(a) * static_cast(b)); \ + } \ + inline TypeName operator/(const TypeName& a, const TypeName& b) { \ + return TypeName(static_cast(a) / static_cast(b)); \ + } \ inline TypeName operator&(const TypeName& a, const TypeName& b) { \ return TypeName(static_cast(a) & static_cast(b)); \ } \ @@ -123,6 +133,14 @@ a = TypeName{a - b}; \ return a; \ }; \ + inline TypeName operator*=(TypeName& a, const TypeName& b) { \ + a = TypeName{a * b}; \ + return a; \ + }; \ + inline TypeName operator/=(TypeName& a, const TypeName& b) { \ + a = TypeName{a / b}; \ + return a; \ + }; \ inline TypeName operator&=(TypeName& a, const TypeName& b) { \ a = TypeName{a & b}; \ return a; \ @@ -155,6 +173,12 @@ constexpr TypeName operator-(const TypeName& a, const TypeName& b) { \ return TypeName(static_cast(a) - static_cast(b)); \ } \ + constexpr TypeName operator*(const TypeName& a, const TypeName& b) { \ + return TypeName(static_cast(a) * static_cast(b)); \ + } \ + constexpr TypeName operator/(const TypeName& a, const TypeName& b) { \ + return TypeName(static_cast(a) / static_cast(b)); \ + } \ constexpr TypeName operator&(const TypeName& a, const TypeName& b) { \ return TypeName(static_cast(a) & static_cast(b)); \ } \ @@ -194,6 +218,14 @@ a = TypeName{a - b}; \ return a; \ }; \ + constexpr TypeName operator*=(TypeName& a, const TypeName& b) { \ + a = TypeName{a * b}; \ + return a; \ + }; \ + constexpr TypeName operator/=(TypeName& a, const TypeName& b) { \ + a = TypeName{a / b}; \ + return a; \ + }; \ constexpr TypeName operator&=(TypeName& a, const TypeName& b) { \ a = TypeName{a & b}; \ return a; \ diff --git a/test/pkmacros.cpp b/test/pkmacros.cpp index 8edcbfd..bbba6f0 100644 --- a/test/pkmacros.cpp +++ b/test/pkmacros.cpp @@ -25,6 +25,8 @@ template bool tsi_test_operators() { + T mtsi01 {0x01}; + T mtsi10 {0x10}; T mtsi0F {0x0F}; T mtsiF0 {0xF0}; T r; @@ -38,6 +40,10 @@ tsi_test_operators() if (r == T{0}) return false; r = mtsi0F - mtsiF0; if (r == T{0}) return false; + r = mtsi0F * mtsiF0; + if (r == T{0}) return false; + r = mtsi10 / mtsi01; + if (r != T{0x10}) return false; r = mtsi0F & mtsiF0; if (r != T{0}) return false; r = mtsi0F | mtsiF0; @@ -60,6 +66,10 @@ tsi_test_operators() if (r == T{0}) return false; r = mtsi0F -= mtsiF0; if (r == T{0}) return false; + r = mtsi0F *= mtsi01; + if (r == T{0}) return false; + r = mtsi10 /= mtsi01; + if (r != T{0x10}) return false; r = mtsi0F &= mtsiF0; if (r != T{0}) return false; r = mtsi0F |= mtsiF0; @@ -84,9 +94,13 @@ tsi_test_operators_constexpr() constexpr T r02 = mtsi0F - T_T{static_cast(mtsiF0)}; if constexpr (r02 == mtsi00) return false; */ - constexpr T r03 = mtsi0F + mtsiF0; + constexpr T r01 = mtsi0F + mtsiF0; + if constexpr (r01 == mtsi00) return false; + constexpr T r02 = mtsi0F - mtsiF0; + if constexpr (r02 == mtsi00) return false; + constexpr T r03 = mtsi0F * mtsiF0; if constexpr (r03 == mtsi00) return false; - constexpr T r04 = mtsi0F - mtsiF0; + constexpr T r04 = mtsiF0 / mtsi0F; if constexpr (r04 == mtsi00) return false; constexpr T r05 = mtsi0F & mtsiF0; if constexpr (r05 != mtsi00) return false; -- cgit v1.2.3