From 7e39515dfef46a26d2af245a7d42a949efc09f80 Mon Sep 17 00:00:00 2001 From: David Sisson Date: Wed, 6 Dec 2017 11:01:19 -0800 Subject: [PATCH 1/2] Changed rotation to support 32 and 64-bit architectures as noted in issue #2060. --- runtime/Go/antlr/utils.go | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/runtime/Go/antlr/utils.go b/runtime/Go/antlr/utils.go index b9d328f10..52d16c759 100644 --- a/runtime/Go/antlr/utils.go +++ b/runtime/Go/antlr/utils.go @@ -353,6 +353,34 @@ func PrintArrayJavaStyle(sa []string) string { return buffer.String() } +// The following routines were lifted from bits.rotate* available in Go 1.9. + +const uintSize = 32 << (^uint(0) >> 32 & 1) // 32 or 64 + +// rotateLeft returns the value of x rotated left by (k mod UintSize) bits. +// To rotate x right by k bits, call RotateLeft(x, -k). +func rotateLeft(x uint, k int) uint { + if uintSize == 32 { + return uint(rotateLeft32(uint32(x), k)) + } + return uint(rotateLeft64(uint64(x), k)) +} + +// rotateLeft32 returns the value of x rotated left by (k mod 32) bits. +func rotateLeft32(x uint32, k int) uint32 { + const n = 32 + s := uint(k) & (n - 1) + return x<>(n-s) +} + +// rotateLeft64 returns the value of x rotated left by (k mod 64) bits. +func rotateLeft64(x uint64, k int) uint64 { + const n = 64 + s := uint(k) & (n - 1) + return x<>(n-s) +} + + // murmur hash const ( c1_32 uint = 0xCC9E2D51 @@ -367,11 +395,11 @@ func murmurInit(seed int) int { func murmurUpdate(h1 int, k1 int) int { var k1u uint k1u = uint(k1) * c1_32 - k1u = (k1u << 15) | (k1u >> 17) // rotl32(k1u, 15) + k1u = rotateLeft(k1u, 15) k1u *= c2_32 var h1u = uint(h1) ^ k1u - h1u = (h1u << 13) | (h1u >> 19) // rotl32(h1u, 13) + k1u = rotateLeft(k1u, 13) h1u = h1u*5 + 0xe6546b64 return int(h1u) } From 2d71d943863f13a48acdeb24740e23dddb611734 Mon Sep 17 00:00:00 2001 From: David Sisson Date: Wed, 6 Dec 2017 11:04:31 -0800 Subject: [PATCH 2/2] Updated formatting. --- runtime/Go/antlr/utils.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/runtime/Go/antlr/utils.go b/runtime/Go/antlr/utils.go index 52d16c759..bba2ffae7 100644 --- a/runtime/Go/antlr/utils.go +++ b/runtime/Go/antlr/utils.go @@ -360,24 +360,24 @@ const uintSize = 32 << (^uint(0) >> 32 & 1) // 32 or 64 // rotateLeft returns the value of x rotated left by (k mod UintSize) bits. // To rotate x right by k bits, call RotateLeft(x, -k). func rotateLeft(x uint, k int) uint { - if uintSize == 32 { - return uint(rotateLeft32(uint32(x), k)) - } - return uint(rotateLeft64(uint64(x), k)) + if uintSize == 32 { + return uint(rotateLeft32(uint32(x), k)) + } + return uint(rotateLeft64(uint64(x), k)) } // rotateLeft32 returns the value of x rotated left by (k mod 32) bits. func rotateLeft32(x uint32, k int) uint32 { - const n = 32 - s := uint(k) & (n - 1) - return x<>(n-s) + const n = 32 + s := uint(k) & (n - 1) + return x<>(n-s) } // rotateLeft64 returns the value of x rotated left by (k mod 64) bits. func rotateLeft64(x uint64, k int) uint64 { - const n = 64 - s := uint(k) & (n - 1) - return x<>(n-s) + const n = 64 + s := uint(k) & (n - 1) + return x<>(n-s) } @@ -395,11 +395,11 @@ func murmurInit(seed int) int { func murmurUpdate(h1 int, k1 int) int { var k1u uint k1u = uint(k1) * c1_32 - k1u = rotateLeft(k1u, 15) + k1u = rotateLeft(k1u, 15) k1u *= c2_32 var h1u = uint(h1) ^ k1u - k1u = rotateLeft(k1u, 13) + k1u = rotateLeft(k1u, 13) h1u = h1u*5 + 0xe6546b64 return int(h1u) }