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