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

后端 未结 5 1863
长发绾君心
长发绾君心 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:15

    Division by a constant value is done quickly by doing a 64bit-multiply and shift-right, for example, like this:

    LDR     R3, =0xA151C331
    UMULL   R3, R2, R1, R3
    MOV     R0, R2,LSR#10
    

    here R1 is divided by 1625. The calculation is done like this: 64bitreg(R2:R3) = R1*0xA151C331, then the result is the upper 32bit right shifted by 10:

    R1*0xA151C331/2^(32+10) = R1*0.00061538461545751488 = R1/1624.99999980
    

    You can calculate your own constants from this formula:

    x / N ==  (x*A)/2^(32+n)   -->       A = 2^(32+n)/N
    

    select the largest n, for which A < 2^32

提交回复
热议问题