-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CRC32c] Support AbstractVector{UInt8} as input #56164
Open
nhz2
wants to merge
5
commits into
JuliaLang:master
Choose a base branch
from
nhz2:nz/crc32c-of-abstract-vector
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6dae738
fallback for crc32c of AbstractVector{UInt8}
nhz2 af75630
use Memory for temp buffer
nhz2 2d34e8a
Update stdlib/CRC32c/test/runtests.jl
nhz2 57815be
move method out of Base
nhz2 e852481
Merge branch 'master' into nz/crc32c-of-abstract-vector
nhz2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ See [`CRC32c.crc32c`](@ref) for more information. | |
""" | ||
module CRC32c | ||
|
||
import Base.FastContiguousSubArray | ||
import Base: DenseBytes | ||
|
||
export crc32c | ||
|
@@ -16,9 +15,9 @@ export crc32c | |
crc32c(data, crc::UInt32=0x00000000) | ||
|
||
Compute the CRC-32c checksum of the given `data`, which can be | ||
an `Array{UInt8}`, a contiguous subarray thereof, or a `String`. Optionally, you can pass | ||
a starting `crc` integer to be mixed in with the checksum. The `crc` parameter | ||
can be used to compute a checksum on data divided into chunks: performing | ||
an `Array{UInt8}`, a contiguous subarray thereof, an `AbstractVector{UInt8}`, or a `String`. | ||
Optionally, you can pass a starting `crc` integer to be mixed in with the checksum. | ||
The `crc` parameter can be used to compute a checksum on data divided into chunks: performing | ||
`crc32c(data2, crc32c(data1))` is equivalent to the checksum of `[data1; data2]`. | ||
(Technically, a little-endian checksum is computed.) | ||
|
||
|
@@ -30,11 +29,26 @@ calling [`take!`](@ref). | |
|
||
For a `String`, note that the result is specific to the UTF-8 encoding | ||
(a different checksum would be obtained from a different Unicode encoding). | ||
To checksum an `a::Array` of some other bitstype, you can do `crc32c(reinterpret(UInt8,a))`, | ||
To checksum an `a::AbstractArray` of some other bitstype without padding, | ||
you can do `crc32c(vec(reinterpret(UInt8,a)))`, | ||
but note that the result may be endian-dependent. | ||
""" | ||
function crc32c end | ||
|
||
function crc32c(a::AbstractVector{UInt8}, crc::UInt32=0x00000000) | ||
# use block size 24576=8192*3, since that is the threshold for | ||
# 3-way parallel SIMD code in the underlying jl_crc32c C function. | ||
last = lastindex(a) | ||
nb = length(a) | ||
buf = Memory{UInt8}(undef, Int(min(nb, 24576))) | ||
while nb > 0 | ||
n = min(nb, 24576) | ||
copyto!(buf, 1, a, last - nb + 1, n) | ||
crc = Base.unsafe_crc32c(buf, n % Csize_t, crc) | ||
nb -= n | ||
end | ||
return crc | ||
end | ||
|
||
function crc32c(a::DenseBytes, crc::UInt32=0x00000000) | ||
Base._crc32c(a, crc) | ||
|
@@ -51,6 +65,5 @@ mixed with a starting `crc` integer. If `nb` is not supplied, then | |
""" | ||
crc32c(io::IO, nb::Integer, crc::UInt32=0x00000000) = Base._crc32c(io, nb, crc) | ||
crc32c(io::IO, crc::UInt32=0x00000000) = Base._crc32c(io, crc) | ||
crc32c(io::IOStream, crc::UInt32=0x00000000) = Base._crc32c(io, crc) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is redundant with the method above. |
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't work if
a
has padding or is multidimensional. I updated the docstring to note thata
must not have padding, and thatvec
should be used in addition toreinterpret(UInt8
.