Skip to content
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

CRC big endian support #97

Merged
merged 25 commits into from
Oct 29, 2024
Merged

CRC big endian support #97

merged 25 commits into from
Oct 29, 2024

Conversation

DmitriyMusatkin
Copy link
Contributor

Issue #, if available:

Description of changes:
big endian support for crc algos + ci

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Copy link
Contributor

@graebm graebm left a comment

Choose a reason for hiding this comment

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

lots of trivial stuff, but looks good

include/aws/checksums/private/crc_util.h Outdated Show resolved Hide resolved
include/aws/checksums/private/crc_util.h Outdated Show resolved Hide resolved
x = ((x >> 8) & m) | (x & m) << 8;
m = 0xffff0000ffff;
x = ((x >> 16) & m) | (x & m) << 16;
return x >> 32 | x << 32;
Copy link
Contributor

Choose a reason for hiding this comment

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

if this algorithm came from somewhere, include a link

Copy link
Contributor

Choose a reason for hiding this comment

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

I worked it out on paper just now ... 😮‍💨 seems right

Copy link
Contributor

Choose a reason for hiding this comment

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

if no link, add comments explaining like
// swap every other byte, now every 2-byte chunk is reversed
...
// swap every other 2-byte chunk, now every 4-byte chunk is reversed
...
// swap the two 4-byte chunks, now all 8 bytes are reversed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This bit twiddling trick is pretty popular in fast crc implementations in the wild. Don't know where it originated, but most libs i looked at included a variant of this.

Poking around more modern hash implementations, like xxHash, that trick seems to have fallen out of favor. Replaced it with compiler built ins and a typical posix byte swap implementation as fallback.

@@ -21,4 +22,32 @@
return val; \
}

/* helper function to reverse byte order on big-endian platforms*/
static inline uint32_t aws_swap_bytes_if_needed_32(uint32_t x) {
Copy link
Contributor

Choose a reason for hiding this comment

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

trivial: maybe I'm just not familiar with the literature, but to me "swap_bytes_if_needed" doesn't indicate which way they would swap, or what would be needed to make them swap

Suggested change
static inline uint32_t aws_swap_bytes_if_needed_32(uint32_t x) {
static inline uint32_t aws_swap_bytes_to_le_if_needed_32(uint32_t x) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

renamed to aws_bswap64/32_if_be.
swap bytes or byte swap (bswap is common intrinsic name) is a fairly typical name for reversing order of all bytes.

include/aws/checksums/private/crc_util.h Outdated Show resolved Hide resolved
tests/crc_test.c Outdated Show resolved Hide resolved
const uint64_t *current = (const uint64_t *)(const void *)input;
while (remaining >= 8) {
uint64_t c1 = *current++ ^ crc;
crc = crc64nvme_table_be[0][c1 & 0xff] ^ crc64nvme_table_be[1][(c1 >> 8) & 0xff] ^
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not smart enough to check this in under a day ... but it looks like the other one, and the tests pass so probably good 👍

@DmitriyMusatkin DmitriyMusatkin merged commit b23b2e2 into main Oct 29, 2024
53 checks passed
@DmitriyMusatkin DmitriyMusatkin deleted the big_end branch October 29, 2024 14:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants