Skip to content

Commit

Permalink
fix crc64 test
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyMusatkin committed Oct 7, 2024
1 parent f40568f commit 10e11bc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
15 changes: 15 additions & 0 deletions include/aws/checksums/private/crc_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,19 @@ static inline uint32_t aws_swap_bytes_if_needed_32(uint32_t x) {
return ((uint32_t)c1 << 24) + ((uint32_t)c2 << 16) + ((uint32_t)c3 << 8) + c4;
}

/* 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;
x = ((x >> 8) & m) | (x & m) << 8;
m = 0xffff0000ffff;
x = ((x >> 16) & m) | (x & m) << 16;
return x >> 32 | x << 32;
}


#endif /* AWS_CHECKSUMS_PRIVATE_CRC_UTIL_H */
18 changes: 3 additions & 15 deletions source/crc64_sw.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

#include <aws/checksums/crc.h>
#include <aws/checksums/private/crc64_priv.h>

#include <aws/common/byte_order.h>
#include <aws/checksums/private/crc_util.h>

/* CRC64NVME slice-by-8 lookup table (bit-reflected poly 0x9a6c9329ac4bc9b5) */
/* little endian variant */
Expand Down Expand Up @@ -1072,20 +1071,9 @@ static uint64_t crc64nvme_table_be[8][256] = {
0xbf6a9be685aa5729, 0x134bbdc39bdcbe08, 0xe729d7acb946856a, 0x4b08f189a7306c4b // [7][0xfc]
}};

/* Reverse the bytes in a 64-bit word. */
static inline uint64_t s_reverse_bytes_64(uint64_t a) {
uint64_t m;

m = UINT64_C(0xff00ff00ff00ff);
a = ((a >> 8) & m) | (a & m) << 8;
m = UINT64_C(0xffff0000ffff);
a = ((a >> 16) & m) | (a & m) << 16;
return a >> 32 | a << 32;
}

/* Calculate a CRC-64 eight bytes at a time on a big-endian architecture. */
static inline uint64_t s_crc64_sw_be(const uint8_t *input, int length, uint64_t prev_crc64) {
uint64_t crc = ~s_reverse_bytes_64(prev_crc64);
uint64_t crc = ~aws_swap_bytes_if_needed_64(prev_crc64);
// Read byte by byte until we reach an 8 byte aligned address
while (length > 0 && ((intptr_t)input & 7)) {
crc = (crc << 8) ^ crc64nvme_table_be[0][((crc >> 56) ^ *input++)];
Expand All @@ -1107,7 +1095,7 @@ static inline uint64_t s_crc64_sw_be(const uint8_t *input, int length, uint64_t
crc = (crc << 8) ^ crc64nvme_table_be[0][((crc >> 56) ^ input[length - remaining]) & 0xff];
remaining--;
}
return ~s_reverse_bytes_64(crc);
return ~aws_swap_bytes_if_needed_64(crc);
}

/** Slow slice-by-8 lookup table based fallback function to compute CRC64NVME. */
Expand Down
3 changes: 2 additions & 1 deletion tests/crc64_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ static int s_test_known_crc(
ASSERT_HEX_EQUALS(expected_crc, result, "%s(%s)", func_name, data_name);

// Compute the residue of the buffer (the CRC of the buffer plus its CRC) - will always be a constant value
uint64_t residue = func((const uint8_t *)&result, 8, result); // assuming little endian
uint64_t result_swapped = aws_swap_bytes_if_needed_64(result);
uint64_t residue = func((const uint8_t *)&result_swapped, 8, result); // assuming little endian
ASSERT_HEX_EQUALS(expected_residue, residue, "len %d residue %s(%s)", length, func_name, data_name);

// chain the crc computation so 2 calls each operate on about 1/2 of the buffer
Expand Down

0 comments on commit 10e11bc

Please sign in to comment.