/*1* Divide a 64-bit unsigned number by a 32-bit unsigned number.2* This routine assumes that the top 32 bits of the dividend are3* non-zero to start with.4* On entry, r3 points to the dividend, which get overwritten with5* the 64-bit quotient, and r4 contains the divisor.6* On exit, r3 contains the remainder.7*8* Copyright (C) 2002 Paul Mackerras, IBM Corp.9*10* This program is free software; you can redistribute it and/or11* modify it under the terms of the GNU General Public License12* as published by the Free Software Foundation; either version13* 2 of the License, or (at your option) any later version.14*/15#include <asm/ppc_asm.h>16#include <asm/processor.h>1718_GLOBAL(__div64_32)19lwz r5,0(r3) # get the dividend into r5/r620lwz r6,4(r3)21cmplw r5,r422li r7,023li r8,024blt 1f25divwu r7,r5,r4 # if dividend.hi >= divisor,26mullw r0,r7,r4 # quotient.hi = dividend.hi / divisor27subf. r5,r0,r5 # dividend.hi %= divisor28beq 3f291: mr r11,r5 # here dividend.hi != 030andis. r0,r5,0xc00031bne 2f32cntlzw r0,r5 # we are shifting the dividend right33li r10,-1 # to make it < 2^32, and shifting34srw r10,r10,r0 # the divisor right the same amount,35addc r9,r4,r10 # rounding up (so the estimate cannot36andc r11,r6,r10 # ever be too large, only too small)37andc r9,r9,r1038addze r9,r939or r11,r5,r1140rotlw r9,r9,r041rotlw r11,r11,r042divwu r11,r11,r9 # then we divide the shifted quantities432: mullw r10,r11,r4 # to get an estimate of the quotient,44mulhwu r9,r11,r4 # multiply the estimate by the divisor,45subfc r6,r10,r6 # take the product from the divisor,46add r8,r8,r11 # and add the estimate to the accumulated47subfe. r5,r9,r5 # quotient48bne 1b493: cmplw r6,r450blt 4f51divwu r0,r6,r4 # perform the remaining 32-bit division52mullw r10,r0,r4 # and get the remainder53add r8,r8,r054subf r6,r10,r6554: stw r7,0(r3) # return the quotient in *r356stw r8,4(r3)57mr r3,r6 # return the remainder in r358blr596061