@@ -37,6 +37,10 @@ include("utilities.jl")
3737reinterpret (x:: FixedPoint ) = x. i
3838reinterpret (:: Type{T} , x:: FixedPoint{T,f} ) where {T,f} = x. i
3939
40+ # static parameters
41+ nbitsfrac (:: Type{X} ) where {T, f, X <: FixedPoint{T,f} } = f
42+ rawtype (:: Type{X} ) where {T, X <: FixedPoint{T} } = T
43+
4044# construction using the (approximate) intended value, i.e., N0f8
4145* (x:: Real , :: Type{X} ) where {X<: FixedPoint } = X (x)
4246
6165# predicates
6266isinteger (x:: FixedPoint{T,f} ) where {T,f} = (x. i& (1 << f- 1 )) == 0
6367
68+ # identities
69+ zero (:: Type{X} ) where {X <: FixedPoint } = X (zero (rawtype (X)), 0 )
70+ oneunit (:: Type{X} ) where {X <: FixedPoint } = X (rawone (X), 0 )
71+ one (:: Type{X} ) where {X <: FixedPoint } = oneunit (X)
72+
73+ # for Julia v1.0, which does not fold `div_float` before inlining
74+ inv_rawone (x) = (@generated ) ? (y = 1.0 / rawone (x); :($ y)) : 1.0 / rawone (x)
75+
6476# traits
77+ sizeof (:: Type{X} ) where {X <: FixedPoint } = sizeof (rawtype (X))
78+ eps (:: Type{X} ) where {X <: FixedPoint } = X (oneunit (rawtype (X)), 0 )
6579typemax (:: Type{T} ) where {T <: FixedPoint } = T (typemax (rawtype (T)), 0 )
6680typemin (:: Type{T} ) where {T <: FixedPoint } = T (typemin (rawtype (T)), 0 )
6781floatmin (:: Type{T} ) where {T <: FixedPoint } = eps (T)
@@ -95,18 +109,25 @@ floattype(::Type{T}) where {T <: Real} = T # fallback
95109floattype (:: Type{T} ) where {T <: Union{ShortInts, Bool} } = Float32
96110floattype (:: Type{T} ) where {T <: Integer } = Float64
97111floattype (:: Type{T} ) where {T <: LongInts } = BigFloat
98- floattype (:: Type{FixedPoint{T,f}} ) where {T <: ShortInts ,f} = Float32
99- floattype (:: Type{FixedPoint{T,f}} ) where {T <: Integer ,f} = Float64
100- floattype (:: Type{FixedPoint{T,f}} ) where {T <: LongInts ,f} = BigFloat
101- floattype (:: Type{F} ) where {F <: FixedPoint } = floattype (supertype (F))
102- floattype (x:: FixedPoint ) = floattype (typeof (x))
103-
104- nbitsfrac (:: Type{FixedPoint{T,f}} ) where {T <: Integer ,f} = f
105- nbitsfrac (:: Type{F} ) where {F <: FixedPoint } = nbitsfrac (supertype (F))
112+ floattype (:: Type{X} ) where {T <: ShortInts , X <: FixedPoint{T} } = Float32
113+ floattype (:: Type{X} ) where {T <: Integer , X <: FixedPoint{T} } = Float64
114+ floattype (:: Type{X} ) where {T <: LongInts , X <: FixedPoint{T} } = BigFloat
106115
107- rawtype (:: Type{FixedPoint{T,f}} ) where {T <: Integer ,f} = T
108- rawtype (:: Type{F} ) where {F <: FixedPoint } = rawtype (supertype (F))
109- rawtype (x:: FixedPoint ) = rawtype (typeof (x))
116+ for f in (:zero , :oneunit , :one , :eps , :rawone , :rawtype , :floattype )
117+ @eval begin
118+ $ f (x:: FixedPoint ) = $ f (typeof (x))
119+ end
120+ end
121+ for f in (:div , :fld , :fld1 )
122+ @eval begin
123+ $ f (x:: X , y:: X ) where {X <: FixedPoint } = $ f (x. i, y. i)
124+ end
125+ end
126+ for f in (:rem , :mod , :mod1 , :min , :max )
127+ @eval begin
128+ $ f (x:: X , y:: X ) where {X <: FixedPoint } = X ($ f (x. i, y. i), 0 )
129+ end
130+ end
110131
111132# Printing. These are used to generate type-symbols, so we need them
112133# before we include any files.
@@ -136,10 +157,6 @@ include("normed.jl")
136157include (" deprecations.jl" )
137158const UF = (N0f8, N6f10, N4f12, N2f14, N0f16)
138159
139- eps (:: Type{T} ) where {T <: FixedPoint } = T (oneunit (rawtype (T)),0 )
140- eps (:: T ) where {T <: FixedPoint } = eps (T)
141- sizeof (:: Type{T} ) where {T <: FixedPoint } = sizeof (rawtype (T))
142-
143160# Promotions for reductions
144161const Treduce = Float64
145162Base. add_sum (x:: FixedPoint , y:: FixedPoint ) = Treduce (x) + Treduce (y)
@@ -150,17 +167,6 @@ Base.reduce_empty(::typeof(Base.mul_prod), ::Type{F}) where {F<:FixedPoint} = on
150167Base. reduce_first (:: typeof (Base. mul_prod), x:: FixedPoint ) = Treduce (x)
151168
152169
153- for f in (:div , :fld , :fld1 )
154- @eval begin
155- $ f (x:: T , y:: T ) where {T <: FixedPoint } = $ f (reinterpret (x),reinterpret (y))
156- end
157- end
158- for f in (:rem , :mod , :mod1 , :min , :max )
159- @eval begin
160- $ f (x:: T , y:: T ) where {T <: FixedPoint } = T ($ f (reinterpret (x),reinterpret (y)),0 )
161- end
162- end
163-
164170"""
165171 sd, ad = scaledual(s::Number, a)
166172
0 commit comments