blob: 93279b664194598ec238c22ed11efb166b470b96 [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)
Saleem Abdulrasoola0d65972014-08-09 20:17:43 +000026
27@ unsigned int __udivsi3(unsigned int divident, unsigned int divisor)
28@ Calculate and return the quotient of the (unsigned) division.
29
Steven Wu84610ba2014-10-04 00:18:59 +000030DEFINE_COMPILERRT_THUMB_FUNCTION(__udivsi3)
Stephen Hines7633afc2013-10-25 06:26:44 +000031#if __ARM_ARCH_EXT_IDIV__
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000032 tst r1, r1
33 beq LOCAL_LABEL(divby0)
Saleem Abdulrasool60639832014-08-09 20:17:37 +000034 udiv r0, r0, r1
Bob Wilsona4cefbd2012-09-29 23:37:01 +000035 bx lr
Bob Wilsona4cefbd2012-09-29 23:37:01 +000036#else
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000037 cmp r1, #1
38 bcc LOCAL_LABEL(divby0)
Joerg Sonnenberger9720fcf2014-07-20 20:53:37 +000039 IT(eq)
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000040 JMPc(lr, eq)
41 cmp r0, r1
Joerg Sonnenberger9720fcf2014-07-20 20:53:37 +000042 ITT(cc)
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000043 movcc r0, #0
44 JMPc(lr, cc)
45 /*
46 * Implement division using binary long division algorithm.
47 *
48 * r0 is the numerator, r1 the denominator.
49 *
50 * The code before JMP computes the correct shift I, so that
51 * r0 and (r1 << I) have the highest bit set in the same position.
52 * At the time of JMP, ip := .Ldiv0block - 12 * I.
53 * This depends on the fixed instruction size of block.
Joerg Sonnenberger9720fcf2014-07-20 20:53:37 +000054 * For ARM mode, this is 12 Bytes, for THUMB mode 14 Bytes.
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000055 *
56 * block(shift) implements the test-and-update-quotient core.
57 * It assumes (r0 << shift) can be computed without overflow and
58 * that (r0 << shift) < 2 * r1. The quotient is stored in r3.
59 */
Stephen Canon5abb5c12011-03-18 16:35:02 +000060
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000061# ifdef __ARM_FEATURE_CLZ
62 clz ip, r0
63 clz r3, r1
64 /* r0 >= r1 implies clz(r0) <= clz(r1), so ip <= r3. */
65 sub r3, r3, ip
Joerg Sonnenberger9720fcf2014-07-20 20:53:37 +000066# if __ARM_ARCH_ISA_THUMB == 2
67 adr ip, LOCAL_LABEL(div0block) + 1
68 sub ip, ip, r3, lsl #1
69# else
Joerg Sonnenberger8f6cf702014-07-20 20:00:26 +000070 adr ip, LOCAL_LABEL(div0block)
Joerg Sonnenberger9720fcf2014-07-20 20:53:37 +000071# endif
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000072 sub ip, ip, r3, lsl #2
73 sub ip, ip, r3, lsl #3
74 mov r3, #0
75 bx ip
76# else
Joerg Sonnenberger9720fcf2014-07-20 20:53:37 +000077# if __ARM_ARCH_ISA_THUMB == 2
78# error THUMB mode requires CLZ or UDIV
79# endif
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000080 mov r2, r0
Joerg Sonnenberger8f6cf702014-07-20 20:00:26 +000081 adr ip, LOCAL_LABEL(div0block)
Stephen Canon5abb5c12011-03-18 16:35:02 +000082
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000083 lsr r3, r2, #16
84 cmp r3, r1
85 movhs r2, r3
86 subhs ip, ip, #(16 * 12)
Stephen Canon5abb5c12011-03-18 16:35:02 +000087
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000088 lsr r3, r2, #8
89 cmp r3, r1
90 movhs r2, r3
91 subhs ip, ip, #(8 * 12)
92
93 lsr r3, r2, #4
94 cmp r3, r1
95 movhs r2, r3
96 subhs ip, #(4 * 12)
97
98 lsr r3, r2, #2
99 cmp r3, r1
100 movhs r2, r3
101 subhs ip, ip, #(2 * 12)
102
103 /* Last block, no need to update r2 or r3. */
104 cmp r1, r2, lsr #1
105 subls ip, ip, #(1 * 12)
106
107 mov r3, #0
108
109 JMP(ip)
110# endif
111
112#define IMM #
113
Saleem Abdulrasool31306b12014-07-27 02:01:15 +0000114#define block(shift) \
115 cmp r0, r1, lsl IMM shift; \
116 ITT(hs); \
Saleem Abdulrasool3a2d6a32014-07-27 02:01:24 +0000117 WIDE(addhs) r3, r3, IMM (1 << shift); \
118 WIDE(subhs) r0, r0, r1, lsl IMM shift
Joerg Sonnenberger59611a82014-01-24 13:43:35 +0000119
120 block(31)
121 block(30)
122 block(29)
123 block(28)
124 block(27)
125 block(26)
126 block(25)
127 block(24)
128 block(23)
129 block(22)
130 block(21)
131 block(20)
132 block(19)
133 block(18)
134 block(17)
135 block(16)
136 block(15)
137 block(14)
138 block(13)
139 block(12)
140 block(11)
141 block(10)
142 block(9)
143 block(8)
144 block(7)
145 block(6)
146 block(5)
147 block(4)
148 block(3)
149 block(2)
150 block(1)
Joerg Sonnenberger8f6cf702014-07-20 20:00:26 +0000151LOCAL_LABEL(div0block):
Joerg Sonnenberger59611a82014-01-24 13:43:35 +0000152 block(0)
153
154 mov r0, r3
155 JMP(lr)
156#endif /* __ARM_ARCH_EXT_IDIV__ */
157
158LOCAL_LABEL(divby0):
159 mov r0, #0
160#ifdef __ARM_EABI__
161 b __aeabi_idiv0
162#else
163 JMP(lr)
Bob Wilsona4cefbd2012-09-29 23:37:01 +0000164#endif
Joerg Sonnenberger59611a82014-01-24 13:43:35 +0000165
166END_COMPILERRT_FUNCTION(__udivsi3)