diff --git a/bits.go b/bits.go index f435b6d..f61013a 100644 --- a/bits.go +++ b/bits.go @@ -53,6 +53,3 @@ func hasZeroByte(x uint64) bitset { func castUint64(m *metadata) uint64 { return *(*uint64)((unsafe.Pointer)(m)) } - -//go:linkname fastrand runtime.fastrand -func fastrand() uint32 diff --git a/bits_amd64.go b/bits_amd64.go index 8b91f57..2d95b40 100644 --- a/bits_amd64.go +++ b/bits_amd64.go @@ -45,6 +45,3 @@ func nextMatch(b *bitset) (s uint32) { *b &= ^(1 << s) // clear bit |s| return } - -//go:linkname fastrand runtime.fastrand -func fastrand() uint32 diff --git a/fastrand.go b/fastrand.go new file mode 100644 index 0000000..fa610b4 --- /dev/null +++ b/fastrand.go @@ -0,0 +1,29 @@ +// Copyright 2023 Dolthub, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build !go1.22 + +package swiss + +import ( + _ "unsafe" +) + +//go:linkname fastrand runtime.fastrand +func fastrand() uint32 + +// randIntN returns a random number in the interval [0, n). +func randIntN(n int) uint32 { + return fastModN(fastrand(), uint32(n)) +} diff --git a/fastrand_1.22.go b/fastrand_1.22.go new file mode 100644 index 0000000..6f35d6f --- /dev/null +++ b/fastrand_1.22.go @@ -0,0 +1,26 @@ +// Copyright 2023 Dolthub, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build go1.22 + +package swiss + +import ( + "math/rand/v2" +) + +// randIntN returns a random number in the interval [0, n). +func randIntN(n int) uint32 { + return rand.Uint32N(uint32(n)) +} diff --git a/map.go b/map.go index e5ad203..d2b188e 100644 --- a/map.go +++ b/map.go @@ -352,8 +352,3 @@ func probeStart(hi h1, groups int) uint32 { func fastModN(x, n uint32) uint32 { return uint32((uint64(x) * uint64(n)) >> 32) } - -// randIntN returns a random number in the interval [0, n). -func randIntN(n int) uint32 { - return fastModN(fastrand(), uint32(n)) -}