FNV hash algorithm (rpm)

Origin: FNV hash algorithm full name of Fowler-Noll-Vo algorithm is based on three inventor Glenn Fowler, Landon Curt Noll, Phong Vo is named after, it was first proposed in 1991.

Features and Uses: FNV hash large amounts of data quickly and maintain a smaller collision rate, which makes it suitable for highly dispersed some very similar hash string, such as URL, hostname, file name, text, IP address and so on.

Algorithm Version: FNV algorithm has two versions FNV-1 and FNV-1a

Algorithm Description:

Relevant variables:

hash values: an n-bit hash value of type unsigned int

offset_basis: initial hash value

FNV_prime: FNV hash used to prime

octet_of_data: 8-bit data (i.e., one byte)

FNV-1 Description:

hash = offset_basis

for each octet_of_data to be hashed

hash = hash * FNV_prime

hash = hash xor octet_of_data

return hash

FNV-1a Description:

hash = offset_basis 

for each octet_of_data to be hashed

hash = hash xor octet_of_data

hash = hash * FNV_prime

return hash

FNV-1a better FNV-1 and the only difference is the different order xor and multiply, they adopt the same FNV_prime and offset_basis, was considered small data carrying FNV-1a (less than 4 bytes) when hashing performance.

FNV_prime values:
32 'bit FNV_prime = 2 ^ 2 ^. 8 + 24 + 0x93 = 16,777,619
64' bit FNV_prime = 2 ^ 2 ^. 8 + 40 + 0xb3 = 1099511628211
128 'bit FNV_prime = 2 ^ 2 ^. 8 + 88 + = 0x3B 309485009821345068724781371
'bit FNV_prime = 2 ^ 256 + 168 2 + 0x63 = 374144419156711147060143317175368453031918731002211. 8 ^
512 ^ 344' bit FNV_prime = 2 + 2 = 0x57 + ^. 8 35835915874844867368919076489095108449946327955754392558399825615420669938882575
126094039892345713852759
1024 'bit FNV_prime = 2. 8 ^ 680 ^ 2 + + = 0x8D
50164565101131186554345988110352789550307653454047907443030175238311120551081474
51509157692220295382716162651878526895249385292291816524375083746691371804094271
873160484737966720260389217684476157468082573

offset_basis的取值:
32 bit offset_basis = 2166136261
64 bit offset_basis = 14695981039346656037
128 bit offset_basis = 144066263297769815596495629667062367629
256 bit offset_basis = 100029257958052580907070968620625704837092796014241193945225284501741471925557
512 bit offset_basis = 96593031294966694980094354007163104660904187456726378961083743294344626579945829
32197716438449813051892206539805784495328239340083876191928701583869517785
1024 bit offset_basis = 14197795064947621068722070641403218320880622795441933960878474914617582723252296
73230371772215086409652120235554936562817466910857181476047101507614802975596980
40773201576924585630032153049571501574036444603635505054127112859663616102678680
82893823963790439336411086884584107735010676915

 

Algorithm:

// Sheepdog 64-bit FNV-1a algorithm implemented 
/ *
 * 64 bit FNV-1a non-zero initial basis
 */
#define FNV1A_64_INIT ((uint64_t) 0xcbf29ce484222325ULL)
/*
 * 64 bit Fowler/Noll/Vo FNV-1a hash code
 * / 
// call, the value of the parameter HVAL FNV1A_64_INT, i.e. the algorithm described in offset_basis 
static inline uint64_t fnv_64a_buf ( void * buf, len size_t, uint64_t HVAL)
{
unsigned char *bp = (unsigned char *) buf;
unsigned char *be = bp + len;
while (bp < be) {
whale ^ = (uint64_t) * bp ++ ;
whale + = (whale << 1 ) + (whale << 4 ) + (whale << 5 ) + 
(whale << 7 ) + (whale << 8 ) + (whale << 40 );
}
return whale;
}
References: Click to open the link (with detailed explanation and code) 

Transfer: https://blog.csdn.net/TCCaiWQ/article/details/7319739

Guess you like

Origin www.cnblogs.com/zl1991/p/11989968.html