Skip to content

Commonize promotion rules #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/FixedPointNumbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,35 @@ include("normed.jl")
include("deprecations.jl")
const UF = (N0f8, N6f10, N4f12, N2f14, N0f16)

# Promotions
promote_rule(::Type{X}, ::Type{Tf}) where {X <: FixedPoint, Tf <: AbstractFloat} =
promote_type(floattype(X), Tf)

# Note that `Tr` does not always have enough domains.
promote_rule(::Type{X}, ::Type{Tr}) where {X <: FixedPoint, Tr <: Rational} = Tr

promote_rule(::Type{X}, ::Type{Ti}) where {X <: FixedPoint, Ti <: Integer} = floattype(X)

function promote_rule(::Type{X1}, ::Type{X2}) where {T1, f1, X1 <: FixedPoint{T1,f1},
T2, f2, X2 <: FixedPoint{T2,f2}}
X = wrapper(X1)
X !== wrapper(X2) && return promote_type(floattype(X1), floattype(X2))

f = max(f1, f2) # ensure we have enough precision
Tp = promote_type(T1, T2)
T = (T1 <: Signed || T2 <: Signed) ? signedtype(Tp) : Tp
# make sure we have enough integer bits
m = max(nbitsint(X1), nbitsint(X2))
_widen_rawtype(X{T,f}, m)
end

function _widen_rawtype(::Type{X}, m) where {T, f, X<:FixedPoint{T,f}}
nbitsint(X) >= m && return X
Tw = widen1(T)
T === Tw && return X
_widen_rawtype(wrapper(X){Tw,f}, m)
end

