Skip to content

Commit

Permalink
Merge pull request #2 from alipsidikp/fix-build-error-on-arm64
Browse files Browse the repository at this point in the history
fix build for different machine architecture
  • Loading branch information
kelindar committed Aug 3, 2022
2 parents fdccc6f + 140d83c commit 36a0585
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: "1.16"
go-version: "1.17"
- name: Check out code
uses: actions/checkout@v2
- name: Install dependencies
Expand Down
6 changes: 6 additions & 0 deletions next_amd64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build amd64

package xxrand

// https://stackoverflow.com/questions/27693145/rdtscp-versus-rdtsc-cpuid
func next() uint64
15 changes: 15 additions & 0 deletions next_generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build !amd64

package xxrand

import "sync/atomic"

// Next returns the next epoch for the random, when called without an explicit
// epoch provided. This one simply reads a time stamp counter if available and
// uses it as the epoch.
var epoch uint64

// genericNext provides a cross-platform implementation, but uses atomic counter instead.
func next() uint64 {
return atomic.AddUint64(&epoch, 1)
}
24 changes: 0 additions & 24 deletions rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ package xxrand

import (
"math/bits"
"runtime"
"sync/atomic"
)

// --------------------------------- 32-bit integers ---------------------------------
Expand Down Expand Up @@ -102,28 +100,6 @@ again: // Source: math/rand. Copyright 2009 The Go Authors.
}

// --------------------------------- Hashing ---------------------------------

// https://stackoverflow.com/questions/27693145/rdtscp-versus-rdtsc-cpuid
func x64tsc() uint64

// Next returns the next epoch for the random, when called without an explicit
// epoch provided. This one simply reads a time stamp counter if available and
// uses it as the epoch.
var next func() uint64
var epoch uint64

// genericNext provides a cross-platform implementation, but uses atomic counter instead.
func genericNext() uint64 {
return atomic.AddUint64(&epoch, 1)
}

func init() {
next = genericNext
if runtime.GOARCH == "amd64" {
next = x64tsc
}
}

// The unrolled xxhash that hashes the input uint32. It produces the exact same output
// as xxh3, which has a good overall distribution and a passing chi2 test.
func xxhash32(v, seed uint32) uint32 {
Expand Down
1 change: 0 additions & 1 deletion rand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func TestBinary(t *testing.T) {

func TestNext(t *testing.T) {
assert.Greater(t, int(next()), 0)
assert.Greater(t, int(genericNext()), 0)
}

func TestIntn(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions tsc_amd64.s
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "textflag.h"

// func x64tsc() uint64
TEXT ·x64tsc(SB),NOSPLIT,$0-8
// func next() uint64
TEXT ·next(SB),NOSPLIT,$0-8
MFENCE
LFENCE
RDTSCP
Expand Down

0 comments on commit 36a0585

Please sign in to comment.