Skip to content

Commit

Permalink
Merge pull request #13809 from JuliaLang/teh/fix_reductions
Browse files Browse the repository at this point in the history
Reductions shouldn't overflow for LinearSlow arrays
  • Loading branch information
timholy committed Oct 28, 2015
2 parents 344fdc2 + a159eb0 commit 00ea197
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 4 additions & 4 deletions base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ function mapfoldl_impl(f, op, v0, itr, i)
# Unroll the while loop once; if v0 is known, the call to op may
# be evaluated at compile time
if done(itr, i)
return v0
return r_promote(op, v0)
else
(x, i) = next(itr, i)
v = op(v0, f(x))
v = op(r_promote(op, v0), f(x))
while !done(itr, i)
(x, i) = next(itr, i)
v = op(v, f(x))
Expand Down Expand Up @@ -71,10 +71,10 @@ function mapfoldr_impl(f, op, v0, itr, i::Integer)
# Unroll the while loop once; if v0 is known, the call to op may
# be evaluated at compile time
if i == 0
return v0
return r_promote(op, v0)
else
x = itr[i]
v = op(f(x), v0)
v = op(f(x), r_promote(op, v0))
while i > 1
x = itr[i -= 1]
v = op(f(x), v)
Expand Down
8 changes: 8 additions & 0 deletions test/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ end
@test sum(collect(map(UInt8,0:255))) == 32640
@test sum(collect(map(UInt8,254:255))) == 509

A = reshape(map(UInt8, 101:109), (3,3))
@test @inferred(sum(A)) == 945
@test @inferred(sum(sub(A, 1:3, 1:3))) == 945

A = reshape(map(UInt8, 1:100), (10,10))
@test @inferred(sum(A)) == 5050
@test @inferred(sum(sub(A, 1:10, 1:10))) == 5050

# issue #11618
@test sum([-0.0]) === -0.0
@test sum([-0.0, -0.0]) === -0.0
Expand Down

1 comment on commit 00ea197

@felipenoris
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

28 thousand commits

Please sign in to comment.