# Promotions for reductions
const Treduce = Float64
Base.add_sum(x::FixedPoint, y::FixedPoint) = Treduce(x) + Treduce(y)
Expand Down
19 changes: 0 additions & 19 deletions src/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,24 +195,5 @@ length(r::AbstractUnitRange{F}) where {F <: Fixed{<:SShorterThanInt,f}} where {f
length(r::AbstractUnitRange{F}) where {F <: Fixed{T}} where {T <: Signed} =
checked_add(checked_sub(floor(T, last(r)), floor(T, first(r))), oneunit(T))

promote_rule(ft::Type{Fixed{T,f}}, ::Type{TI}) where {T,f,TI <: Integer} = Fixed{T,f}
promote_rule(::Type{Fixed{T,f}}, ::Type{TF}) where {T,f,TF <: AbstractFloat} = TF
promote_rule(::Type{Fixed{T,f}}, ::Type{Rational{TR}}) where {T,f,TR} = Rational{TR}

@generated function promote_rule(::Type{Fixed{T1,f1}}, ::Type{Fixed{T2,f2}}) where {T1,T2,f1,f2}
f = max(f1, f2) # ensure we have enough precision
T = promote_type(T1, T2)
# make sure we have enough integer bits
i1, i2 = bitwidth(T1)-f1, bitwidth(T2)-f2 # number of integer bits for each
i = bitwidth(T)-f
while i < max(i1, i2)
Tw = widen1(T)
T == Tw && break
T = Tw
i = bitwidth(T)-f
end
:(Fixed{$T,$f})
end

# TODO: Document and check that it still does the right thing.
decompose(x::Fixed{T,f}) where {T,f} = x.i, -f, 1
21 changes: 0 additions & 21 deletions src/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -303,24 +303,3 @@ length(r::AbstractUnitRange{N}) where {N <: Normed{<:UShorterThanInt}} =
floor(Int, last(r)) - floor(Int, first(r)) + 1
length(r::AbstractUnitRange{N}) where {N <: Normed{T}} where {T<:Unsigned} =
r.start > r.stop ? T(0) : checked_add(floor(T, last(r)) - floor(T, first(r)), oneunit(T))

# Promotions
promote_rule(::Type{T}, ::Type{Tf}) where {T <: Normed,Tf <: AbstractFloat} = promote_type(floattype(T), Tf)
promote_rule(::Type{T}, ::Type{R}) where {T <: Normed,R <: Rational} = R
function promote_rule(::Type{T}, ::Type{Ti}) where {T <: Normed,Ti <: Union{Signed, Unsigned}}
floattype(T)
end
@generated function promote_rule(::Type{Normed{T1,f1}}, ::Type{Normed{T2,f2}}) where {T1,T2,f1,f2}
f = max(f1, f2) # ensure we have enough precision
T = promote_type(T1, T2)
# make sure we have enough integer bits
i1, i2 = bitwidth(T1)-f1, bitwidth(T2)-f2 # number of integer bits for each
i = bitwidth(T)-f
while i < max(i1, i2)
Tw = widen1(T)
T == Tw && break
T = Tw
i = bitwidth(T)-f
end
:(Normed{$T,$f})
end
5 changes: 3 additions & 2 deletions src/utilities.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# utility functions and macros, which are independent of `FixedPoint`
bitwidth(T::Type) = 8sizeof(T)

widen1(T::Type) = T # fallback
widen1(::Type{Int8}) = Int16
widen1(::Type{UInt8}) = UInt16
widen1(::Type{Int16}) = Int32
Expand All @@ -9,8 +10,6 @@ widen1(::Type{Int32}) = Int64
widen1(::Type{UInt32}) = UInt64
widen1(::Type{Int64}) = Int128
widen1(::Type{UInt64}) = UInt128
widen1(::Type{Int128}) = Int128
widen1(::Type{UInt128}) = UInt128
widen1(x::Integer) = x % widen1(typeof(x))

signedtype(::Type{T}) where {T <: Integer} = typeof(signed(zero(T)))
Expand Down Expand Up @@ -48,3 +47,5 @@ if !signbit(signed(unsafe_trunc(UInt, -12.345)))
# exclude BigFloat (issue #202)
_unsafe_trunc(::Type{T}, x::BigFloat) where {T <: Integer} = unsafe_trunc(T, x)
end

wrapper(@nospecialize(T)) = Base.typename(T).wrapper
25 changes: 18 additions & 7 deletions test/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -455,13 +455,24 @@ end
@test Fixed{Int16,3}(-1) == Fixed{Int8,5}(-1)
@test Fixed{Int16,3}(0.25) == Fixed{Int8,5}(0.25)

@test promote_type(Q0f7,Float32,Int) == Float32
@test promote_type(Q0f7,Int,Float32) == Float32
@test promote_type(Int,Q0f7,Float32) == Float32
@test promote_type(Int,Float32,Q0f7) == Float32
@test promote_type(Float32,Int,Q0f7) == Float32
@test promote_type(Float32,Q0f7,Int) == Float32
@test promote_type(Q0f7,Q1f6,Q2f5,Q3f4,Q4f3,Q5f2) == Fixed{Int128,7}
@test @inferred(promote_type(Q0f7, Float64)) === Float64
@test @inferred(promote_type(Float32, Q7f24)) === Float64

@test @inferred(promote_type(Q0f7, Int8)) === Float32
@test @inferred(promote_type(Int128, Q7f24)) === Float64

@test @inferred(promote_type(Q0f15, Rational{UInt8})) === Rational{UInt8}

@test @inferred(promote_type(Q0f7, Float32, Int)) === Float32
@test @inferred(promote_type(Q0f7, Int, Float32)) === Float32
@test @inferred(promote_type(Int, Q0f7, Float32)) === Float32
@test @inferred(promote_type(Int, Float32, Q0f7)) === Float32
@test @inferred(promote_type(Float32, Int, Q0f7)) === Float32
@test @inferred(promote_type(Float32, Q0f7, Int)) === Float32

@test @inferred(promote_type(Q0f7,Q1f6,Q2f5,Q3f4,Q4f3,Q5f2)) == Fixed{Int128,7}

@test @inferred(promote_type(Q0f7, N0f32)) === Float64
end

@testset "show" begin
Expand Down
25 changes: 18 additions & 7 deletions test/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -485,13 +485,24 @@ end
@test Normed{UInt16,4}(1) == Normed{UInt8,6}(1)
@test Normed{UInt16,4}(0.2) == Normed{UInt8,6}(0.2)

@test promote_type(N0f8,Float32,Int) == Float32
@test promote_type(N0f8,Int,Float32) == Float32
@test promote_type(Int,N0f8,Float32) == Float32
@test promote_type(Int,Float32,N0f8) == Float32
@test promote_type(Float32,Int,N0f8) == Float32
@test promote_type(Float32,N0f8,Int) == Float32
@test promote_type(N0f8,N1f7,N2f6,N3f5,N4f4,N5f3) == Normed{UInt128,8}
@test @inferred(promote_type(N0f8, Float64)) === Float64
@test @inferred(promote_type(Float32, N8f24)) === Float64

@test @inferred(promote_type(N0f8, Int8)) === Float32
@test @inferred(promote_type(Int128, N8f24)) === Float64

@test @inferred(promote_type(N0f16, Rational{Int8})) === Rational{Int8}

@test @inferred(promote_type(N0f8, Float32, Int)) === Float32
@test @inferred(promote_type(N0f8, Int, Float32)) === Float32
@test @inferred(promote_type(Int, N0f8, Float32)) === Float32
@test @inferred(promote_type(Int, Float32, N0f8)) === Float32
@test @inferred(promote_type(Float32, Int, N0f8)) === Float32
@test @inferred(promote_type(Float32, N0f8, Int)) === Float32

@test @inferred(promote_type(N0f8,N1f7,N2f6,N3f5,N4f4,N5f3)) === Normed{UInt128,8}

@test @inferred(promote_type(N0f8, Q0f31)) === Float64
end

@testset "show" begin
Expand Down