@@ -18,27 +18,50 @@ using Compat
1818# f => Number of Bytes reserved for fractional part
1919abstract 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+
2137export
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
4060reinterpret (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
6285widen1 (:: Type{UInt64} ) = UInt128
6386widen1 (x:: Integer ) = x % widen1 (typeof (x))
6487
88+ # This IOBuffer is used during module definition to generate typealias names
89+ _iotypealias = IOBuffer ()
90+
6591include (" fixed.jl" )
6692include (" ufixed.jl" )
6793include (" deprecations.jl" )
6894
69-
7095# Promotions for reductions
7196const Treduce = Float64
7297r_promote {T} (:: typeof (@functorize (+ )), x:: FixedPoint{T} ) = Treduce (x)
@@ -98,15 +123,4 @@ scaledual{T<:FixedPoint}(Tdual::Type, x::Union{T,AbstractArray{T}}) =
98123scaledual {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-
112126end # module
0 commit comments