On Fri, 6 Apr 2001, [Iso-8859-1] K=FCbek Tony wrote: > Hi, >=20 > Scott Dattalo wrote: Actually Bob. And it's a damned clever idea. > >Tony, > >I hate to do this to you, but here is simpler(?) way to round to nearest= 5: >=20 > Thankfully accepted :) >=20 > >Given 25 bit number HI:MID:LO > >Its value is > >LO + 256*MID + 65536*HI > >Which is equal to > >LO+255*MID+MID+65535*HI+HI > >But 255*MID and 65535*HI are both multiples of 5, so: > >LO+MID+HI =3D=3D HI:MID:LO (mod 5) > >This can be at most a 10 bit number, let us call it H2:L2 > >now we can: > > movf L2,W,A > > andlw 0x0F > > movwf ONES,A > > > > swapf L2,W,A > > andlw 0x0F > > btfsc L2,4 > > addlw 5 ; worth 6, not 1 > > btfsc H2,0 > > addlw 6 > > btfsc H2,1 > > addlw 2 >=20 > I'll be darned, I certanily hope there are no such shortcuts for > the 10's multiple, that would make my latest 3 hours a total waste :). actually the 10's aren't much harder: lo + 256*mid + 65536*hi =3D lo +250*mid + 65530*hi + 6*(mid + hi) clrf h2 movf mid, w addwf hi,w rlfc h2,f movwf l2 ;mid+hi addwf l2,f ;2*mid+2*hi skpnc incf h2,f addwf l2,f ;3*mid+3*hi skpnc incf h2,f clrc =20 rlf temp,w ;6*mid+6*hi skpnc incf h2,f addwf lo,w skpnc incf h2,f at this point, h2:l2 is congruent to the original 24-bit number modulo 10 a= nd is no more than 14-bits. But this is off the top of my head and not necessaril= y optimized... >=20 > Brilliant ! Yeah! Let's by Bob a beer. Scott -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu