@@ -37,6 +37,10 @@ include("utilities.jl")
37
37
reinterpret (x:: FixedPoint ) = x. i
38
38
reinterpret (:: Type{T} , x:: FixedPoint{T,f} ) where {T,f} = x. i
39
39
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
+
40
44
# construction using the (approximate) intended value, i.e., N0f8
41
45
* (x:: Real , :: Type{X} ) where {X<: FixedPoint } = X (x)
42
46
61
65
# predicates
62
66
isinteger (x:: FixedPoint{T,f} ) where {T,f} = (x. i& (1 << f- 1 )) == 0
63
67
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
+
64
76
# traits
77
+ sizeof (:: Type{X} ) where {X <: FixedPoint } = sizeof (rawtype (X))
78
+ eps (:: Type{X} ) where {X <: FixedPoint } = X (oneunit (rawtype (X)), 0 )
65
79
typemax (:: Type{T} ) where {T <: FixedPoint } = T (typemax (rawtype (T)), 0 )
66
80
typemin (:: Type{T} ) where {T <: FixedPoint } = T (typemin (rawtype (T)), 0 )
67
81
floatmin (:: Type{T} ) where {T <: FixedPoint } = eps (T)
@@ -95,18 +109,25 @@ floattype(::Type{T}) where {T <: Real} = T # fallback
95
109
floattype (:: Type{T} ) where {T <: Union{ShortInts, Bool} } = Float32
96
110
floattype (:: Type{T} ) where {T <: Integer } = Float64
97
111
floattype (:: 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
106
115
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
110
131
111
132
# Printing. These are used to generate type-symbols, so we need them
112
133
# before we include any files.
@@ -136,10 +157,6 @@ include("normed.jl")
136
157
include (" deprecations.jl" )
137
158
const UF = (N0f8, N6f10, N4f12, N2f14, N0f16)
138
159
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
-
143
160
# Promotions for reductions
144
161
const Treduce = Float64
145
162
Base. 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
150
167
Base. reduce_first (:: typeof (Base. mul_prod), x:: FixedPoint ) = Treduce (x)
151
168
152
169
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
-
164
170
"""
165
171
sd, ad = scaledual(s::Number, a)
166
172
0 commit comments