Skip to content

Commit 685ed1c

Browse files
Fix hashing on big-endian systems
Co-authored-by: Sarevalak <[email protected]>
1 parent 55a408e commit 685ed1c

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

murmur.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3939
package bloom
4040

4141
import (
42+
"encoding/binary"
4243
"math/bits"
4344
"unsafe"
4445
)
@@ -59,8 +60,8 @@ type digest128 struct {
5960
func (d *digest128) bmix(p []byte) {
6061
nblocks := len(p) / block_size
6162
for i := 0; i < nblocks; i++ {
62-
t := (*[2]uint64)(unsafe.Pointer(&p[i*block_size]))
63-
k1, k2 := t[0], t[1]
63+
b := (*[16]byte)(unsafe.Pointer(&p[i*block_size]))
64+
k1, k2 := binary.LittleEndian.Uint64(b[:8]), binary.LittleEndian.Uint64(b[8:])
6465
d.bmix_words(k1, k2)
6566
}
6667
}
@@ -269,8 +270,8 @@ func (d *digest128) sum256(data []byte) (hash1, hash2, hash3, hash4 uint64) {
269270
// we do not want to append to an actual array!!!
270271
if tail_length+1 == block_size {
271272
// We are left with no tail!!!
272-
word1 := *(*uint64)(unsafe.Pointer(&tail[0]))
273-
word2 := uint64(*(*uint32)(unsafe.Pointer(&tail[8])))
273+
word1 := binary.LittleEndian.Uint64(tail[:8])
274+
word2 := uint64(binary.LittleEndian.Uint32(tail[8 : 8+4]))
274275
word2 = word2 | (uint64(tail[12]) << 32) | (uint64(tail[13]) << 40) | (uint64(tail[14]) << 48)
275276
// We append 1.
276277
word2 = word2 | (uint64(1) << 56)

0 commit comments

Comments
 (0)