Skip to content
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

Update reduction machinery for 0.7 #100

Merged
merged 2 commits into from
Feb 1, 2018
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
27 changes: 17 additions & 10 deletions src/FixedPointNumbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Base: ==, <, <=, -, +, *, /, ~, isapprox,
zero, oneunit, one, typemin, typemax, realmin, realmax, eps, sizeof, reinterpret,
float, trunc, round, floor, ceil, bswap,
div, fld, rem, mod, mod1, fld1, min, max, minmax,
start, next, done, reducedim_init, rand
start, next, done, rand
if isdefined(Base, :rem1)
import Base: rem1
end
Expand Down Expand Up @@ -134,20 +134,27 @@ sizeof(::Type{T}) where {T <: FixedPoint} = sizeof(rawtype(T))
# Promotions for reductions
const Treduce = Float64
if isdefined(Base, :r_promote)
# Julia v0.6
Base.r_promote(::typeof(+), x::FixedPoint{T}) where {T} = Treduce(x)
Base.r_promote(::typeof(*), x::FixedPoint{T}) where {T} = Treduce(x)
Base.reducedim_init(f::typeof(identity),
op::typeof(+),
A::AbstractArray{T}, region) where {T <: FixedPoint} =
Base.reducedim_initarray(A, region, zero(Treduce))
Base.reducedim_init(f::typeof(identity),
op::typeof(*),
A::AbstractArray{T}, region) where {T <: FixedPoint} =
Base.reducedim_initarray(A, region, oneunit(Treduce))
else
Base.promote_sys_size(::Type{<:FixedPoint}) = Treduce
# Julia v0.7
Base.add_sum(x::FixedPoint, y::FixedPoint) = Treduce(x) + Treduce(y)
Base.reduce_empty(::typeof(Base.add_sum), ::Type{F}) where {F<:FixedPoint} = zero(Treduce)
Base.reduce_first(::typeof(Base.add_sum), x::FixedPoint) = Treduce(x)
Base.mul_prod(x::FixedPoint, y::FixedPoint) = Treduce(x) * Treduce(y)
Base.reduce_empty(::typeof(Base.mul_prod), ::Type{F}) where {F<:FixedPoint} = one(Treduce)
Base.reduce_first(::typeof(Base.mul_prod), x::FixedPoint) = Treduce(x)
end

reducedim_init(f::typeof(identity),
op::typeof(+),
A::AbstractArray{T}, region) where {T <: FixedPoint} =
reducedim_initarray(A, region, zero(Treduce))
reducedim_init(f::typeof(identity),
op::typeof(*),
A::AbstractArray{T}, region) where {T <: FixedPoint} =
reducedim_initarray(A, region, oneunit(Treduce))

for f in (:div, :fld, :fld1)
@eval begin
Expand Down
1 change: 1 addition & 0 deletions src/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ struct Fixed{T <: Signed,f} <: FixedPoint{T, f}
# selected by passing an extra dummy argument
Fixed{T, f}(i::Integer, _) where {T,f} = new{T, f}(i % T)
Fixed{T, f}(x) where {T,f} = convert(Fixed{T,f}, x)
Fixed{T, f}(x::Fixed{T,f}) where {T,f} = x
end

reinterpret(::Type{Fixed{T,f}}, x::T) where {T <: Signed,f} = Fixed{T,f}(x, 0)
Expand Down
1 change: 1 addition & 0 deletions src/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ struct Normed{T<:Unsigned,f} <: FixedPoint{T,f}

Normed{T, f}(i::Integer,_) where {T,f} = new{T, f}(i%T) # for setting by raw representation
Normed{T, f}(x) where {T,f} = convert(Normed{T,f}, x)
Normed{T, f}(x::Normed{T,f}) where {T,f} = x
end

typechar(::Type{X}) where {X <: Normed} = 'N'
Expand Down
8 changes: 6 additions & 2 deletions test/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ end
function testtrunc(inc::T) where {T}
incf = convert(Float64, inc)
tm = reinterpret(typemax(T))/reinterpret(one(T))
x = zero(T)
local x = zero(T)
for i = 0 : min(1e6, reinterpret(typemax(T))-1)
xf = incf*i
try
Expand Down Expand Up @@ -305,7 +305,11 @@ end
@test N0f16(Float16(1.0)) === N0f16(1.0)
@test Float16(1.0) % N0f16 === N0f16(1.0)

if VERSION >= v"0.7.0-DEV.1790"
if VERSION >= v"0.7.0-DEV.2657"
a = N0f8[0.2, 0.4]
@test summary(a) == "2-element Array{N0f8,1} with eltype Normed{UInt8,8}"
@test summary(view(a, 1:2)) == "2-element view(::Array{N0f8,1}, 1:2) with eltype Normed{UInt8,8}"
elseif VERSION >= v"0.7.0-DEV.1790"
a = N0f8[0.2, 0.4]
@test summary(a) == "2-element Array{N0f8,1} with eltype FixedPointNumbers.Normed{UInt8,8}"
@test summary(view(a, 1:2)) == "2-element view(::Array{N0f8,1}, 1:2) with eltype FixedPointNumbers.Normed{UInt8,8}"
Expand Down
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using FixedPointNumbers, Base.Test

@test isempty(detect_ambiguities(FixedPointNumbers, Base, Core))
if VERSION < v"0.7.0-"
@test isempty(detect_ambiguities(FixedPointNumbers, Base, Core))
end

for f in ["normed.jl", "fixed.jl"]
println("Testing $f")
Expand Down