]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/hash.h
Use the standard fixed-size integer types directly
[libs/core.git] / source / core / hash.h
index 1eebcce5342b75064034b7f68e5b2c82909b7fe2..85ee5dd74c046a4cdbc3b793cfd07d22dca2132f 100644 (file)
@@ -1,22 +1,17 @@
 #ifndef MSP_CORE_HASH_H_
 #define MSP_CORE_HASH_H_
 
+#include <cstdint>
 #include <string>
 
 namespace Msp {
 
-#ifdef WIN32
-typedef __uint64 HashValue64;
-#else
-typedef unsigned long long HashValue64;
-#endif
-
 /**
 Computes a 32-bit Fowler-Noll-Vo (FNV-1a) hash.  The number of bits can be
 limited to less than 32, in which case XOR-folding is used to reduce the hash
 size.
 */
-unsigned hash32(const void *, unsigned, unsigned = 32);
+std::uint32_t hash32(const void *, unsigned, unsigned = 32);
 
 /**
 Convenience function to compute a 32-bit hash of a string.
@@ -28,14 +23,20 @@ inline unsigned hash32(const std::string &s, unsigned b = 32)
 Computes a 64-bit Fowler-Noll-Vo (FNV-1a) hash.  Note that even if bits is
 limited to 32 or less, this does not produce the same result as hash32.
 */
-HashValue64 hash64(const void *, unsigned, unsigned = 64);
+std::uint64_t hash64(const void *, unsigned, unsigned = 64);
 
 /**
 Convenience function to compute a 64-bit hash of a string.
 */
-inline HashValue64 hash64(const std::string &s, unsigned b = 64)
+inline std::uint64_t hash64(const std::string &s, unsigned b = 64)
 { return hash64(s.data(), s.size(), b); }
 
+inline std::uint32_t fold32(std::uint64_t hash)
+{ return hash^(hash>>32); }
+
+inline std::uint16_t fold16(std::uint64_t hash)
+{ return hash^(hash>>16)^(hash>>32)^(hash>>48); }
+
 } // namespace Msp
 
 #endif