Skip to content

Commit 21bd132

Browse files
authored
Merge pull request #26 from golang/master
[pull] master from golang:master
2 parents 7c5a9fc + 2403e59 commit 21bd132

12 files changed

Lines changed: 234 additions & 108 deletions

src/go/build/deps_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ var depsRules = `
8888
internal/strconv,
8989
internal/trace/tracev2,
9090
math/bits,
91-
structs
91+
structs,
92+
simd/archsimd
9293
< internal/bytealg
9394
< internal/stringslite
9495
< internal/unsafeheader

src/internal/runtime/maps/memhash_386.s

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
#include "textflag.h"
66

77
// hash function using AES hardware instructions
8-
TEXT ·MemHash32(SB),NOSPLIT,$0-12
9-
CMPB ·UseAeshash(SB), $0
10-
JEQ noaes
8+
TEXT ·memHash32AES(SB),NOSPLIT,$0-12
119
MOVL p+0(FP), AX // ptr to data
1210
MOVL h+4(FP), X0 // seed
1311
PINSRD $1, (AX), X0 // data
@@ -16,12 +14,8 @@ TEXT ·MemHash32(SB),NOSPLIT,$0-12
1614
AESENC ·aeskeysched+32(SB), X0
1715
MOVL X0, ret+8(FP)
1816
RET
19-
noaes:
20-
JMP ·memHash32Fallback(SB)
2117

22-
TEXT ·MemHash64(SB),NOSPLIT,$0-12
23-
CMPB ·UseAeshash(SB), $0
24-
JEQ noaes
18+
TEXT ·memHash64AES(SB),NOSPLIT,$0-12
2519
MOVL p+0(FP), AX // ptr to data
2620
MOVQ (AX), X0 // data
2721
PINSRD $2, h+4(FP), X0 // seed
@@ -30,29 +24,19 @@ TEXT ·MemHash64(SB),NOSPLIT,$0-12
3024
AESENC ·aeskeysched+32(SB), X0
3125
MOVL X0, ret+8(FP)
3226
RET
33-
noaes:
34-
JMP ·memHash64Fallback(SB)
3527

36-
TEXT ·MemHash(SB),NOSPLIT,$0-16
37-
CMPB ·UseAeshash(SB), $0
38-
JEQ noaes
28+
TEXT ·memHashAES(SB),NOSPLIT,$0-16
3929
MOVL p+0(FP), AX // ptr to data
4030
MOVL s+8(FP), BX // size
4131
LEAL ret+12(FP), DX
4232
JMP ·aeshashbody<>(SB)
43-
noaes:
44-
JMP ·memHashFallback(SB)
4533

46-
TEXT ·StrHash(SB),NOSPLIT,$0-12
47-
CMPB ·UseAeshash(SB), $0
48-
JEQ noaes
34+
TEXT ·strHashAES(SB),NOSPLIT,$0-12
4935
MOVL p+0(FP), AX // ptr to string object
5036
MOVL 4(AX), BX // length of string
5137
MOVL (AX), AX // string data
5238
LEAL ret+8(FP), DX
5339
JMP ·aeshashbody<>(SB)
54-
noaes:
55-
JMP ·strHashFallback(SB)
5640

5741
// AX: data
5842
// BX: length

src/internal/runtime/maps/memhash_aes.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,32 @@ import (
1010
"unsafe"
1111
)
1212

13-
//go:noescape
14-
func MemHash(p unsafe.Pointer, h, s uintptr) uintptr
13+
const memHashAESImplemented = true
1514

16-
//go:noescape
17-
func MemHash32(p unsafe.Pointer, h uintptr) uintptr
15+
func MemHash(p unsafe.Pointer, h, s uintptr) uintptr {
16+
if UseAeshash {
17+
return memHashAES(p, h, s)
18+
}
19+
return memHashFallback(p, h, s)
20+
}
1821

19-
//go:noescape
20-
func MemHash64(p unsafe.Pointer, h uintptr) uintptr
22+
func MemHash32(p unsafe.Pointer, h uintptr) uintptr {
23+
if UseAeshash {
24+
return memHash32AES(p, h)
25+
}
26+
return memHash32Fallback(p, h)
27+
}
2128

22-
//go:noescape
23-
func StrHash(p unsafe.Pointer, h uintptr) uintptr
29+
func MemHash64(p unsafe.Pointer, h uintptr) uintptr {
30+
if UseAeshash {
31+
return memHash64AES(p, h)
32+
}
33+
return memHash64Fallback(p, h)
34+
}
35+
36+
func StrHash(p unsafe.Pointer, h uintptr) uintptr {
37+
if UseAeshash {
38+
return strHashAES(p, h)
39+
}
40+
return strHashFallback(p, h)
41+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2026 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build (amd64 && !goexperiment.simd) || arm64 || 386
6+
7+
package maps
8+
9+
import (
10+
"unsafe"
11+
)
12+
13+
const memHashUsesVAES = false
14+
15+
// stabs for assembly implementations
16+
//
17+
//go:noescape
18+
func memHashAES(p unsafe.Pointer, h, s uintptr) uintptr
19+
20+
//go:noescape
21+
func memHash32AES(p unsafe.Pointer, h uintptr) uintptr
22+
23+
//go:noescape
24+
func memHash64AES(p unsafe.Pointer, h uintptr) uintptr
25+
26+
//go:noescape
27+
func strHashAES(p unsafe.Pointer, h uintptr) uintptr
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2026 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build amd64 && goexperiment.simd
6+
7+
package maps
8+
9+
import (
10+
"simd/archsimd"
11+
"unsafe"
12+
)
13+
14+
const memHashUsesVAES = true
15+
16+
func memHash32AES(p unsafe.Pointer, seed uintptr) uintptr {
17+
var state archsimd.Uint64x2
18+
state = state.SetElem(0, uint64(seed)).SetElem(1, uint64(*(*uint32)(p)))
19+
20+
hash := state.
21+
AsUint8x16().
22+
AESEncryptOneRound(archsimd.LoadUint32x4((*[4]uint32)(unsafe.Pointer(&aeskeysched[0])))).
23+
AESEncryptOneRound(archsimd.LoadUint32x4((*[4]uint32)(unsafe.Pointer(&aeskeysched[16])))).
24+
AESEncryptOneRound(archsimd.LoadUint32x4((*[4]uint32)(unsafe.Pointer(&aeskeysched[32])))).
25+
AsUint64x2().
26+
GetElem(0)
27+
return uintptr(hash)
28+
}
29+
30+
func memHash64AES(p unsafe.Pointer, seed uintptr) uintptr {
31+
var state archsimd.Uint64x2
32+
state = state.SetElem(0, uint64(seed)).SetElem(1, *(*uint64)(p))
33+
34+
hash := state.
35+
AsUint8x16().
36+
AESEncryptOneRound(archsimd.LoadUint32x4((*[4]uint32)(unsafe.Pointer(&aeskeysched[0])))).
37+
AESEncryptOneRound(archsimd.LoadUint32x4((*[4]uint32)(unsafe.Pointer(&aeskeysched[16])))).
38+
AESEncryptOneRound(archsimd.LoadUint32x4((*[4]uint32)(unsafe.Pointer(&aeskeysched[32])))).
39+
AsUint64x2().
40+
GetElem(0)
41+
return uintptr(hash)
42+
}
43+
44+
// TODO: Both strHashAES and memHashAES use aeshashbody that is quite large.
45+
// So there is no point in rewriting them using simd intrinsics, since they won't be inlinable.
46+
// Maybe in future we can do it for better maitanability.
47+
//
48+
//go:noescape
49+
func memHashAES(p unsafe.Pointer, h, s uintptr) uintptr
50+
51+
//go:noescape
52+
func strHashAES(p unsafe.Pointer, h uintptr) uintptr

src/internal/runtime/maps/memhash_amd64.s

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,21 @@
44

55
#include "textflag.h"
66

7-
// func MemHash32(p unsafe.Pointer, h uintptr) uintptr
8-
// ABIInternal for performance.
9-
TEXT ·MemHash32<ABIInternal>(SB),NOSPLIT,$0-24
10-
// AX = ptr to data
11-
// BX = seed
12-
CMPB ·UseAeshash(SB), $0
13-
JEQ noaes
14-
MOVQ BX, X0 // X0 = seed
15-
PINSRD $2, (AX), X0 // data
16-
AESENC ·aeskeysched+0(SB), X0
17-
AESENC ·aeskeysched+16(SB), X0
18-
AESENC ·aeskeysched+32(SB), X0
19-
MOVQ X0, AX // return X0
20-
RET
21-
noaes:
22-
JMP ·memHash32Fallback<ABIInternal>(SB)
23-
24-
// func MemHash64(p unsafe.Pointer, h uintptr) uintptr
25-
// ABIInternal for performance.
26-
TEXT ·MemHash64<ABIInternal>(SB),NOSPLIT,$0-24
27-
// AX = ptr to data
28-
// BX = seed
29-
CMPB ·UseAeshash(SB), $0
30-
JEQ noaes
31-
MOVQ BX, X0 // X0 = seed
32-
PINSRQ $1, (AX), X0 // data
33-
AESENC ·aeskeysched+0(SB), X0
34-
AESENC ·aeskeysched+16(SB), X0
35-
AESENC ·aeskeysched+32(SB), X0
36-
MOVQ X0, AX // return X0
37-
RET
38-
noaes:
39-
JMP ·memHash64Fallback<ABIInternal>(SB)
40-
41-
// func MemHash(p unsafe.Pointer, h, s uintptr) uintptr
7+
// func memHashAES(p unsafe.Pointer, h, s uintptr) uintptr
428
// hash function using AES hardware instructions
43-
TEXT ·MemHash<ABIInternal>(SB),NOSPLIT,$0-32
9+
TEXT ·memHashAES<ABIInternal>(SB),NOSPLIT,$0-32
4410
// AX = ptr to data
4511
// BX = seed
4612
// CX = size
47-
CMPB ·UseAeshash(SB), $0
48-
JEQ noaes
4913
JMP ·aeshashbody<>(SB)
50-
noaes:
51-
JMP ·memHashFallback<ABIInternal>(SB)
5214

53-
// func strhash(p unsafe.Pointer, h uintptr) uintptr
54-
TEXT ·StrHash<ABIInternal>(SB),NOSPLIT,$0-24
15+
// func strhashAES(p unsafe.Pointer, h uintptr) uintptr
16+
TEXT ·strHashAES<ABIInternal>(SB),NOSPLIT,$0-24
5517
// AX = ptr to string struct
5618
// BX = seed
57-
CMPB ·UseAeshash(SB), $0
58-
JEQ noaes
5919
MOVQ 8(AX), CX // length of string
6020
MOVQ (AX), AX // string data
6121
JMP ·aeshashbody<>(SB)
62-
noaes:
63-
JMP ·strHashFallback<ABIInternal>(SB)
6422

6523
// AX: data
6624
// BX: hash seed

src/internal/runtime/maps/memhash_arm64.s

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
#include "textflag.h"
66

7-
// func MemHash32(p unsafe.Pointer, h uintptr) uintptr
8-
TEXT ·MemHash32<ABIInternal>(SB),NOSPLIT|NOFRAME,$0-24
9-
MOVB ·UseAeshash(SB), R10
10-
CBZ R10, noaes
7+
// func memHash32AES(p unsafe.Pointer, h uintptr) uintptr
8+
TEXT ·memHash32AES<ABIInternal>(SB),NOSPLIT|NOFRAME,$0-24
119
MOVD $·aeskeysched+0(SB), R3
1210

1311
VEOR V0.B16, V0.B16, V0.B16
@@ -23,13 +21,9 @@ TEXT ·MemHash32<ABIInternal>(SB),NOSPLIT|NOFRAME,$0-24
2321

2422
VMOV V0.D[0], R0
2523
RET
26-
noaes:
27-
B ·memHash32Fallback<ABIInternal>(SB)
2824

29-
// func MemHash64(p unsafe.Pointer, h uintptr) uintptr
30-
TEXT ·MemHash64<ABIInternal>(SB),NOSPLIT|NOFRAME,$0-24
31-
MOVB ·UseAeshash(SB), R10
32-
CBZ R10, noaes
25+
// func memHash64AES(p unsafe.Pointer, h uintptr) uintptr
26+
TEXT ·memHash64AES<ABIInternal>(SB),NOSPLIT|NOFRAME,$0-24
3327
MOVD $·aeskeysched+0(SB), R3
3428

3529
VEOR V0.B16, V0.B16, V0.B16
@@ -45,25 +39,15 @@ TEXT ·MemHash64<ABIInternal>(SB),NOSPLIT|NOFRAME,$0-24
4539

4640
VMOV V0.D[0], R0
4741
RET
48-
noaes:
49-
B ·memHash64Fallback<ABIInternal>(SB)
5042

51-
// func MemHash(p unsafe.Pointer, h, size uintptr) uintptr
52-
TEXT ·MemHash<ABIInternal>(SB),NOSPLIT|NOFRAME,$0-32
53-
MOVB ·UseAeshash(SB), R10
54-
CBZ R10, noaes
43+
// func memHashAES(p unsafe.Pointer, h, size uintptr) uintptr
44+
TEXT ·memHashAES<ABIInternal>(SB),NOSPLIT|NOFRAME,$0-32
5545
B ·aeshashbody<>(SB)
56-
noaes:
57-
B ·memHashFallback<ABIInternal>(SB)
5846

59-
// func StrHash(p unsafe.Pointer, h uintptr) uintptr
60-
TEXT ·StrHash<ABIInternal>(SB),NOSPLIT|NOFRAME,$0-24
61-
MOVB ·UseAeshash(SB), R10
62-
CBZ R10, noaes
47+
// func strHashAES(p unsafe.Pointer, h uintptr) uintptr
48+
TEXT ·strHashAES<ABIInternal>(SB),NOSPLIT|NOFRAME,$0-24
6349
LDP (R0), (R0, R2) // string data / length
6450
B ·aeshashbody<>(SB)
65-
noaes:
66-
B ·strHashFallback<ABIInternal>(SB)
6751

6852
// R0: data
6953
// R1: seed data

src/internal/runtime/maps/memhash_noaes.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ import (
1111
)
1212

1313
// AES hashing not implemented for these architectures
14+
const memHashAESImplemented = false
15+
const memHashUsesVAES = false
16+
17+
func memHash32AES(p unsafe.Pointer, h uintptr) uintptr {
18+
panic("memHash32AES not implemented")
19+
}
20+
21+
func memHash64AES(p unsafe.Pointer, h uintptr) uintptr {
22+
panic("memHash64AES not implemented")
23+
}
24+
25+
func strHashAES(p unsafe.Pointer, h uintptr) uintptr {
26+
panic("strHashAES not implemented")
27+
}
28+
1429
func MemHash(p unsafe.Pointer, h, s uintptr) uintptr {
1530
return memHashFallback(p, h, s)
1631
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2026 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build !goexperiment.simd
6+
7+
#include "textflag.h"
8+
9+
// func memHash32AES(p unsafe.Pointer, h uintptr) uintptr
10+
// ABIInternal for performance.
11+
TEXT ·memHash32AES<ABIInternal>(SB),NOSPLIT,$0-24
12+
// AX = ptr to data
13+
// BX = seed
14+
MOVQ BX, X0 // X0 = seed
15+
PINSRD $2, (AX), X0 // data
16+
AESENC ·aeskeysched+0(SB), X0
17+
AESENC ·aeskeysched+16(SB), X0
18+
AESENC ·aeskeysched+32(SB), X0
19+
MOVQ X0, AX // return X0
20+
RET
21+
22+
// func memHash64AES(p unsafe.Pointer, h uintptr) uintptr
23+
// ABIInternal for performance.
24+
TEXT ·memHash64AES<ABIInternal>(SB),NOSPLIT,$0-24
25+
// AX = ptr to data
26+
// BX = seed
27+
MOVQ BX, X0 // X0 = seed
28+
PINSRQ $1, (AX), X0 // data
29+
AESENC ·aeskeysched+0(SB), X0
30+
AESENC ·aeskeysched+16(SB), X0
31+
AESENC ·aeskeysched+32(SB), X0
32+
MOVQ X0, AX // return X0
33+
RET

0 commit comments

Comments
 (0)