Home | History | Annotate | Line # | Download | only in arm
divsi3.S revision 1.2
      1 /*===-- divsi3.S - 32-bit signed integer divide ---------------------------===//
      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 __divsi3 (32-bit signed integer divide) function
     11  * for the ARM architecture as a wrapper around the unsigned routine.
     12  *
     13  *===----------------------------------------------------------------------===*/
     14 
     15 #include "../assembly.h"
     16 
     17 #define ESTABLISH_FRAME \
     18     push   {r4, r7, lr}    ;\
     19     add     r7,     sp, #4   ;\
     20     sub     sp,     #4
     21 #define CLEAR_FRAME_AND_RETURN \
     22     add     sp,     #4   ;\
     23     pop    {r4, r7, pc}
     24 
     25 	.syntax unified
     26 	.text
     27 #if __ARM_ARCH_ISA_THUMB == 2
     28 	.thumb
     29 #endif
     30 
     31 	.p2align 3
     32 // Ok, APCS and AAPCS agree on 32 bit args, so it's safe to use the same routine.
     33 DEFINE_AEABI_FUNCTION_ALIAS(__aeabi_idiv, __divsi3)
     34 
     35 @ int __divsi3(int divident, int divisor)
     36 @   Calculate and return the quotient of the (signed) division.
     37 
     38 #if __ARM_ARCH_ISA_THUMB == 2
     39 DEFINE_COMPILERRT_THUMB_FUNCTION(__divsi3)
     40 #else
     41 DEFINE_COMPILERRT_FUNCTION(__divsi3)
     42 #endif
     43 #if __ARM_ARCH_EXT_IDIV__
     44    tst     r1,r1
     45    beq     LOCAL_LABEL(divzero)
     46    sdiv    r0, r0, r1
     47    bx      lr
     48 LOCAL_LABEL(divzero):
     49    mov     r0,#0
     50    bx      lr
     51 #else
     52 ESTABLISH_FRAME
     53 //  Set aside the sign of the quotient.
     54     eor     r4,     r0, r1
     55 //  Take absolute value of a and b via abs(x) = (x^(x >> 31)) - (x >> 31).
     56     eor     r2,     r0, r0, asr #31
     57     eor     r3,     r1, r1, asr #31
     58     sub     r0,     r2, r0, asr #31
     59     sub     r1,     r3, r1, asr #31
     60 //  abs(a) / abs(b)
     61     bl      SYMBOL_NAME(__udivsi3)
     62 //  Apply sign of quotient to result and return.
     63     eor     r0,     r0, r4, asr #31
     64     sub     r0,     r0, r4, asr #31
     65     CLEAR_FRAME_AND_RETURN
     66 #endif
     67 END_COMPILERRT_FUNCTION(__divsi3)
     68