Skip to content

Commit

Permalink
Improve hash function, fix the last few warnings in picomodel.
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Jan 28, 2017
1 parent cfc38e5 commit c744ece
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions libs/picomodel/picomodel.c
Expand Up @@ -1404,25 +1404,26 @@ int PicoGetHashTableSize( void )
#define HASH_NORMAL_EPSILON 0.02f
#endif

// djb2 Hash function taken from http://www.cse.yorku.ca/~oz/hash.html
unsigned int calculateHash(unsigned char* str)
// djb2 Hash function adapted from http://www.cse.yorku.ca/~oz/hash.html
unsigned int calculateHash(unsigned char* str, size_t len)
{
unsigned int hash = 5381;
int c;

while (c = *str++)
{
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
}
int c, i;

for (i = 0; i < len; ++i)
{
c = str[i];
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
}

return hash;
}

unsigned int PicoVertexCoordGenerateHash( picoVec3_t xyz )
{
unsigned int hash = 0;
// Allocate bytes for the 3 doubles plus the trailing 0
unsigned char buffer[sizeof(picoVec3_t) + 1];
// Allocate bytes for the 3 doubles
unsigned char buffer[sizeof(picoVec3_t)];

#ifndef HASH_USE_EPSILON
hash += ~(*((unsigned int*) &xyz[ 0 ]) << 15);
Expand All @@ -1442,9 +1443,8 @@ unsigned int PicoVertexCoordGenerateHash( picoVec3_t xyz )
/* greebo: Copy the memory occupied by the floor'ed doubles
to the unsigned char array and hash them */
memcpy(buffer, &xyz_epsilonspace[0], sizeof(picoVec3_t));
buffer[sizeof(picoVec3_t)] = '\0';

hash = calculateHash(buffer);
hash = calculateHash(buffer, sizeof(picoVec3_t));

/* greebo: This was the previous, compiler-warning-producing code
Expand Down Expand Up @@ -1547,8 +1547,15 @@ picoVertexCombinationHash_t *PicoFindVertexCombinationInHashTable( picoVertexCom
#endif

/* check color */
if( *((int*) vertexCombinationHash->vcd.color) != *((int*) color) )
if (vertexCombinationHash->vcd.color[0] != color[0] ||
vertexCombinationHash->vcd.color[1] != color[1] ||
vertexCombinationHash->vcd.color[2] != color[2] ||
vertexCombinationHash->vcd.color[3] != color[3])
/* greebo: Old code:
if( *((int*) vertexCombinationHash->vcd.color) != *((int*) color) )*/
{
continue;
}

/* gotcha */
return vertexCombinationHash;
Expand Down

0 comments on commit c744ece

Please sign in to comment.