llvm-project
69 строк · 2.1 Кб
1//===-- divmodsi4.S - 32-bit signed integer divide and modulus ------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the __divmodsi4 (32-bit signed integer divide and
10// modulus) function for the ARM architecture. A naive digit-by-digit
11// computation is employed for simplicity.
12//
13//===----------------------------------------------------------------------===//
14
15#include "../assembly.h"
16
17#define ESTABLISH_FRAME \
18push {r4-r7, lr} ;\
19add r7, sp, #12
20#define CLEAR_FRAME_AND_RETURN \
21pop {r4-r7, pc}
22
23.syntax unified
24.text
25DEFINE_CODE_STATE
26
27@ int __divmodsi4(int divident, int divisor, int *remainder)
28@ Calculate the quotient and remainder of the (signed) division. The return
29@ value is the quotient, the remainder is placed in the variable.
30
31.p2align 3
32DEFINE_COMPILERRT_FUNCTION(__divmodsi4)
33#if __ARM_ARCH_EXT_IDIV__
34tst r1, r1
35beq LOCAL_LABEL(divzero)
36mov r3, r0
37sdiv r0, r3, r1
38mls r1, r0, r1, r3
39str r1, [r2]
40bx lr
41LOCAL_LABEL(divzero):
42mov r0, #0
43bx lr
44#else
45ESTABLISH_FRAME
46// Set aside the sign of the quotient and modulus, and the address for the
47// modulus.
48eor r4, r0, r1
49mov r5, r0
50mov r6, r2
51// Take the absolute value of a and b via abs(x) = (x^(x >> 31)) - (x >> 31).
52eor ip, r0, r0, asr #31
53eor lr, r1, r1, asr #31
54sub r0, ip, r0, asr #31
55sub r1, lr, r1, asr #31
56// Unsigned divmod:
57bl SYMBOL_NAME(__udivmodsi4)
58// Apply the sign of quotient and modulus
59ldr r1, [r6]
60eor r0, r0, r4, asr #31
61eor r1, r1, r5, asr #31
62sub r0, r0, r4, asr #31
63sub r1, r1, r5, asr #31
64str r1, [r6]
65CLEAR_FRAME_AND_RETURN
66#endif
67END_COMPILERRT_FUNCTION(__divmodsi4)
68
69NO_EXEC_STACK_DIRECTIVE
70
71