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