blob: 8695257ecb7df6c6ceb2ef81ec324299f2d2e654 [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 Abdulrasool1b8f1a42014-06-16 16:05:24 +000019#if __ARM_ARCH_ISA_THUMB == 2
20 .thumb
21#endif
Saleem Abdulrasool57aa97f2014-06-01 04:07:03 +000022
Saleem Abdulrasool8f2efc32014-06-16 16:36:25 +000023#if __ARM_ARCH_ISA_THUMB == 2
24#define IT it
25#define ITT itt
26#else
27#define IT @
28#define ITT @
29#endif
30
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000031 .p2align 2
Anton Korobeynikov75e3c192011-04-19 17:51:24 +000032DEFINE_AEABI_FUNCTION_ALIAS(__aeabi_uidiv, __udivsi3)
Stephen Canon5abb5c12011-03-18 16:35:02 +000033DEFINE_COMPILERRT_FUNCTION(__udivsi3)
Stephen Hines7633afc2013-10-25 06:26:44 +000034#if __ARM_ARCH_EXT_IDIV__
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000035 tst r1, r1
36 beq LOCAL_LABEL(divby0)
37 mov r3, r0
38 udiv r0, r3, r1
39 mls r1, r0, r1, r3
Bob Wilsona4cefbd2012-09-29 23:37:01 +000040 bx lr
Bob Wilsona4cefbd2012-09-29 23:37:01 +000041#else
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000042 cmp r1, #1
Saleem Abdulrasool8f2efc32014-06-16 16:36:25 +000043 IT cc
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000044 bcc LOCAL_LABEL(divby0)
Saleem Abdulrasool8f2efc32014-06-16 16:36:25 +000045 IT eq
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000046 JMPc(lr, eq)
47 cmp r0, r1
Saleem Abdulrasool8f2efc32014-06-16 16:36:25 +000048 ITT cc
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000049 movcc r0, #0
50 JMPc(lr, cc)
51 /*
52 * Implement division using binary long division algorithm.
53 *
54 * r0 is the numerator, r1 the denominator.
55 *
56 * The code before JMP computes the correct shift I, so that
57 * r0 and (r1 << I) have the highest bit set in the same position.
58 * At the time of JMP, ip := .Ldiv0block - 12 * I.
59 * This depends on the fixed instruction size of block.
60 *
61 * block(shift) implements the test-and-update-quotient core.
62 * It assumes (r0 << shift) can be computed without overflow and
63 * that (r0 << shift) < 2 * r1. The quotient is stored in r3.
64 */
Stephen Canon5abb5c12011-03-18 16:35:02 +000065
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000066# ifdef __ARM_FEATURE_CLZ
67 clz ip, r0
68 clz r3, r1
69 /* r0 >= r1 implies clz(r0) <= clz(r1), so ip <= r3. */
70 sub r3, r3, ip
71 adr ip, LOCAL_LABEL(div0block)
72 sub ip, ip, r3, lsl #2
73 sub ip, ip, r3, lsl #3
74 mov r3, #0
75 bx ip
76# else
77 mov r2, r0
78 adr ip, LOCAL_LABEL(div0block)
Stephen Canon5abb5c12011-03-18 16:35:02 +000079
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000080 lsr r3, r2, #16
81 cmp r3, r1
82 movhs r2, r3
83 subhs ip, ip, #(16 * 12)
Stephen Canon5abb5c12011-03-18 16:35:02 +000084
Joerg Sonnenberger59611a82014-01-24 13:43:35 +000085 lsr r3, r2, #8
86 cmp r3, r1
87 movhs r2, r3
88 subhs ip, ip, #(8 * 12)
89
90 lsr r3, r2, #4
91 cmp r3, r1
92 movhs r2, r3
93 subhs ip, #(4 * 12)
94
95 lsr r3, r2, #2
96 cmp r3, r1
97 movhs r2, r3
98 subhs ip, ip, #(2 * 12)
99
100 /* Last block, no need to update r2 or r3. */
101 cmp r1, r2, lsr #1
102 subls ip, ip, #(1 * 12)
103
104 mov r3, #0
105
106 JMP(ip)
107# endif
108
109#define IMM #
110
Saleem Abdulrasool8f2efc32014-06-16 16:36:25 +0000111#define block(shift) \
112 cmp r0, r1, lsl IMM shift; \
113 ITT hs; \
114 addhs r3, r3, IMM (1 << shift); \
Joerg Sonnenberger59611a82014-01-24 13:43:35 +0000115 subhs r0, r0, r1, lsl IMM shift
116
117 block(31)
118 block(30)
119 block(29)
120 block(28)
121 block(27)
122 block(26)
123 block(25)
124 block(24)
125 block(23)
126 block(22)
127 block(21)
128 block(20)
129 block(19)
130 block(18)
131 block(17)
132 block(16)
133 block(15)
134 block(14)
135 block(13)
136 block(12)
137 block(11)
138 block(10)
139 block(9)
140 block(8)
141 block(7)
142 block(6)
143 block(5)
144 block(4)
145 block(3)
146 block(2)
147 block(1)
148LOCAL_LABEL(div0block):
149 block(0)
150
151 mov r0, r3
152 JMP(lr)
153#endif /* __ARM_ARCH_EXT_IDIV__ */
154
155LOCAL_LABEL(divby0):
156 mov r0, #0
157#ifdef __ARM_EABI__
158 b __aeabi_idiv0
159#else
160 JMP(lr)
Bob Wilsona4cefbd2012-09-29 23:37:01 +0000161#endif
Joerg Sonnenberger59611a82014-01-24 13:43:35 +0000162
163END_COMPILERRT_FUNCTION(__udivsi3)