Skip to content

Commit a4bbcd4

Browse files
zhangyunhao116randall77
authored andcommitted
net: use fastrand64 in randInt
Change-Id: If3d8391d81e8de869dbb3c857f0570817e8aa440 Reviewed-on: https://go-review.googlesource.com/c/go/+/400914 Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Wayne Zuo <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 01b8f5e commit a4bbcd4

File tree

2 files changed

+4
-7
lines changed

2 files changed

+4
-7
lines changed

src/net/dnsclient.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@ import (
1313
)
1414

1515
// provided by runtime
16-
func fastrand() uint32
16+
func fastrandu() uint
1717

1818
func randInt() int {
19-
x, y := fastrand(), fastrand() // 32-bit halves
20-
u := uint(x)<<31 ^ uint(int32(y)) // full uint, even on 64-bit systems; avoid 32-bit shift on 32-bit systems
21-
i := int(u >> 1) // clear sign bit, even on 32-bit systems
22-
return i
19+
return int(fastrandu() >> 1) // clear sign bit
2320
}
2421

2522
func randIntn(n int) int {

src/runtime/stubs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ func fastrandu() uint {
199199
//go:linkname sync_fastrandn sync.fastrandn
200200
func sync_fastrandn(n uint32) uint32 { return fastrandn(n) }
201201

202-
//go:linkname net_fastrand net.fastrand
203-
func net_fastrand() uint32 { return fastrand() }
202+
//go:linkname net_fastrandu net.fastrandu
203+
func net_fastrandu() uint { return fastrandu() }
204204

205205
//go:linkname os_fastrand os.fastrand
206206
func os_fastrand() uint32 { return fastrand() }

0 commit comments

Comments
 (0)