Skip to content

Commit 398e5dc

Browse files
authored
Merge pull request #51 from JuliaMath/teh/compact_printing
Compact systematic display of values
2 parents 8eb2fb6 + af2a5c7 commit 398e5dc

File tree

11 files changed

+567
-514
lines changed

11 files changed

+567
-514
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ is the number of fraction bits.
2828

2929
For `T<:Signed` (a signed integer), there is a fixed-point type
3030
`Fixed{T, f}`; for `T<:Unsigned` (an unsigned integer), there is the
31-
`UFixed{T, f}` type. However, there are slight differences in behavior
31+
`Normed{T, f}` type. However, there are slight differences in behavior
3232
that go beyond signed/unsigned distinctions.
3333

3434
The `Fixed{T,f}` types use 1 bit for sign, and `f` bits to represent
@@ -43,23 +43,23 @@ is interpreted as if the integer representation has been divided by
4343

4444
because the range of `Int8` is from -128 to 127.
4545

46-
In contrast, the `UFixed{T,f}`, with `f` fraction bits, map the closed
46+
In contrast, the `Normed{T,f}`, with `f` fraction bits, map the closed
4747
interval [0.0,1.0] to the span of numbers with `f` bits. For example,
48-
the `UFixed8` type (aliased to `UFixed{UInt8,8}`) is represented
48+
the `Normed8` type (aliased to `Normed{UInt8,8}`) is represented
4949
internally by a `UInt8`, and makes `0x00` equivalent to `0.0` and
50-
`0xff` to `1.0`. Consequently, `UFixed` numbers are scaled by `2^f-1`
51-
rather than `2^f`. The type aliases `UFixed10`, `UFixed12`,
52-
`UFixed14`, and `UFixed16` are all based on `UInt16` and reach the
50+
`0xff` to `1.0`. Consequently, `Normed` numbers are scaled by `2^f-1`
51+
rather than `2^f`. The type aliases `Normed10`, `Normed12`,
52+
`Normed14`, and `Normed16` are all based on `UInt16` and reach the
5353
value `1.0` at 10, 12, 14, and 16 bits, respectively (`0x03ff`,
5454
`0x0fff`, `0x3fff`, and `0xffff`).
5555

