xref: /netbsd-src/sys/external/bsd/compiler_rt/dist/lib/builtins/arm/divmodsi4.S (revision f6e43b3179e19718c2af5950ddbd1384060c23e7)
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-r6, lr}
20#define CLEAR_FRAME_AND_RETURN \
21    pop    {r4-r6, pc}
22
23	.syntax unified
24	.text
25#if __ARM_ARCH_ISA_THUMB == 2
26	.thumb
27#endif
28
29@ int __divmodsi4(int divident, int divisor, int *remainder)
30@   Calculate the quotient and remainder of the (signed) division.  The return
31@   value is the quotient, the remainder is placed in the variable.
32
33	.p2align 3
34#if __ARM_ARCH_ISA_THUMB == 2
35DEFINE_COMPILERRT_THUMB_FUNCTION(__divmodsi4)
36#else
37DEFINE_COMPILERRT_FUNCTION(__divmodsi4)
38#endif
39#if __ARM_ARCH_EXT_IDIV__
40	tst     r1, r1
41	beq     LOCAL_LABEL(divzero)
42	mov 	r3, r0
43	sdiv	r0, r3, r1
44	mls 	r1, r0, r1, r3
45	str 	r1, [r2]
46	bx  	lr
47LOCAL_LABEL(divzero):
48	mov     r0, #0
49	bx      lr
50#else
51    ESTABLISH_FRAME
52//  Set aside the sign of the quotient and modulus, and the address for the
53//  modulus.
54    eor     r4,     r0, r1
55    mov     r5,     r0
56    mov     r6,     r2
57//  Take the absolute value of a and b via abs(x) = (x^(x >> 31)) - (x >> 31).
58    eor     ip,     r0, r0, asr #31
59    eor     lr,     r1, r1, asr #31
60    sub     r0,     ip, r0, asr #31
61    sub     r1,     lr, r1, asr #31
62//  Unsigned divmod:
63    bl      SYMBOL_NAME(__udivmodsi4)
64//  Apply the sign of quotient and modulus
65    ldr     r1,    [r6]
66    eor     r0,     r0, r4, asr #31
67    eor     r1,     r1, r5, asr #31
68    sub     r0,     r0, r4, asr #31
69    sub     r1,     r1, r5, asr #31
70    str     r1,    [r6]
71    CLEAR_FRAME_AND_RETURN
72#endif
73END_COMPILERRT_FUNCTION(__divmodsi4)
74