@@ -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 }
@@ -35,6 +37,7 @@ zero{T,f}(::Type{UFixed{T,f}}) = UFixed{T,f}(zero(T),0)
3537 f = 2 ^ nbitsfrac (T)- 1
3638 :( T ($ f,0 ) )
3739end
40+ one {f} (:: Type{UFixed{UInt128,f}} ) = UFixed {UInt128,f} (UInt128 (2 )^ f- 1 , 0 )
3841zero (x:: UFixed ) = zero (typeof (x))
3942 one (x:: UFixed ) = one (typeof (x))
4043rawone (v) = reinterpret (one (v))
@@ -65,7 +68,7 @@ rem{T<:UFixed}(x::Real, ::Type{T}) = reinterpret(T, _unsafe_trunc(rawtype(T), ro
6568
6669convert (:: Type{BigFloat} , x:: UFixed ) = reinterpret (x)* (1 / BigFloat (rawone (x)))
6770function convert {T<:AbstractFloat} (:: Type{T} , x:: UFixed )
68- y = reinterpret (x)* (1 / convert (T, rawone (x)))
71+ y = reinterpret (x)* (one ( rawtype (x)) / convert (T, rawone (x)))
6972 convert (T, y) # needed for types like Float16 which promote arithmetic to Float32
7073end
7174convert (:: Type{Bool} , x:: UFixed ) = x == zero (x) ? false : true
@@ -80,13 +83,17 @@ sizeof{T<:UFixed}(::Type{T}) = sizeof(rawtype(T))
8083abs (x:: UFixed ) = x
8184
8285# Arithmetic
86+ @generated function floattype {U<:UFixed} (:: Type{U} )
87+ eps (U) < eps (Float32) ? :(Float64) : :(Float32)
88+ end
89+
8390(- ){T<: UFixed }(x:: T ) = T (- reinterpret (x), 0 )
8491(~ ){T<: UFixed }(x:: T ) = T (~ reinterpret (x), 0 )
8592
8693+ {T,f}(x:: UFixed{T,f} , y:: UFixed{T,f} ) = UFixed {T,f} (convert (T, x. i+ y. i),0 )
8794- {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))
95+ * {T<: UFixed }(x:: T , y:: T ) = convert (T,convert (floattype (T) , x)* convert (floattype (T) , y))
96+ / {T<: UFixed }(x:: T , y:: T ) = convert (T,convert (floattype (T) , x)/ convert (floattype (T) , y))
9097
9198# Comparisons
9299 < {T<: UFixed }(x:: T , y:: T ) = reinterpret (x) < reinterpret (y)
0 commit comments