Perhaps I have been selling myself short ! Whist I thank you for your
suggestions at the risk of sounding sarcastic which is not my intent, there
is nothing there that I couldn't get from my 1967 university textbook.. Or
from experienced engineers in my first job at International Computers Ltd
in 1969.
The basic non restoring algorithm must have been developed by Turing or
Wilkes or one of there contemporaries back in the fifties. For interest,
or not, This my take on it. - 55 memories, 2 working registers, worst case
156 instructions. Please criticise to your hearts content
.;=============================================================================;
; This Routine divides DIVIDEND BY DIVISOR for numbers less than 1. That is
;
; numbers with an implied binary point aat the extreme left. The divisor
;
; must be greater than the dividend so thaat the quotient remains less than
1 ;
; ENTRY: Dividend in REGA. Divisor in REGB
;
; EXIT: Quotient in REGAX,REGA W is 0
;
; (IF Dividend > Divisor W is -1 and REGAX,REGA = REGA -REGB)
;
; USES: REGQ, DCNT
;
;=============================================================================;
DIV88 MOVLF 8,DCNT
CLRF REGQ
CLRF REGAX
SUB21 REGAX,REGA,REGB ; REGA = REGA - REGB
BTFSC _C ; initial subtraction should be negative
RETLW -1 ; if Divisor is greater than dividend
D880 RLF REGA ; REGA = 2* REGA + carry from previous operation
RLF REGAX ; so that carry is in A0
BTFSC REGA,0 ; skip if negative
GOTO D881
RLF REGQ ; Negative result
BCF REGQ,0 ; zero shifted into quotient
BCF REGA,0 ; clear saved carry from dividend
ADD21 REGAX,REGA,REGB ; REGA = REGAA + REGB
GOTO D882
D881 RLF REGQ ; Positive result
BSF REGQ,0 ; one shifted in
BCF REGA,0 ; clear saved carry
SUB21 REGAX,REGA,REGB ; REGA = REGAA - REGB
D882 DECFSZ DCNT
GOTO D880
RLF REGQ,W
MOVWF REGAX
BTFSC REGAX,0
RETLW 0
ADD11 REGA,REGB
RETLW 0
__________________________________________
David C Brown
43 Bings Road
Whaley Bridge
High Peak Phone: 01663 733236
Derbyshire eMail: ***@gmail.com
SK23 7ND web: www.bings-knowle.co.uk/dcb
<http://www.jb.man.ac.uk/~dcb>
*Sent from my etch-a-sketch*
Post by Walter BanksPost by smplxPost by Walter BanksI depends how much you know about the numbers. This works as long as
the MSbit of x is 0.
No magic in this and not particularly fast.
w..
//
// divide x/y and return a fraction
// x < y always
//
// reference in 16 bits
//
uint16_t div_fract (uint16_t x,y)
{
char count = 16 ; // size of result in bits
do
{
x <<= 1;
if (x >= y)
{
x -= y;
x |= 1;
}
}
while(--count);
return x;
}
Hi Walter, I don't understand how the above is supposed to work.
Surely the "x |= 1" is wrong? Don't you need a seperate variable build
the result in?
Quite correct. Optimization is throw all the code you don't need and no
more.
moral. don't post untested code.
//
// divide x/y and return an _Fract
// x < y always
//
// reference in 16 bits
//
uint16_t div_fract (uint16_t x,y)
{
char count = 16 ; // size of result in bits
uint16_t result = 0;
do
{
x <<= 1;
result <<= 1;
if (x >= y)
{
x -= y;
result |= 1;
}
}
while(--count);
return result;
}
Post by smplxOn a mid range PIC the "(x >= y)" would need to be performed as
"(x - y >= 0)" so why not compute "x = x - y" and compare with 0?
That is essentially my compiler does.
Post by smplxRegards
Sergio Masci
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist