Re: division algorithm
From: Thad Smith (ThadSmith_at_acm.org)
Date: 03/21/04
- Next message: Howard Kaikow: "Re: MS Word / Excel File Viewer API"
- Previous message: Michael Wojcik: "Re: Is it possible to count time in micosecond under Linux?"
- In reply to: Gerry Quinn: "Re: division algorithm"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 20 Mar 2004 23:15:44 -0700
Gerry Quinn wrote:
>
> In article <c3end3$4mg$1@inews.gazeta.pl>, "ArturS" <mail_skrzynka@CUTTHISwp.pl> wrote:
> >hi,
> >Does anyone know how to divide one 2-bytes number by another which is 1-byte
> >long ; there are only one-byte registers available (microcontroller 8051).
> >Any suggestions ?
>
> A simple way might be to get a lower limit by dividing separately into
> the upper and lower bytes - then correct by subtraction.
Not really. When you work out the details, you still do a lot of
shifting and comparing. You're better off with the standard shift and
subtract.
In general if you require an integer division which exceeds the capacity
of the native divide instruction, you have a decision to either
1) implement a shift and subtract loop, with one bit generated each
pass, or
2) use the divide instruction to generate a trial divisor, which you can
then multiply by the divisor and subtract from the working remainder of
the numerator.
For the second method, you usually generate one full word (divisor
width) of quotient on a single pass, subject to correction if the trial
divisor was wrong. There is a setup operation, though, to normalize the
numerator and divisor to get the highest number of bits per iteration.
I suspect that the optimum switching point between shift/subtract loop
and word-wide trial divisor is around 20 - 100 bits of quotient, as far
as speed optimization goes. The trial divisor method requires a lot
more code and attention to details, though.
The Borland Turbo C library which supported the 16-bit 808x family used
a shift and subtract loop for 32/32 integer divides, even though a 32/16
bit divide was native.
Thad
- Next message: Howard Kaikow: "Re: MS Word / Excel File Viewer API"
- Previous message: Michael Wojcik: "Re: Is it possible to count time in micosecond under Linux?"
- In reply to: Gerry Quinn: "Re: division algorithm"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|