Skip to content

Commit

Permalink
Backport "Use sampler-based Random API (#206)" (#305)
Browse files Browse the repository at this point in the history
This also backports the changes in PR #270, #278.
  • Loading branch information
kimikage committed May 28, 2024
1 parent c372941 commit 73917f8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
5 changes: 4 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ uuid = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
version = "0.8.5"

[deps]
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[compat]
StableRNGs = "1"
julia = "1"

[extras]
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
test = ["StableRNGs", "Test"]
14 changes: 11 additions & 3 deletions src/FixedPointNumbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import Base: ==, <, <=, -, +, *, /, ~, isapprox,
big, rationalize, float, trunc, round, floor, ceil, bswap, clamp,
div, fld, rem, mod, mod1, fld1, min, max, minmax,
signed, unsigned, copysign, flipsign, signbit,
rand, length
length

import Statistics # for _mean_promote
import Random: Random, AbstractRNG, SamplerType, rand!

using Base.Checked: checked_add, checked_sub, checked_div

Expand Down Expand Up @@ -326,8 +327,15 @@ scaledual(::Type{Tdual}, x::AbstractArray{T}) where {Tdual, T <: FixedPoint} =
throw(ArgumentError("$X is $bitstring type representing $n values from $Xmin to $Xmax; cannot represent $x"))
end

rand(::Type{T}) where {T <: FixedPoint} = reinterpret(T, rand(rawtype(T)))
rand(::Type{T}, sz::Dims) where {T <: FixedPoint} = reinterpret(T, rand(rawtype(T), sz))
function Random.rand(r::AbstractRNG, ::SamplerType{X}) where X <: FixedPoint
X(rand(r, rawtype(X)), 0)
end

function rand!(r::AbstractRNG, A::Array{X}, ::SamplerType{X}) where {T, X <: FixedPoint{T}}
At = unsafe_wrap(Array, reinterpret(Ptr{T}, pointer(A)), size(A))
Random.rand!(r, At, SamplerType{T}())
A
end

if VERSION >= v"1.1" # work around https://github.com/JuliaLang/julia/issues/34121
include("precompile.jl")
Expand Down
2 changes: 2 additions & 0 deletions src/precompile.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Random

function _precompile_()
ccall(:jl_generating_output, Cint, ()) == 1 || return nothing
normedtypes = (N0f8, N0f16) # precompiled Normed types
Expand Down
4 changes: 3 additions & 1 deletion test/fixed.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FixedPointNumbers, Statistics, Test
using FixedPointNumbers, Statistics, Random, StableRNGs, Test
using FixedPointNumbers: bitwidth

# issue #288
Expand Down Expand Up @@ -404,6 +404,8 @@ end
@test ndims(a) == 2 && eltype(a) == F
@test size(a) == (3,5)
end
@test !(rand(Q0f15) == rand(Q0f15) == rand(Q0f15)) # If this fails, we should suspect a bug.
@test rand(StableRNG(1234), Q0f7) === 0.531Q0f7
end

@testset "floatmin" begin
Expand Down
4 changes: 3 additions & 1 deletion test/normed.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FixedPointNumbers, Statistics, Test
using FixedPointNumbers, Statistics, Random, StableRNGs, Test
using FixedPointNumbers: bitwidth

# issue #288
Expand Down Expand Up @@ -595,6 +595,8 @@ end
@test ndims(a) == 2 && eltype(a) == T
@test size(a) == (3,5)
end
@test !(rand(N0f16) == rand(N0f16) == rand(N0f16)) # If this fails, we should suspect a bug.
@test rand(StableRNG(1234), N0f8) === 0.267N0f8
end

@testset "Overflow with Float16" begin
Expand Down

0 comments on commit 73917f8

Please sign in to comment.