blob: d31e510c8f38204410a453a7ba827b3665e9a1ae [file] [log] [blame]
Stephen Canon6bbe0bb2011-03-18 16:35:02 +00001/*===-- 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.align 3
26DEFINE_COMPILERRT_FUNCTION(__divmodsi4)
Nick Kledzika09d09d2013-05-24 19:38:11 +000027#if __ARM_ARCH_7S__
28 tst r1, r1
29 beq LOCAL_LABEL(divzero)
30 mov r3, r0
31 sdiv r0, r3, r1
32 mls r1, r0, r1, r3
33 str r1, [r2]
34 bx lr
35LOCAL_LABEL(divzero):
36 mov r0, #0
37 bx lr
38#else
Stephen Canon6bbe0bb2011-03-18 16:35:02 +000039 ESTABLISH_FRAME
40// Set aside the sign of the quotient and modulus, and the address for the
41// modulus.
42 eor r4, r0, r1
43 mov r5, r0
44 mov r6, r2
45// Take the absolute value of a and b via abs(x) = (x^(x >> 31)) - (x >> 31).
46 eor ip, r0, r0, asr #31
47 eor lr, r1, r1, asr #31
48 sub r0, ip, r0, asr #31
49 sub r1, lr, r1, asr #31
50// Unsigned divmod:
Anton Korobeynikov647fc732011-04-19 17:50:09 +000051 bl SYMBOL_NAME(__udivmodsi4)
Stephen Canon6bbe0bb2011-03-18 16:35:02 +000052// Apply the sign of quotient and modulus
53 ldr r1, [r6]
54 eor r0, r0, r4, asr #31
Stephen Canon6bbe0bb2011-03-18 16:35:02 +000055 eor r1, r1, r5, asr #31
Stephen Canon2caeeef2011-03-21 17:35:26 +000056 sub r0, r0, r4, asr #31
Stephen Canon6bbe0bb2011-03-18 16:35:02 +000057 sub r1, r1, r5, asr #31
58 str r1, [r6]
59 CLEAR_FRAME_AND_RETURN
Nick Kledzika09d09d2013-05-24 19:38:11 +000060#endif