Home | History | Annotate | Line # | Download | only in arm
      1  1.1  joerg /*===-- divmodsi4.S - 32-bit signed integer divide and modulus ------------===//
      2  1.1  joerg  *
      3  1.1  joerg  *                     The LLVM Compiler Infrastructure
      4  1.1  joerg  *
      5  1.1  joerg  * This file is dual licensed under the MIT and the University of Illinois Open
      6  1.1  joerg  * Source Licenses. See LICENSE.TXT for details.
      7  1.1  joerg  *
      8  1.1  joerg  *===----------------------------------------------------------------------===//
      9  1.1  joerg  *
     10  1.1  joerg  * This file implements the __divmodsi4 (32-bit signed integer divide and
     11  1.1  joerg  * modulus) function for the ARM architecture.  A naive digit-by-digit
     12  1.1  joerg  * computation is employed for simplicity.
     13  1.1  joerg  *
     14  1.1  joerg  *===----------------------------------------------------------------------===*/
     15  1.1  joerg 
     16  1.1  joerg #include "../assembly.h"
     17  1.1  joerg 
     18  1.1  joerg #define ESTABLISH_FRAME    \
     19  1.3  skrll     push   {r4-r6, lr}
     20  1.1  joerg #define CLEAR_FRAME_AND_RETURN \
     21  1.3  skrll     pop    {r4-r6, pc}
     22  1.1  joerg 
     23  1.2    rin 	.syntax unified
     24  1.2    rin 	.text
     25  1.2    rin #if __ARM_ARCH_ISA_THUMB == 2
     26  1.2    rin 	.thumb
     27  1.2    rin #endif
     28  1.2    rin 
     29  1.2    rin @ int __divmodsi4(int divident, int divisor, int *remainder)
     30  1.2    rin @   Calculate the quotient and remainder of the (signed) division.  The return
     31  1.2    rin @   value is the quotient, the remainder is placed in the variable.
     32  1.2    rin 
     33  1.2    rin 	.p2align 3
     34  1.2    rin #if __ARM_ARCH_ISA_THUMB == 2
     35  1.2    rin DEFINE_COMPILERRT_THUMB_FUNCTION(__divmodsi4)
     36  1.2    rin #else
     37  1.1  joerg DEFINE_COMPILERRT_FUNCTION(__divmodsi4)
     38  1.2    rin #endif
     39  1.1  joerg #if __ARM_ARCH_EXT_IDIV__
     40  1.1  joerg 	tst     r1, r1
     41  1.1  joerg 	beq     LOCAL_LABEL(divzero)
     42  1.1  joerg 	mov 	r3, r0
     43  1.1  joerg 	sdiv	r0, r3, r1
     44  1.1  joerg 	mls 	r1, r0, r1, r3
     45  1.1  joerg 	str 	r1, [r2]
     46  1.1  joerg 	bx  	lr
     47  1.1  joerg LOCAL_LABEL(divzero):
     48  1.1  joerg 	mov     r0, #0
     49  1.1  joerg 	bx      lr
     50  1.1  joerg #else
     51  1.1  joerg     ESTABLISH_FRAME
     52  1.1  joerg //  Set aside the sign of the quotient and modulus, and the address for the
     53  1.1  joerg //  modulus.
     54  1.1  joerg     eor     r4,     r0, r1
     55  1.1  joerg     mov     r5,     r0
     56  1.1  joerg     mov     r6,     r2
     57  1.1  joerg //  Take the absolute value of a and b via abs(x) = (x^(x >> 31)) - (x >> 31).
     58  1.1  joerg     eor     ip,     r0, r0, asr #31
     59  1.1  joerg     eor     lr,     r1, r1, asr #31
     60  1.1  joerg     sub     r0,     ip, r0, asr #31
     61  1.1  joerg     sub     r1,     lr, r1, asr #31
     62  1.1  joerg //  Unsigned divmod:
     63  1.1  joerg     bl      SYMBOL_NAME(__udivmodsi4)
     64  1.1  joerg //  Apply the sign of quotient and modulus
     65  1.1  joerg     ldr     r1,    [r6]
     66  1.1  joerg     eor     r0,     r0, r4, asr #31
     67  1.1  joerg     eor     r1,     r1, r5, asr #31
     68  1.1  joerg     sub     r0,     r0, r4, asr #31
     69  1.1  joerg     sub     r1,     r1, r5, asr #31
     70  1.1  joerg     str     r1,    [r6]
     71  1.1  joerg     CLEAR_FRAME_AND_RETURN
     72  1.1  joerg #endif
     73  1.1  joerg END_COMPILERRT_FUNCTION(__divmodsi4)
     74