Skip to content

Commit 5b37112

Browse files
Minor improvement to Encode and Decode now 71.7% faster
1 parent 9971cb5 commit 5b37112

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

numerics/hilbert/hilbert.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func Encode(x, y int32) int64 {
6464
for s := int32(n >> 1); s > 0; s >>= 1 {
6565
rx = boolToInt(x&s > 0)
6666
ry = boolToInt(y&s > 0)
67-
d += int64(int64(s) * int64(s) * int64(((3 * rx) ^ ry)))
67+
d += int64(int64(s) * int64(s) * int64(rx<<1|(rx^ry)))
6868
x, y = rotate(s, rx, ry, x, y)
6969
}
7070

@@ -79,7 +79,7 @@ func Decode(h int64) (int32, int32) {
7979
t := h
8080

8181
for s := int64(1); s < int64(n); s <<= 1 {
82-
rx = 1 & (t / 2)
82+
rx = 1 & (t >> 1)
8383
ry = 1 & (t ^ rx)
8484
x, y = rotate(int32(s), int32(rx), int32(ry), x, y)
8585
x += int32(s * rx)

0 commit comments

Comments
 (0)