Skip to content

Commit 620ce1c

Browse files
committed
Use clmul for CRC32.
The crc32 instructions are non-standard and not supported by upstream clang. According to perftest, this only slows things down by a few cycles before: I00003 crc32_perftest.c:33] CRC32 computed in 11343 cycles. I00004 crc32_perftest.c:33] CRC32 computed in 11317 cycles. I00005 crc32_perftest.c:33] CRC32 computed in 11314 cycles. I00006 crc32_perftest.c:33] CRC32 computed in 11314 cycles. I00007 crc32_perftest.c:33] CRC32 computed in 11314 cycles. I00008 crc32_perftest.c:33] CRC32 computed in 11314 cycles. I00009 crc32_perftest.c:33] CRC32 computed in 11314 cycles. I00010 crc32_perftest.c:33] CRC32 computed in 11314 cycles. I00011 crc32_perftest.c:33] CRC32 computed in 11314 cycles. I00012 crc32_perftest.c:33] CRC32 computed in 11314 cycles. after: I00003 crc32_perftest.c:33] CRC32 computed in 11358 cycles. I00004 crc32_perftest.c:33] CRC32 computed in 11319 cycles. I00005 crc32_perftest.c:33] CRC32 computed in 11318 cycles. I00006 crc32_perftest.c:33] CRC32 computed in 11318 cycles. I00007 crc32_perftest.c:33] CRC32 computed in 11318 cycles. I00008 crc32_perftest.c:33] CRC32 computed in 11318 cycles. I00009 crc32_perftest.c:33] CRC32 computed in 11318 cycles. I00010 crc32_perftest.c:33] CRC32 computed in 11318 cycles. I00011 crc32_perftest.c:33] CRC32 computed in 11318 cycles. Signed-off-by: Kor Nielsen <[email protected]>
1 parent deb6598 commit 620ce1c

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

sw/device/lib/base/crc32.c

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,40 @@
1010
#include "sw/device/lib/base/memory.h"
1111

1212
#ifdef OT_PLATFORM_RV32
13+
enum {
14+
/**
15+
* CRC32 polynomial.
16+
*/
17+
kCrc32Poly = 0xedb88320,
18+
kCrc32Mu = 0xf7011641,
19+
};
20+
21+
1322
OT_WARN_UNUSED_RESULT
14-
static uint32_t crc32_internal_add8(uint32_t ctx, uint8_t byte) {
15-
ctx ^= byte;
23+
static uint32_t crc32_i(uint32_t x) {
24+
uint32_t result;
1625
asm(".option push;"
17-
".option arch, +zbr0p93;"
18-
"crc32.b %0, %1;"
26+
".option arch, +zbc;"
27+
"clmul %[result], %[x], %[mu];"
28+
"clmulr %[result], %[result], %[poly];"
1929
".option pop;"
20-
: "+r"(ctx));
21-
return ctx;
30+
: [result] "=&r"(result)
31+
: [x] "r"(x),
32+
[mu] "r"(kCrc32Mu),
33+
[poly] "r"(kCrc32Poly));
34+
return result;
35+
}
36+
37+
OT_WARN_UNUSED_RESULT
38+
static uint32_t crc32_internal_add8(uint32_t ctx, uint8_t byte) {
39+
ctx ^= byte;
40+
return crc32_i(ctx << 24) ^ (ctx >> 8);
2241
}
2342

2443
OT_WARN_UNUSED_RESULT
2544
static uint32_t crc32_internal_add32(uint32_t ctx, uint32_t word) {
2645
ctx ^= word;
27-
asm(".option push;"
28-
".option arch, +zbr0p93;"
29-
"crc32.w %0, %1;"
30-
".option pop;"
31-
: "+r"(ctx));
32-
return ctx;
46+
return crc32_i(ctx);
3347
}
3448
#else
3549
enum {

0 commit comments

Comments
 (0)