Skip to content

Commit 9971cb5

Browse files
Decode is now 65% faster
1 parent 49c601c commit 9971cb5

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

numerics/hilbert/hilbert.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ func rotate(n, rx, ry int32, x, y int32) (int32, int32) {
5151
y = n - 1 - y
5252
}
5353

54-
t := x
55-
x = y
56-
y = t
54+
x, y = y, x
5755
}
5856
return x, y
5957
}
@@ -63,7 +61,7 @@ func rotate(n, rx, ry int32, x, y int32) (int32, int32) {
6361
func Encode(x, y int32) int64 {
6462
var rx, ry int32
6563
var d int64
66-
for s := int32(n >> 1); s > 0; s = s >> 1 {
64+
for s := int32(n >> 1); s > 0; s >>= 1 {
6765
rx = boolToInt(x&s > 0)
6866
ry = boolToInt(y&s > 0)
6967
d += int64(int64(s) * int64(s) * int64(((3 * rx) ^ ry)))
@@ -80,13 +78,13 @@ func Decode(h int64) (int32, int32) {
8078
var x, y int32
8179
t := h
8280

83-
for s := int64(1); s < int64(n); s *= 2 {
81+
for s := int64(1); s < int64(n); s <<= 1 {
8482
rx = 1 & (t / 2)
8583
ry = 1 & (t ^ rx)
8684
x, y = rotate(int32(s), int32(rx), int32(ry), x, y)
8785
x += int32(s * rx)
8886
y += int32(s * ry)
89-
t /= 4
87+
t >>= 2
9088
}
9189

9290
return x, y

0 commit comments

Comments
 (0)