xref: /openbmc/linux/arch/microblaze/lib/divsi3.S (revision 8730046c)
1#include <linux/linkage.h>
2
3/*
4* Divide operation for 32 bit integers.
5*	Input :	Dividend in Reg r5
6*		Divisor in Reg r6
7*	Output: Result in Reg r3
8*/
9	.text
10	.globl	__divsi3
11	.type __divsi3, @function
12	.ent __divsi3
13__divsi3:
14	.frame	r1, 0, r15
15
16	addik	r1, r1, -16
17	swi	r28, r1, 0
18	swi	r29, r1, 4
19	swi	r30, r1, 8
20	swi	r31, r1, 12
21
22	beqi	r6, div_by_zero /* div_by_zero - division error */
23	beqi	r5, result_is_zero /* result is zero */
24	bgeid	r5, r5_pos
25	xor	r28, r5, r6 /* get the sign of the result */
26	rsubi	r5, r5, 0 /* make r5 positive */
27r5_pos:
28	bgei	r6, r6_pos
29	rsubi	r6, r6, 0 /* make r6 positive */
30r6_pos:
31	addik	r30, r0, 0 /* clear mod */
32	addik	r3, r0, 0 /* clear div */
33	addik	r29, r0, 32 /* initialize the loop count */
34
35	/* first part try to find the first '1' in the r5 */
36div0:
37	blti	r5, div2 /* this traps r5 == 0x80000000 */
38div1:
39	add	r5, r5, r5 /* left shift logical r5 */
40	bgtid	r5, div1
41	addik	r29, r29, -1
42div2:
43	/* left shift logical r5 get the '1' into the carry */
44	add	r5, r5, r5
45	addc	r30, r30, r30 /* move that bit into the mod register */
46	rsub	r31, r6, r30 /* try to subtract (r30 a r6) */
47	blti	r31, mod_too_small
48	/* move the r31 to mod since the result was positive */
49	or	r30, r0, r31
50	addik	r3, r3, 1
51mod_too_small:
52	addik	r29, r29, -1
53	beqi	r29, loop_end
54	add	r3, r3, r3 /* shift in the '1' into div */
55	bri	div2 /* div2 */
56loop_end:
57	bgei	r28, return_here
58	brid	return_here
59	rsubi	r3, r3, 0 /* negate the result */
60div_by_zero:
61result_is_zero:
62	or	r3, r0, r0 /* set result to 0 */
63return_here:
64/* restore values of csrs and that of r3 and the divisor and the dividend */
65	lwi	r28, r1, 0
66	lwi	r29, r1, 4
67	lwi	r30, r1, 8
68	lwi	r31, r1, 12
69	rtsd	r15, 8
70	addik	r1, r1, 16
71
72.size __divsi3, . - __divsi3
73.end __divsi3
74