Skip to content
Closed
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
13 changes: 3 additions & 10 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -660,16 +660,9 @@ prod(x::Tuple{}) = 1
# than the general prod definition is available.
prod(x::Tuple{Int, Vararg{Int}}) = *(x...)

all(x::Tuple{}) = true
all(x::Tuple{Bool}) = x[1]
all(x::Tuple{Bool, Bool}) = x[1]&x[2]
all(x::Tuple{Bool, Bool, Bool}) = x[1]&x[2]&x[3]
# use generic reductions for the rest

any(x::Tuple{}) = false
any(x::Tuple{Bool}) = x[1]
any(x::Tuple{Bool, Bool}) = x[1]|x[2]
any(x::Tuple{Bool, Bool, Bool}) = x[1]|x[2]|x[3]
all(x::NTuple{N,Bool}) where N = N <= 32 ? reduce(&, x; init = true) : _all(x, :)

any(x::NTuple{N,Bool}) where N = N <= 32 ? reduce(|, x; init = false) : _any(x, :)
Comment on lines +663 to +665
Copy link
Member

@nsajko nsajko Jan 17, 2025

Choose a reason for hiding this comment

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

Given that the tuple is homogeneous, there's no need for recursion, we can handle inputs of all sizes with a loop. This results in the same LLVM code for a length-32 tuple as with your variant with reduce:

Suggested change
all(x::NTuple{N,Bool}) where N = N <= 32 ? reduce(&, x; init = true) : _all(x, :)
any(x::NTuple{N,Bool}) where N = N <= 32 ? reduce(|, x; init = false) : _any(x, :)
function _all_bool(x::Tuple{Vararg{Bool}})
@_terminates_locally_meta
r = true
for i eachindex(x)
r &= getfield(x, i, false) # avoid `getindex` bounds checking to help vectorization
end
r
end
function _any_bool(x::Tuple{Vararg{Bool}})
@_terminates_locally_meta
r = false
for i eachindex(x)
r |= getfield(x, i, false) # avoid `getindex` bounds checking to help vectorization
end
r
end
any(x::Tuple{Vararg{Bool}}) = _any_bool(x)
all(x::Tuple{Vararg{Bool}}) = _all_bool(x)
all(x::Tuple{Bool}) = _all_bool(x) # disambiguate


# a version of `in` esp. for NamedTuple, to make it pure, and not compiled for each tuple length
function sym_in(x::Symbol, itr::Tuple{Vararg{Symbol}})
Expand Down