Skip to content

Commit 65689dd

Browse files
committed
Deprecate n*uf8 in favor of reinterpret(N0f8, n)
1 parent f226e62 commit 65689dd

File tree

3 files changed

+48
-38
lines changed

3 files changed

+48
-38
lines changed

src/deprecations.jl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import Base.@deprecate_binding
88
@deprecate_binding UFixed16 N0f16
99

1010
@deprecate_binding UfixedBase UFixed
11-
@deprecate_binding UfixedConstructor UFixedConstructor
1211
@deprecate_binding Ufixed UFixed
1312
@deprecate_binding Ufixed8 N0f8
1413
@deprecate_binding Ufixed10 N6f10
@@ -33,3 +32,23 @@ Compat.@dep_vectorize_1arg Real ufixed10
3332
Compat.@dep_vectorize_1arg Real ufixed12
3433
Compat.@dep_vectorize_1arg Real ufixed14
3534
Compat.@dep_vectorize_1arg Real ufixed16
35+
36+
## The next lines mimic the floating-point literal syntax "3.2f0"
37+
# construction using a UInt, i.e., 0xccuf8
38+
immutable UFixedConstructor{T,f} end
39+
function *{T,f}(n::Integer, ::UFixedConstructor{T,f})
40+
i = 8*sizeof(T)-f
41+
io = IOBuffer()
42+
show(io, n)
43+
nstr = takebuf_string(io)
44+
cstr = typeof(n) == T ? nstr : "convert($T, $nstr)"
45+
Base.depwarn("$(nstr)uf$f is deprecated, please use reinterpret(N$(i)f$f, $cstr) instead", :*)
46+
reinterpret(UFixed{T,f}, convert(T, n))
47+
end
48+
const uf8 = UFixedConstructor{UInt8,8}()
49+
const uf10 = UFixedConstructor{UInt16,10}()
50+
const uf12 = UFixedConstructor{UInt16,12}()
51+
const uf14 = UFixedConstructor{UInt16,14}()
52+
const uf16 = UFixedConstructor{UInt16,16}()
53+
54+
@deprecate_binding UfixedConstructor UFixedConstructor

src/ufixed.jl

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,6 @@ end
2727

2828
reinterpret{T<:Unsigned, f}(::Type{UFixed{T,f}}, x::T) = UFixed{T,f}(x, 0)
2929

30-
## The next lines mimic the floating-point literal syntax "3.2f0"
31-
# construction using a UInt, i.e., 0xccuf8
32-
immutable UFixedConstructor{T,f} end
33-
*{T,f}(n::Integer, ::UFixedConstructor{T,f}) = UFixed{T,f}(n,0)
34-
const uf8 = UFixedConstructor{UInt8,8}()
35-
const uf10 = UFixedConstructor{UInt16,10}()
36-
const uf12 = UFixedConstructor{UInt16,12}()
37-
const uf14 = UFixedConstructor{UInt16,14}()
38-
const uf16 = UFixedConstructor{UInt16,16}()
39-
4030
zero{T,f}(::Type{UFixed{T,f}}) = UFixed{T,f}(zero(T),0)
4131
function one{T<:UFixed}(::Type{T})
4232
T(typemax(rawtype(T)) >> (8*sizeof(T)-nbitsfrac(T)), 0)

test/ufixed.jl

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,29 @@ using FixedPointNumbers
22
using Base.Test
33
using Compat
44

5-
@test reinterpret(0xa2uf8) == 0xa2
6-
@test reinterpret(0xa2uf10) == 0xa2
7-
@test reinterpret(0xa2uf12) == 0xa2
8-
@test reinterpret(0xa2uf14) == 0xa2
9-
@test reinterpret(0xa2uf16) == 0xa2
10-
11-
@test reinterpret(N0f8, 0xa2) == 0xa2uf8
12-
@test reinterpret(N6f10, 0x1fa2) == 0x1fa2uf10
13-
@test reinterpret(N4f12, 0x1fa2) == 0x1fa2uf12
14-
@test reinterpret(N2f14, 0x1fa2) == 0x1fa2uf14
15-
@test reinterpret(N0f16, 0x1fa2) == 0x1fa2uf16
5+
@test reinterpret(N0f8, 0xa2).i === 0xa2
6+
@test reinterpret(N6f10, 0x1fa2).i === 0x1fa2
7+
@test reinterpret(N4f12, 0x1fa2).i === 0x1fa2
8+
@test reinterpret(N2f14, 0x1fa2).i === 0x1fa2
9+
@test reinterpret(N0f16, 0x1fa2).i === 0x1fa2
10+
11+
@test reinterpret(reinterpret(N0f8, 0xa2)) === 0xa2
12+
@test reinterpret(reinterpret(N6f10, 0x00a2)) === 0x00a2
13+
@test reinterpret(reinterpret(N4f12, 0x00a2)) === 0x00a2
14+
@test reinterpret(reinterpret(N2f14, 0x00a2)) === 0x00a2
15+
@test reinterpret(reinterpret(N0f16, 0x00a2)) === 0x00a2
16+
1617
@test 0.635N0f8 == N0f8(0.635)
1718
@test 0.635N6f10 == N6f10(0.635)
1819
@test 0.635N4f12 == N4f12(0.635)
1920
@test 0.635N2f14 == N2f14(0.635)
2021
@test 0.635N0f16 == N0f16(0.635)
2122

