1*94b212c2SPaul Mackerras/* 2*94b212c2SPaul Mackerras * Divide a 64-bit unsigned number by a 32-bit unsigned number. 3*94b212c2SPaul Mackerras * This routine assumes that the top 32 bits of the dividend are 4*94b212c2SPaul Mackerras * non-zero to start with. 5*94b212c2SPaul Mackerras * On entry, r3 points to the dividend, which get overwritten with 6*94b212c2SPaul Mackerras * the 64-bit quotient, and r4 contains the divisor. 7*94b212c2SPaul Mackerras * On exit, r3 contains the remainder. 8*94b212c2SPaul Mackerras * 9*94b212c2SPaul Mackerras * Copyright (C) 2002 Paul Mackerras, IBM Corp. 10*94b212c2SPaul Mackerras * 11*94b212c2SPaul Mackerras * This program is free software; you can redistribute it and/or 12*94b212c2SPaul Mackerras * modify it under the terms of the GNU General Public License 13*94b212c2SPaul Mackerras * as published by the Free Software Foundation; either version 14*94b212c2SPaul Mackerras * 2 of the License, or (at your option) any later version. 15*94b212c2SPaul Mackerras */ 16*94b212c2SPaul Mackerras#include "ppc_asm.h" 17*94b212c2SPaul Mackerras 18*94b212c2SPaul Mackerras .globl __div64_32 19*94b212c2SPaul Mackerras__div64_32: 20*94b212c2SPaul Mackerras lwz r5,0(r3) # get the dividend into r5/r6 21*94b212c2SPaul Mackerras lwz r6,4(r3) 22*94b212c2SPaul Mackerras cmplw r5,r4 23*94b212c2SPaul Mackerras li r7,0 24*94b212c2SPaul Mackerras li r8,0 25*94b212c2SPaul Mackerras blt 1f 26*94b212c2SPaul Mackerras divwu r7,r5,r4 # if dividend.hi >= divisor, 27*94b212c2SPaul Mackerras mullw r0,r7,r4 # quotient.hi = dividend.hi / divisor 28*94b212c2SPaul Mackerras subf. r5,r0,r5 # dividend.hi %= divisor 29*94b212c2SPaul Mackerras beq 3f 30*94b212c2SPaul Mackerras1: mr r11,r5 # here dividend.hi != 0 31*94b212c2SPaul Mackerras andis. r0,r5,0xc000 32*94b212c2SPaul Mackerras bne 2f 33*94b212c2SPaul Mackerras cntlzw r0,r5 # we are shifting the dividend right 34*94b212c2SPaul Mackerras li r10,-1 # to make it < 2^32, and shifting 35*94b212c2SPaul Mackerras srw r10,r10,r0 # the divisor right the same amount, 36*94b212c2SPaul Mackerras add r9,r4,r10 # rounding up (so the estimate cannot 37*94b212c2SPaul Mackerras andc r11,r6,r10 # ever be too large, only too small) 38*94b212c2SPaul Mackerras andc r9,r9,r10 39*94b212c2SPaul Mackerras or r11,r5,r11 40*94b212c2SPaul Mackerras rotlw r9,r9,r0 41*94b212c2SPaul Mackerras rotlw r11,r11,r0 42*94b212c2SPaul Mackerras divwu r11,r11,r9 # then we divide the shifted quantities 43*94b212c2SPaul Mackerras2: mullw r10,r11,r4 # to get an estimate of the quotient, 44*94b212c2SPaul Mackerras mulhwu r9,r11,r4 # multiply the estimate by the divisor, 45*94b212c2SPaul Mackerras subfc r6,r10,r6 # take the product from the divisor, 46*94b212c2SPaul Mackerras add r8,r8,r11 # and add the estimate to the accumulated 47*94b212c2SPaul Mackerras subfe. r5,r9,r5 # quotient 48*94b212c2SPaul Mackerras bne 1b 49*94b212c2SPaul Mackerras3: cmplw r6,r4 50*94b212c2SPaul Mackerras blt 4f 51*94b212c2SPaul Mackerras divwu r0,r6,r4 # perform the remaining 32-bit division 52*94b212c2SPaul Mackerras mullw r10,r0,r4 # and get the remainder 53*94b212c2SPaul Mackerras add r8,r8,r0 54*94b212c2SPaul Mackerras subf r6,r10,r6 55*94b212c2SPaul Mackerras4: stw r7,0(r3) # return the quotient in *r3 56*94b212c2SPaul Mackerras stw r8,4(r3) 57*94b212c2SPaul Mackerras mr r3,r6 # return the remainder in r3 58*94b212c2SPaul Mackerras blr 59