#12444 - poslundc - Fri Nov 14, 2003 2:05 am
I'm going to start writing my sprintf function pretty soon... but rather than reinvent the wheel entirely, I thought I'd see if anyone had any tips for writing this, or even perhaps knew the location of an original source I could look at.
My biggest concern, unsurprisingly, is base-conversion for printing decimal numbers. Don't know if anyone has any suggestions for doing this, hopefully without having to divide. :P
Dan.
#12445 - torne - Fri Nov 14, 2003 2:20 am
1) You could just use the one in newlib
2) You could look at the source for newlib (or libc, or dietlibc) to see one.
#12448 - poslundc - Fri Nov 14, 2003 5:57 am
Thanks, that's just what I was looking for.
I think I will write my own routine, since I only need to support two format specifiers for my game (%d and %s), and I can write a more efficient routine in Thumb assembler. (Among other things, it will use SWI for division and modulus in the base-conversion rather than the gcc operators used by newlib!)
Dan.
#12456 - col - Fri Nov 14, 2003 4:00 pm
poslundc wrote: |
Thanks, that's just what I was looking for.
I think I will write my own routine, since I only need to support two format specifiers for my game (%d and %s), and I can write a more efficient routine in Thumb assembler. (Among other things, it will use SWI for division and modulus in the base-conversion rather than the gcc operators used by newlib!)
Dan. |
I think this a case where you should NOT use SWI :)
base conversion uses divisions by small constants. These can be turned into a handfull of shift and subtract instructions - so divide by 10 is just a few cycles !
If you don't know how, you could writeyour code in c, let gcc do the divide by const optimizations, and grab the compiler generated asm(assuming that you have optimisations switched on)
Also, you shouldn't need a modulus at all !
heres some code that can generate instructions for specific constants:
search on google for
"_OPTIMIZING INTEGER DIVISION BY A CONSTANT DIVISOR_
by Robert Grappel"
and look at the cached page i you don't have a ddj acount
here is some good info on binary to decimal conversion
http://www.cs.uiowa.edu/~jones/bcd/decimal.html
cheers
Col