@@ -18,27 +18,58 @@ using Compat
1818# f => Number of Bytes reserved for fractional part
1919abstract FixedPoint{T <: Integer , f} <: Real
2020
21+
22+ # printing
23+ const subscripts = (' ₀' ,' ₁' ,' ₂' ,' ₃' ,' ₄' ,' ₅' ,' ₆' ,' ₇' ,' ₈' ,' ₉' )
24+ const superscripts = (' ⁰' ,' ¹' ,' ²' ,' ³' ,' ⁴' ,' ⁵' ,' ⁶' ,' ⁷' ,' ⁸' ,' ⁹' )
25+ function showtype {X<:FixedPoint} (io:: IO , :: Type{X} )
26+ print (io, typechar (X))
27+ f = nbitsfrac (X)
28+ mdigs = digits (sizeof (X)* 8 - f- signbits (X))
29+ fdigs = digits (f)
30+ for i = length (mdigs): - 1 : 1
31+ print (io, superscripts[mdigs[i]+ 1 ])
32+ end
33+ for i = length (fdigs): - 1 : 1
34+ print (io, subscripts[fdigs[i]+ 1 ])
35+ end
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+
2145export
2246 FixedPoint,
2347 Fixed,
2448 UFixed,
49+ # "special" typealiases
2550 Fixed16,
2651 UFixed8,
52+ U8,
2753 UFixed10,
2854 UFixed12,
2955 UFixed14,
3056 UFixed16,
31- # literal constructor constants
57+ U16,
58+ # Q and U typealiases are exported in separate source files
59+ # literal constructor constants
3260 uf8,
3361 uf10,
3462 uf12,
3563 uf14,
3664 uf16,
37- # Functions
65+ # Functions
3866 scaledual
3967
4068reinterpret (x:: FixedPoint ) = x. i
4169
70+ # construction using the (approximate) intended value, i.e., 0.8U⁰₈
71+ * {X<: FixedPoint }(x:: Real , :: Type{X} ) = X (x)
72+
4273# comparison
4374== {T <: FixedPoint }(x:: T , y:: T ) = x. i == y. i
4475 < {T <: FixedPoint }(x:: T , y:: T ) = x. i < y. i
@@ -62,11 +93,13 @@ widen1(::Type{Int64}) = Int128
6293widen1 (:: Type{UInt64} ) = UInt128
6394widen1 (x:: Integer ) = x % widen1 (typeof (x))
6495
96+ # This IOBuffer is used during module definition to generate typealias names
97+ _iotypealias = IOBuffer ()
98+
6599include (" fixed.jl" )
66100include (" ufixed.jl" )
67101include (" deprecations.jl" )
68102
69-
70103# Promotions for reductions
71104const Treduce = Float64
72105r_promote {T} (:: typeof (@functorize (+ )), x:: FixedPoint{T} ) = Treduce (x)
@@ -98,15 +131,4 @@ scaledual{T<:FixedPoint}(Tdual::Type, x::Union{T,AbstractArray{T}}) =
98131scaledual {Tdual<:Number, T<:FixedPoint} (b:: Tdual , x:: Union{T,AbstractArray{T}} ) =
99132 convert (Tdual, b/ one (T)), reinterpret (rawtype (T), x)
100133
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-
112134end # module
0 commit comments