github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/util/xxhash3/util.go (about) 1 // Copyright 2021 ByteDance Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package xxhash3 implements https://github.com/Cyan4973/xxHash/blob/dev/xxhash.h 16 package xxhash3 17 18 import ( 19 "math/bits" 20 "unsafe" 21 22 "golang.org/x/sys/cpu" 23 ) 24 25 var ( 26 avx2 = cpu.X86.HasAVX2 27 sse2 = cpu.X86.HasSSE2 28 hashfunc = [2]func(unsafe.Pointer, int) uint64{xxh3HashSmall, xxh3HashLarge} 29 hashfunc128 = [2]func(unsafe.Pointer, int) [2]uint64{xxh3HashSmall128, xxh3HashLarge128} 30 ) 31 32 type funcUnsafe int 33 34 const ( 35 hashSmall funcUnsafe = iota 36 hashLarge 37 ) 38 39 func mix(a, b uint64) uint64 { 40 hi, lo := bits.Mul64(a, b) 41 return hi ^ lo 42 } 43 func xxh3RRMXMX(h64 uint64, length uint64) uint64 { 44 h64 ^= bits.RotateLeft64(h64, 49) ^ bits.RotateLeft64(h64, 24) 45 h64 *= 0x9fb21c651e98df25 46 h64 ^= (h64 >> 35) + length 47 h64 *= 0x9fb21c651e98df25 48 h64 ^= (h64 >> 28) 49 return h64 50 } 51 52 func xxh64Avalanche(h64 uint64) uint64 { 53 h64 *= prime64_2 54 h64 ^= h64 >> 29 55 h64 *= prime64_3 56 h64 ^= h64 >> 32 57 return h64 58 } 59 60 func xxh3Avalanche(x uint64) uint64 { 61 x ^= x >> 37 62 x *= 0x165667919e3779f9 63 x ^= x >> 32 64 return x 65 }