18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#include <linux/linkage.h>
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci/*
58c2ecf20Sopenharmony_ci* Unsigned divide operation.
68c2ecf20Sopenharmony_ci*	Input :	Divisor in Reg r5
78c2ecf20Sopenharmony_ci*		Dividend in Reg r6
88c2ecf20Sopenharmony_ci*	Output: Result in Reg r3
98c2ecf20Sopenharmony_ci*/
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci	.text
128c2ecf20Sopenharmony_ci	.globl	__udivsi3
138c2ecf20Sopenharmony_ci	.type __udivsi3, @function
148c2ecf20Sopenharmony_ci	.ent __udivsi3
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci__udivsi3:
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci	.frame	r1, 0, r15
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci	addik	r1, r1, -12
218c2ecf20Sopenharmony_ci	swi	r29, r1, 0
228c2ecf20Sopenharmony_ci	swi	r30, r1, 4
238c2ecf20Sopenharmony_ci	swi	r31, r1, 8
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	beqi	r6, div_by_zero /* div_by_zero /* division error */
268c2ecf20Sopenharmony_ci	beqid	r5, result_is_zero /* result is zero */
278c2ecf20Sopenharmony_ci	addik	r30, r0, 0 /* clear mod */
288c2ecf20Sopenharmony_ci	addik	r29, r0, 32 /* initialize the loop count */
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/* check if r6 and r5 are equal - if yes, return 1 */
318c2ecf20Sopenharmony_ci	rsub	r18, r5, r6
328c2ecf20Sopenharmony_ci	beqid	r18, return_here
338c2ecf20Sopenharmony_ci	addik	r3, r0, 1
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* check if (uns)r6 is greater than (uns)r5. in that case, just return 0 */
368c2ecf20Sopenharmony_ci	xor	r18, r5, r6
378c2ecf20Sopenharmony_ci	bgeid	r18, 16
388c2ecf20Sopenharmony_ci	add	r3, r0, r0 /* we would anyways clear r3 */
398c2ecf20Sopenharmony_ci	blti	r6, return_here /* r6[bit 31 = 1] hence is greater */
408c2ecf20Sopenharmony_ci	bri	checkr6
418c2ecf20Sopenharmony_ci	rsub	r18, r6, r5 /* microblazecmp */
428c2ecf20Sopenharmony_ci	blti	r18, return_here
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/* if r6 [bit 31] is set, then return result as 1 */
458c2ecf20Sopenharmony_cicheckr6:
468c2ecf20Sopenharmony_ci	bgti	r6, div0
478c2ecf20Sopenharmony_ci	brid	return_here
488c2ecf20Sopenharmony_ci	addik	r3, r0, 1
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci/* first part try to find the first '1' in the r5 */
518c2ecf20Sopenharmony_cidiv0:
528c2ecf20Sopenharmony_ci	blti	r5, div2
538c2ecf20Sopenharmony_cidiv1:
548c2ecf20Sopenharmony_ci	add	r5, r5, r5 /* left shift logical r5 */
558c2ecf20Sopenharmony_ci	bgtid	r5, div1
568c2ecf20Sopenharmony_ci	addik	r29, r29, -1
578c2ecf20Sopenharmony_cidiv2:
588c2ecf20Sopenharmony_ci/* left shift logical r5 get the '1' into the carry */
598c2ecf20Sopenharmony_ci	add	r5, r5, r5
608c2ecf20Sopenharmony_ci	addc	r30, r30, r30 /* move that bit into the mod register */
618c2ecf20Sopenharmony_ci	rsub	r31, r6, r30 /* try to subtract (r30 a r6) */
628c2ecf20Sopenharmony_ci	blti	r31, mod_too_small
638c2ecf20Sopenharmony_ci/* move the r31 to mod since the result was positive */
648c2ecf20Sopenharmony_ci	or	r30, r0, r31
658c2ecf20Sopenharmony_ci	addik	r3, r3, 1
668c2ecf20Sopenharmony_cimod_too_small:
678c2ecf20Sopenharmony_ci	addik	r29, r29, -1
688c2ecf20Sopenharmony_ci	beqi	r29, loop_end
698c2ecf20Sopenharmony_ci	add	r3, r3, r3 /* shift in the '1' into div */
708c2ecf20Sopenharmony_ci	bri	div2 /* div2 */
718c2ecf20Sopenharmony_ciloop_end:
728c2ecf20Sopenharmony_ci	bri	return_here
738c2ecf20Sopenharmony_cidiv_by_zero:
748c2ecf20Sopenharmony_ciresult_is_zero:
758c2ecf20Sopenharmony_ci	or	r3, r0, r0 /* set result to 0 */
768c2ecf20Sopenharmony_cireturn_here:
778c2ecf20Sopenharmony_ci/* restore values of csrs and that of r3 and the divisor and the dividend */
788c2ecf20Sopenharmony_ci	lwi	r29, r1, 0
798c2ecf20Sopenharmony_ci	lwi	r30, r1, 4
808c2ecf20Sopenharmony_ci	lwi	r31, r1, 8
818c2ecf20Sopenharmony_ci	rtsd	r15, 8
828c2ecf20Sopenharmony_ci	addik	r1, r1, 12
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci.size __udivsi3, . - __udivsi3
858c2ecf20Sopenharmony_ci.end __udivsi3
86