-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstubs.go
53 lines (46 loc) · 1.55 KB
/
stubs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package hash
import (
"unsafe"
"sync/atomic"
)
// Should be a built-in for unsafe.Pointer?
//go:nosplit
func add(p unsafe.Pointer, x uintptr) unsafe.Pointer {
return unsafe.Pointer(uintptr(p) + x)
}
var fastrandState uint64
//go:nosplit
func fastrand() uint32 {
var s1, s0 uint32
for i := 0; i < 3; i++ {
state := atomic.LoadUint64(&fastrandState)
frand := *(*[]uint32)(noescape(unsafe.Pointer(&state)))
// Implement xorshift64+: 2 32-bit xorshift sequences added together.
// Shift triplet [17,7,16] was calculated as indicated in Marsaglia's
// Xorshift paper: https://www.jstatsoft.org/article/view/v008i14/xorshift.pdf
// This generator passes the SmallCrush suite, part of TestU01 framework:
// http://simul.iro.umontreal.ca/testu01/tu01.html
s1, s0 = frand[0], frand[1]
s1 ^= s1 << 17
s1 = s1 ^ s0 ^ s1>>7 ^ s0>>16
frand[0], frand[1] = s0, s1
newState := *(*uint64)(noescape(unsafe.Pointer(&frand)))
if atomic.CompareAndSwapUint64(&fastrandState, state, newState) {
break
}
}
return s0 + s1
}
// noescape hides a pointer from escape analysis. noescape is
// the identity function but escape analysis doesn't think the
// output depends on the input. noescape is inlined and currently
// compiles down to zero instructions.
// USE CAREFULLY!
//go:nosplit
func noescape(p unsafe.Pointer) unsafe.Pointer {
x := uintptr(p)
return unsafe.Pointer(x ^ 0) //nolint:staticcheck
}