How does one do integer (signed or unsigned) division on ARM?

后端 未结 5 1845
长发绾君心
长发绾君心 2020-12-06 00:33

I\'m working on Cortex-A8 and Cortex-A9 in particular. I know that some architectures don\'t come with integer division, but what is the best way to do it other than convert

5条回答
  •  执念已碎
    2020-12-06 01:22

    Some copy-pasta from elsewhere for an integer divide: Basically, 3 instructions per bit. From this website, though I've seen it many other places as well. This site also has a nice version which may be faster in general.

    
    @ Entry  r0: numerator (lo) must be signed positive
    @        r2: deniminator (den) must be non-zero and signed negative
    idiv:
            lo .req r0; hi .req r1; den .req r2
            mov hi, #0 @ hi = 0
            adds lo, lo, lo
            .rept 32 @ repeat 32 times
              adcs hi, den, hi, lsl #1
              subcc hi, hi, den
              adcs lo, lo, lo
            .endr
            mov pc, lr @ return
    @ Exit   r0: quotient (lo)
    @        r1: remainder (hi)
    

提交回复
热议问题