-
Notifications
You must be signed in to change notification settings - Fork 54
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
Changes from 22 commits
4d17d9e
24f2e80
c21a354
808bb66
57664ba
fa6b5ee
da8e25a
4e56076
99b3ba1
0db1134
4853bc6
2fb5fcc
60dab57
0697afb
c504099
2b964da
f40568f
10e11bc
51aee7e
abdc60f
b608ac9
857c273
a11c146
1dd1eb5
86d99ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
|
||
#include <aws/common/byte_order.h> | ||
#include <aws/common/stdint.h> | ||
#include <limits.h> | ||
|
||
|
@@ -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) { | ||
if (!aws_is_big_endian()) { | ||
return x; | ||
} | ||
|
||
uint8_t c1 = x & 0xFF; | ||
uint8_t c2 = (x >> 8) & 0xFF; | ||
uint8_t c3 = (x >> 16) & 0xFF; | ||
uint8_t c4 = (x >> 24) & 0xFF; | ||
|
||
return ((uint32_t)c1 << 24) + ((uint32_t)c2 << 16) + ((uint32_t)c3 << 8) + c4; | ||
DmitriyMusatkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/* Reverse the bytes in a 64-bit word. */ | ||
static inline uint64_t aws_swap_bytes_if_needed_64(uint64_t x) { | ||
if (!aws_is_big_endian()) { | ||
return x; | ||
} | ||
|
||
uint64_t m; | ||
m = 0xff00ff00ff00ff; | ||
DmitriyMusatkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
x = ((x >> 8) & m) | (x & m) << 8; | ||
m = 0xffff0000ffff; | ||
DmitriyMusatkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
x = ((x >> 16) & m) | (x & m) << 16; | ||
return x >> 32 | x << 32; | ||
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. if this algorithm came from somewhere, include a link 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. I worked it out on paper just now ... 😮💨 seems right 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. if no link, add comments explaining like 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 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. |
||
} | ||
|
||
#endif /* AWS_CHECKSUMS_PRIVATE_CRC_UTIL_H */ |
Large diffs are not rendered by default.
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.
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
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.
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.