@@ -41,16 +41,27 @@ rawone(v) = reinterpret(one(v))
4141
4242# Conversions
4343convert {T<:UFixed} (:: Type{T} , x:: T ) = x
44- convert {T1<:UFixed} (:: Type{T1} , x:: UFixed ) = reinterpret (T1, round (rawtype (T1), (rawone (T1)/ rawone (x))* reinterpret (x)))
44+ function convert {T<:UFixed} (:: Type{T} , x:: UFixed )
45+ y = round ((rawone (T)/ rawone (x))* reinterpret (x))
46+ (0 <= y) & (y <= typemax (rawtype (T))) || throw_converterror (T, x)
47+ reinterpret (T, _unsafe_trunc (rawtype (T), y))
48+ end
4549convert (:: Type{UFixed16} , x:: UFixed8 ) = reinterpret (UFixed16, convert (UInt16, 0x0101 * reinterpret (x)))
4650convert {U<:UFixed} (:: Type{U} , x:: Real ) = _convert (U, rawtype (U), x)
47- _convert {U<:UFixed,T} (:: Type{U} , :: Type{T} , x) = U (round (T, widen1 (rawone (U))* x), 0 )
48- _convert {U<:UFixed } (:: Type{U} , :: Type{UInt128} , x) = U (round (UInt128, rawone (U)* x), 0 )
51+ function _convert {U<:UFixed,T} (:: Type{U} , :: Type{T} , x)
52+ y = round (widen1 (rawone (U))* x)
53+ (0 <= y) & (y <= typemax (T)) || throw_converterror (U, x)
54+ U (_unsafe_trunc (T, y), 0 )
55+ end
56+ function _convert {U<:UFixed} (:: Type{U} , :: Type{UInt128} , x)
57+ y = round (rawone (U)* x) # for UInt128, we can't widen
58+ (0 <= y) & (y <= typemax (UInt128)) & (x <= Float64 (typemax (U))) || throw_converterror (U, x)
59+ U (_unsafe_trunc (UInt128, y), 0 )
60+ end
4961
5062rem {T<:UFixed} (x:: T , :: Type{T} ) = x
51- rem {T<:UFixed} (x:: UFixed , :: Type{T} ) = reinterpret (T, unsafe_trunc (rawtype (T), round ((rawone (T)/ rawone (x))* reinterpret (x))))
52- rem {T<:UFixed} (x:: Real , :: Type{T} ) = reinterpret (T, unsafe_trunc (rawtype (T), round (rawone (T)* x)))
53- rem {T<:UFixed} (x:: Integer , :: Type{T} ) = reinterpret (T, (rawone (T)* x) % rawtype (T)) # can be deleted once unsafe_trunc supports integer types (julia #18629)
63+ rem {T<:UFixed} (x:: UFixed , :: Type{T} ) = reinterpret (T, _unsafe_trunc (rawtype (T), round ((rawone (T)/ rawone (x))* reinterpret (x))))
64+ rem {T<:UFixed} (x:: Real , :: Type{T} ) = reinterpret (T, _unsafe_trunc (rawtype (T), round (rawone (T)* x)))
5465
5566convert (:: Type{BigFloat} , x:: UFixed ) = reinterpret (x)* (1 / BigFloat (rawone (x)))
5667function convert {T<:AbstractFloat} (:: Type{T} , x:: UFixed )
163174 end
164175 :(UFixed{$ T,$ f})
165176end
177+
178+ _unsafe_trunc {T} (:: Type{T} , x:: Integer ) = x % T
179+ _unsafe_trunc {T} (:: Type{T} , x) = unsafe_trunc (T, x)
0 commit comments