blob: 165b2b58acb43f532624e7ffe918d8a53f6e6601 [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
Saleem Abdulrasool48d4e4d2014-10-07 02:39:13 +000030#if __ARM_ARCH_ISA_THUMB == 2
Steven Wu84610ba2014-10-04 00:18:59 +000031DEFINE_COMPILERRT_THUMB_FUNCTION(__udivsi3)
Saleem Abdulrasool48d4e4d2014-10-07 02:39:13 +000032#else
33DEFINE_COMPILERRT_FUNCTION(__udivsi3)
34#endif
Stephen Hines7633afc2013-10-25 06:26:44 +000035#if __ARM_ARCH_EXT_IDIV__
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000036 tst r1, r1
37 beq LOCAL_LABEL(divby0)
Saleem Abdulrasool60639832014-08-09 20:17:37 +000038 udiv r0, r0, r1
Bob Wilsona4cefbd2012-09-29 23:37:01 +000039 bx lr
Bob Wilsona4cefbd2012-09-29 23:37:01 +000040#else
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000041 cmp r1, #1
42 bcc LOCAL_LABEL(divby0)
Joerg Sonnenberger9720fcf2014-07-20 20:53:37 +000043 IT(eq)
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000044 JMPc(lr, eq)
45 cmp r0, r1
Joerg Sonnenberger9720fcf2014-07-20 20:53:37 +000046 ITT(cc)
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000047 movcc r0, #0
48 JMPc(lr, cc)
49 /*
50 * Implement division using binary long division algorithm.
51 *
52 * r0 is the numerator, r1 the denominator.
53 *
54 * The code before JMP computes the correct shift I, so that
55 * r0 and (r1 << I) have the highest bit set in the same position.
56 * At the time of JMP, ip := .Ldiv0block - 12 * I.
57 * This depends on the fixed instruction size of block.
Joerg Sonnenberger9720fcf2014-07-20 20:53:37 +000058 * For ARM mode, this is 12 Bytes, for THUMB mode 14 Bytes.
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000059 *
60 * block(shift) implements the test-and-update-quotient core.
61 * It assumes (r0 << shift) can be computed without overflow and
62 * that (r0 << shift) < 2 * r1. The quotient is stored in r3.
63 */
Stephen Canon5abb5c12011-03-18 16:35:02 +000064
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000065# ifdef __ARM_FEATURE_CLZ
66 clz ip, r0
67 clz r3, r1
68 /* r0 >= r1 implies clz(r0) <= clz(r1), so ip <= r3. */
69 sub r3, r3, ip
Joerg Sonnenberger9720fcf2014-07-20 20:53:37 +000070# if __ARM_ARCH_ISA_THUMB == 2
71 adr ip, LOCAL_LABEL(div0block) + 1
72 sub ip, ip, r3, lsl #1
73# else
Joerg Sonnenberger8f6cf702014-07-20 20:00:26 +000074 adr ip, LOCAL_LABEL(div0block)
Joerg Sonnenberger9720fcf2014-07-20 20:53:37 +000075# endif
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000076 sub ip, ip, r3, lsl #2
77 sub ip, ip, r3, lsl #3
78 mov r3, #0
79 bx ip
80# else
Joerg Sonnenberger9720fcf2014-07-20 20:53:37 +000081# if __ARM_ARCH_ISA_THUMB == 2
82# error THUMB mode requires CLZ or UDIV
83# endif
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000084 mov r2, r0
Joerg Sonnenberger8f6cf702014-07-20 20:00:26 +000085 adr ip, LOCAL_LABEL(div0block)
Stephen Canon5abb5c12011-03-18 16:35:02 +000086
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000087 lsr r3, r2, #16
88 cmp r3, r1
89 movhs r2, r3
90 subhs ip, ip, #(16 * 12)
Stephen Canon5abb5c12011-03-18 16:35:02 +000091
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000092 lsr r3, r2, #8
93 cmp r3, r1
94 movhs r2, r3
95 subhs ip, ip, #(8 * 12)
96
97 lsr r3, r2, #4
98 cmp r3, r1
99 movhs r2, r3
100 subhs ip, #(4 * 12)
101
102 lsr r3, r2, #2
103 cmp r3, r1
104 movhs r2, r3
105 subhs ip, ip, #(2 * 12)
106
107 /* Last block, no need to update r2 or r3. */
108 cmp r1, r2, lsr #1
109 subls ip, ip, #(1 * 12)
110
111 mov r3, #0
112
113 JMP(ip)
114# endif
115
116#define IMM #
117
Saleem Abdulrasool31306b12014-07-27 02:01:15 +0000118#define block(shift) \
119 cmp r0, r1, lsl IMM shift; \
120 ITT(hs); \
Saleem Abdulrasool3a2d6a32014-07-27 02:01:24 +0000121 WIDE(addhs) r3, r3, IMM (1 << shift); \
122 WIDE(subhs) r0, r0, r1, lsl IMM shift
Joerg Sonnenberger59611a82014-01-24 13:43:35 +0000123
124 block(31)
125 block(30)
126 block(29)
127 block(28)
128 block(27)
129 block(26)
130 block(25)
131 block(24)
132 block(23)
133 block(22)
134 block(21)
135 block(20)
136 block(19)
137 block(18)
138 block(17)
139 block(16)
140 block(15)
141 block(14)
142 block(13)
143 block(12)
144 block(11)
145 block(10)
146 block(9)
147 block(8)
148 block(7)
149 block(6)
150 block(5)
151 block(4)
152 block(3)
153 block(2)
154 block(1)
Joerg Sonnenberger8f6cf702014-07-20 20:00:26 +0000155LOCAL_LABEL(div0block):
Joerg Sonnenberger59611a82014-01-24 13:43:35 +0000156 block(0)
157
158 mov r0, r3
159 JMP(lr)
160#endif /* __ARM_ARCH_EXT_IDIV__ */
161
162LOCAL_LABEL(divby0):
163 mov r0, #0
164#ifdef __ARM_EABI__
165 b __aeabi_idiv0
166#else
167 JMP(lr)
Bob Wilsona4cefbd2012-09-29 23:37:01 +0000168#endif
Joerg Sonnenberger59611a82014-01-24 13:43:35 +0000169
170END_COMPILERRT_FUNCTION(__udivsi3)