piclist 2001\04\05\101721a >
Thread: clever rounding out there ?
www.piclist.com/techref/microchip/devices.htm?key=pic
picon face BY : Bob Ammerman email (remove spam text)



Tony,

I like your idea of figuring out the contribution of each bit to the ones
and 10's digits. Here is a quick way to compute them:


Assume: FSR0 points to the LSByte of your value:

   movlw  0
   btfsc    INDF0,0
   addlw    1
   btfsc    INDF0,1
   addlw    2
   btfsc    INDF0,2
   addlw    4
   btfsc    INDF0,3
   addlw    8
   btfsc    INDF0,4
   addlw    6
   btfsc    INDF0,5
   addlw    2
   btfsc    INDF0,6
   addlw    4
   btfsc    POSTINC0,7
   addlw    8

etc.

This will take 48 cycles/instructions to compute the 1's part.

The same pattern can be repeated to compute the 10's. Note that the four
LSBits of the first byte have no affect on the 10's so you can do this in
40cycles/instructions. Of course you'd start with the MSBits and work to the
lsbits since the code above leaves FSR0 pointing to the MSBits.

That's only 88 cycles/instructions to extract the ones and tens.

Wait a minute!!!

Another thought in computing the 1's part....
Except for the 1's bit, each bit in a nibble already has the right value,
so:

   movf      INDF0,W             ; Get low nibble
   andlw    0x0F
   movwf   ones
   swapf    INDF0,W    ; Get 2nd nibble
   andlw    0x0F
   btfsc      POSTINC0,0    ; Low bit set?
   addlw    5                        ; Yes: it is worth 6, not 1
   addwf    ones,F

   movf      INDF0,W    ; Get 3rd nibble
   andlw    0x0F
   btfsc      INDF0,0    ; Low bit set?
   addlw    5                        ; Yes, it is worth 6, not 1
   addwf    ones,F
   swapf    INDF0,W    ; Get 4th nibble
   andlw    0x0F
   btfsc      POSTINC0,0    ; Low bit set?
   addlw    5                        ; Yes, it is worth 6, not 1
   addwf    ones,F

   movf      INDF0,W    ; Get 5rd nibble
   andlw    0x0F
   btfsc      INDF0,0    ; Low bit set?
   addlw    5                        ; Yes, it is worth 6, not 1
   addwf    ones,F
   swapf    INDF0,W    ; Get 6th nibble
   andlw    0x0F
   btfsc      INDF0,0    ; Low bit set?
   addlw    5                        ; Yes, it is worth 6, not 1
   addwf    ones,F

That's 28 (for the ones)+40 (for the tens) == 68 instructions/cycles instead
of 88!

Bob Ammerman
RAm Systems
(contract development of high performance, high function, low-level
software)

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listserv.....spamspam_OUTmitvma.mit.edu with SET PICList DIGEST in the body


<000f01c0bdda$b663fc20$fce8c540@CDS> 7bit

See also: www.piclist.com/techref/microchip/devices.htm?key=pic
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) clever rounding out there ?

month overview.

new search...