c++chashcryengine

How does this Hashing function from CryEngine work?


unsigned int HashString( const char *string ) {
    const char* p;
    unsigned hash = 40503;

    for ( p = string; *p != '\0'; ++p ) {
        hash += *p;
        hash += ( hash << 10 );
        hash ^= ( hash >> 6 );
    }
    hash += ( hash << 3 );
    hash ^= ( hash >> 11 );
    hash += ( hash << 15 );

    return hash;
}

Just wandering over their code. I've never seen a hashing function like this before though.

I'm not too seasoned when it comes to bitwise ops, I know how bit shifting and masking works, but only in a rudimentary scenario like checking if bits are set.

What exactly does this do?


Solution

  • Read here for a general overview, and go down for "One-at-a-Time hash" (by Jenkins), which coincides with this one.

    Also see this Wikipedia entry, mentioned in this answer.

    "how exactly is this a good hash?" Not exactly. Those shifts are a little arbitrary, justified mostly from some heuristics and empirical tests.