18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Divide a 64-bit unsigned number by a 32-bit unsigned number.
48c2ecf20Sopenharmony_ci * This routine assumes that the top 32 bits of the dividend are
58c2ecf20Sopenharmony_ci * non-zero to start with.
68c2ecf20Sopenharmony_ci * On entry, r3 points to the dividend, which get overwritten with
78c2ecf20Sopenharmony_ci * the 64-bit quotient, and r4 contains the divisor.
88c2ecf20Sopenharmony_ci * On exit, r3 contains the remainder.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Copyright (C) 2002 Paul Mackerras, IBM Corp.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci#include <asm/ppc_asm.h>
138c2ecf20Sopenharmony_ci#include <asm/processor.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci_GLOBAL(__div64_32)
168c2ecf20Sopenharmony_ci	lwz	r5,0(r3)	# get the dividend into r5/r6
178c2ecf20Sopenharmony_ci	lwz	r6,4(r3)
188c2ecf20Sopenharmony_ci	cmplw	r5,r4
198c2ecf20Sopenharmony_ci	li	r7,0
208c2ecf20Sopenharmony_ci	li	r8,0
218c2ecf20Sopenharmony_ci	blt	1f
228c2ecf20Sopenharmony_ci	divwu	r7,r5,r4	# if dividend.hi >= divisor,
238c2ecf20Sopenharmony_ci	mullw	r0,r7,r4	# quotient.hi = dividend.hi / divisor
248c2ecf20Sopenharmony_ci	subf.	r5,r0,r5	# dividend.hi %= divisor
258c2ecf20Sopenharmony_ci	beq	3f
268c2ecf20Sopenharmony_ci1:	mr	r11,r5		# here dividend.hi != 0
278c2ecf20Sopenharmony_ci	andis.	r0,r5,0xc000
288c2ecf20Sopenharmony_ci	bne	2f
298c2ecf20Sopenharmony_ci	cntlzw	r0,r5		# we are shifting the dividend right
308c2ecf20Sopenharmony_ci	li	r10,-1		# to make it < 2^32, and shifting
318c2ecf20Sopenharmony_ci	srw	r10,r10,r0	# the divisor right the same amount,
328c2ecf20Sopenharmony_ci	addc	r9,r4,r10	# rounding up (so the estimate cannot
338c2ecf20Sopenharmony_ci	andc	r11,r6,r10	# ever be too large, only too small)
348c2ecf20Sopenharmony_ci	andc	r9,r9,r10
358c2ecf20Sopenharmony_ci	addze	r9,r9
368c2ecf20Sopenharmony_ci	or	r11,r5,r11
378c2ecf20Sopenharmony_ci	rotlw	r9,r9,r0
388c2ecf20Sopenharmony_ci	rotlw	r11,r11,r0
398c2ecf20Sopenharmony_ci	divwu	r11,r11,r9	# then we divide the shifted quantities
408c2ecf20Sopenharmony_ci2:	mullw	r10,r11,r4	# to get an estimate of the quotient,
418c2ecf20Sopenharmony_ci	mulhwu	r9,r11,r4	# multiply the estimate by the divisor,
428c2ecf20Sopenharmony_ci	subfc	r6,r10,r6	# take the product from the divisor,
438c2ecf20Sopenharmony_ci	add	r8,r8,r11	# and add the estimate to the accumulated
448c2ecf20Sopenharmony_ci	subfe.	r5,r9,r5	# quotient
458c2ecf20Sopenharmony_ci	bne	1b
468c2ecf20Sopenharmony_ci3:	cmplw	r6,r4
478c2ecf20Sopenharmony_ci	blt	4f
488c2ecf20Sopenharmony_ci	divwu	r0,r6,r4	# perform the remaining 32-bit division
498c2ecf20Sopenharmony_ci	mullw	r10,r0,r4	# and get the remainder
508c2ecf20Sopenharmony_ci	add	r8,r8,r0
518c2ecf20Sopenharmony_ci	subf	r6,r10,r6
528c2ecf20Sopenharmony_ci4:	stw	r7,0(r3)	# return the quotient in *r3
538c2ecf20Sopenharmony_ci	stw	r8,4(r3)
548c2ecf20Sopenharmony_ci	mr	r3,r6		# return the remainder in r3
558c2ecf20Sopenharmony_ci	blr
56