blob: bb5f8a069f477fe2dbf49a22ed0d32944f8be574 [file] [log] [blame]
Joerg Sonnenbergerdde27002014-01-30 18:41:32 +00001/*===-- udivsi3.S - 32-bit unsigned integer divide ------------------------===//
Stephen Canon5abb5c12011-03-18 16:35:02 +00002 *
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 *
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000010 * This file implements the __udivsi3 (32-bit unsigned integer divide)
11 * function for the ARM 32-bit architecture.
Stephen Canon5abb5c12011-03-18 16:35:02 +000012 *
13 *===----------------------------------------------------------------------===*/
14
15#include "../assembly.h"
16
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000017 .syntax unified
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000018 .text
Saleem Abdulrasool8f2efc32014-06-16 16:36:25 +000019
Joerg Sonnenberger9720fcf2014-07-20 20:53:37 +000020#if __ARM_ARCH_ISA_THUMB == 2
21 .thumb
22#endif
23
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000024 .p2align 2
Anton Korobeynikov75e3c192011-04-19 17:51:24 +000025DEFINE_AEABI_FUNCTION_ALIAS(__aeabi_uidiv, __udivsi3)
Stephen Canon5abb5c12011-03-18 16:35:02 +000026DEFINE_COMPILERRT_FUNCTION(__udivsi3)
Stephen Hines7633afc2013-10-25 06:26:44 +000027#if __ARM_ARCH_EXT_IDIV__
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000028 tst r1, r1
29 beq LOCAL_LABEL(divby0)
30 mov r3, r0
31 udiv r0, r3, r1
32 mls r1, r0, r1, r3
Bob Wilsona4cefbd2012-09-29 23:37:01 +000033 bx lr
Bob Wilsona4cefbd2012-09-29 23:37:01 +000034#else
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000035 cmp r1, #1
36 bcc LOCAL_LABEL(divby0)
Joerg Sonnenberger9720fcf2014-07-20 20:53:37 +000037 IT(eq)
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000038 JMPc(lr, eq)
39 cmp r0, r1
Joerg Sonnenberger9720fcf2014-07-20 20:53:37 +000040 ITT(cc)
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000041 movcc r0, #0
42 JMPc(lr, cc)
43 /*
44 * Implement division using binary long division algorithm.
45 *
46 * r0 is the numerator, r1 the denominator.
47 *
48 * The code before JMP computes the correct shift I, so that
49 * r0 and (r1 << I) have the highest bit set in the same position.
50 * At the time of JMP, ip := .Ldiv0block - 12 * I.
51 * This depends on the fixed instruction size of block.
Joerg Sonnenberger9720fcf2014-07-20 20:53:37 +000052 * For ARM mode, this is 12 Bytes, for THUMB mode 14 Bytes.
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000053 *
54 * block(shift) implements the test-and-update-quotient core.
55 * It assumes (r0 << shift) can be computed without overflow and
56 * that (r0 << shift) < 2 * r1. The quotient is stored in r3.
57 */
Stephen Canon5abb5c12011-03-18 16:35:02 +000058
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000059# ifdef __ARM_FEATURE_CLZ
60 clz ip, r0
61 clz r3, r1
62 /* r0 >= r1 implies clz(r0) <= clz(r1), so ip <= r3. */
63 sub r3, r3, ip
Joerg Sonnenberger9720fcf2014-07-20 20:53:37 +000064# if __ARM_ARCH_ISA_THUMB == 2
65 adr ip, LOCAL_LABEL(div0block) + 1
66 sub ip, ip, r3, lsl #1
67# else
Joerg Sonnenberger8f6cf702014-07-20 20:00:26 +000068 adr ip, LOCAL_LABEL(div0block)
Joerg Sonnenberger9720fcf2014-07-20 20:53:37 +000069# endif
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000070 sub ip, ip, r3, lsl #2
71 sub ip, ip, r3, lsl #3
72 mov r3, #0
73 bx ip
74# else
Joerg Sonnenberger9720fcf2014-07-20 20:53:37 +000075# if __ARM_ARCH_ISA_THUMB == 2
76# error THUMB mode requires CLZ or UDIV
77# endif
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000078 mov r2, r0
Joerg Sonnenberger8f6cf702014-07-20 20:00:26 +000079 adr ip, LOCAL_LABEL(div0block)
Stephen Canon5abb5c12011-03-18 16:35:02 +000080
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000081 lsr r3, r2, #16
82 cmp r3, r1
83 movhs r2, r3
84 subhs ip, ip, #(16 * 12)
Stephen Canon5abb5c12011-03-18 16:35:02 +000085
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000086 lsr r3, r2, #8
87 cmp r3, r1
88 movhs r2, r3
89 subhs ip, ip, #(8 * 12)
90
91 lsr r3, r2, #4
92 cmp r3, r1
93 movhs r2, r3
94 subhs ip, #(4 * 12)
95
96 lsr r3, r2, #2
97 cmp r3, r1
98 movhs r2, r3
99 subhs ip, ip, #(2 * 12)
100
101 /* Last block, no need to update r2 or r3. */
102 cmp r1, r2, lsr #1
103 subls ip, ip, #(1 * 12)
104
105 mov r3, #0
106
107 JMP(ip)
108# endif
109
110#define IMM #
111
Saleem Abdulrasool31306b12014-07-27 02:01:15 +0000112#define block(shift) \
113 cmp r0, r1, lsl IMM shift; \
114 ITT(hs); \
Saleem Abdulrasool3a2d6a32014-07-27 02:01:24 +0000115 WIDE(addhs) r3, r3, IMM (1 << shift); \
116 WIDE(subhs) r0, r0, r1, lsl IMM shift
Joerg Sonnenberger59611a82014-01-24 13:43:35 +0000117
118 block(31)
119 block(30)
120 block(29)
121 block(28)
122 block(27)
123 block(26)
124 block(25)
125 block(24)
126 block(23)
127 block(22)
128 block(21)
129 block(20)
130 block(19)
131 block(18)
132 block(17)
133 block(16)
134 block(15)
135 block(14)
136 block(13)
137 block(12)
138 block(11)
139 block(10)
140 block(9)
141 block(8)
142 block(7)
143 block(6)
144 block(5)
145 block(4)
146 block(3)
147 block(2)
148 block(1)
Joerg Sonnenberger8f6cf702014-07-20 20:00:26 +0000149LOCAL_LABEL(div0block):
Joerg Sonnenberger59611a82014-01-24 13:43:35 +0000150 block(0)
151
152 mov r0, r3
153 JMP(lr)
154#endif /* __ARM_ARCH_EXT_IDIV__ */
155
156LOCAL_LABEL(divby0):
157 mov r0, #0
158#ifdef __ARM_EABI__
159 b __aeabi_idiv0
160#else
161 JMP(lr)
Bob Wilsona4cefbd2012-09-29 23:37:01 +0000162#endif
Joerg Sonnenberger59611a82014-01-24 13:43:35 +0000163
164END_COMPILERRT_FUNCTION(__udivsi3)