@@ -19,11 +19,7 @@ typealias UFixed16 UFixed{UInt16,16}
19
19
20
20
const UF = (UFixed8, UFixed10, UFixed12, UFixed14, UFixed16)
21
21
22
- for (uf) in UF
23
- T = rawtype (uf)
24
- f = nbitsfrac (uf)
25
- @eval reinterpret (:: Type{UFixed{$T,$f}} , x:: $T ) = UFixed {$T,$f} (x, 0 )
26
- end
22
+ reinterpret {T<:Unsigned, f} (:: Type{UFixed{T,f}} , x:: T ) = UFixed {T,f} (x, 0 )
27
23
28
24
# The next lines mimic the floating-point literal syntax "3.2f0"
29
25
immutable UFixedConstructor{T,f} end
@@ -35,14 +31,7 @@ const uf14 = UFixedConstructor{UInt16,14}()
35
31
const uf16 = UFixedConstructor {UInt16,16} ()
36
32
37
33
zero {T,f} (:: Type{UFixed{T,f}} ) = UFixed {T,f} (zero (T),0 )
38
- for uf in UF
39
- TT = rawtype (uf)
40
- f = nbitsfrac (uf)
41
- T = UFixed{TT,f}
42
- @eval begin
43
- one (:: Type{$T} ) = $ T ($ (2 ^ f- 1 ),0 )
44
- end
45
- end
34
+ one {T<:UFixed} (:: Type{T} ) = T ((2 ^ nbitsfrac (T)- 1 ),0 )
46
35
zero (x:: UFixed ) = zero (typeof (x))
47
36
one (x:: UFixed ) = one (typeof (x))
48
37
rawone (v) = reinterpret (one (v))
@@ -95,15 +84,21 @@ abs(x::UFixed) = x
95
84
# Functions
96
85
trunc {T<:UFixed} (x:: T ) = T (div (reinterpret (x), rawone (T))* rawone (T),0 )
97
86
floor {T<:UFixed} (x:: T ) = trunc (x)
98
- for T in UF
99
- f = nbitsfrac (T)
100
- R = rawtype (T)
101
- roundmask = convert (R, 1 << (f- 1 ))
102
- k = 8 * sizeof (R)- f
103
- ceilmask = (typemax (R)<< k)>> k
104
- @eval begin
105
- round (x:: $T ) = (y = trunc (x); return convert (rawtype ($ T), reinterpret (x)- reinterpret (y))& $ roundmask> 0 ? $ T (y+ one ($ T)) : y)
106
- ceil (x:: $T ) = (y = trunc (x); return convert (rawtype ($ T), reinterpret (x)- reinterpret (y))& $ ceilmask > 0 ? $ T (y+ one ($ T)) : y)
87
+ @generated function round {T,f} (x:: UFixed{T,f} )
88
+ mask = convert (T, 1 << (f- 1 ))
89
+ quote
90
+ y = trunc (x)
91
+ return convert (T, reinterpret (x)- reinterpret (y)) & $ mask> 0 ?
92
+ UFixed {T,f} (y+ one (UFixed{T,f})) : y
93
+ end
94
+ end
95
+ @generated function ceil {T,f} (x:: UFixed{T,f} )
96
+ k = 8 * sizeof (T)- f
97
+ mask = (typemax (T)<< k)>> k
98
+ quote
99
+ y = trunc (x)
100
+ return convert (T, reinterpret (x)- reinterpret (y)) & ($ mask)> 0 ?
101
+ UFixed {T,f} (y+ one (UFixed{T,f})) : y
107
102
end
108
103
end
109
104
@@ -152,25 +147,8 @@ function decompose(x::UFixed)
152
147
end
153
148
154
149
# Promotions
155
- for T in UF
156
- @eval begin
157
- promote_rule (:: Type{$T} , :: Type{Float32} ) = Float32
158
- promote_rule (:: Type{$T} , :: Type{Float64} ) = Float64
159
- promote_rule {TR<:Rational} (:: Type{$T} , :: Type{TR} ) = TR
160
- end
161
- for Ti in (Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64)
162
- Tp = eps (convert (Float32, typemax (Ti))) > eps (T) ? Float64 : Float32
163
- @eval begin
164
- promote_rule (:: Type{$T} , :: Type{$Ti} ) = $ Tp
165
- end
166
- end
167
- end
168
-
169
- # Show
170
- function show {T,f} (io:: IO , x:: UFixed{T,f} )
171
- print (io, " UFixed" , f)
172
- print (io, " (" )
173
- showcompact (io, x)
174
- print (io, " )" )
175
- end
176
- showcompact {T,f} (io:: IO , x:: UFixed{T,f} ) = show (io, round (convert (Float64,x), ceil (Int,f/ _log2_10)))
150
+ promote_rule {T<:UFixed} (:: Type{T} , :: Type{Float32} ) = Float32
151
+ promote_rule {T<:UFixed} (:: Type{T} , :: Type{Float64} ) = Float64
152
+ promote_rule {T<:UFixed, R<:Rational} (:: Type{T} , :: Type{R} ) = R
153
+ promote_rule {T<:UFixed, Ti<:Union{Signed,Unsigned}} (:: Type{T} , :: Type{Ti} ) =
154
+ eps (convert (Float32, typemax (Ti))) > eps (T) ? Float64 : Float32
0 commit comments