> You may want to read this thread 
> (http://www.microchip.com/forums/fb.ashx?m=542528) about dividing by a 
> constant for PIC18:
> in only 4
> instructions. It may be used for multiple right shift too
> (divide by a
> power of 2).
> Assembler is often the better way to tune libraries. You
> can't beat it
> on this subject, in any language.
> By using conditional compilation you may deal with the
> different
> processors and use different code optimizations.

Of course. But, as I've seen, people don't like assembler here too much. And 
the libs are really very readable.

And I had thought of using multiplications instead of divisions, but that does 
not make much sense without a hardware multiplier.

You could divide a byte by 10 like this:

divided=((value<<4)+(value<<3)+(value<<1)-(value>>1)+25)>>8

but I'm not sure if this is much more efficient than using the subtraction 
method. Also, four different shift operations plus the 16bit additions may 
almost use the ressources of a software multiplication.

A byte-optimized assembler implementation of the subtraction method could look 
like this:

var byte dividend, result

assembler

  local div_loop_50, div_loop_10

  clrf result
  movlw 50
div_loop_50:
  incf result,f
  subwf dividend,f
  btfsc STATUS_C
  goto div_loop_50
  addwf dividend,f
  bcf STATUS_C
  rlf result,w
  addwf result,f
  addwf result,f
  movlw 10
div_loop_10:
  incf result,f
  subwf dividend,f
  btfsc STATUS_C
  goto div_loop_10
  movlw 6
  subwf result,f
end assembler

Should run in an average of 35 instruction cycles and uses 18 words of flash, 
20 on PIC18. As the shift version needs 16bit values, I'm sure it won't be far 
cheaper, it will definitely need temporary ram. If you need the modulo, add 
another "addwf dividend,f" after the div_10_loop.

The print library served it's purpose for years now, even if it used tens of 
thousands of instruction cycles. I would prefer to make it use less ram, less 
flash and, say, less than 1000 cycles. I'm not so eager to invest weeks of 
optimization to get the last nanosecond out of it.

Greets,
Kiste


-- 
You received this message because you are subscribed to the Google Groups 
"jallib" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/jallib?hl=en.

Reply via email to