From a159eb086994f8cb4e8ab399c401767c52fa4afc Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Wed, 28 Oct 2015 15:13:17 -0500 Subject: [PATCH] Reductions shouldn't overflow for LinearSlow arrays --- base/reduce.jl | 8 ++++---- test/reduce.jl | 8 ++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/base/reduce.jl b/base/reduce.jl index 7dbf314e7b601..1033394282f68 100644 --- a/base/reduce.jl +++ b/base/reduce.jl @@ -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)) @@ -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) diff --git a/test/reduce.jl b/test/reduce.jl index c5accb3db1542..79709e437bffe 100644 --- a/test/reduce.jl +++ b/test/reduce.jl @@ -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