medfall

A super great game engine
Log | Files | Refs

fnv.h (709B)


      1 #pragma once
      2 
      3 #include "intrinsics.h"
      4 #include "array.h"
      5 
      6 u32 fnv1a32( const void * data, size_t n, u32 hash = UINT32_C( 2166136261 ) ) {
      7 	const u32 prime = U32( 16777619 );
      8 
      9 	const char * cdata = ( const char * ) data;
     10 	for( size_t i = 0; i < n; i++ ) {
     11 		hash = ( hash ^ cdata[ i ] ) * prime;
     12 	}
     13 	return hash;
     14 }
     15 
     16 forceinline u64 fnv1a( const void * data, size_t n ) {
     17 	const u64 basis = U64( 14695981039346656037 );
     18 	const u64 prime = U64( 1099511628211 );
     19 
     20 	const char * cdata = ( const char * ) data;
     21 	u64 hash = basis;
     22 	for( size_t i = 0; i < n; i++ ) {
     23 		hash = ( hash ^ cdata[ i ] ) * prime;
     24 	}
     25 	return hash;
     26 }
     27 
     28 forceinline u64 fnv1a( const array< u8 > a ) {
     29 	return fnv1a( a.ptr(), a.num_bytes() );
     30 }