From 1f44e74465d2c41fbbb11dfff8f2a22564215793 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 22 Apr 2021 13:44:49 +0200 Subject: [PATCH] Test if conversion is using the sse when possible --- include/duckhandy/implem/int_conv.hpp | 7 ++- test/unit/int_conv_test.cpp | 66 ++++++++++++++------------- 2 files changed, 40 insertions(+), 33 deletions(-) diff --git a/include/duckhandy/implem/int_conv.hpp b/include/duckhandy/implem/int_conv.hpp index 9d941c6..7285b57 100644 --- a/include/duckhandy/implem/int_conv.hpp +++ b/include/duckhandy/implem/int_conv.hpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -176,6 +177,8 @@ namespace dhandy { template struct AryConversion { + constexpr static const bool is_sse = false; + template constexpr static I from_ary (const C* beg, const C* end) { I retval = 0; @@ -196,12 +199,14 @@ namespace dhandy { }; template - struct AryConversion::value and not std::is_same::value and sizeof(I) <= sizeof(uint32_t)>::type> { + struct AryConversion::value and not std::is_same::value and sizeof(I) <= sizeof(std::uint32_t)>::type> { + constexpr static const bool is_sse = true; template static I from_ary (C* beg, C* end) { return to_integer_sse(beg, end - beg); } }; template struct AryConversion { + constexpr static const bool is_sse = false; template constexpr static bool from_ary (C* beg, C* end) { if (end == beg) return false; diff --git a/test/unit/int_conv_test.cpp b/test/unit/int_conv_test.cpp index eed46a1..7d1055b 100644 --- a/test/unit/int_conv_test.cpp +++ b/test/unit/int_conv_test.cpp @@ -30,14 +30,16 @@ template using int_info_2 = dhandy::implem::int_info; namespace { template - void AryConversionTestHelper (const std::string_view& s, T expected) { + void AryConversionTestHelper (const std::string_view& s, T expected, bool expect_sse) { using AryConversion = dhandy::implem::AryConversion, false>; + CHECK(AryConversion::is_sse == expect_sse); CHECK(AryConversion::from_ary(s.data(), s.data() + s.size()) == expected); } template - void AryConversionTestHelperUp (const std::string_view& s, T expected) { + void AryConversionTestHelperUp (const std::string_view& s, T expected, bool expect_sse) { using AryConversion = dhandy::implem::AryConversion, false>; + CHECK(AryConversion::is_sse == expect_sse); CHECK(AryConversion::from_ary(s.data(), s.data() + s.size()) == expected); } } //unnamed namespace @@ -137,35 +139,35 @@ TEST_CASE ("Check int to char array conversions", "[s2i][int_conv]") { } TEST_CASE ("Check char array to int conversions", "[i2s][int_conv]") { - AryConversionTestHelper("0", 0); - AryConversionTestHelper("0", 0); - AryConversionTestHelper("0", 0); - AryConversionTestHelper("ff", 0xff); - AryConversionTestHelper("ff", 0xff); - AryConversionTestHelper("rs", 1000); - AryConversionTestHelper("20", 16); - AryConversionTestHelper("1", 1); - AryConversionTestHelper("10", 10); - AryConversionTestHelper("100", 100); - AryConversionTestHelper("999", 999); - AryConversionTestHelper("1000", 1000); - AryConversionTestHelper("1001", 1001); - AryConversionTestHelper("12345", 12345); - AryConversionTestHelper("123456", 123456); - AryConversionTestHelper("1234567", 1234567); - AryConversionTestHelper("12345678", 12345678); - AryConversionTestHelper("123456789", 123456789); - AryConversionTestHelper("2147483647", 2147483647); - AryConversionTestHelper("ffffffffffffffff", 0xffffffffffffffff); - AryConversionTestHelper("ffffffffffffffff", 0xffffffffffffffff); - AryConversionTestHelper("7fffffffffffffff", 0x7fffffffffffffff); - AryConversionTestHelper("7fffffff", 0x7fffffff); - AryConversionTestHelper("1", true); - AryConversionTestHelper("0", false); - AryConversionTestHelperUp("ABCDEF123456ABCD", 0xabcdef123456abcd); + AryConversionTestHelper("0", 0, true); + AryConversionTestHelper("0", 0, true); + AryConversionTestHelper("0", 0, true); + AryConversionTestHelper("ff", 0xff, true); + AryConversionTestHelper("ff", 0xff, true); + AryConversionTestHelper("rs", 1000, true); + AryConversionTestHelper("20", 16, true); + AryConversionTestHelper("1", 1, true); + AryConversionTestHelper("10", 10, true); + AryConversionTestHelper("100", 100, true); + AryConversionTestHelper("999", 999, true); + AryConversionTestHelper("1000", 1000, true); + AryConversionTestHelper("1001", 1001, true); + AryConversionTestHelper("12345", 12345, true); + AryConversionTestHelper("123456", 123456, true); + AryConversionTestHelper("1234567", 1234567, true); + AryConversionTestHelper("12345678", 12345678, true); + AryConversionTestHelper("123456789", 123456789, true); + AryConversionTestHelper("2147483647", 2147483647, true); + AryConversionTestHelper("ffffffffffffffff", 0xffffffffffffffff, false); + AryConversionTestHelper("ffffffffffffffff", 0xffffffffffffffff, false); + AryConversionTestHelper("7fffffffffffffff", 0x7fffffffffffffff, false); + AryConversionTestHelper("7fffffff", 0x7fffffff, true); + AryConversionTestHelper("1", true, false); + AryConversionTestHelper("0", false, false); + AryConversionTestHelperUp("ABCDEF123456ABCD", 0xabcdef123456abcd, false); - AryConversionTestHelper("-1", -1); - AryConversionTestHelper("-50000", -50000); - AryConversionTestHelper("-1", -1); - AryConversionTestHelper("-510123123123", -510123123123); + AryConversionTestHelper("-1", -1, sizeof(signed int) <= sizeof(std::uint32_t)); + AryConversionTestHelper("-50000", -50000, sizeof(signed int) <= sizeof(std::uint32_t)); + AryConversionTestHelper("-1", -1, false); + AryConversionTestHelper("-510123123123", -510123123123, false); }