cnlohr / rv003usb

CH32V003 RISC-V Pure Software USB Controller
MIT License
440 stars 44 forks source link

c.sari for crc0 code #6

Closed duk-37 closed 1 year ago

duk-37 commented 1 year ago

Mentioned by @wChris_ on YouTube. Saves a compressed instruction.

// a0 current bit/temp, a3 running. a4 polynomial
do0_crc:
  slli     a0,a3,31
  c.srai   a0,31
  c.srli    a3,1
  c.and  a0,a4
  c.xor    a3,a0

Using the same srai trick we can also have a six-instruction generic CRC without having flipped bits beforehand. I don't think we need this, though, given all our CRCs can be specialized. If we're short on space we can do it though.

// a0 current bit/temp, a3 running, t0 polynomial
c.xor   a0, a3
c.slli  a0, 31
c.srai  a0, 31
// Depending on alignment needed these next two instructions can be flipped.
c.srli  a3, 1
and  a0, a0, t0
//
c.xor   a3, a0
duk-37 commented 1 year ago

7