If you don't mind the overflow then just silence the warning. Unsigned integer arithmetic is guaranteed to be modulo 2n arithmetic where n is the number of bits in the value representation, so this is well-defined no matter what. The warning is a sillywarning; it's warning you that you're using the main feature of unsigned integers.
I find that with a local #pragma warning( disable: 4307 ) for the function, the warning still appears for every use of the function.
This rewrite silences the warning for the 32-bit hash function:
constexpr auto hashFnv1a( char const* s, unsigned h = Fnv1aBasis )
-> unsigned
{
return !*s ? h : hashFnv1a(s + 1, static_cast<unsigned>( 1ULL*(h ^ *s) * Fnv1aPrime ));
}
Even extensive googling didn't find any way to disable the sillywarning about overflow of unsigned values while leaving it on for signed ones, so to deal with the 64-bit hash function it appears that the only recourse is to implement a constexpr 64-bit unsigned multiplication function. Since it's constexpr it doesn't matter if it's particularly efficient or not. So:
#include <stdint.h>
namespace b32 {
static constexpr uint32_t Fnv1aBasis = 0x811C9DC5u;
static constexpr uint32_t Fnv1aPrime = 0x01000193u;
constexpr auto hashFnv1a( char const* s, uint32_t h = Fnv1aBasis )
-> uint32_t
{ return !*s ? h : hashFnv1a(s + 1, static_cast<uint32_t>( 1ULL*(h ^ *s)*Fnv1aPrime )); }
} // namespace b32
namespace b64 {
static constexpr uint64_t Fnv1aBasis = 0xCBF29CE484222325uLL;
static constexpr uint64_t Fnv1aPrime = 0x100000001B3uLL;
constexpr auto lo( uint64_t x )
-> uint64_t
{ return x & uint32_t( -1 ); }
constexpr auto hi( uint64_t x )
-> uint64_t
{ return x >> 32; }
constexpr auto mulu64( uint64_t a, uint64_t b )
-> uint64_t
{
return 0
+ (lo( a )*lo( b ) & uint32_t(-1))
+ (
(
(
(
(
hi( lo( a )*lo( b ) ) +
lo( a )*hi( b )
)
& uint32_t(-1)
)
+ hi( a )*lo( b )
)
& uint32_t(-1)
)
<< 32
);
}
constexpr auto hashFnv1a( char const* s, uint64_t h = Fnv1aBasis )
-> uint64_t
{ return !*s ? h : hashFnv1a( s + 1, mulu64( h ^ *s, Fnv1aPrime ) ); }
} // namepace b64
#include <assert.h>
#include <iostream>
using namespace std;
auto main()
-> int
{
constexpr auto x = b64::mulu64( b64::Fnv1aBasis, b64::Fnv1aPrime );
#ifdef _MSC_VER
# pragma warning( push )
# pragma warning( disable: 4307 )
constexpr auto y = b64::Fnv1aBasis*b64::Fnv1aPrime;
# pragma warning( pop )
#else
constexpr auto y = b64::Fnv1aBasis*b64::Fnv1aPrime;
#endif
cout << x << endl;
cout << y << endl;
assert( x == y );
static constexpr const char* const s = "blah!";
constexpr unsigned xs = b32::hashFnv1a( s );
constexpr uint64_t ys = b64::hashFnv1a( s );
int a[1 + xs%2]; (void) a;
int b[1 + ys%2]; (void) b;
}
unsigned long long?/W2and/W4respectively.uint32_tin place ofint(unsigned intmight be 16 bits). Alsocharcould besignedorunsigned, and ifsignedcould be 1's complement.unsigned long longbut since it does a multiplication for each character in the string it'll only work if the string is short enough. It also wouldn't work if I actually wanted a 64-bit hash (unless I were to use some 128-bit integer). I would expect compile-time hashing of strings to be a quite common use-case for constexpr, but I can't see how it could ever work if overflows trigger warnings since so many hash algorithms relies on overflows.(h ^ (static_cast<unsigned int>(*s))