@@ -9,7 +9,9 @@ immutable UFixed{T<:Unsigned,f} <: FixedPoint{T,f}
99end
1010
1111 rawtype {T,f} (:: Type{UFixed{T,f}} ) = T
12+ rawtype (x:: Number ) = rawtype (typeof (x))
1213nbitsfrac {T,f} (:: Type{UFixed{T,f}} ) = f
14+ nbitsfrac (x:: Number ) = nbitsfract (typeof (x))
1315
1416typealias UFixed8 UFixed{UInt8,8 }
1517typealias UFixed10 UFixed{UInt16,10 }
@@ -31,9 +33,8 @@ const uf14 = UFixedConstructor{UInt16,14}()
3133const uf16 = UFixedConstructor {UInt16,16} ()
3234
3335zero {T,f} (:: Type{UFixed{T,f}} ) = UFixed {T,f} (zero (T),0 )
34- @generated function one {T<:UFixed} (:: Type{T} )
35- f = 2 ^ nbitsfrac (T)- 1
36- :( T ($ f,0 ) )
36+ function one {T<:UFixed} (:: Type{T} )
37+ T (typemax (rawtype (T)) >> (8 * sizeof (T)- nbitsfrac (T)), 0 )
3738end
3839zero (x:: UFixed ) = zero (typeof (x))
3940 one (x:: UFixed ) = one (typeof (x))
@@ -65,7 +66,7 @@ rem{T<:UFixed}(x::Real, ::Type{T}) = reinterpret(T, _unsafe_trunc(rawtype(T), ro
6566
6667convert (:: Type{BigFloat} , x:: UFixed ) = reinterpret (x)* (1 / BigFloat (rawone (x)))
6768function convert {T<:AbstractFloat} (:: Type{T} , x:: UFixed )
68- y = reinterpret (x)* (1 / convert (T, rawone (x)))
69+ y = reinterpret (x)* (one ( rawtype (x)) / convert (T, rawone (x)))
6970 convert (T, y) # needed for types like Float16 which promote arithmetic to Float32
7071end
7172convert (:: Type{Bool} , x:: UFixed ) = x == zero (x) ? false : true
@@ -80,13 +81,17 @@ sizeof{T<:UFixed}(::Type{T}) = sizeof(rawtype(T))
8081abs (x:: UFixed ) = x
8182
8283# Arithmetic
84+ @generated function floattype {U<:UFixed} (:: Type{U} )
85+ eps (U) < eps (Float32) ? :(Float64) : :(Float32)
86+ end
87+
8388(- ){T<: UFixed }(x:: T ) = T (- reinterpret (x), 0 )
8489(~ ){T<: UFixed }(x:: T ) = T (~ reinterpret (x), 0 )
8590
8691+ {T,f}(x:: UFixed{T,f} , y:: UFixed{T,f} ) = UFixed {T,f} (convert (T, x. i+ y. i),0 )
8792- {T,f}(x:: UFixed{T,f} , y:: UFixed{T,f} ) = UFixed {T,f} (convert (T, x. i- y. i),0 )
88- * {T<: UFixed }(x:: T , y:: T ) = convert (T,convert (Float32 , x)* convert (Float32 , y))
89- / {T<: UFixed }(x:: T , y:: T ) = convert (T,convert (Float32 , x)/ convert (Float32 , y))
93+ * {T<: UFixed }(x:: T , y:: T ) = convert (T,convert (floattype (T) , x)* convert (floattype (T) , y))
94+ / {T<: UFixed }(x:: T , y:: T ) = convert (T,convert (floattype (T) , x)/ convert (floattype (T) , y))
9095
9196# Comparisons
9297 < {T<: UFixed }(x:: T , y:: T ) = reinterpret (x) < reinterpret (y)
0 commit comments