@@ -16,11 +16,9 @@ if isdefined(Base, :rem1)
1616end
1717using Base: @pure
1818
19- using Compat
20-
2119# T => BaseType
2220# f => Number of Bytes reserved for fractional part
23- @compat abstract type FixedPoint{T <: Integer , f} <: Real end
21+ abstract type FixedPoint{T <: Integer , f} <: Real end
2422
2523
2624export
@@ -39,21 +37,21 @@ export
3937 scaledual
4038
4139reinterpret (x:: FixedPoint ) = x. i
42- reinterpret {T,f} (:: Type{T} , x:: FixedPoint{T,f} ) = x. i
40+ reinterpret (:: Type{T} , x:: FixedPoint{T,f} ) where {T,f} = x. i
4341
4442# construction using the (approximate) intended value, i.e., N0f8
45- * {X <: FixedPoint } (x:: Real , :: Type{X} ) = X (x)
43+ * (x:: Real , :: Type{X} ) where {X <: FixedPoint } = X (x)
4644
4745# comparison
48- == {T <: FixedPoint } (x:: T , y:: T ) = x. i == y. i
49- < {T <: FixedPoint } (x:: T , y:: T ) = x. i < y. i
50- <= {T <: FixedPoint } (x:: T , y:: T ) = x. i <= y. i
46+ == (x:: T , y:: T ) where {T <: FixedPoint } = x. i == y. i
47+ < (x:: T , y:: T ) where {T <: FixedPoint } = x. i < y. i
48+ <= (x:: T , y:: T ) where {T <: FixedPoint } = x. i <= y. i
5149"""
5250 isapprox(x::FixedPoint, y::FixedPoint; rtol=0, atol=max(eps(x), eps(y)))
5351
5452For FixedPoint numbers, the default criterion is that `x` and `y` differ by no more than `eps`, the separation between adjacent fixed-point numbers.
5553"""
56- function isapprox {T<:FixedPoint} (x:: T , y:: T ; rtol= 0 , atol= max (eps (x), eps (y)))
54+ function isapprox (x:: T , y:: T ; rtol= 0 , atol= max (eps (x), eps (y))) where {T <: FixedPoint }
5755 maxdiff = T (atol+ rtol* max (abs (x), abs (y)))
5856 rx, ry, rd = reinterpret (x), reinterpret (y), reinterpret (maxdiff)
5957 abs (signed (widen1 (rx))- signed (widen1 (ry))) <= rd
@@ -63,13 +61,13 @@ function isapprox(x::FixedPoint, y::FixedPoint; rtol=0, atol=max(eps(x), eps(y))
6361end
6462
6563# predicates
66- isinteger {T,f} (x:: FixedPoint{T,f} ) = (x. i& (1 << f- 1 )) == 0
64+ isinteger (x:: FixedPoint{T,f} ) where {T,f} = (x. i& (1 << f- 1 )) == 0
6765
6866# traits
69- typemax {T<: FixedPoint} (:: Type{T} ) = T (typemax (rawtype (T)), 0 )
70- typemin {T<: FixedPoint} (:: Type{T} ) = T (typemin (rawtype (T)), 0 )
71- realmin {T<: FixedPoint} (:: Type{T} ) = eps (T)
72- realmax {T<: FixedPoint} (:: Type{T} ) = typemax (T)
67+ typemax (:: Type{T} ) where {T <: FixedPoint } = T (typemax (rawtype (T)), 0 )
68+ typemin (:: Type{T} ) where {T <: FixedPoint } = T (typemin (rawtype (T)), 0 )
69+ realmin (:: Type{T} ) where {T <: FixedPoint } = eps (T)
70+ realmax (:: Type{T} ) where {T <: FixedPoint } = typemax (T)
7371
7472widen1 (:: Type{Int8} ) = Int16
7573widen1 (:: Type{UInt8} ) = UInt16
@@ -84,84 +82,84 @@ widen1(x::Integer) = x % widen1(typeof(x))
8482
8583const ShortInts = Union{Int8,UInt8,Int16,UInt16}
8684
87- floattype {T<:ShortInts,f} (:: Type{FixedPoint{T,f}} ) = Float32
88- floattype {T<:Integer,f} (:: Type{FixedPoint{T,f}} ) = Float64
89- floattype {F<:FixedPoint} (:: Type{F} ) = floattype (supertype (F))
85+ floattype (:: Type{FixedPoint{T,f}} ) where {T <: ShortInts ,f} = Float32
86+ floattype (:: Type{FixedPoint{T,f}} ) where {T <: Integer ,f} = Float64
87+ floattype (:: Type{F} ) where {F <: FixedPoint } = floattype (supertype (F))
9088floattype (x:: FixedPoint ) = floattype (typeof (x))
9189
92- nbitsfrac {T<:Integer,f} (:: Type{FixedPoint{T,f}} ) = f
93- nbitsfrac {F<:FixedPoint} (:: Type{F} ) = nbitsfrac (supertype (F))
90+ nbitsfrac (:: Type{FixedPoint{T,f}} ) where {T <: Integer ,f} = f
91+ nbitsfrac (:: Type{F} ) where {F <: FixedPoint } = nbitsfrac (supertype (F))
9492
95- rawtype {T<:Integer,f} (:: Type{FixedPoint{T,f}} ) = T
96- rawtype {F<:FixedPoint} (:: Type{F} ) = rawtype (supertype (F))
93+ rawtype (:: Type{FixedPoint{T,f}} ) where {T <: Integer ,f} = T
94+ rawtype (:: Type{F} ) where {F <: FixedPoint } = rawtype (supertype (F))
9795rawtype (x:: FixedPoint ) = rawtype (typeof (x))
9896
9997# This IOBuffer is used during module definition to generate typealias names
10098_iotypealias = IOBuffer ()
10199
102100# Printing. These are used to generate type-symbols, so we need them
103101# before we include any files.
104- function showtype {X<:FixedPoint} (io:: IO , :: Type{X} )
102+ function showtype (io:: IO , :: Type{X} ) where {X <: FixedPoint }
105103 print (io, typechar (X))
106104 f = nbitsfrac (X)
107105 m = sizeof (X)* 8 - f- signbits (X)
108106 print (io, m, ' f' , f)
109107 io
110108end
111- function show {T,f} (io:: IO , x:: FixedPoint{T,f} )
109+ function show (io:: IO , x:: FixedPoint{T,f} ) where {T,f}
112110 showcompact (io, x)
113111 showtype (io, typeof (x))
114112end
115113const _log2_10 = 3.321928094887362
116- showcompact {T,f} (io:: IO , x:: FixedPoint{T,f} ) = show (io, round (convert (Float64,x), ceil (Int,f/ _log2_10)))
114+ showcompact (io:: IO , x:: FixedPoint{T,f} ) where {T,f} = show (io, round (convert (Float64,x), ceil (Int,f/ _log2_10)))
117115
118116
119117include (" fixed.jl" )
120118include (" normed.jl" )
121119include (" deprecations.jl" )
122120
123- eps {T<:FixedPoint} (:: Type{T} ) = T (one (rawtype (T)),0 )
124- eps {T<: FixedPoint}( :: T ) = eps (T)
125- sizeof {T<:FixedPoint} (:: Type{T} ) = sizeof (rawtype (T))
121+ eps (:: Type{T} ) where {T <: FixedPoint } = T (one (rawtype (T)),0 )
122+ eps ( :: T ) where {T <: FixedPoint } = eps (T)
123+ sizeof (:: Type{T} ) where {T <: FixedPoint } = sizeof (rawtype (T))
126124
127125# Promotions for reductions
128126const Treduce = Float64
129- r_promote {T} (:: typeof (+ ), x:: FixedPoint{T} ) = Treduce (x)
130- r_promote {T} (:: typeof (* ), x:: FixedPoint{T} ) = Treduce (x)
127+ r_promote (:: typeof (+ ), x:: FixedPoint{T} ) where {T} = Treduce (x)
128+ r_promote (:: typeof (* ), x:: FixedPoint{T} ) where {T} = Treduce (x)
131129
132- reducedim_init {T<:FixedPoint} (f:: typeof (identity),
130+ reducedim_init (f:: typeof (identity),
133131 op:: typeof (+ ),
134- A:: AbstractArray{T} , region) =
132+ A:: AbstractArray{T} , region) where {T <: FixedPoint } =
135133 reducedim_initarray (A, region, zero (Treduce))
136- reducedim_init {T<:FixedPoint} (f:: typeof (identity),
134+ reducedim_init (f:: typeof (identity),
137135 op:: typeof (* ),
138- A:: AbstractArray{T} , region) =
136+ A:: AbstractArray{T} , region) where {T <: FixedPoint } =
139137 reducedim_initarray (A, region, one (Treduce))
140138
141139for f in (:div , :fld , :fld1 )
142140 @eval begin
143- $ f {T<:FixedPoint} (x:: T , y:: T ) = $ f (reinterpret (x),reinterpret (y))
141+ $ f (x:: T , y:: T ) where {T <: FixedPoint } = $ f (reinterpret (x),reinterpret (y))
144142 end
145143end
146144for f in (:rem , :mod , :mod1 , :rem1 , :min , :max )
147145 if f === :rem1 && ! isdefined (Base, :rem1 )
148146 continue
149147 end
150148 @eval begin
151- $ f {T<:FixedPoint} (x:: T , y:: T ) = T ($ f (reinterpret (x),reinterpret (y)),0 )
149+ $ f (x:: T , y:: T ) where {T <: FixedPoint } = T ($ f (reinterpret (x),reinterpret (y)),0 )
152150 end
153151end
154152
155153# When multiplying by a float, reduce two multiplies to one.
156154# Particularly useful for arrays.
157155scaledual (Tdual:: Type , x) = one (Tdual), x
158- scaledual {Tdual<:Number} (b:: Tdual , x) = b, x
159- scaledual {T<:FixedPoint} (Tdual:: Type , x:: Union{T,AbstractArray{T}} ) =
156+ scaledual (b:: Tdual , x) where {Tdual <: Number } = b, x
157+ scaledual (Tdual:: Type , x:: Union{T,AbstractArray{T}} ) where {T <: FixedPoint } =
160158 convert (Tdual, 1 / one (T)), reinterpret (rawtype (T), x)
161- scaledual {Tdual<:Number, T<:FixedPoint} (b:: Tdual , x:: Union{T,AbstractArray{T}} ) =
159+ scaledual (b:: Tdual , x:: Union{T,AbstractArray{T}} ) where {Tdual <: Number ,T <: FixedPoint } =
162160 convert (Tdual, b/ one (T)), reinterpret (rawtype (T), x)
163161
164- @noinline function throw_converterror {T<:FixedPoint} (:: Type{T} , x)
162+ @noinline function throw_converterror (:: Type{T} , x) where {T <: FixedPoint }
165163 n = 2 ^ (8 * sizeof (T))
166164 bitstring = sizeof (T) == 1 ? " an 8-bit" : " a $(8 * sizeof (T)) -bit"
167165 io = IOBuffer ()
@@ -170,7 +168,7 @@ scaledual{Tdual<:Number, T<:FixedPoint}(b::Tdual, x::Union{T,AbstractArray{T}})
170168 throw (ArgumentError (" $T is $bitstring type representing $n values from $Tmin to $Tmax ; cannot represent $x " ))
171169end
172170
173- rand {T<:FixedPoint} (:: Type{T} ) = reinterpret (T, rand (rawtype (T)))
174- rand {T<:FixedPoint} (:: Type{T} , sz:: Dims ) = reinterpret (T, rand (rawtype (T), sz))
171+ rand (:: Type{T} ) where {T <: FixedPoint } = reinterpret (T, rand (rawtype (T)))
172+ rand (:: Type{T} , sz:: Dims ) where {T <: FixedPoint } = reinterpret (T, rand (rawtype (T), sz))
175173
176174end # module
0 commit comments