blob: 4e30be9f41dab4af4405d7f44ec0a4a1720ee039 [file] [log] [blame]
Joerg Sonnenberger59611a82014-01-24 13:43:35 +00001/*===-- udivmodsi4.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
Stephen Canon5abb5c12011-03-18 16:35:02 +000018
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000019#ifdef ARM_HAS_BX
20#define JMP(r) bx r
21#define JMPc(r,c) bx##c r
22#else
23#define JMP(r) mov pc, r
24#define JMPc(r,c) mov##c pc, r
25#endif
Stephen Canon5abb5c12011-03-18 16:35:02 +000026
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000027 .text
28 .arm
29 .p2align 2
Anton Korobeynikov75e3c192011-04-19 17:51:24 +000030DEFINE_AEABI_FUNCTION_ALIAS(__aeabi_uidiv, __udivsi3)
Stephen Canon5abb5c12011-03-18 16:35:02 +000031DEFINE_COMPILERRT_FUNCTION(__udivsi3)
Stephen Hines7633afc2013-10-25 06:26:44 +000032#if __ARM_ARCH_EXT_IDIV__
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000033 tst r1, r1
34 beq LOCAL_LABEL(divby0)
35 mov r3, r0
36 udiv r0, r3, r1
37 mls r1, r0, r1, r3
Bob Wilsona4cefbd2012-09-29 23:37:01 +000038 bx lr
Bob Wilsona4cefbd2012-09-29 23:37:01 +000039#else
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000040 cmp r1, #1
41 bcc LOCAL_LABEL(divby0)
42 JMPc(lr, eq)
43 cmp r0, r1
44 movcc r0, #0
45 JMPc(lr, cc)
46 /*
47 * Implement division using binary long division algorithm.
48 *
49 * r0 is the numerator, r1 the denominator.
50 *
51 * The code before JMP computes the correct shift I, so that
52 * r0 and (r1 << I) have the highest bit set in the same position.
53 * At the time of JMP, ip := .Ldiv0block - 12 * I.
54 * This depends on the fixed instruction size of block.
55 *
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
66 adr ip, LOCAL_LABEL(div0block)
67 sub ip, ip, r3, lsl #2
68 sub ip, ip, r3, lsl #3
69 mov r3, #0
70 bx ip
71# else
72 mov r2, r0
73 adr ip, LOCAL_LABEL(div0block)
Stephen Canon5abb5c12011-03-18 16:35:02 +000074
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000075 lsr r3, r2, #16
76 cmp r3, r1
77 movhs r2, r3
78 subhs ip, ip, #(16 * 12)
Stephen Canon5abb5c12011-03-18 16:35:02 +000079
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000080 lsr r3, r2, #8
81 cmp r3, r1
82 movhs r2, r3
83 subhs ip, ip, #(8 * 12)
84
85 lsr r3, r2, #4
86 cmp r3, r1
87 movhs r2, r3
88 subhs ip, #(4 * 12)
89
90 lsr r3, r2, #2
91 cmp r3, r1
92 movhs r2, r3
93 subhs ip, ip, #(2 * 12)
94
95 /* Last block, no need to update r2 or r3. */
96 cmp r1, r2, lsr #1
97 subls ip, ip, #(1 * 12)
98
99 mov r3, #0
100
101 JMP(ip)
102# endif
103
104#define IMM #
105
106#define block(shift) \
107 cmp r0, r1, lsl IMM shift; \
108 addhs r3, r3, IMM (1 << shift); \
109 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)
142LOCAL_LABEL(div0block):
143 block(0)
144
145 mov r0, r3
146 JMP(lr)
147#endif /* __ARM_ARCH_EXT_IDIV__ */
148
149LOCAL_LABEL(divby0):
150 mov r0, #0
151#ifdef __ARM_EABI__
152 b __aeabi_idiv0
153#else
154 JMP(lr)
Bob Wilsona4cefbd2012-09-29 23:37:01 +0000155#endif
Joerg Sonnenberger59611a82014-01-24 13:43:35 +0000156
157END_COMPILERRT_FUNCTION(__udivsi3)