xref: /OK3568_Linux_fs/kernel/arch/powerpc/lib/div64.S (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun/* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun/*
3*4882a593Smuzhiyun * Divide a 64-bit unsigned number by a 32-bit unsigned number.
4*4882a593Smuzhiyun * This routine assumes that the top 32 bits of the dividend are
5*4882a593Smuzhiyun * non-zero to start with.
6*4882a593Smuzhiyun * On entry, r3 points to the dividend, which get overwritten with
7*4882a593Smuzhiyun * the 64-bit quotient, and r4 contains the divisor.
8*4882a593Smuzhiyun * On exit, r3 contains the remainder.
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * Copyright (C) 2002 Paul Mackerras, IBM Corp.
11*4882a593Smuzhiyun */
12*4882a593Smuzhiyun#include <asm/ppc_asm.h>
13*4882a593Smuzhiyun#include <asm/processor.h>
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun_GLOBAL(__div64_32)
16*4882a593Smuzhiyun	lwz	r5,0(r3)	# get the dividend into r5/r6
17*4882a593Smuzhiyun	lwz	r6,4(r3)
18*4882a593Smuzhiyun	cmplw	r5,r4
19*4882a593Smuzhiyun	li	r7,0
20*4882a593Smuzhiyun	li	r8,0
21*4882a593Smuzhiyun	blt	1f
22*4882a593Smuzhiyun	divwu	r7,r5,r4	# if dividend.hi >= divisor,
23*4882a593Smuzhiyun	mullw	r0,r7,r4	# quotient.hi = dividend.hi / divisor
24*4882a593Smuzhiyun	subf.	r5,r0,r5	# dividend.hi %= divisor
25*4882a593Smuzhiyun	beq	3f
26*4882a593Smuzhiyun1:	mr	r11,r5		# here dividend.hi != 0
27*4882a593Smuzhiyun	andis.	r0,r5,0xc000
28*4882a593Smuzhiyun	bne	2f
29*4882a593Smuzhiyun	cntlzw	r0,r5		# we are shifting the dividend right
30*4882a593Smuzhiyun	li	r10,-1		# to make it < 2^32, and shifting
31*4882a593Smuzhiyun	srw	r10,r10,r0	# the divisor right the same amount,
32*4882a593Smuzhiyun	addc	r9,r4,r10	# rounding up (so the estimate cannot
33*4882a593Smuzhiyun	andc	r11,r6,r10	# ever be too large, only too small)
34*4882a593Smuzhiyun	andc	r9,r9,r10
35*4882a593Smuzhiyun	addze	r9,r9
36*4882a593Smuzhiyun	or	r11,r5,r11
37*4882a593Smuzhiyun	rotlw	r9,r9,r0
38*4882a593Smuzhiyun	rotlw	r11,r11,r0
39*4882a593Smuzhiyun	divwu	r11,r11,r9	# then we divide the shifted quantities
40*4882a593Smuzhiyun2:	mullw	r10,r11,r4	# to get an estimate of the quotient,
41*4882a593Smuzhiyun	mulhwu	r9,r11,r4	# multiply the estimate by the divisor,
42*4882a593Smuzhiyun	subfc	r6,r10,r6	# take the product from the divisor,
43*4882a593Smuzhiyun	add	r8,r8,r11	# and add the estimate to the accumulated
44*4882a593Smuzhiyun	subfe.	r5,r9,r5	# quotient
45*4882a593Smuzhiyun	bne	1b
46*4882a593Smuzhiyun3:	cmplw	r6,r4
47*4882a593Smuzhiyun	blt	4f
48*4882a593Smuzhiyun	divwu	r0,r6,r4	# perform the remaining 32-bit division
49*4882a593Smuzhiyun	mullw	r10,r0,r4	# and get the remainder
50*4882a593Smuzhiyun	add	r8,r8,r0
51*4882a593Smuzhiyun	subf	r6,r10,r6
52*4882a593Smuzhiyun4:	stw	r7,0(r3)	# return the quotient in *r3
53*4882a593Smuzhiyun	stw	r8,4(r3)
54*4882a593Smuzhiyun	mr	r3,r6		# return the remainder in r3
55*4882a593Smuzhiyun	blr
56