Hi Scott, Thanks for your answer. However, I'm not sure you understand what I want : I pretty much want a routine for the PIC which is equivalent to a regular DIV instruction on a simple 16 bit processor. It should take a 16bit dividend and an 8bit divisor, and return an 8 bit quotient and an 8bit remainder. Perhaps I am not understanding something, but your routine,although fascinating and I will surely put it in my code archive, is not all that similar since it only handles 8bit arguments. I am a bit hesitant to post my code because I am doing this as an exerci se in optimization and I really don't want people to "give me answers" yet :) I just want some well optimized code examples to compare my code's performance and size. Currently, my code is 40 words long and takes an average of about 200 cycles (a max of about 250 and a min of about 70) to do the divide. Microchip's code from their app notes is a bit better on speed (190 something cycles max, ~ 170 min) but is two words longer (42 words). I also wrote a 32 bit by 16 bit routine which returns 16bit quotient and remainder,based on exactly the same method. It is 64 lines long and takes about 600 cycles average. I don't recall what the length of Mchip's equivalent code is,but their code is a bit faster (about 500 cycles max). My hope is to improve my programming skill by at least matching Mchip's performance,maybe even exceede it (it seems that I may have already done slightly better on code length on the 16/8 routine). Any comments on what is close to "the best" time possible for a divide o f 16/8 or 32/16 on a PIC would be greatly appreciated. Thanks again, Sean At 09:14 AM 4/18/99 -0700, you wrote: >The fastest 8 by 8 divide is Andy Warren's 81-cycle screamer. Bob F. >(where are you Bob) has a ton of different arithmetic routines - I'm >almost certain he has this one. > >While looking at the arctan(y/x) problem, I wrote a division routine that >does almost what you want (maybe I should've posted to the other >thread...). In particular, I was interested in the fractional remainder of >y divided by x. For example, I wanted to find the 'a' that satisfies: > > a y > ----- = --- > 256 x > >Here's an untested routine that I'll prematurely post with the hopes that >it will be useful and optimized by Reggie or Dmitry: > >FRAC_DIV: >;------------------- >;Fractional division >; >; Given x,y this routine finds: >; a = 256 * y / x >; > > movlw 8 ;number of bits in the result > movwf cnt > clrf a ; the result > movf x,w > >L1: > > clrc > rlf y,f ;if msb of y is set we know x rlf a,f ;and that the lsb of 'a' should be set > subwf y,f ;But we still need to subtract the > ;divisor from the dividend just in > ;case y is less than 256. > skpnc ;If y>x, but y<256 > bsf a,0 ; we still need to set a:0 > > btfss a,0 ;If y addwf y,f ;done the subtraction > > decfsz cnt,f > goto L1 > > return > >Again, THIS IS UNTESTED. > >Scott > | | Sean Breheny | Amateur Radio Callsign: KA3YXM | Electrical Engineering Student \--------------=---------------- Save lives, please look at http://www.all.org Personal page: http://www.people.cornell.edu/pages/shb7 mailto:shb7@cornell.edu ICQ #: 3329174