]> git.proxmox.com Git - rustc.git/blob - src/libcompiler_builtins/compiler-rt/lib/builtins/arm/umodsi3.S
New upstream version 1.25.0+dfsg1
[rustc.git] / src / libcompiler_builtins / compiler-rt / lib / builtins / arm / umodsi3.S
1 /*===-- umodsi3.S - 32-bit unsigned integer 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 __umodsi3 (32-bit unsigned integer modulus)
11 * function for the ARM 32-bit architecture.
12 *
13 *===----------------------------------------------------------------------===*/
14
15 #include "../assembly.h"
16
17 .syntax unified
18 .text
19 DEFINE_CODE_STATE
20
21 @ unsigned int __umodsi3(unsigned int divident, unsigned int divisor)
22 @ Calculate and return the remainder of the (unsigned) division.
23
24 .p2align 2
25 DEFINE_COMPILERRT_FUNCTION(__umodsi3)
26 #if __ARM_ARCH_EXT_IDIV__
27 tst r1, r1
28 beq LOCAL_LABEL(divby0)
29 udiv r2, r0, r1
30 mls r0, r2, r1, r0
31 bx lr
32 #else
33 cmp r1, #1
34 bcc LOCAL_LABEL(divby0)
35 ITT(eq)
36 moveq r0, #0
37 JMPc(lr, eq)
38 cmp r0, r1
39 IT(cc)
40 JMPc(lr, cc)
41 /*
42 * Implement division using binary long division algorithm.
43 *
44 * r0 is the numerator, r1 the denominator.
45 *
46 * The code before JMP computes the correct shift I, so that
47 * r0 and (r1 << I) have the highest bit set in the same position.
48 * At the time of JMP, ip := .Ldiv0block - 8 * I.
49 * This depends on the fixed instruction size of block.
50 * For ARM mode, this is 8 Bytes, for THUMB mode 10 Bytes.
51 *
52 * block(shift) implements the test-and-update-quotient core.
53 * It assumes (r0 << shift) can be computed without overflow and
54 * that (r0 << shift) < 2 * r1. The quotient is stored in r3.
55 */
56
57 # ifdef __ARM_FEATURE_CLZ
58 clz ip, r0
59 clz r3, r1
60 /* r0 >= r1 implies clz(r0) <= clz(r1), so ip <= r3. */
61 sub r3, r3, ip
62 # if defined(USE_THUMB_2)
63 adr ip, LOCAL_LABEL(div0block) + 1
64 sub ip, ip, r3, lsl #1
65 # else
66 adr ip, LOCAL_LABEL(div0block)
67 # endif
68 sub ip, ip, r3, lsl #3
69 bx ip
70 # else
71 # if defined(USE_THUMB_2)
72 # error THUMB mode requires CLZ or UDIV
73 # endif
74 mov r2, r0
75 adr ip, LOCAL_LABEL(div0block)
76
77 lsr r3, r2, #16
78 cmp r3, r1
79 movhs r2, r3
80 subhs ip, ip, #(16 * 8)
81
82 lsr r3, r2, #8
83 cmp r3, r1
84 movhs r2, r3
85 subhs ip, ip, #(8 * 8)
86
87 lsr r3, r2, #4
88 cmp r3, r1
89 movhs r2, r3
90 subhs ip, #(4 * 8)
91
92 lsr r3, r2, #2
93 cmp r3, r1
94 movhs r2, r3
95 subhs ip, ip, #(2 * 8)
96
97 /* Last block, no need to update r2 or r3. */
98 cmp r1, r2, lsr #1
99 subls ip, ip, #(1 * 8)
100
101 JMP(ip)
102 # endif
103
104 #define IMM #
105
106 #define block(shift) \
107 cmp r0, r1, lsl IMM shift; \
108 IT(hs); \
109 WIDE(subhs) r0, r0, r1, lsl IMM shift
110
111 block(31)
112 block(30)
113 block(29)
114 block(28)
115 block(27)
116 block(26)
117 block(25)
118 block(24)
119 block(23)
120 block(22)
121 block(21)
122 block(20)
123 block(19)
124 block(18)
125 block(17)
126 block(16)
127 block(15)
128 block(14)
129 block(13)
130 block(12)
131 block(11)
132 block(10)
133 block(9)
134 block(8)
135 block(7)
136 block(6)
137 block(5)
138 block(4)
139 block(3)
140 block(2)
141 block(1)
142 LOCAL_LABEL(div0block):
143 block(0)
144 JMP(lr)
145 #endif /* __ARM_ARCH_EXT_IDIV__ */
146
147 LOCAL_LABEL(divby0):
148 mov r0, #0
149 #ifdef __ARM_EABI__
150 b __aeabi_idiv0
151 #else
152 JMP(lr)
153 #endif
154
155 END_COMPILERRT_FUNCTION(__umodsi3)
156
157 NO_EXEC_STACK_DIRECTIVE
158