Searching \ for '[PIC]: clever rounding out there ?' in subject line. ()
Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devices.htm?key=pic
Search entire site for: 'clever rounding out there ?'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: clever rounding out there ?'
2001\04\05@050808 by

Kübek Tony wrote:

> 3: My current 'idea', try to 'extract' the ones and tens from the
>    input ( 24 bit ) and somehow use it to round with. Sofar
>    i've managed to extract it but don't know really if this is the way
>    to go. I'm using about 200-300 cycles for the extraction
>    and have the tens in one byte and the ones in another byte.

Tony, i've been thinking about this rounding off
thing and I hope i'm not annoying you with suggestions
but the topic is intriguing to me. :o)

I think the 10s and 1s solution would work, how about:

(example, round 24bit binary number to nearest decimal 50)

1. add 25 to the 24bit number.
2. extract the 10s and 1s digits.
3. if 10s is greater than 5 subtract 5 from it.
4. multiply 10s by 10 (simple lookup table, 5 bytes)
5. add 1s to 10s (this is now the 50 modulus)
6. subtract the modulus from the 24bit number

All of those are very quick to perform, apart
from step 2; the extracting of the 10s and 1s.
You said you have done this but 300 cycles on
an 18 series PIC seems a lot.

Have you looked for an algorithm that extracts
BCD digits from a binary number? If you could find
a good refined system for this and just use the
section that extracts the last two digits??
:o)
-Roman

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

Tony,

How much code space can you spare for this function?

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

{Original Message removed}
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 listservmitvma.mit.edu with SET PICList DIGEST in the body

Bob Ammerman wrote:

{Quote hidden}

Excellent work Bob! So, it would take about:

1. add 25 to the 24bit number. (6)
2. extract the 10s and 1s digits. (68)
3. if 10s is greater than 5 subtract 5 from it. (7)
4. multiply 10s by 10 (simple lookup table, 5 bytes) (5)
5. add 1s to 10s (this is now the 50 modulus) (2)
6. subtract the modulus from the 24bit number (6)

That's only about 94 instructions, give or take
a few. :o)

So that would be on a 18C series PIC, how much
larger would it become if using only a 16 series
PIC I wonder??
-Roman

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

More... (looser matching)
- Last day of these posts
- In 2001 , 2002 only
- Today
- New search...