Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
104 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// +build amd64,go1.6 | ||
|
||
#include "textflag.h" | ||
|
||
// func cpuid(eaxArg, ecxArg uint32) (eax, ebx, ecx, edx uint32) | ||
TEXT ·cpuid(SB), NOSPLIT, $0-24 | ||
MOVL eaxArg+0(FP), AX | ||
MOVL ecxArg+4(FP), CX | ||
CPUID | ||
MOVL AX, eax+8(FP) | ||
MOVL BX, ebx+12(FP) | ||
MOVL CX, ecx+16(FP) | ||
MOVL DX, edx+20(FP) | ||
RET | ||
|
||
// func EncodeInt(lat, lng float64) uint64 | ||
TEXT ·EncodeInt(SB), NOSPLIT, $0 | ||
CMPB ·useAsm(SB), $1 | ||
JNE fallback | ||
|
||
#define LATF X0 | ||
#define LATI R8 | ||
#define LNGF X1 | ||
#define LNGI R9 | ||
#define TEMP R10 | ||
#define GHSH R11 | ||
#define MASK BX | ||
|
||
MOVSD lat+0(FP), LATF | ||
MOVSD lng+8(FP), LNGF | ||
|
||
MOVQ $0x5555555555555555, MASK | ||
|
||
MULSD $(0.005555555555555556), LATF | ||
ADDSD $(1.5), LATF | ||
|
||
MULSD $(0.002777777777777778), LNGF | ||
ADDSD $(1.5), LNGF | ||
|
||
MOVQ LNGF, LNGI | ||
SHRQ $20, LNGI | ||
|
||
MOVQ LATF, LATI | ||
SHRQ $20, LATI | ||
PDEPQ MASK, LATI, GHSH | ||
|
||
PDEPQ MASK, LNGI, TEMP | ||
|
||
SHLQ $1, TEMP | ||
XORQ TEMP, GHSH | ||
|
||
MOVQ GHSH, ret+16(FP) | ||
RET | ||
|
||
fallback: | ||
JMP ·encodeInt(SB) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,19 @@ | ||
package geohash | ||
|
||
import "testing" | ||
import ( | ||
"testing" | ||
) | ||
|
||
func randomPoints(n int) [][2]float64 { | ||
var points [][2]float64 | ||
for i := 0; i < n; i++ { | ||
lat, lon := RandomPoint() | ||
points = append(points, [2]float64{lat, lon}) | ||
} | ||
return points | ||
} | ||
const lat, lng = 33.0, -72.0 | ||
|
||
func BenchmarkEncode(b *testing.B) { | ||
points := randomPoints(b.N) | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
Encode(points[i][0], points[i][1]) | ||
_ = Encode(lat, lng) | ||
} | ||
} | ||
|
||
func BenchmarkEncodeInt(b *testing.B) { | ||
points := randomPoints(b.N) | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
EncodeInt(points[i][0], points[i][1]) | ||
_ = EncodeInt(lat, lng) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// +build amd64,go1.6 | ||
|
||
package geohash | ||
|
||
// useAsm flag determines whether the assembly version of EncodeInt will be | ||
// used. By Default we fall back to encodeInt. | ||
var useAsm bool | ||
|
||
// cpuid executes the CPUID instruction to obtain processor identification and | ||
// feature information. | ||
func cpuid(eaxArg, ecxArg uint32) (eax, ebx, ecx, edx uint32) | ||
|
||
// hasBMI2 returns whether the CPU supports Bit Manipulation Instruction Set | ||
// 2. | ||
func hasBMI2() bool { | ||
_, ebx, _, _ := cpuid(7, 0) | ||
return ebx&(1<<8) != 0 | ||
} | ||
|
||
// init determines whether to use assembly version by performing CPU feature | ||
// check. | ||
func init() { | ||
useAsm = hasBMI2() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// +build !amd64 !go1.6 | ||
|
||
// Define NOSPLIT ourselves since "textflag.h" is missing in old Go versions. | ||
#define NOSPLIT 4 | ||
|
||
TEXT ·EncodeInt(SB), NOSPLIT, $0 | ||
JMP ·encodeInt(SB) |