github.com/moontrade/nogc@v0.1.7/hash/fnv.go (about)

     1  package hash
     2  
     3  /////////////////////////////////////////////////////////////////////////////////////////////////////////
     4  // fnv
     5  /////////////////////////////////////////////////////////////////////////////////////////////////////////
     6  
     7  func FNV32a(v uint32) uint32 {
     8  	const (
     9  		offset32 = uint32(2166136261)
    10  		prime32  = uint32(16777619)
    11  	)
    12  	hash := offset32
    13  	hash ^= uint32(byte(v))
    14  	hash *= prime32
    15  	hash ^= uint32(byte(v >> 8))
    16  	hash *= prime32
    17  	hash ^= uint32(byte(v >> 16))
    18  	hash *= prime32
    19  	hash ^= uint32(byte(v >> 24))
    20  	hash *= prime32
    21  	return hash
    22  }
    23  
    24  func FNV32(v uint32) uint32 {
    25  	const (
    26  		offset32 = uint32(2166136261)
    27  		prime32  = uint32(16777619)
    28  	)
    29  	hash := offset32
    30  	hash *= prime32
    31  	hash ^= uint32(byte(v))
    32  	hash *= prime32
    33  	hash ^= uint32(byte(v >> 8))
    34  	hash *= prime32
    35  	hash ^= uint32(byte(v >> 16))
    36  	hash *= prime32
    37  	hash ^= uint32(byte(v >> 24))
    38  	return hash
    39  }
    40  
    41  func FNV64a(v uint64) uint64 {
    42  	const (
    43  		offset64 = uint64(14695981039346656037)
    44  		prime64  = uint64(1099511628211)
    45  	)
    46  	hash := offset64
    47  	hash ^= uint64(byte(v))
    48  	hash *= prime64
    49  	hash ^= uint64(byte(v >> 8))
    50  	hash *= prime64
    51  	hash ^= uint64(byte(v >> 16))
    52  	hash *= prime64
    53  	hash ^= uint64(byte(v >> 24))
    54  	hash *= prime64
    55  	hash ^= uint64(byte(v >> 32))
    56  	hash *= prime64
    57  	hash ^= uint64(byte(v >> 40))
    58  	hash *= prime64
    59  	hash ^= uint64(byte(v >> 48))
    60  	hash *= prime64
    61  	hash ^= uint64(byte(v >> 56))
    62  	hash *= prime64
    63  	return hash
    64  }
    65  
    66  func FNV64(v uint64) uint64 {
    67  	const (
    68  		offset64 = uint64(14695981039346656037)
    69  		prime64  = uint64(1099511628211)
    70  	)
    71  	hash := offset64
    72  	hash *= prime64
    73  	hash ^= uint64(byte(v))
    74  	hash *= prime64
    75  	hash ^= uint64(byte(v >> 8))
    76  	hash *= prime64
    77  	hash ^= uint64(byte(v >> 16))
    78  	hash *= prime64
    79  	hash ^= uint64(byte(v >> 24))
    80  	hash *= prime64
    81  	hash ^= uint64(byte(v >> 32))
    82  	hash *= prime64
    83  	hash ^= uint64(byte(v >> 40))
    84  	hash *= prime64
    85  	hash ^= uint64(byte(v >> 48))
    86  	hash *= prime64
    87  	hash ^= uint64(byte(v >> 56))
    88  	return hash
    89  }