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