jgharston wrote:
I was needing to update some decimal printout code. I was initially using a routine that used a tables of powers of ten, but was needing to be able to print up to 32-bit values and didn't like the 40 bytes needed for the tens table. After some searching I found this thread, and the link to John Brooks' "clever V flag" routine.
So, you're looking for small code+data, rather than fast. (And as it happens, you're after converting 4 byte rather than 2 byte values.)
I had a thought, which of course I haven't tested:
- include a constant which is the largest power of ten you need: that's 1e4 or 1e9 for 2 or 4 byte inputs
- hard code a multiply-by-ten routine
- repeat 5 times (or 10 times for a 4 byte input)
- - subtract the big power of ten as many times as you can - the count is a digit of output unless it's a leading zero
- - if it's not zero, multiply up your input by 10
(For a bit of a speedup, you could double and redouble your big power of ten, or do something equivalent, so it takes up to 4 steps rather than 9 to figure out a digit.)
Apologies if this is already mentioned upthread! And apologies if it totally fails to work.