Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Shouldn't processors have a special instruction for this operation?


Rotate through carry works for that. (Maybe not on RISC-V, it’s kind of picky about things you can’t easily access in high-level languages.) Unsigned floored, x86:

  add eax, ebx
  rcr eax, 1
ARM:

  adds r0, r0, r1
  rrx r0, r0
I’d need to think a bit more to come up with a signed version.


>I’d need to think a bit more to come up with a signed version.

Invert the high bits, turning two's complement into biased format (0 = lowest negative number, 0xFF...F = highest positive number). Then do the add+rotate and convert the result back.


Those four integer instructions are generally quite fast already. So unless this operation is needed extremely often there is probably no point in dedicating hardware to it.




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: