X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fhash.cpp;fp=source%2Fcore%2Fhash.cpp;h=26e60592618e9e15847927e51e9dfec8626b6bd6;hb=cf20196055ed5074c448a16144c47b7e8a32c6a8;hp=0000000000000000000000000000000000000000;hpb=928694ff233bd4383f30b05bf6d2d37562b77a9a;p=libs%2Fcore.git diff --git a/source/core/hash.cpp b/source/core/hash.cpp new file mode 100644 index 0000000..26e6059 --- /dev/null +++ b/source/core/hash.cpp @@ -0,0 +1,49 @@ +#include +#include "hash.h" + +using namespace std; + +namespace Msp { + +/* +http://en.wikipedia.org/wiki/Fowler-Noll-Vo_hash_function +http://www.isthe.com/chongo/tech/comp/fnv/index.html +*/ + +unsigned hash32(const void *data, unsigned size, unsigned bits) +{ + if(bits==0 || bits>32) + throw invalid_argument("hash32"); + + static const unsigned prime = 16777619; + static const unsigned offset = 2166136261U; + + unsigned result = offset; + for(unsigned i=0; i(data)+i))*prime; + + if(bits<32) + result = (result>>bits ^ result) & ((1<64) + throw invalid_argument("hash64"); + + static const HashValue64 prime = 1099511628211ULL; + static const HashValue64 offset = 14695981039346656037ULL; + + HashValue64 result = offset; + for(unsigned i=0; i(data)+i))*prime; + + if(bits<64) + result = (result>>bits ^ result) & ((1ULL<