162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Divide a 64-bit unsigned number by a 32-bit unsigned number. 462306a36Sopenharmony_ci * This routine assumes that the top 32 bits of the dividend are 562306a36Sopenharmony_ci * non-zero to start with. 662306a36Sopenharmony_ci * On entry, r3 points to the dividend, which get overwritten with 762306a36Sopenharmony_ci * the 64-bit quotient, and r4 contains the divisor. 862306a36Sopenharmony_ci * On exit, r3 contains the remainder. 962306a36Sopenharmony_ci * 1062306a36Sopenharmony_ci * Copyright (C) 2002 Paul Mackerras, IBM Corp. 1162306a36Sopenharmony_ci */ 1262306a36Sopenharmony_ci#include <asm/ppc_asm.h> 1362306a36Sopenharmony_ci#include <asm/processor.h> 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci_GLOBAL(__div64_32) 1662306a36Sopenharmony_ci lwz r5,0(r3) # get the dividend into r5/r6 1762306a36Sopenharmony_ci lwz r6,4(r3) 1862306a36Sopenharmony_ci cmplw r5,r4 1962306a36Sopenharmony_ci li r7,0 2062306a36Sopenharmony_ci li r8,0 2162306a36Sopenharmony_ci blt 1f 2262306a36Sopenharmony_ci divwu r7,r5,r4 # if dividend.hi >= divisor, 2362306a36Sopenharmony_ci mullw r0,r7,r4 # quotient.hi = dividend.hi / divisor 2462306a36Sopenharmony_ci subf. r5,r0,r5 # dividend.hi %= divisor 2562306a36Sopenharmony_ci beq 3f 2662306a36Sopenharmony_ci1: mr r11,r5 # here dividend.hi != 0 2762306a36Sopenharmony_ci andis. r0,r5,0xc000 2862306a36Sopenharmony_ci bne 2f 2962306a36Sopenharmony_ci cntlzw r0,r5 # we are shifting the dividend right 3062306a36Sopenharmony_ci li r10,-1 # to make it < 2^32, and shifting 3162306a36Sopenharmony_ci srw r10,r10,r0 # the divisor right the same amount, 3262306a36Sopenharmony_ci addc r9,r4,r10 # rounding up (so the estimate cannot 3362306a36Sopenharmony_ci andc r11,r6,r10 # ever be too large, only too small) 3462306a36Sopenharmony_ci andc r9,r9,r10 3562306a36Sopenharmony_ci addze r9,r9 3662306a36Sopenharmony_ci or r11,r5,r11 3762306a36Sopenharmony_ci rotlw r9,r9,r0 3862306a36Sopenharmony_ci rotlw r11,r11,r0 3962306a36Sopenharmony_ci divwu r11,r11,r9 # then we divide the shifted quantities 4062306a36Sopenharmony_ci2: mullw r10,r11,r4 # to get an estimate of the quotient, 4162306a36Sopenharmony_ci mulhwu r9,r11,r4 # multiply the estimate by the divisor, 4262306a36Sopenharmony_ci subfc r6,r10,r6 # take the product from the divisor, 4362306a36Sopenharmony_ci add r8,r8,r11 # and add the estimate to the accumulated 4462306a36Sopenharmony_ci subfe. r5,r9,r5 # quotient 4562306a36Sopenharmony_ci bne 1b 4662306a36Sopenharmony_ci3: cmplw r6,r4 4762306a36Sopenharmony_ci blt 4f 4862306a36Sopenharmony_ci divwu r0,r6,r4 # perform the remaining 32-bit division 4962306a36Sopenharmony_ci mullw r10,r0,r4 # and get the remainder 5062306a36Sopenharmony_ci add r8,r8,r0 5162306a36Sopenharmony_ci subf r6,r10,r6 5262306a36Sopenharmony_ci4: stw r7,0(r3) # return the quotient in *r3 5362306a36Sopenharmony_ci stw r8,4(r3) 5462306a36Sopenharmony_ci mr r3,r6 # return the remainder in r3 5562306a36Sopenharmony_ci blr 56