From 39ce7a8e67c365288e8ab9cd38ad32cd6733ad14 Mon Sep 17 00:00:00 2001 From: kimikage Date: Sat, 28 Dec 2019 11:56:12 +0900 Subject: [PATCH] Modify Fixed constructors This adds explicit input range checks. --- src/fixed.jl | 28 +++++++++++++++++++++++++--- test/fixed.jl | 22 +++++++++++++++------- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/fixed.jl b/src/fixed.jl index df747140..d185e58c 100644 --- a/src/fixed.jl +++ b/src/fixed.jl @@ -48,17 +48,39 @@ fracmask(x::Fixed{T,f}) where {T, f} = ~intmask(x) # Signed # constructor-style conversions function _convert(::Type{F}, x::Fixed{T2,f2}) where {T, T2, f, f2, F <: Fixed{T,f}} - y = round(((1<> f) <= x) & (x <= (typemax(T) >> f)) + reinterpret(F, unsafe_trunc(T, x) << f) + else + throw_converterror(F, x) + end end function _convert(::Type{F}, x::AbstractFloat) where {T, f, F <: Fixed{T,f}} - reinterpret(F, round(T, trunc(widen1(T),x)<