[comp.sys.acorn] integer division

zrzm0370@rusmv1.rus.uni-stuttgart.de (Joerg Scheurich) (06/10/91)

Hi 

So i know, there is no Assembler-command for integer-division in the 
ARM. Therefor a fast division-routine is a thing of common interest.

In a earlier Posting i wrote about the bad quick hacked division-routine
for the demo of my formula-tool (math. formula ==> assemblermacros).

Now i wrote a better routine, but i don't know, if there is a better 
algorithm.

The shorter and slower 32-Bit/32-Bit-division-routine requires
382     Cycles in 100 Bytes, with usage of 4 32-Bit-registers and 4 Byte memory.
With unrolling the loop,
the faster  and longer 32-Bit/32-Bit-division-routine requires 
182     Cycles in 692 Bytes, with usage of 4 32-Bit-registers.
  
This is very faster then the old routine (which requires 2**33 Cycles in 
worst case and requires all time of the universe, if there is a division
by zero ... )

Cause the INTERNAL-assembler-command of the INTEL 8086
for a                  32-Bit/16-Bit-division  requires
165-184 Cycles in 2-4 Bytes, with usage of 3  16-Bit-registers,
i think, the result is ok.   

The result is nothing against the INTERNAL-assembler-command of the ARM
for a                  32-Bit*32-Bit-multiplication which  requires
17     Cycles in    4 Bytes, with usage of 1-3 32-Bit-registers,
but my Professor in digital electronics used to say:
"If a program contain much divisions, the programmer is a idiot" ...

Know someone a better (faster or shorter) algorithm ?

so long
MUFTI

( internet:    zrzm0111@helpdesk.rus.uni-stuttgart.de
  ( from janet: zrzm0111%de.uni-stuttgart.rus.helpdesk@NFS-RELAY )
  bitnet:      ZRZM  AT DS0RUS1I )

This is not a posting of a binary. Cause the readable routines contains very 
much linefeeds and spaces, I send them in submit-extract-format (41 lines). 

Posting-number: Volume 01, Issue 07
Submitted-by: MUFTI
Archive-name: Division Macros/part01

table
 !"#$%&'()*+,-./0123456789:;<=>?
@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_
begin 644 <SubExtWrk$Dir>.Work.File-Z
M'YV07YBDF4,'@,&#"!,J7,BPH<.'$",B!$'1!@P8!BEJQ A (T6.'CW&@#&Cz
M8PT8,FB<K!%#AHR.(&K,N'&C(T>).'/JW,FSIT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O144<@EGr
MT4RU@3AUF$-7SZ  5M76MC,(D'?!M"\W/>L-[QEC/X4I3T62D*"<-.@NNYA0q
MQL$-5](E5.G&Y-R0""&B%#EO>A4Z226Q=E#7=4-VMQNXADY7(Q"5:,TH&J[Jp
CXBFCH]IH1S\*F*5Z^,,@#K&(1TSB$IOXQ"A.L8I7S.(6>QA7o
 n
end

chughes@maths.tcd.ie (Conrad Hughes) (06/11/91)

In <1991Jun10.112536.15897@rusmv1.rus.uni-stuttgart.de>
zrzm0370@rusmv1.rus.uni-stuttgart.de (Joerg Scheurich) writes
about division..

>382     Cycles in 100 Bytes, with usage of 4 32-Bit-registers
>and 4 Byte memory.

>With unrolling the loop,
>the faster  and longer 32-Bit/32-Bit-division-routine requires 
>182     Cycles in 692 Bytes, with usage of 4 32-Bit-registers.

This divides a 64bit number by a 32bit one, with 32bit result, in 97
instructions/388 bytes/97 clock cycles..

Entry: A=MSW of 64bit no., B=LSW of 64bit no., C=32bit no.
	C _must_ be negative (if it's positive, RSB C,C,#0),
	A _must_ be positive (if not, RSBS B,B,#0:RSC A,A,#0)
ADDS B,B,B
ADCS A,C,A,LSL#1:SUBCC A,A,C:ADCS B,B,B [repeat these three 32 times]
Exit: A=Remainder of division, B=result, C unchanged.
	..if you don't need the remainder, get rid of the last
	SUBCC A,A,C.
	The result will be unsigned, and flags will (obviously)
	reflect the last addition.
Adjust as you will to get 32/32 division (Initialise A to 0, or AB
= numerator shifted some way) or any other variety you like..
Rolling up the loop it could get slightly complicated since every
instruction requires flag information from the previous one ;-)
Something like SUB count,count,#1:TEQ count,#0:BNE loop with count
initialised to #32 might do the trick, but it'll slow it down to at
least 288 cycles (so what if it only uses 32 bytes?? ;-}), which is
pretty unacceptable unless you're on an ARM3..

This is off the top of my head and I'm nowhere near the Arc, so if
it doesn't work (it should - I've typed it in enough times now..)
flame me..

Conrad
-- 
Will you sell me one of those if I shave my head
Get me out of town is what Fireball said
Never trust a man in a blue trench coat
Never drive a car when you're dead                      Tom Waits