22-
@test N0f8(1.0) == 0xffuf8
23-
@test N0f8(0.5) == 0x80uf8
24-
@test N2f14(1.0) == 0x3fffuf14
23+
@test N0f8(1.0) == reinterpret(N0f8, 0xff)
24+
@test N0f8(0.5) == reinterpret(N0f8, 0x80)
25+
@test N2f14(1.0) == reinterpret(N2f14, 0x3fff)
2526
v = @compat N4f12.([2])
26-
@test v == N4f12[0x1ffeuf12]
27+
@test v == N4f12[reinterpret(N4f12, 0x1ffe)]
2728
@test isa(v, Vector{N4f12})
2829

2930
UF2 = (UFixed{UInt32,16}, UFixed{UInt64,3}, UFixed{UInt64,51}, UFixed{UInt128,7}, UFixed{UInt128,51})
@@ -114,7 +115,7 @@ end
114115

115116
x = N0f8(0b01010001, 0)
116117
@test ~x == N0f8(0b10101110, 0)
117-
@test -x == 0xafuf8
118+
@test -x == reinterpret(N0f8, 0xaf)
118119

119120
@test isa(float(one(UFixed{UInt8,7})), Float32)
120121
@test isa(float(one(UFixed{UInt32,18})), Float64)
@@ -192,16 +193,16 @@ end
192193
@test !(N0f8(0.5) < N0f8(0.5))
193194
@test N0f8(0.5) <= N0f8(0.5)
194195

195-
@test div(0x10uf8, 0x02uf8) == fld(0x10uf8, 0x02uf8) == 8
196-
@test div(0x0fuf8, 0x02uf8) == fld(0x0fuf8, 0x02uf8) == 7
197-
@test Base.fld1(0x10uf8, 0x02uf8) == 8
198-
@test Base.fld1(0x0fuf8, 0x02uf8) == 8
199-
@test mod(0x10uf8, 0x02uf8) == rem(0x10uf8, 0x02uf8) == 0
200-
@test mod(0x0fuf8, 0x02uf8) == rem(0x0fuf8, 0x02uf8) == 0x01uf8
201-
@test mod1(0x10uf8, 0x02uf8) == 0x02uf8
202-
@test mod1(0x0fuf8, 0x02uf8) == 0x01uf8
196+
@test div(reinterpret(N0f8, 0x10), reinterpret(N0f8, 0x02)) == fld(reinterpret(N0f8, 0x10), reinterpret(N0f8, 0x02)) == 8
197+
@test div(reinterpret(N0f8, 0x0f), reinterpret(N0f8, 0x02)) == fld(reinterpret(N0f8, 0x0f), reinterpret(N0f8, 0x02)) == 7
198+
@test Base.fld1(reinterpret(N0f8, 0x10), reinterpret(N0f8, 0x02)) == 8
199+
@test Base.fld1(reinterpret(N0f8, 0x0f), reinterpret(N0f8, 0x02)) == 8
200+
@test mod(reinterpret(N0f8, 0x10), reinterpret(N0f8, 0x02)) == rem(reinterpret(N0f8, 0x10), reinterpret(N0f8, 0x02)) == 0
201+
@test mod(reinterpret(N0f8, 0x0f), reinterpret(N0f8, 0x02)) == rem(reinterpret(N0f8, 0x0f), reinterpret(N0f8, 0x02)) == reinterpret(N0f8, 0x01)
202+
@test mod1(reinterpret(N0f8, 0x10), reinterpret(N0f8, 0x02)) == reinterpret(N0f8, 0x02)
203+
@test mod1(reinterpret(N0f8, 0x0f), reinterpret(N0f8, 0x02)) == reinterpret(N0f8, 0x01)
203204

204-
r = 1uf8:1uf8:48uf8
205+
r = reinterpret(N0f8, 0x01):reinterpret(N0f8, 0x01):reinterpret(N0f8, convert(UInt8, 48))
205206
@test length(r) == 48
206207

207208
counter = 0
@@ -235,7 +236,7 @@ end
235236
@test promote_type(Float32,N0f8,Int) == Float32
236237

237238
# Show
238-
x = 0xaauf8
239+
x = reinterpret(N0f8, 0xaa)
239240
iob = IOBuffer()
240241
show(iob, x)
241242
str = takebuf_string(iob)
@@ -269,7 +270,7 @@ generic_scale!(rfixed, ad, b)
269270
@test rfloat == rfixed
270271

271272
# reductions
272-
a = N0f8[0xffuf8, 0xffuf8]
273+
a = N0f8[reinterpret(N0f8, 0xff), reinterpret(N0f8, 0xff)]
273274
@test sum(a) == 2.0
274275
@test sum(a, 1) == [2.0]
275276

0 commit comments

Comments
 (0)