Skip to content

Commit 75953aa

Browse files
Merge branch 'dev' of github.com:hioa-cs/IncludeOS into dev
2 parents dc39379 + 5f96af3 commit 75953aa

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

api/util/crc32.hpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,41 @@ static uint32_t crc_32_tab[] =
8181

8282
for (; len; --len, ++buf)
8383
partial = UPDC32(*buf, partial);
84-
84+
8585
return partial;
8686

8787
#undef UPDC32
8888
}
8989

90+
#ifdef __SSE4_2__
91+
#include <immintrin.h>
92+
93+
inline uint32_t crc32_hw(const uint8_t* buffer, size_t len)
94+
{
95+
uint32_t hash = 0xFFFFFFFF;
96+
for (size_t i = 0; i < len; i++) {
97+
hash = _mm_crc32_u8(hash, buffer[i]);
98+
}
99+
return hash ^ 0xFFFFFFFF;
100+
}
101+
#endif
102+
103+
/** Ethernet/ZIP **/
90104
inline uint32_t crc32(const void* buf, size_t len)
91105
{
92106
return ~crc32(0xFFFFFFFF, (const char*) buf, len);
93107
}
94108

109+
/** Intel (iSCSI) or vanilla-polynomial, DONT mix with other code **/
110+
/** This variant uses the fastest CRC method possible, but we don't
111+
know which polynomial it will use, so use with care! **/
112+
inline uint32_t crc32_fast(const void* buf, size_t len)
113+
{
114+
#ifdef __SSE4_2__
115+
return crc32_hw((const uint8_t*) buf, len);
116+
#else
117+
return ~crc32(0xFFFFFFFF, (const char*) buf, len);
118+
#endif
119+
}
120+
95121
#endif

src/kernel/sanity_checks.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ static void self_test_gconstr() {
4040

4141
static uint32_t generate_ro_crc() noexcept
4242
{
43-
uint32_t crc = CRC32_BEGIN();
44-
crc = crc32(crc, &_TEXT_START_, &_RODATA_END_ - &_TEXT_START_);
45-
return CRC32_VALUE(crc);
43+
return crc32_fast(&_TEXT_START_, &_RODATA_END_ - &_TEXT_START_);
4644
}
4745

4846
extern "C"

src/kernel/softreset.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void OS::resume_softreset(intptr_t addr)
3535
/// validate soft-reset data
3636
const uint32_t csum_copy = data->checksum;
3737
data->checksum = 0;
38-
uint32_t crc = crc32(data, sizeof(softreset_t));
38+
uint32_t crc = crc32_fast(data, sizeof(softreset_t));
3939
if (crc != csum_copy) {
4040
kprintf("[!] Failed to verify CRC of softreset data: %08x vs %08x\n",
4141
crc, csum_copy);
@@ -66,7 +66,7 @@ void* __os_store_soft_reset(void* extra, size_t extra_len)
6666
data->extra = extra;
6767
data->extra_len = extra_len;
6868

69-
uint32_t csum = crc32(data, sizeof(softreset_t));
69+
uint32_t csum = crc32_fast(data, sizeof(softreset_t));
7070
data->checksum = csum;
7171
return data;
7272
}

0 commit comments

Comments
 (0)