Skip to content

Commit

Permalink
Merge pull request #5 from giordano/mg/fix
Browse files Browse the repository at this point in the history
Fix new definition of `vec_count_ones`
  • Loading branch information
haampie authored Nov 30, 2022
2 parents 75d9b39 + f735c8b commit 64d4d41
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/FastPrimeSieve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,25 @@ const ps = (1, 7, 11, 13, 17, 19, 23, 29)
"""
Population count of a vector of UInt8s for counting prime numbers.
"""
function vec_count_ones(xs::Vector{UInt8})
function vec_count_ones(xs::Vector{UInt8}, n = length(xs))
count = 0
xs64 = reinterpret(UInt, xs)
# Multiple of `sizeof(UInt64)` less than or equal to n
bytes = n & -sizeof(UInt64)
# Explanation: running `count_ones` on `UInt64` is faster than on `UInt8` because
# `count_ones(::UInt8)` converts the output to `Int`, which kills the performance of
# `count_ones`. One could think of making `count::UInt8` and casting
# `count_ones(x)%UInt8`, but that'd make it impossible to work on vectors with more than
# 31 elements without overflowing, so we have to work with the wider type to get the
# best performance and still get meaningful results. Let's reinterpret as much as
# possible of the vector as `UInt64`, on the rest runs `count_ones` without packing.
xs64 = reinterpret(UInt64, @inbounds(@view(xs[1:bytes])))
@simd for x in xs64
count += count_ones(x)
end
# Remainder of the vector which doesn't fit into a `UInt64`.
@simd for idx in (bytes + 1):n
count += @inbounds count_ones(xs[idx])
end
return count
end

Expand Down

2 comments on commit 64d4d41

@haampie
Copy link
Owner Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/121119

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.1 -m "<description of version>" 64d4d41ef3c534868d23afdc79f33d2882e10aa0
git push origin v0.1.1

Please sign in to comment.