comp.sys.acorn (129/147)
Path: cix!slxsys!uknet!ukc!ieunet!tcdcs!maths.tcd.ie!chughes
From: chughes@maths.tcd.ie (Conrad Hughes)
Newsgroups: comp.sys.acorn
Subject: Re: fast divide
Message-ID: <1991Oct31.131524.17080@maths.tcd.ie>
Date: 31 Oct 91 13:15:24 GMT
References: <3351@m1.cs.man.ac.uk>
Organization: Dept. of Maths, Trinity College, Dublin, Ireland.
Lines: 46

In <3351@m1.cs.man.ac.uk> rogersh%t3b@uk.ac.man.cs (Huw J. Rogers) writes:

>	Can people post/email the fastest 32 bit divide/modulus
>routines they have for the ARM? ObjAsm stuff is preferred, but
>BASIC assembler (ugh!) will do... ;-)

>	I'm sure this is of interest to more than just me!
This is getting to be an FAQ..

DEFFNdiv(A,B,C,R):LOCALA%:[OPTpass:MOVS C,A,LSR#31:RSBNE A,A,#0
 :ADDS C,C,A,LSL#16:MOV A,A,LSR#16:ADDS A,B,A:SUBCC A,A,B:]
  FORA%=1TO31:[OPTpass:ADCS C,C,C:ADCS A,B,A,LSL#1:SUBCC A,A,B:]NEXT
[OPTpass:ADCS R,C,C:RSBCC R,R,#0:]=0

This is the BASIC assembler code to do the division.. Use it this way:

FNdiv(reg_top,reg_bot,reg_misc,reg_res)

while in the BASIC assembler.. It inlines the code. Register reg_res
contains 32768*(reg_top/reg_bot); reg_bot _must_ be negative (if you
don't know what sign it'll be, tack the following in at the beginning:

CMP B,#0:RSBPL B,B,#0:RSBPL A,A,#0

...) , and reg_top can be either sign.. If you need slightly higher
accuracy (I think I can increase the accuracy by half a bit) and
don't worry about registers, tell me.  reg_res can be any register,
including reg_top, reg_bot or reg_misc.

If you want to turn it into a loop, do so.. But remember that the
fundamental unit of of the routine takes 3 clock counts, a branch-loop
takes five! Also, it'll be straightforward enough to turn into a
subroutine..

If you need any clarification of it, ask me...

Someone posted another version which is faster for a logarithmic
distribution of numbers, with 0 and 1 occuring most frequently; I
haven't got it to hand, but could dig it up if nobody else posts
it.

 Conrad --SICK--
-- 
Suicide City, Paranoid State. Geddout! trip to, heave'n'ho, up, down, to'n'fro
you have no word please leave us here close our eyes to the octopus ride isn't
it good to be lost in the wood isn't it bad so quiet there in the wood . . .


