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