Skip to content

Commit 6a6ff0b

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 6a6ff0b

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

sw/device/lib/base/crc32.c

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,37 @@
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+
1321
OT_WARN_UNUSED_RESULT
14-
static uint32_t crc32_internal_add8(uint32_t ctx, uint8_t byte) {
15-
ctx ^= byte;
22+
static uint32_t crc32_i(uint32_t x) {
23+
uint32_t result;
1624
asm(".option push;"
17-
".option arch, +zbr0p93;"
18-
"crc32.b %0, %1;"
25+
".option arch, +zbc;"
26+
"clmul %[result], %[x], %[mu];"
27+
"clmulr %[result], %[result], %[poly];"
1928
".option pop;"
20-
: "+r"(ctx));
21-
return ctx;
29+
: [result] "=&r"(result)
30+
: [x] "r"(x), [mu] "r"(kCrc32Mu), [poly] "r"(kCrc32Poly));
31+
return result;
32+
}
33+
34+
OT_WARN_UNUSED_RESULT
35+
static uint32_t crc32_internal_add8(uint32_t ctx, uint8_t byte) {
36+
ctx ^= byte;
37+
return crc32_i(ctx << 24) ^ (ctx >> 8);
2238
}
2339

2440
OT_WARN_UNUSED_RESULT
2541
static uint32_t crc32_internal_add32(uint32_t ctx, uint32_t word) {
2642
ctx ^= word;
27-
asm(".option push;"
28-
".option arch, +zbr0p93;"
29-
"crc32.w %0, %1;"
30-
".option pop;"
31-
: "+r"(ctx));
32-
return ctx;
43+
return crc32_i(ctx);
3344
}
3445
#else
3546
enum {

0 commit comments

Comments
 (0)