Article View: comp.sys.powerpc
Article #2Re: ANSWER: algorithm to perform 64-bit / 32-bit signed & unsigned divide
From: Charles B. Cranston
Date: Fri, 02 Sep 1994 22:49
Date: Fri, 02 Sep 1994 22:49
88 lines
3176 bytes
3176 bytes
These are my first two assembly language programs on the power PC.
dmult multiplies two 32 bit numbers to get a 64 bit result,
ddiv divides a 32 bit number into a 64 bit number to get a 32 bit result.
it probably needs to be protected from overflow...
=====
dmult ;
MulLW R6,R3,R4 ; Generate low word of result
StW R6,4(R5) ; Save result
MulHWU R7,R3,R4 ; Generate high word of result
StW R7,0(R5) ; Save result
BLR ; Return to caller
=====
ddiv ;
RLWINM R6,R5,16,16,31 ; Get upper shifted B
RLWINM R7,R5,16,0,15 ; Get lower shifted B
LI R0,2 ; Do this loop twice
MTCtr R0 ; Set count register
@010 ;
RLWINM R11,R10,16,0,15 ; Generate hi word second time through
AddI R0,R6,1 ; Get [B upper 16] + 1 for trial division
DivWU R10,R3,R0 ; Get estimate for high digit of quotient
RLWINM R0,R10,16,0,15 ; Get shifted estimate
MulLW R9,R0,R5 ; Get low word of [shifted digit times full B]
MulHWU R8,R0,R5 ; Get high word of [shifted digit times full B]
SubFC R4,R9,R4 ; Get remainder low
SubFE R3,R8,R3 ; Get remainder high
@040 ;
CmpLW R3,R6 ; High order word says we need restoring subtract?
BGT @050 ; Yes, go do restoring subtract
BNE @060 ; No, we now have upper quotient digit
CmpLW R4,R7 ; Low order word says we need restoring subtract?
BLT @060 ; No, we now have upper quotient digit
@050 ;
AddI R10,R10,1 ; Increment quotient digit
SubFC R4,R7,R4 ; Restoring subtract lower
SubFE R3,R6,R3 ; Restoring subtract upper
B @040 ; Back to test if another cycle needed
#
@060 ;
RLWINM R3,R3,16,0,15 ; Left shift accumulator
RLWIMI R3,R4,16,16,31 ; Left shift accumulator
RLWINM R4,R4,16,0,15 ; Left shift accumulator
BDNZ @010 ; Do this loop twice
#
RLWIMI R11,R10,0,16,31 ; Assemble estimated quotient
MR R3,R11 ; Get answer
BLR ; Return to caller
=====
void
dotest(long x, long b)
{
long q,a[2],dd[64];
dmult(x,b,a);
q = ddiv(a[0],a[1],b);
if (q != x) {
dd[0] = x;
dd[1] = b;
dd[2] = a[0];
dd[3] = a[1];
dd[4] = q;
dumplong(5,dd);
}
}
void
main()
{
long i,x,b;
InitGraf( (Ptr) &qd.thePort );
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs(nil);
InitCursor();
display(message);
for (i=0; i<1000000; i++) {
x = randlong();
b = randlong() | 0x80000000;
dotest(x,b);
}
}
Technical Details
Message-ID:
<348a5a$dhs@haven.umd.edu>Path:
arc1-ca.pugleaf.net!wupost!news.utdallas.edu!corpgate!bnrgate!nott!torn!howland.reston.ans.net!europa.eng.gtefsd.com!news.umbc.edu!haven.umd.edu!zben-mac-ii.umd.edu!zbenReferences:
<joe.777905986@babel.ho.att.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!wupost!news.utdallas.edu!corpgate!bnrgate!nott!torn!howland.reston.ans.net!europa.eng.gtefsd.com!news.umbc.edu!haven.umd.edu!zben-mac-ii.umd.edu!zben From: Charles B. Cranston <zben@ni.umd.edu> Newsgroups: comp.sys.powerpc,comp.arch.arithmetic,gnu.misc.discuss,comp.programming,sci.math,alt.sources Subject: Re: ANSWER: algorithm to perform 64-bit / 32-bit signed & unsigned divide Date: 2 Sep 1994 22:49:14 GMT Organization: Network Infrastructure U Maryland College Park Lines: 88 Distribution: world Message-ID: <348a5a$dhs@haven.umd.edu> References: <joe.777905986@babel.ho.att.com> NNTP-Posting-Host: zben-mac-ii.umd.edu X-Newsreader: Nuntius Version 1.2 X-XXMessage-ID: <AA8D242B3B020862@zben-mac-ii.umd.edu> X-XXDate: Fri, 2 Sep 1994 22:49:15 GMT