Skip to content

Commit 717eed9

Browse files
committed
Compact systematic display of values
1 parent ccb4267 commit 717eed9

File tree

5 files changed

+68
-19
lines changed

5 files changed

+68
-19
lines changed

src/FixedPointNumbers.jl

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,50 @@ using Compat
2626
# f => Number of Bytes reserved for fractional part
2727
abstract FixedPoint{T <: Integer, f} <: Real
2828

29+
30+
# Printing. These are used to generate type-symbols, so we need them early.
31+
function showtype{X<:FixedPoint}(io::IO, ::Type{X})
32+
print(io, typechar(X))
33+
f = nbitsfrac(X)
34+
m = sizeof(X)*8-f-signbits(X)
35+
print(io, m, 'f', f)
36+
io
37+
end
38+
function show{T,f}(io::IO, x::FixedPoint{T,f})
39+
showcompact(io, x)
40+
showtype(io, typeof(x))
41+
end
42+
const _log2_10 = 3.321928094887362
43+
showcompact{T,f}(io::IO, x::FixedPoint{T,f}) = show(io, round(convert(Float64,x), ceil(Int,f/_log2_10)))
44+
2945
export
3046
FixedPoint,
3147
Fixed,
3248
UFixed,
49+
# "special" typealiases
3350
Fixed16,
3451
UFixed8,
52+
U8,
3553
UFixed10,
3654
UFixed12,
3755
UFixed14,
3856
UFixed16,
39-
# literal constructor constants
57+
U16,
58+
# Q and U typealiases are exported in separate source files
59+
# literal constructor constants
4060
uf8,
4161
uf10,
4262
uf12,
4363
uf14,
4464
uf16,
45-
# Functions
65+
# Functions
4666
scaledual
4767

4868
reinterpret(x::FixedPoint) = x.i
4969

70+
# construction using the (approximate) intended value, i.e., 0.8U⁰₈
71+
*{X<:FixedPoint}(x::Real, ::Type{X}) = X(x)
72+
5073
# comparison
5174
=={T <: FixedPoint}(x::T, y::T) = x.i == y.i
5275
<{T <: FixedPoint}(x::T, y::T) = x.i < y.i
@@ -90,6 +113,8 @@ floattype{T<:ShortInts,f}(::Type{FixedPoint{T,f}}) = Float32
90113
floattype{T,f}(::Type{FixedPoint{T,f}}) = Float64
91114
floattype(x::FixedPoint) = floattype(supertype(typeof(x)))
92115

116+
# This IOBuffer is used during module definition to generate typealias names
117+
_iotypealias = IOBuffer()
93118

94119
include("fixed.jl")
95120
include("ufixed.jl")
@@ -141,17 +166,6 @@ scaledual{T<:FixedPoint}(Tdual::Type, x::Union{T,AbstractArray{T}}) =
141166
scaledual{Tdual<:Number, T<:FixedPoint}(b::Tdual, x::Union{T,AbstractArray{T}}) =
142167
convert(Tdual, b/one(T)), reinterpret(rawtype(T), x)
143168

144-
# printing
145-
function show{T,f}(io::IO, x::FixedPoint{T,f})
146-
shorttype = typeof(x)<:UFixed ? "UFixed" : "Fixed"
147-
print(io, shorttype, "{", T, ",", f, "}")
148-
print(io, "(")
149-
showcompact(io, x)
150-
print(io, ")")
151-
end
152-
const _log2_10 = 3.321928094887362
153-
showcompact{T,f}(io::IO, x::FixedPoint{T,f}) = show(io, round(Float64(x), ceil(Int,f/_log2_10)))
154-
155169
@noinline function throw_converterror{T<:FixedPoint}(::Type{T}, x)
156170
n = 2^(8*sizeof(T))
157171
bitstring = sizeof(T) == 1 ? "an 8-bit" : "a $(8*sizeof(T))-bit"

src/fixed.jl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,24 @@ immutable Fixed{T <: Signed,f} <: FixedPoint{T, f}
99
Fixed(x) = convert(Fixed{T,f}, x)
1010
end
1111

