1/*===-- umodsi3.S - 32-bit unsigned integer modulus -----------------------===//
2 *
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 *
10 * This file implements the __umodsi3 (32-bit unsigned integer modulus)
11 * function for the ARM 32-bit architecture.
12 *
13 *===----------------------------------------------------------------------===*/
14
15#include "../assembly.h"
16
17	.syntax unified
18	.text
19
20	.p2align 2
21DEFINE_COMPILERRT_FUNCTION(__umodsi3)
22#if __ARM_ARCH_EXT_IDIV__
23	tst     r1, r1
24	beq     LOCAL_LABEL(divby0)
25	mov 	r3, r0
26	udiv	r0, r3, r1
27	mls 	r1, r0, r1, r3
28	str 	r1, [r2]
29	bx  	lr
30#else
31	cmp	r1, #1
32	bcc	LOCAL_LABEL(divby0)
33	moveq	r0, #0
34	JMPc(lr, eq)
35	cmp	r0, r1
36	JMPc(lr, cc)
37	/*
38	 * Implement division using binary long division algorithm.
39	 *
40	 * r0 is the numerator, r1 the denominator.
41	 *
42	 * The code before JMP computes the correct shift I, so that
43	 * r0 and (r1 << I) have the highest bit set in the same position.
44	 * At the time of JMP, ip := .Ldiv0block - 8 * I.
45	 * This depends on the fixed instruction size of block.
46	 *
47	 * block(shift) implements the test-and-update-quotient core.
48	 * It assumes (r0 << shift) can be computed without overflow and
49	 * that (r0 << shift) < 2 * r1. The quotient is stored in r3.
50	 */
51
52#  ifdef __ARM_FEATURE_CLZ
53	clz	ip, r0
54	clz	r3, r1
55	/* r0 >= r1 implies clz(r0) <= clz(r1), so ip <= r3. */
56	sub	r3, r3, ip
57	adr	ip, LOCAL_LABEL(div0block)
58	sub	ip, ip, r3, lsl #3
59	bx	ip
60#  else
61	mov	r2, r0
62	adr	ip, LOCAL_LABEL(div0block)
63
64	lsr	r3, r2, #16
65	cmp	r3, r1
66	movhs	r2, r3
67	subhs	ip, ip, #(16 * 8)
68
69	lsr	r3, r2, #8
70	cmp	r3, r1
71	movhs	r2, r3
72	subhs	ip, ip, #(8 * 8)
73
74	lsr	r3, r2, #4
75	cmp	r3, r1
76	movhs	r2, r3
77	subhs	ip, #(4 * 8)
78
79	lsr	r3, r2, #2
80	cmp	r3, r1
81	movhs	r2, r3
82	subhs	ip, ip, #(2 * 8)
83
84	/* Last block, no need to update r2 or r3. */
85	cmp	r1, r2, lsr #1
86	subls	ip, ip, #(1 * 8)
87
88	JMP(ip)
89#  endif
90
91#define	IMM	#
92
93#define block(shift) \
94	cmp	r0, r1, lsl IMM shift; \
95	subhs	r0, r0, r1, lsl IMM shift
96
97	block(31)
98	block(30)
99	block(29)
100	block(28)
101	block(27)
102	block(26)
103	block(25)
104	block(24)
105	block(23)
106	block(22)
107	block(21)
108	block(20)
109	block(19)
110	block(18)
111	block(17)
112	block(16)
113	block(15)
114	block(14)
115	block(13)
116	block(12)
117	block(11)
118	block(10)
119	block(9)
120	block(8)
121	block(7)
122	block(6)
123	block(5)
124	block(4)
125	block(3)
126	block(2)
127	block(1)
128LOCAL_LABEL(div0block):
129	block(0)
130	JMP(lr)
131#endif /* __ARM_ARCH_EXT_IDIV__ */
132
133LOCAL_LABEL(divby0):
134	mov	r0, #0
135#ifdef __ARM_EABI__
136	b	__aeabi_idiv0
137#else
138	JMP(lr)
139#endif
140
141END_COMPILERRT_FUNCTION(__umodsi3)
142