Skip to content

Commit 266948a

Browse files
committed
Add depwarn in constructor of Fixed{T,f} where f == 8sizeof(T)
This also adds the domain checks for `f` in the constructors.
1 parent 3048c18 commit 266948a

File tree

4 files changed

+36
-12
lines changed

4 files changed

+36
-12
lines changed

src/fixed.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ struct Fixed{T <: Signed, f} <: FixedPoint{T, f}
1515

1616
# constructor for manipulating the representation;
1717
# selected by passing an extra dummy argument
18-
Fixed{T, f}(i::Integer, _) where {T,f} = new{T, f}(i % T)
18+
function Fixed{T, f}(i::Integer, _) where {T, f}
19+
if f == bitwidth(T)
20+
Base.depwarn("The `f`, which is the same as the number of rawtype bits, will be denied in the future.", :Fixed)
21+
end
22+
0 <= f <= bitwidth(T) || throw(DomainError(f, "f must be between 0 and the number of non-sign bits")) # TODO: change the upper limit
23+
new{T, f}(i % T)
24+
end
1925
end
2026

2127
Fixed{T, f}(x::AbstractChar) where {T,f} = throw(ArgumentError("Fixed cannot be constructed from a Char"))

src/normed.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ underlying bits used. For example, `N0f8` is aliased to `Normed{UInt8,8}` and
1313
struct Normed{T <: Unsigned, f} <: FixedPoint{T, f}
1414
i::T
1515

16-
Normed{T, f}(i::Integer,_) where {T,f} = new{T, f}(i%T) # for setting by raw representation
16+
function Normed{T, f}(i::Integer, _) where {T, f}
17+
1 <= f <= bitwidth(T) || throw(DomainError(f, "f must be between 1 and the number of rawtype bits"))
18+
new{T, f}(i % T)
19+
end
1720
end
1821

1922
Normed{T, f}(x::AbstractChar) where {T,f} = throw(ArgumentError("Normed cannot be constructed from a Char"))

test/fixed.jl

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ function test_fixed(::Type{T}, f) where {T}
5050
end
5151
end
5252

53+
@testset "domain of f" begin
54+
# TODO: change the upper limit
55+
@test_logs (:warn, r"will be denied in the future") zero(Fixed{Int8,8})
56+
@test_throws DomainError zero(Fixed{Int8,-1})
57+
# @test_throws DomainError zero(Fixed{Int8,8})
58+
@test_throws DomainError zero(Fixed{Int8,9})
59+
# @test_throws DomainError zero(Fixed{Int16,16})
60+
@test_throws DomainError zero(Fixed{Int16,17})
61+
end
62+
5363
@testset "reinterpret" begin
5464
@test reinterpret(Q0f7, signed(0xa2)) === -0.734375Q0f7
5565
@test reinterpret(Q5f10, signed(0x00a2)) === 0.158203125Q5f10
@@ -65,7 +75,7 @@ end
6575
# TODO: change back to InexactError when it allows message strings
6676
@test_throws ArgumentError one(Q0f15)
6777
@test_throws ArgumentError oneunit(Q0f31)
68-
@test_throws ArgumentError one(Fixed{Int8,8})
78+
@test_throws ArgumentError one(Fixed{Int8,8}) # TODO: remove this at end of its support
6979
end
7080

7181
@testset "conversion" begin
@@ -79,7 +89,7 @@ end
7989
end
8090

8191
@testset "test_fixed" begin
82-
for (TI, f) in [(Int8, 8), (Int16, 8), (Int16, 10), (Int32, 16)]
92+
for (TI, f) in [(Int8, 7), (Int16, 8), (Int16, 10), (Int32, 16)]
8393
T = Fixed{TI,f}
8494
# println(" Testing $T")
8595
test_fixed(T, f)
@@ -112,8 +122,7 @@ end
112122
end
113123

114124
@testset "reductions" begin
115-
F8 = Fixed{Int8,8}
116-
a = F8[0.498, 0.1]
125+
a = Q0f7[0.75, 0.5]
117126
acmp = Float64(a[1]) + Float64(a[2])
118127
@test sum(a) == acmp
119128
@test sum(a, dims=1) == [acmp]
@@ -126,7 +135,7 @@ end
126135
end
127136

128137
@testset "convert result type" begin
129-
x = Fixed{Int8,8}(0.3)
138+
x = Fixed{Int8,7}(0.75)
130139
for T in (Float16, Float32, Float64, BigFloat)
131140
y = convert(T, x)
132141
@test isa(y, T)
@@ -154,11 +163,10 @@ end
154163
end
155164

156165
@testset "rand" begin
157-
for T in (Fixed{Int8,8}, Fixed{Int16,8}, Fixed{Int16,10}, Fixed{Int32,16})
158-
a = rand(T)
159-
@test isa(a, T)
160-
a = rand(T, (3, 5))
161-
@test ndims(a) == 2 && eltype(a) == T
166+
for F in (Fixed{Int8,7}, Fixed{Int16,8}, Fixed{Int16,10}, Fixed{Int32,16})
167+
@test isa(rand(F), F)
168+
a = rand(F, (3, 5))
169+
@test ndims(a) == 2 && eltype(a) == F
162170
@test size(a) == (3,5)
163171
end
164172
end

test/normed.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
using FixedPointNumbers, Test
22
using FixedPointNumbers: bitwidth
33

4+
@testset "domain of f" begin
5+
@test_throws DomainError zero(Normed{UInt8,-1})
6+
@test_throws DomainError zero(Normed{UInt8,0})
7+
@test_throws DomainError zero(Normed{UInt8,9})
8+
@test_throws DomainError zero(Normed{UInt16,17})
9+
end
10+
411
@testset "reinterpret" begin
512
@test reinterpret(N0f8, 0xa2).i === 0xa2
613
@test reinterpret(N6f10, 0x1fa2).i === 0x1fa2

0 commit comments

Comments
 (0)