From 06761ca562c2f9cd3e024e616f21907b5978c69f Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sat, 19 Aug 2023 12:27:57 +0100 Subject: [PATCH] #745 Assertion triggered in hash.h when compiling for MSP430 --- include/etl/hash.h | 65 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 15 deletions(-) diff --git a/include/etl/hash.h b/include/etl/hash.h index 8f0e79ff..214ec8c5 100644 --- a/include/etl/hash.h +++ b/include/etl/hash.h @@ -189,11 +189,18 @@ namespace etl template<> struct hash { - ETL_STATIC_ASSERT(sizeof(size_t) >= sizeof(wchar_t), "size_t smaller than type"); - size_t operator ()(wchar_t v) const { - return static_cast(v); + // If it's the same size as a size_t. + if ETL_IF_CONSTEXPR(sizeof(size_t) >= sizeof(v)) + { + return static_cast(v); + } + else + { + uint8_t* p = reinterpret_cast(&v); + return private_hash::generic_hash(p, p + sizeof(v)); + } } }; @@ -204,11 +211,18 @@ namespace etl template<> struct hash { - ETL_STATIC_ASSERT(sizeof(size_t) >= sizeof(short), "size_t smaller than type"); - size_t operator ()(short v) const { - return static_cast(v); + // If it's the same size as a size_t. + if ETL_IF_CONSTEXPR(sizeof(size_t) >= sizeof(v)) + { + return static_cast(v); + } + else + { + uint8_t* p = reinterpret_cast(&v); + return private_hash::generic_hash(p, p + sizeof(v)); + } } }; @@ -219,11 +233,18 @@ namespace etl template<> struct hash { - ETL_STATIC_ASSERT(sizeof(size_t) >= sizeof(unsigned short), "size_t smaller than type"); - size_t operator ()(unsigned short v) const { - return static_cast(v); + // If it's the same size as a size_t. + if ETL_IF_CONSTEXPR(sizeof(size_t) >= sizeof(v)) + { + return static_cast(v); + } + else + { + uint8_t* p = reinterpret_cast(&v); + return private_hash::generic_hash(p, p + sizeof(v)); + } } }; @@ -234,11 +255,18 @@ namespace etl template<> struct hash { - ETL_STATIC_ASSERT(sizeof(size_t) >= sizeof(int), "size_t smaller than type"); - size_t operator ()(int v) const { - return static_cast(v); + // If it's the same size as a size_t. + if ETL_IF_CONSTEXPR(sizeof(size_t) >= sizeof(v)) + { + return static_cast(v); + } + else + { + uint8_t* p = reinterpret_cast(&v); + return private_hash::generic_hash(p, p + sizeof(v)); + } } }; @@ -249,11 +277,18 @@ namespace etl template<> struct hash { - ETL_STATIC_ASSERT(sizeof(size_t) >= sizeof(unsigned int), "size_t smaller than type"); - size_t operator ()(unsigned int v) const { - return static_cast(v); + // If it's the same size as a size_t. + if ETL_IF_CONSTEXPR(sizeof(size_t) >= sizeof(v)) + { + return static_cast(v); + } + else + { + uint8_t* p = reinterpret_cast(&v); + return private_hash::generic_hash(p, p + sizeof(v)); + } } };