12-
typealias Fixed16 Fixed{Int32, 16}
13-
1412
rawtype{T,f}(::Type{Fixed{T,f}}) = T
1513
nbitsfrac{T,f}(::Type{Fixed{T,f}}) = f
1614
floattype{T<:Fixed}(::Type{T}) = floattype(supertype(T))
15+
typechar{X<:Fixed}(::Type{X}) = 'Q'
16+
signbits{X<:Fixed}(::Type{X}) = 1
17+
18+
for T in (Int8, Int16, Int32, Int64)
19+
for f in 0:sizeof(T)*8-1
20+
sym = Symbol(takebuf_string(showtype(_iotypealias, Fixed{T,f})))
21+
@eval begin
22+
typealias $sym Fixed{$T,$f}
23+
export $sym
24+
end
25+
end
26+
end
27+
28+
# ASCII typealiases
29+
typealias Fixed16 Fixed{Int32,16}
1730

1831
# basic operators
1932
-{T,f}(x::Fixed{T,f}) = Fixed{T,f}(-x.i,0)

src/ufixed.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,34 @@ end
1212
rawtype(x::Number) = rawtype(typeof(x))
1313
nbitsfrac{T,f}(::Type{UFixed{T,f}}) = f
1414
floattype{T<:UFixed}(::Type{T}) = floattype(supertype(T))
15+
typechar{X<:UFixed}(::Type{X}) = 'N'
16+
signbits{X<:UFixed}(::Type{X}) = 0
17+
18+
for T in (UInt8, UInt16, UInt32, UInt64)
19+
for f in 0:sizeof(T)*8
20+
sym = Symbol(takebuf_string(showtype(_iotypealias, UFixed{T,f})))
21+
@eval begin
22+
typealias $sym UFixed{$T,$f}
23+
export $sym
24+
end
25+
end
26+
end
1527

28+
# ASCII typealiases
29+
typealias U8 UFixed{UInt8,8}
1630
typealias UFixed8 UFixed{UInt8,8}
1731
typealias UFixed10 UFixed{UInt16,10}
1832
typealias UFixed12 UFixed{UInt16,12}
1933
typealias UFixed14 UFixed{UInt16,14}
2034
typealias UFixed16 UFixed{UInt16,16}
35+
typealias U16 UFixed{UInt16,16}
2136

2237
const UF = (UFixed8, UFixed10, UFixed12, UFixed14, UFixed16)
2338

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

26-
# The next lines mimic the floating-point literal syntax "3.2f0"
41+
## The next lines mimic the floating-point literal syntax "3.2f0"
42+
# construction using a UInt, i.e., 0xccuf8
2743
immutable UFixedConstructor{T,f} end
2844
*{T,f}(n::Integer, ::UFixedConstructor{T,f}) = UFixed{T,f}(n,0)
2945
const uf8 = UFixedConstructor{UInt8,8}()

test/fixed.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ end
107107
@test isa(float(one(Fixed{Int32,25})), Float64)
108108

109109
# Show
110-
x = Fixed{Int32,3}(0.25)
110+
x = Fixed{Int32,5}(0.25)
111111
iob = IOBuffer()
112112
show(iob, x)
113113
str = takebuf_string(iob)
114-
@test startswith(str, "Fixed{Int32,3}(")
114+
@test str == "0.25Q26f5"
115115
@test eval(parse(str)) == x

test/ufixed.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ using Compat
1313
@test reinterpret(UFixed12, 0x1fa2) == 0x1fa2uf12
1414
@test reinterpret(UFixed14, 0x1fa2) == 0x1fa2uf14
1515
@test reinterpret(UFixed16, 0x1fa2) == 0x1fa2uf16
16+
@test 0.635N0f8 == UFixed8(0.635)
17+
@test 0.635N6f10 == UFixed10(0.635)
18+
@test 0.635N4f12 == UFixed12(0.635)
19+
@test 0.635N2f14 == UFixed14(0.635)
20+
@test 0.635N0f16 == UFixed16(0.635)
1621

1722
@test UFixed8(1.0) == 0xffuf8
1823
@test UFixed8(0.5) == 0x80uf8
@@ -234,7 +239,8 @@ x = 0xaauf8
234239
iob = IOBuffer()
235240
show(iob, x)
236241
str = takebuf_string(iob)
237-
@test str == "UFixed{UInt8,8}(0.667)"
242+
@test str == "0.667N0f8"
243+
@test eval(parse(str)) == x
238244

239245
# scaledual
240246
function generic_scale!(C::AbstractArray, X::AbstractArray, s::Number)

0 commit comments

Comments
 (0)