Skip to content

Commit 3f1fc0d

Browse files
authored
make add_sum(::Bool, ::BitIntegerSmall) promote to Int (#58374)
1 parent 053c739 commit 3f1fc0d

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

base/reduce.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The reduction operator used in `sum`. The main difference from [`+`](@ref) is th
1414
integers are promoted to `Int`/`UInt`.
1515
"""
1616
add_sum(x, y) = x + y
17-
add_sum(x::BitSignedSmall, y::BitSignedSmall) = Int(x) + Int(y)
17+
add_sum(x::Union{Bool,BitIntegerSmall}, y::Union{Bool,BitIntegerSmall}) = Int(x) + Int(y)
1818
add_sum(x::BitUnsignedSmall, y::BitUnsignedSmall) = UInt(x) + UInt(y)
1919
add_sum(x::Real, y::Real)::Real = x + y
2020

@@ -1073,8 +1073,8 @@ julia> count(i->(4<=i<=6), [2,3,4,5,6])
10731073
julia> count([true, false, true, true])
10741074
3
10751075
1076-
julia> count(>(3), 1:7, init=0x03)
1077-
0x07
1076+
julia> count(>(3), 1:7, init=UInt(0))
1077+
0x0000000000000004
10781078
```
10791079
"""
10801080
count(itr; init=0) = count(identity, itr; init)
@@ -1083,8 +1083,10 @@ count(f, itr; init=0) = _simple_count(f, itr, init)
10831083

10841084
_simple_count(pred, itr, init) = sum(_bool(pred), itr; init)
10851085

1086-
function _simple_count(::typeof(identity), x::Array{Bool}, init::T=0) where {T}
1087-
n::T = init
1086+
function _simple_count(::typeof(identity), x::Array{Bool}, init=0)
1087+
v0 = Base.add_sum(init, false)
1088+
T = typeof(v0)
1089+
n::T = v0
10881090
chunks = length(x) ÷ sizeof(UInt)
10891091
mask = 0x0101010101010101 % UInt
10901092
GC.@preserve x begin

test/reduce.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -586,11 +586,11 @@ struct NonFunctionIsZero end
586586
@test count(NonFunctionIsZero(), [0]) == 1
587587
@test count(NonFunctionIsZero(), [1]) == 0
588588

589-
@test count(Iterators.repeated(true, 3), init=0x04) === 0x07
590-
@test count(!=(2), Iterators.take(1:7, 3), init=Int32(0)) === Int32(2)
591-
@test count(identity, [true, false], init=Int8(5)) === Int8(6)
592-
@test count(!, [true false; false true], dims=:, init=Int16(0)) === Int16(2)
593-
@test isequal(count(identity, [true false; false true], dims=2, init=UInt(4)), reshape(UInt[5, 5], 2, 1))
589+
@test count(Iterators.repeated(true, 3), init=UInt(0)) === UInt(3)
590+
@test count(!=(2), Iterators.take(1:7, 3), init=Int32(0)) === 2
591+
@test count(identity, [true, false], init=Int8(0)) === 1
592+
@test count(!, [true false; false true], dims=:, init=Int16(0)) === 2
593+
@test isequal(count(identity, [true false; false true], dims=2, init=UInt(0)), reshape(UInt[1, 1], 2, 1))
594594

595595
## cumsum, cummin, cummax
596596

test/reducedim.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ safe_minabs(A::Array{T}, region) where {T} = safe_mapslices(minimum, abs.(A), re
8888
@test @inferred(count(!, Breduc, dims=region)) safe_count(.!Breduc, region)
8989

9090
@test isequal(
91-
@inferred(count(Breduc, dims=region, init=0x02)),
92-
safe_count(Breduc, region) .% UInt8 .+ 0x02,
91+
@inferred(Array{UInt8,ndims(Breduc)}, count(Breduc, dims=region, init=0x00)),
92+
safe_count(Breduc, region),
9393
)
9494
@test isequal(
95-
@inferred(count(!, Breduc, dims=region, init=Int16(0))),
96-
safe_count(.!Breduc, region) .% Int16,
95+
@inferred(Array{Int16,ndims(Breduc)}, count(!, Breduc, dims=region, init=Int16(0))),
96+
safe_count(.!Breduc, region),
9797
)
9898
end
9999

@@ -693,7 +693,7 @@ end
693693
@test_throws TypeError count!([1], [1])
694694
end
695695

696-
@test @inferred(count(false:true, dims=:, init=0x0004)) === 0x0005
696+
@test @inferred(UInt16, count(false:true, dims=:, init=0x0000)) === 1
697697
@test @inferred(count(isodd, reshape(1:9, 3, 3), dims=:, init=Int128(0))) === Int128(5)
698698

699699
@testset "reduced_index for BigInt (issue #39995)" begin

0 commit comments

Comments
 (0)