forked from flyingmutant/rand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global_test.go
59 lines (50 loc) · 1.11 KB
/
global_test.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
54
55
56
57
58
59
// Copyright 2022 Gregory Petrosyan <gregory.petrosyan@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package rand_test
import (
"math/bits"
"sync/atomic"
"testing"
"github.com/kokizzu/rand"
)
const (
wyrandAdd = 0xa0761d6478bd642f
wyrandXor = 0xe7037ed1a0b428db
)
func wyrand64(state *uint64) uint64 {
s := *state + wyrandAdd
*state = s
hi, lo := bits.Mul64(s, s^wyrandXor)
return hi ^ lo
}
func wyrand64Atomic(state *uint64) uint64 {
s := atomic.AddUint64(state, wyrandAdd)
hi, lo := bits.Mul64(s, s^wyrandXor)
return hi ^ lo
}
func BenchmarkRand64(b *testing.B) {
var s uint64
for i := 0; i < b.N; i++ {
s = rand.Uint64()
}
sinkUint64 = s
}
func BenchmarkWyRand64(b *testing.B) {
var s uint64
var state uint64
for i := 0; i < b.N; i++ {
s = wyrand64(&state)
}
sinkUint64 = s
}
func BenchmarkWyRand64Atomic(b *testing.B) {
var s uint64
var state uint64
for i := 0; i < b.N; i++ {
s = wyrand64Atomic(&state)
}
sinkUint64 = s
}