Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Arm/RISC-V targets without integer division instructions #46

Open
jserv opened this issue Feb 4, 2021 · 1 comment
Open

Support Arm/RISC-V targets without integer division instructions #46

jserv opened this issue Feb 4, 2021 · 1 comment
Assignees

Comments

@jserv
Copy link
Collaborator

jserv commented Feb 4, 2021

Some Arm targets such as Cortex-A9 might lack of integer division instructions, and the current shecc would not generate the correct instruction sequence for them. Consequently, we have to provide the fallback by means of software emulation. Here is the reference implementation for integer division:

__div:
        push  {fp, lr}
        add   fp, sp, #4
        // check for divide by zero
        cmp   r1, #0
        beq   __div_end
        push  {r0, r1}
        // variables
        mov   r0, #0        // quotient
        pop   {r1, r2}       // dividend / remainder, divisor
        mov   r3, #1        // bit field
 __div_shift:
        // shift divisor left until it exceeds dividend
        // the bit field will be shifted by one less
        cmp   r2, r1
        lslls r2, r2, #1
        lslls r3, r3, #1
        bls   __div_shift
__div_sub:
        // cmp sets the carry flag if r1 - r2 is positive, which is weird
        cmp   r1, r2
        // subtract divisor from the remainder if it was smaller
        // this also sets the carry flag since the result is positive
        subcs r1, r1, r2
        // add bit field to the quotient if the divisor was smaller
        addcs r0, r0, r3
        // shift bit field right, setting the carry flag if it underflows
        lsrs  r3, r3, #1
        // shift divisor right if bit field has not underflowed
        lsrcc r2, r2, #1
        // loop if bit field has not underflowed
        bcc   __div_sub
__div_end:
        sub   sp, fp, #4
        pop   {fp, pc}

Reference:

@jserv jserv changed the title Support Arm targets without integer division instructions Support Arm/RISC-V targets without integer division instructions Oct 27, 2023
@jserv jserv assigned vacantron and unassigned eecheng87 Oct 27, 2023
@jserv
Copy link
Collaborator Author

jserv commented Dec 22, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants