divmodsi4.S revision 5d71de26cedae3dafc17449fe0182045c0bd20e8
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#define CLEAR_FRAME_AND_RETURN \
22    pop    {r4-r7, pc}
23
24	.syntax unified
25	.text
26#if __ARM_ARCH_ISA_THUMB == 2
27	.thumb
28#endif
29
30	.p2align 3
31DEFINE_COMPILERRT_FUNCTION(__divmodsi4)
32#if __ARM_ARCH_EXT_IDIV__
33	tst     r1, r1
34	beq     LOCAL_LABEL(divzero)
35	mov 	r3, r0
36	sdiv	r0, r3, r1
37	mls 	r1, r0, r1, r3
38	str 	r1, [r2]
39	bx  	lr
40LOCAL_LABEL(divzero):
41	mov     r0, #0
42	bx      lr
43#else
44    ESTABLISH_FRAME
45//  Set aside the sign of the quotient and modulus, and the address for the
46//  modulus.
47    eor     r4,     r0, r1
48    mov     r5,     r0
49    mov     r6,     r2
50//  Take the absolute value of a and b via abs(x) = (x^(x >> 31)) - (x >> 31).
51    eor     ip,     r0, r0, asr #31
52    eor     lr,     r1, r1, asr #31
53    sub     r0,     ip, r0, asr #31
54    sub     r1,     lr, r1, asr #31
55//  Unsigned divmod:
56    bl      SYMBOL_NAME(__udivmodsi4)
57//  Apply the sign of quotient and modulus
58    ldr     r1,    [r6]
59    eor     r0,     r0, r4, asr #31
60    eor     r1,     r1, r5, asr #31
61    sub     r0,     r0, r4, asr #31
62    sub     r1,     r1, r5, asr #31
63    str     r1,    [r6]
64    CLEAR_FRAME_AND_RETURN
65#endif
66END_COMPILERRT_FUNCTION(__divmodsi4)
67