@@ -26,26 +26,27 @@ using Compat
2626# f => Number of Bytes reserved for fractional part
2727abstract FixedPoint{T <: Integer , f} <: Real
2828
29+
2930export
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
4845reinterpret (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
9091floattype {T,f} (:: Type{FixedPoint{T,f}} ) = Float64
9192floattype (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
94114include (" fixed.jl" )
95- include (" ufixed .jl" )
115+ include (" normed .jl" )
96116include (" deprecations.jl" )
97117
98118eps {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-
124136for 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}}) =
141153scaledual {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"
0 commit comments