On Mon, 2 Dec 2002, Jinx wrote: > Is there a quick way to divide a 16-bit number by 257 or does > it just look as though there should be ? > > The Code Generator > > http://www.piclist.com/techref/piclist/codegen/constdivmul.htm > > seems to be unable to do this at 16 bits. I hoped it would produce > something that could be further optimised. All that's needed is > INT(16-bit / 257), the remainder can be found by * and - after the > division. I've tried a few simple shift/subtract ideas, and full 16-bit > division is still an option if necessary - just wondering if there's a > simple logical solution Recall: 1/(A + B) = 1/A * (1 - B/A + (B/A)^2 -+ ...) !/257 is especially simple: let A = 256, and B = 1: 1/(256+1) = 1/256 * ( 1 - 1/256 + 1/65536 -+ ...) If your 16-bit number is 'x': x / 257 ~ x/256 - x/65536 I guess it depends on how much accuracy you need, but you may get away with: divide_by_257: movf x_hi,w ; x / 257 ~= x /256 subwf x_lo,w skpc ;if the high byte is greater than the low byte addlw -1 ;subtract 1 from the approximation addwf x_lo,w return Scott -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body