tuyau

Client/server for transfering files (like cp)
git clone https://noulin.net/git/tuyau.git
Log | Files | Refs | README

hashfunctions.c (995B)


      1 
      2 #include "libsheepyObject.h"
      3 #include "hashfunctions.h"
      4 
      5 /**
      6  * FNV 1-a string hash.
      7  */
      8 u32 strHashFNV1A(char *k) {
      9   u32 hash = 2166136261U;
     10   for (const u8* ptr = (u8*)k; *ptr; ptr++) {
     11     hash = (hash ^ *ptr) * 16777619U;
     12   }
     13   return hash;
     14 }
     15 
     16 /**
     17  * Integer reversible hash function for 32 bits.
     18  * Implementation of the Robert Jenkins "4-byte Integer Hashing",
     19  * from http://burtleburtle.net/bob/hash/integer.html
     20  */
     21 u32 u32Hash(u32 k) {
     22   k -= k << 6;
     23   k ^= k >> 17;
     24   k -= k << 9;
     25   k ^= k << 4;
     26   k -= k << 3;
     27   k ^= k << 10;
     28   k ^= k >> 15;
     29   return k;
     30 }
     31 
     32 /**
     33  * Integer reversible hash function for 64 bits.
     34  * Implementation of the Thomas Wang "Integer Hash Function",
     35  * from http://web.archive.org/web/20071223173210/http://www.concentric.net/~Ttwang/tech/inthash.htm
     36  */
     37 u64 u64Hash(u64 k) {
     38   k = ~k + (k << 21);
     39   k = k ^ (k >> 24);
     40   k = k + (k << 3) + (k << 8);
     41   k = k ^ (k >> 14);
     42   k = k + (k << 2) + (k << 4);
     43   k = k ^ (k >> 28);
     44   k = k + (k << 31);
     45   return k;
     46 }
     47