[Hash function Ipv4]

Poul-Henning Kamp phk at phk.freebsd.dk
Sat Jun 2 19:01:10 UTC 2012


In message <DUB116-W41324220EB6A5F7FD8B06A97090 at phx.gbl>, enrico d'urso writes:
>

>I'm looking for an Hash function for Ipv4 addresses.
>
>What are good ones?

They are generally very hard to hash well, for all sorts of reasons
related to how we use them.

One way that used to work reasonably well for me:

	uint32_t ipv4, h;

	h = ipv4 % HASH;
	ipv4 /= HASH;
	h ^= ipv4 % HASH;
	h ^= ipv4 / HASH;

Where HASH was a prime number near to 2^11

However, I cannot rule out that the good results I saw was a result
if RIPE's allocation policy at the time.


-- 
Poul-Henning Kamp       | UNIX since Zilog Zeus 3.20
phk at FreeBSD.ORG         | TCP/IP since RFC 956
FreeBSD committer       | BSD since 4.3-tahoe    
Never attribute to malice what can adequately be explained by incompetence.


More information about the freebsd-hackers mailing list