How to calculate crc32 over a single byte data using clmul

133 views
Skip to first unread message

g terry

unread,
Apr 15, 2024, 9:08:50 PMApr 15
to RISC-V SW Dev
Hi there,
I am trying to accelerate the crc calculation in my application code with clmul instruction. I know the basic idea of CRC and am able to get correct CRC value for data with length larger than 4 bytes using clmul instructions. However when the size of remaing data becomes less than 4 bytes, let's say just one byte, the clmul-based solution doesn't work as expected, it will produce CRC for {0x00, 0x00, 0x00, 0x1B}, not just for {0x1B}. Indeed those clmul instructions are designed to operate on 32bit registers. So is it possible to use them to get CRC32 for a single byte? Any advise are welcomed.
BTW: using lookup-table based solution, I do get CRC32 value for single byte data.

BR,
Terry

Kristof Kiekens

unread,
Apr 16, 2024, 7:02:23 AMApr 16
to RISC-V SW Dev, te...@unifra.io
Hi Terry,

I think you indeed need padding.  I never heard about using lookup tables.
Did you try both?
0x0000001B
0x1B000000

Regards,
Kristof
Op dinsdag 16 april 2024 om 03:08:50 UTC+2 schreef te...@unifra.io:

Tommy Murphy

unread,
Apr 16, 2024, 7:20:33 AMApr 16
to Kristof Kiekens, RISC-V SW Dev, te...@unifra.io

g terry

unread,
Apr 16, 2024, 10:23:41 AMApr 16
to RISC-V SW Dev, kristof...@gmail.com, g terry
Hi Kristof,

I did tried both of them. The result I got is CRC value for four bytes "0x0000001B" or "0x1B000000", not just for the single byte "0x1B". There are some online crc calculator such as this one:   Online CRC-8 CRC-16 CRC-32 Calculator (crccalc.com), the single byte "0x1B" does has its own crc32 value which is different from "0x0000001B". I guess when use clmul to calculate crc32 for a single byte data, more actions/steps may need be taken to counteract the fact that clmul operates on 32bit register naturally.
BR,
Terry

Kristof Kiekens

unread,
Apr 16, 2024, 12:19:59 PMApr 16
to RISC-V SW Dev, te...@unifra.io, Kristof Kiekens
Have a look at this: 

From what I understood you can reverse cycle it. As you mentioned indeed counteract the side effect of padding.

Regards,
Kristof


Op dinsdag 16 april 2024 om 16:23:41 UTC+2 schreef te...@unifra.io:

Tommy Murphy

unread,
Apr 16, 2024, 2:43:17 PMApr 16
to g terry, RISC-V SW Dev
> it will produce CRC for {0x00, 0x00, 0x00, 0x1B}, not just for {0x1B}. 

Isn't that correct given that CRC-32 produces a 32-bit hash/checksum for the input data? E.g.


"CRC-32 (Cyclic Redundancy Check 32) is a checksum algorithm that hashes byte sequences to 32 bit values."

g terry

unread,
Apr 17, 2024, 9:03:43 PMApr 17
to Tommy Murphy, RISC-V SW Dev
Hi Tommy,
Thanks for your help. What you said is totally correct except that mine is a special or picky case where the "byte sequences" only include a single byte. The only question here is that the clmul instruction naturally operates on 4-bytes data, I am  trying to figure out how to use it to calculate a CRC32 for a single byte. In the WoWaster project, when the "byte sequence" contains less than 4-byte data, a lookup-table will be used rather than the clmul, this is correct as well, but in my case, I can't switch to use lookup-table and have to use clmul.
BR,
Terry

g terry

unread,
Apr 17, 2024, 9:04:42 PMApr 17
to Kristof Kiekens, RISC-V SW Dev
Hi Kristof,
Thanks very much. I will study those links.

BR,
Terry

g terry

unread,
Apr 17, 2024, 10:57:24 PMApr 17
to Kristof Kiekens, RISC-V SW Dev
Hi Kristof,

Many many thanks. The link you provide does work for me. I can get the expected CRC32 value for one single byte now.

BR,
Terry
Reply all
Reply to author
Forward
0 new messages