Skip to content

Instantly share code, notes, and snippets.

@microcai
Created May 30, 2014 08:26
Show Gist options
  • Save microcai/21de0de9a37926cb3aa1 to your computer and use it in GitHub Desktop.
Save microcai/21de0de9a37926cb3aa1 to your computer and use it in GitHub Desktop.
/*
* If you think you are smart than the compiler , then you can optimize this code.
* otherwise, just leave it untouched.
*
* the base idea this code is that, simplify the md5 digest Algorithm to calculate only 4byte.
* thus, only 4 basic md5 round is needed.
* after 4 basic md5 round, shift the result and xor itself.
* the the lower 16bits is the peerhash.
*
* have fun reading this crap. ^_^
*/
boost::uint16_t peerid_hash(boost::uint32_t peerid)
{
unsigned int a = 0x67452301;
{
a += ((((0xefcdab89)) & ((0x98badcfe))) | ((~(0xefcdab89)) & ((0x10325476)))) + (peerid)
+ (unsigned int) (3614090360);
a = (((a) << ((7))) | ((a) >> (32 - ((7)))));
a += (0xefcdab89);
}
{
a += ((((0xefcdab89)) & ((0x10325476))) | (((0x98badcfe)) & (~(0x10325476)))) + (peerid)
+ (unsigned int) (4129170786);
a = (((a) << ((5))) | ((a) >> (32 - ((5)))));
a += (0xefcdab89);
}
{
a += (((0xefcdab89)) ^ ((0x98badcfe)) ^ ((0x10325476))) + (peerid) + (unsigned int) (4294588738);
a = (((a) << ((4))) | ((a) >> (32 - ((4)))));
a += (0xefcdab89);
}
{
a += (((0x98badcfe)) ^ (((0xefcdab89)) | (~(0x10325476)))) + (peerid) + (unsigned int) (4096336452);
a = (((a) << ((6))) | ((a) >> (32 - ((6)))));
a += (0xefcdab89);
}
return (a ^ (a >> 16)) & 0xFFFF;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment