From 79ca881bc1120a1ee038f4ef58fe23ffea602f47 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 13 Aug 2020 23:54:06 +0100 Subject: [PATCH] Warning fix This fixes ary_to_int() for negative values --- include/duckhandy/implem/int_conv.hpp | 16 ++++++++-------- test/unit/int_conv_test.cpp | 5 +++++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/include/duckhandy/implem/int_conv.hpp b/include/duckhandy/implem/int_conv.hpp index d7048ec..7d717be 100644 --- a/include/duckhandy/implem/int_conv.hpp +++ b/include/duckhandy/implem/int_conv.hpp @@ -166,6 +166,13 @@ namespace dhandy { } }; + template + [[gnu::always_inline,gnu::pure]] + inline T negated_ifn (T n, bool negate) { + //return static_cast(((static_cast(n) - (mask bitand 1)) xor mask) bitor ((mask bitand 1) << 31)); + return (negate ? -n : n); + } + template struct AryConversion { template @@ -183,7 +190,7 @@ namespace dhandy { retval += Tr::from_digit(beg[i]) * factor; factor *= Base; } - return retval; + return negated_ifn(retval, was_negative); } }; @@ -204,13 +211,6 @@ namespace dhandy { } }; - template - [[gnu::always_inline,gnu::pure]] - inline T negated_ifn (T n, bool negate) { - //return static_cast(((static_cast(n) - (mask bitand 1)) xor mask) bitor ((mask bitand 1) << 31)); - return (negate ? -n : n); - } - template [[gnu::pure]] T to_integer_sse (const C* s, std::size_t l) { diff --git a/test/unit/int_conv_test.cpp b/test/unit/int_conv_test.cpp index 9b947f3..26be172 100644 --- a/test/unit/int_conv_test.cpp +++ b/test/unit/int_conv_test.cpp @@ -155,4 +155,9 @@ TEST_CASE ("Check char array to int conversions", "[i2s][int_conv]") { AryConversionTestHelper("7fffffff", 0x7fffffff); AryConversionTestHelper("1", true); AryConversionTestHelper("0", false); + + AryConversionTestHelper("-1", -1); + AryConversionTestHelper("-50000", -50000); + AryConversionTestHelper("-1", -1); + AryConversionTestHelper("-510123123123", -510123123123); }