56-
To construct such a number, use `convert(UFixed12, 1.3)`, `UFixed12(1.3)`, `UFixed{UInt16,12}(1.3)`, or the literal syntax
57-
`0x14ccuf12`. The latter syntax means to construct a `UFixed12` (it ends in
56+
To construct such a number, use `convert(Normed12, 1.3)`, `Normed12(1.3)`, `Normed{UInt16,12}(1.3)`, or the literal syntax
57+
`0x14ccuf12`. The latter syntax means to construct a `Normed12` (it ends in
5858
`uf12`) from the `UInt16` value `0x14cc`.
5959

6060
More generally, an arbitrary number of bits from any of the standard unsigned
6161
integer widths can be used for the fractional part. For example:
62-
`UFixed{UInt32,16}`, `UFixed{UInt64,3}`, `UFixed{UInt128,7}`.
62+
`Normed{UInt32,16}`, `Normed{UInt64,3}`, `Normed{UInt128,7}`.
6363

6464
There currently is no literal syntax for signed `Fixed` numbers.
6565

contrib/deprecation_UFixed.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
sed -i "s/UFixed8/N0f8/g" $*
2+
sed -i "s/UFixed10/N6f10/g" $*
3+
sed -i "s/UFixed12/N4f12/g" $*
4+
sed -i "s/UFixed14/N2f14/g" $*
5+
sed -i "s/UFixed16/N0f16/g" $*
6+
# For types from ColorTypes
7+
sed -i "s/U8/N0f8/g" $*
8+
sed -i "s/U16/N0f16/g" $*

src/FixedPointNumbers.jl

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

29+
2930
export
3031
FixedPoint,
3132
Fixed,
32-
UFixed,
33-
Fixed16,
34-
UFixed8,
35-
UFixed10,
36-
UFixed12,
37-
UFixed14,
38-
UFixed16,
39-
# literal constructor constants
33+
Normed,
34+
# "special" typealiases
35+
# Q and U typealiases are exported in separate source files
36+
# literal constructor constants
4037
uf8,
4138
uf10,
4239
uf12,
4340
uf14,
4441
uf16,
45-
# Functions
42+
# Functions
4643
scaledual
4744

4845
reinterpret(x::FixedPoint) = x.i
46+
reinterpret{T,f}(::Type{T}, x::FixedPoint{T,f}) = x.i
47+
48+
# construction using the (approximate) intended value, i.e., N0f8
49+
*{X<:FixedPoint}(x::Real, ::Type{X}) = X(x)
4950

5051
# comparison
5152
=={T <: FixedPoint}(x::T, y::T) = x.i == y.i
@@ -90,9 +91,28 @@ floattype{T<:ShortInts,f}(::Type{FixedPoint{T,f}}) = Float32
9091
floattype{T,f}(::Type{FixedPoint{T,f}}) = Float64
9192
floattype(x::FixedPoint) = floattype(supertype(typeof(x)))
9293

94+
# This IOBuffer is used during module definition to generate typealias names
95+
_iotypealias = IOBuffer()
96+
97+
# Printing. These are used to generate type-symbols, so we need them
98+
# before we include any files.
99+
function showtype{X<:FixedPoint}(io::IO, ::Type{X})
100+
print(io, typechar(X))
101+
f = nbitsfrac(X)
102+
m = sizeof(X)*8-f-signbits(X)
103+
print(io, m, 'f', f)
104+
io
105+
end
106+
function show{T,f}(io::IO, x::FixedPoint{T,f})
107+
showcompact(io, x)
108+
showtype(io, typeof(x))
109+
end
110+
const _log2_10 = 3.321928094887362
111+
showcompact{T,f}(io::IO, x::FixedPoint{T,f}) = show(io, round(convert(Float64,x), ceil(Int,f/_log2_10)))
112+
93113

94114
include("fixed.jl")
95-
include("ufixed.jl")
115+
include("normed.jl")
96116
include("deprecations.jl")
97117

98118
eps{T<:FixedPoint}(::Type{T}) = T(one(rawtype(T)),0)
@@ -113,14 +133,6 @@ reducedim_init{T<:FixedPoint}(f::typeof(@functorize(identity)),
113133
A::AbstractArray{T}, region) =
114134
reducedim_initarray(A, region, one(Treduce))
115135

116-
# TODO: rewrite this by @generated
117-
for T in tuple(Fixed16, UF...)
118-
R = rawtype(T)
119-
@eval begin
120-
reinterpret(::Type{$R}, x::$T) = x.i
121-
end
122-
end
123-
124136
for f in (:div, :fld, :fld1)
125137
@eval begin
126138
$f{T<:FixedPoint}(x::T, y::T) = $f(reinterpret(x),reinterpret(y))
@@ -141,17 +153,6 @@ scaledual{T<:FixedPoint}(Tdual::Type, x::Union{T,AbstractArray{T}}) =
141153
scaledual{Tdual<:Number, T<:FixedPoint}(b::Tdual, x::Union{T,AbstractArray{T}}) =
142154
convert(Tdual, b/one(T)), reinterpret(rawtype(T), x)
143155

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-
155156
@noinline function throw_converterror{T<:FixedPoint}(::Type{T}, x)
156157
n = 2^(8*sizeof(T))
157158
bitstring = sizeof(T) == 1 ? "an 8-bit" : "a $(8*sizeof(T))-bit"

src/deprecations.jl

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,57 @@
11
import Base.@deprecate_binding
22

3-
@deprecate_binding UfixedBase UFixed
4-
@deprecate_binding UfixedConstructor UFixedConstructor
5-
@deprecate_binding Ufixed UFixed
6-
@deprecate_binding Ufixed8 UFixed8
7-
@deprecate_binding Ufixed10 UFixed10
8-
@deprecate_binding Ufixed12 UFixed12
9-
@deprecate_binding Ufixed14 UFixed14
10-
@deprecate_binding Ufixed16 UFixed16
11-
12-
@deprecate_binding Fixed32 Fixed16
3+
@deprecate_binding Fixed16 Q15f16
4+
@deprecate_binding UFixed8 N0f8
5+
@deprecate_binding UFixed10 N6f10
6+
@deprecate_binding UFixed12 N4f12
7+
@deprecate_binding UFixed14 N2f14
8+
@deprecate_binding UFixed16 N0f16
9+
10+
@deprecate_binding UfixedBase Normed
11+
@deprecate_binding Ufixed Normed
12+
@deprecate_binding UFixed Normed
13+
@deprecate_binding Ufixed8 N0f8
14+
@deprecate_binding Ufixed10 N6f10
15+
@deprecate_binding Ufixed12 N4f12
16+
@deprecate_binding Ufixed14 N2f14
17+
@deprecate_binding Ufixed16 N0f16
18+
19+
@deprecate_binding Fixed32 Q15f16
20+
21+
const UF = (N0f8, N6f10, N4f12, N2f14, N0f16)
22+
1323
@deprecate Fixed(x::Real) convert(Fixed{Int32, 16}, x)
1424

15-
@deprecate ufixed8(x) UFixed8(x)
16-
@deprecate ufixed10(x) UFixed10(x)
17-
@deprecate ufixed12(x) UFixed12(x)
18-
@deprecate ufixed14(x) UFixed14(x)
19-
@deprecate ufixed16(x) UFixed16(x)
25+
@deprecate ufixed8(x) N0f8(x)
26+
@deprecate ufixed10(x) N6f10(x)
27+
@deprecate ufixed12(x) N4f12(x)
28+
@deprecate ufixed14(x) N2f14(x)
29+
@deprecate ufixed16(x) N0f16(x)
2030

2131
Compat.@dep_vectorize_1arg Real ufixed8
2232
Compat.@dep_vectorize_1arg Real ufixed10
2333
Compat.@dep_vectorize_1arg Real ufixed12
2434
Compat.@dep_vectorize_1arg Real ufixed14
2535
Compat.@dep_vectorize_1arg Real ufixed16
36+
37+
## The next lines mimic the floating-point literal syntax "3.2f0"
38+
# construction using a UInt, i.e., 0xccuf8
39+
immutable NormedConstructor{T,f} end
40+
function *{T,f}(n::Integer, ::NormedConstructor{T,f})
41+
i = 8*sizeof(T)-f
42+
io = IOBuffer()
43+
show(io, n)
44+
nstr = takebuf_string(io)
45+
cstr = typeof(n) == T ? nstr : "convert($T, $nstr)"
46+
Base.depwarn("$(nstr)uf$f is deprecated, please use reinterpret(N$(i)f$f, $cstr) instead", :*)
47+
reinterpret(Normed{T,f}, convert(T, n))
48+
end
49+
const uf8 = NormedConstructor{UInt8,8}()
50+
const uf10 = NormedConstructor{UInt16,10}()
51+
const uf12 = NormedConstructor{UInt16,12}()
52+
const uf14 = NormedConstructor{UInt16,14}()
53+
const uf16 = NormedConstructor{UInt16,16}()
54+
55+
@deprecate_binding UfixedConstructor NormedConstructor
56+
@deprecate_binding UFixedConstructor NormedConstructor
57+

src/fixed.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +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
reinterpret{T<:Signed, f}(::Type{Fixed{T,f}}, x::T) = Fixed{T,f}(x, 0)
1513

1614
rawtype{T,f}(::Type{Fixed{T,f}}) = T
1715
nbitsfrac{T,f}(::Type{Fixed{T,f}}) = f
1816
floattype{T<:Fixed}(::Type{T}) = floattype(supertype(T))
17+
typechar{X<:Fixed}(::Type{X}) = 'Q'
18+
signbits{X<:Fixed}(::Type{X}) = 1
19+
20+
for T in (Int8, Int16, Int32, Int64)
21+
for f in 0:sizeof(T)*8-1
22+
sym = Symbol(takebuf_string(showtype(_iotypealias, Fixed{T,f})))
23+
@eval begin
24+
typealias $sym Fixed{$T,$f}
25+
export $sym
26+
end
27+
end
28+
end
1929

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

0 commit comments

Comments
 (0)