refractor:
I combined one of the algorithms from the Acorn info you provided and one from one of my DSP app notes. The resulting algorithm is shown below... first in C, and then in ASM.
Test conditions: I had my 3D game process each group of vertices 100 times. While there is still a lot of other processing that happens (ie; texture mapping and such), repeating the processing 100 times at least puts some focus on the divide routine.
The original divide routine was defined as such:
- Code:
inline long idiv16(long x, long y)
{
__int64 z=((__int64)x<<16)/y;
return((long)z);
}
I've named the new routine, "nr-mod" as it is a modified (sort of unrolled) Newton-Raphson approach.
- Code:
// long results=idiv16(long numerator,long denominator);
// assumes numerator, denominator are 16:16
// results is 16:16
inline long idiv16(long n,long d)
{
long g,t0,t1,t2;
long sign=d&0x80000000;
if(sign)
d=-d;
//we need a good guess to start the Newton-Raphson interation
//the logic that follows is from a suggestion by David Seal (dseal@acorn.co.uk) found at:
//http://www.poppyfields.net/acorn/tech/division.shtml (toward the bottom)
g=3;
long td=d;
while((td&0x40000000)==0)
{
td<<=1;
g<<=1;
}
//now we have our guess for the modified Newton-Raphson iteration
//the following iterative convergence algorithm is from:
//Cavanagh, J. 1984. Digital Computer Arithmetic. McGraw-Hill. Page 284.
t1=imul16(g,d); //t1=g*d
t2=imul16(g,n); //t2=g*n
t0=0x20000-t1; //t0=2-t1
t1=imul16(t0,t1); //t1=(2-(g*d))*(g*d)
t2=imul16(t0,t2); //t2=(2-(g*d))*(g*n)
t0=0x20000-t1; //t0=2-((2-(g*d))*(g*d))
t1=imul16(t0,t1); //t1=(2-((2-(g*d))*(g*d)))*(2-(g*d))*(g*d)
t2=imul16(t0,t2); //t2=(2-((2-(g*d))*(g*d)))*(2-(g*d))*(g*n)
t0=0x20000-t1; //t0=2-((2-((2-(g*d))*(g*d)))*(2-(g*d))*(g*d))
t0=imul16(t0,t2); //t0=(2-((2-((2-(g*d))*(g*d)))*(2-(g*d))*(g*d)))*
// (2-((2-(g*d))*(g*d)))*(2-(g*d))*(g*n)
if(sign)
t0=-t0;
return(t0);
}
I didn't bother optimizing the C routine, as I will use the ASM version on the PPC. I just needed the C routine for running on the PC. Here is the ASM routine. Note, it assumes XScale, and the latest version of ARMASM. If you don't have the latest ARMASM, (but do have an XScale) then put DCD 0xE16F3F11 in place of the clz instruction. If you don't have an XScale, you need to put some code there to count the leading zeros.
- Code:
;// idiv16
;//
;// long results=idiv16(long numerator,long denominator);
;// assumes numerator, denominator are 16:16
;// results is 16:16
;// register definitions
numerator RN 0
denominator RN 1
guess RN 2
t0 RN 0
t1 RN 1
t2 RN 3
n0 RN 6
n1 RN 7
sign RN 4
fix2 RN 5
PROC
stmdb sp!, {r4 - r7, lr} ;// save r4-r7, return address
movs sign,denominator ;// copy denominator to sign updating flags, N flag well be set if negative
mvnmi denominator,denominator ;// if MI (negative), take one's complement
addmi denominator,denominator,#1 ;// if MI (negative), add one to get two's complement
;//we need a good guess to start the Newton-Raphson interation
;//the logic that follows is from a suggestion by David Seal (dseal@acorn.co.uk) found at:
;//http://www.poppyfields.net/acorn/tech/division.shtml (toward the bottom)
mov guess,#3 ;// initial guess, two LSB
clz t2,denominator ;// find most significant bit
sub t2,t2,#1 ;// we shift the guess up by one less than the number of leading zeros, so dec the count
mov guess,guess,lsl t2 ;// shift guess up
;//now we have our guess for the modified Newton-Raphson iteration
;//the following interative convergence algorithm is from:
;//Cavanagh, J. 1984. Digital Computer Arithmetic. McGraw-Hill. Page 284.
smull t2,n1,guess,numerator
smull t1,n0,guess,denominator
mov fix2,#0x20000 ;// setup a constant to subtract off (done here to eat up some of the multiply delay)
mov t2,t2,lsr #16 ;// shift low 32 bits of results down 16
orr t2,t2,n1,lsl #16 ;// shift high 32 bits up 16 and or in with low, equiv to c=(long)(((__int64)a*(__int64)b)>>16);
;// t2=guess*numerator
mov t1,t1,lsr #16
orr t1,t1,n0,lsl #16 ;// t1=guess*denominator
sub t0,fix2,t1 ;// t0=0x20000-t1
smull t1,n0,t0,t1 ;// repeat above
smull t2,n1,t0,t2
mov t1,t1,lsr #16
orr t1,t1,n0,lsl #16
sub t0,fix2,t1
mov t2,t2,lsr #16
orr t2,t2,n1,lsl #16
smull t1,n0,t0,t1 ;// repeat above
smull t2,n1,t0,t2
mov t1,t1,lsr #16
orr t1,t1,n0,lsl #16
sub t0,fix2,t1
mov t2,t2,lsr #16
orr t2,t2,n1,lsl #16
smull t0,n0,t0,t2 ;// last step
mov t0,t0,lsr #16
orr t0,t0,n0,lsl #16
movs sign,sign ;// copy sign whilst updating flags, N flags well be set if negative
mvnmi t0,t0 ;// if MI (negative) take one's complement
addmi t0,t0,#1 ;// if MI (negative) add one to get two's complement
ldmia sp!, {r4 - r7, pc} ;// restore r4-r7 and return
ENDP
With this routine being called throughout my game, I noticed no difference in rendering accuracy, so I am assuming that the routine is doing a pretty good job. Maybe someone else can test the accuracy. Here are the results:
Dell Axim @ 200MHz
----------------------
original: 2.6 fps
nr-mod in C: 3.5 fps
nr-mod in ASM: 4.0 fps
Dell Axim @ 400MHz
----------------------
orginal: 5.2 fps
nr-mod in C: 6.8 fps
nr-mod in ASM: 7.3 fps
Obviously, the nr-mod routine is a good bit faster than the C LIB routine. I was a little surprised that the ASM routine was not that much faster than the C routine. I guess I need to go back over the ASM routine and see if I missed something.
It is important to note that these results are from a game, one that is completely filling the screen with textured polys, lightmaps, dynamic lighting, playing MP3 and what not. There are *not* many divide routines relative to the rest of the code. Thus the increase in fps is quite amazing. I will probably write a simple test program that focuses only on the divide routine.
Note that at 400MHz, the nr-mod (ASM) routine is starting to fall off a little bit. It is not twice the fps as when running at 200MHz. This is very typical of the XScale as it is starting to outrun memory. Note to Intel... most program do need to access memory, so that should be fast.
It would be great if others could take this routine, try it out, optimize it some more and post their results.
Have fun!
rcp