Skip to content

Commit 6e49178

Browse files
committed
Use sampler-based Random API
This avoids the performance problem with `ReinterpretArray`. On the other hand, the time for generating the random number array with the default RNG, is significantly increased.
1 parent 33f2583 commit 6e49178

File tree

4 files changed

+16
-15
lines changed

4 files changed

+16
-15
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ uuid = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
33
version = "0.8.4"
44

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

89
[compat]

src/FixedPointNumbers.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import Base: ==, <, <=, -, +, *, /, ~, isapprox,
77
big, rationalize, float, trunc, round, floor, ceil, bswap, clamp,
88
div, fld, rem, mod, mod1, fld1, min, max, minmax,
99
signed, unsigned, copysign, flipsign, signbit,
10-
rand, length
10+
length
1111

1212
import Statistics # for _mean_promote
13+
import Random
1314

1415
using Base.Checked: checked_add, checked_sub, checked_div
1516

@@ -353,8 +354,9 @@ scaledual(::Type{Tdual}, x::AbstractArray{T}) where {Tdual, T <: FixedPoint} =
353354
throw(ArgumentError(String(take!(io))))
354355
end
355356

356-
rand(::Type{T}) where {T <: FixedPoint} = reinterpret(T, rand(rawtype(T)))
357-
rand(::Type{T}, sz::Dims) where {T <: FixedPoint} = reinterpret(T, rand(rawtype(T), sz))
357+
function Random.rand(r::Random.AbstractRNG, ::Random.SamplerType{X}) where X <: FixedPoint
358+
X(rand(r, rawtype(X)), 0)
359+
end
358360

359361
if VERSION >= v"1.1" # work around https://github.com/JuliaLang/julia/issues/34121
360362
include("precompile.jl")

test/fixed.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using FixedPointNumbers, Statistics, Test
1+
using FixedPointNumbers, Statistics, Random, Test
22
using FixedPointNumbers: bitwidth
33

44
function test_op(fun::F, ::Type{T}, fx, fy, fxf, fyf, tol) where {F,T}
@@ -371,12 +371,13 @@ end
371371
end
372372

373373
@testset "rand" begin
374-
for F in (Fixed{Int8,7}, Fixed{Int16,8}, Fixed{Int16,10}, Fixed{Int32,16})
374+
for F in (Q0f7, Q7f8, Q5f10, Q15f16)
375375
@test isa(rand(F), F)
376376
a = rand(F, (3, 5))
377-
@test ndims(a) == 2 && eltype(a) == F
377+
@test ndims(a) == 2 && eltype(a) === F
378378
@test size(a) == (3,5)
379379
end
380+
@test rand(MersenneTwister(1234), Q0f7) === -0.156Q0f7
380381
end
381382

382383
@testset "floatmin" begin

test/normed.jl

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using FixedPointNumbers, Statistics, Test
1+
using FixedPointNumbers, Statistics, Random, Test
22
using FixedPointNumbers: bitwidth
33

44
@testset "domain of f" begin
@@ -552,16 +552,13 @@ end
552552
end
553553

554554
@testset "rand" begin
555-
for T in (Normed{UInt8,8}, Normed{UInt8,6},
556-
Normed{UInt16,16}, Normed{UInt16,14},
557-
Normed{UInt32,32}, Normed{UInt32,30},
558-
Normed{UInt64,64}, Normed{UInt64,62})
559-
a = rand(T)
560-
@test isa(a, T)
561-
a = rand(T, (3, 5))
562-
@test ndims(a) == 2 && eltype(a) == T
555+
for N in (N0f8, N2f6, N0f16, N2f14, N0f32, N2f30, N0f64, N2f62)
556+
@test isa(rand(N), N)
557+
a = rand(N, (3, 5))
558+
@test ndims(a) == 2 && eltype(a) === N
563559
@test size(a) == (3,5)
564560
end
561+
@test rand(MersenneTwister(1234), N0f8) === 0.925N0f8
565562
end
566563

567564
@testset "Overflow with Float16" begin

0 commit comments

Comments
 (0)