Skip to content

Commit 3e7e0c1

Browse files
committed
Compact systematic display of values
1 parent b8506b5 commit 3e7e0c1

File tree

5 files changed

+68
-20
lines changed

5 files changed

+68
-20
lines changed

src/FixedPointNumbers.jl

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,50 @@ using Compat
1818
# f => Number of Bytes reserved for fractional part
1919
abstract FixedPoint{T <: Integer, f} <: Real
2020

21+
22+
# Printing. These are used to generate type-symbols, so we need them early.
23+
function showtype{X<:FixedPoint}(io::IO, ::Type{X})
24+
print(io, typechar(X))
25+
f = nbitsfrac(X)
26+
m = sizeof(X)*8-f-signbits(X)
27+
print(io, m, 'f', f)
28+
io
29+
end
30+
function show{T,f}(io::IO, x::FixedPoint{T,f})
31+
showcompact(io, x)
32+
showtype(io, typeof(x))
33+
end
34+
const _log2_10 = 3.321928094887362
35+
showcompact{T,f}(io::IO, x::FixedPoint{T,f}) = show(io, round(convert(Float64,x), ceil(Int,f/_log2_10)))
36+
2137
export
2238
FixedPoint,
2339
Fixed,
2440
UFixed,
41+
# "special" typealiases
2542
Fixed16,
2643
UFixed8,
44+
U8,
2745
UFixed10,
2846
UFixed12,
2947
UFixed14,
3048
UFixed16,
31-
# literal constructor constants
49+
U16,
50+
# Q and U typealiases are exported in separate source files
51+
# literal constructor constants
3252
uf8,
3353
uf10,
3454
uf12,
3555
uf14,
3656
uf16,
37-
# Functions
57+
# Functions
3858
scaledual
3959

4060
reinterpret(x::FixedPoint) = x.i
4161

62+
# construction using the (approximate) intended value, i.e., 0.8U⁰₈
63+
*{X<:FixedPoint}(x::Real, ::Type{X}) = X(x)
64+
4265
# comparison
4366
=={T <: FixedPoint}(x::T, y::T) = x.i == y.i
4467
<{T <: FixedPoint}(x::T, y::T) = x.i < y.i
@@ -62,11 +85,13 @@ widen1(::Type{Int64}) = Int128
6285
widen1(::Type{UInt64}) = UInt128
6386
widen1(x::Integer) = x % widen1(typeof(x))
6487

88+
# This IOBuffer is used during module definition to generate typealias names
89+
_iotypealias = IOBuffer()
90+
6591
include("fixed.jl")
6692
include("ufixed.jl")
6793
include("deprecations.jl")
6894

69-
7095
# Promotions for reductions
7196
const Treduce = Float64
7297
r_promote{T}(::typeof(@functorize(+)), x::FixedPoint{T}) = Treduce(x)
@@ -98,15 +123,4 @@ scaledual{T<:FixedPoint}(Tdual::Type, x::Union{T,AbstractArray{T}}) =
98123
scaledual{Tdual<:Number, T<:FixedPoint}(b::Tdual, x::Union{T,AbstractArray{T}}) =
99124
convert(Tdual, b/one(T)), reinterpret(rawtype(T), x)
100125

101-
# printing
102-
function show{T,f}(io::IO, x::FixedPoint{T,f})
103-
shorttype = typeof(x)<:UFixed ? "UFixed" : "Fixed"
104-
print(io, shorttype, "{", T, ",", f, "}")
105-
print(io, "(")
106-
showcompact(io, x)
107-
print(io, ")")
108-
end
109-
const _log2_10 = 3.321928094887362
110-
showcompact{T,f}(io::IO, x::FixedPoint{T,f}) = show(io, round(convert(Float64,x), ceil(Int,f/_log2_10)))
111-
112126
end # module

src/fixed.jl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,23 @@ 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
14+
typechar{X<:Fixed}(::Type{X}) = 'Q'
15+
signbits{X<:Fixed}(::Type{X}) = 1
16+
17+
for T in (Int8, Int16, Int32, Int64)
18+
for f in 0:sizeof(T)*8-1
19+
sym = Symbol(takebuf_string(showtype(_iotypealias, Fixed{T,f})))
20+
@eval begin
21+
typealias $sym Fixed{$T,$f}
22+
export $sym
23+
end
24+
end
25+
end
26+
27+
# ASCII typealiases
28+
typealias Fixed16 Fixed{Int32,16}
1629

1730
# basic operators
1831
-{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
@@ -10,18 +10,34 @@ end
1010

1111
rawtype{T,f}(::Type{UFixed{T,f}}) = T
1212
nbitsfrac{T,f}(::Type{UFixed{T,f}}) = f
13+
typechar{X<:UFixed}(::Type{X}) = 'N'
14+
signbits{X<:UFixed}(::Type{X}) = 0
15+
16+
for T in (UInt8, UInt16, UInt32, UInt64)
17+
for f in 0:sizeof(T)*8
18+
sym = Symbol(takebuf_string(showtype(_iotypealias, UFixed{T,f})))
19+
@eval begin
20+
typealias $sym UFixed{$T,$f}
21+
export $sym
22+
end
23+
end
24+
end
1325

26+
# ASCII typealiases
27+
typealias U8 UFixed{UInt8,8}
1428
typealias UFixed8 UFixed{UInt8,8}
1529
typealias UFixed10 UFixed{UInt16,10}
1630
typealias UFixed12 UFixed{UInt16,12}
1731
typealias UFixed14 UFixed{UInt16,14}
1832
typealias UFixed16 UFixed{UInt16,16}
33+
typealias U16 UFixed{UInt16,16}
1934

2035
const UF = (UFixed8, UFixed10, UFixed12, UFixed14, UFixed16)
2136

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

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

test/fixed.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ for T in (Float16, Float32, Float64, BigFloat)
8484
end
8585

8686
# Show
87-
x = Fixed{Int32,3}(0.25)
87+
x = Fixed{Int32,5}(0.25)
8888
iob = IOBuffer()
8989
show(iob, x)
9090
str = takebuf_string(iob)
91-
@test startswith(str, "Fixed{Int32,3}(")
91+
@test str == "0.25Q26f5"
9292
@test eval(parse(str)) == x

test/ufixed.jl

Lines changed: 6 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
@@ -159,7 +164,7 @@ x = 0xaauf8
159164
iob = IOBuffer()
160165
show(iob, x)
161166
str = takebuf_string(iob)
162-
@test startswith(str, "UFixed{UInt8,8}(")
167+
@test str == "0.667N0f8"
163168
@test eval(parse(str)) == x
164169

165170
# scaledual

0 commit comments

Comments
 (0)