@@ -26,27 +26,50 @@ using Compat
2626# f => Number of Bytes reserved for fractional part
2727abstract 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+
2945export
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
4868reinterpret (x:: FixedPoint ) = x. i
4969
70+ # construction using the (approximate) intended value, i.e., N0f8
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
90113floattype {T,f} (:: Type{FixedPoint{T,f}} ) = Float64
91114floattype (x:: FixedPoint ) = floattype (supertype (typeof (x)))
92115
116+ # This IOBuffer is used during module definition to generate typealias names
117+ _iotypealias = IOBuffer ()
93118
94119include (" fixed.jl" )
95120include (" ufixed.jl" )
@@ -141,17 +166,6 @@ scaledual{T<:FixedPoint}(Tdual::Type, x::Union{T,AbstractArray{T}}) =
141166scaledual {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"
0 commit comments