优化crc速度

This commit is contained in:
cyy_mac
2026-07-26 20:51:57 +08:00
parent cd9f8e204d
commit 265173e746
3 changed files with 60 additions and 12 deletions

27
tests/test_common.py Normal file
View File

@@ -0,0 +1,27 @@
"""Common codec utility tests."""
import struct
from go1_pro_sdk.utils.common import gen_crc
def _gen_crc_bitwise(data: bytes) -> bytes:
crc = 0xFFFFFFFF
for (word,) in struct.iter_unpack("<I", data):
for bit in range(32):
x = (crc >> 31) & 1
crc <<= 1
crc &= 0xFFFFFFFF
if x ^ (1 & (word >> (31 - bit))):
crc ^= 0x04c11db7
return struct.pack("<I", crc)
def test_gen_crc_matches_reference_bitwise():
samples = [
b"",
bytes(4),
bytes(range(64)),
b"\xfe\xef\xff\x00" + bytes(range(252)) * 2,
]
for sample in samples:
assert gen_crc(sample) == _gen_crc_bitwise(sample)