Hi Jakub, On Tue, 14 Jun 2011, Jakub Jelinek wrote:
You'd use it as: void puthexl (unsigned long value, FILE *f) { char buf[2 + CHAR_BIT * sizeof (value) / 4]; if (value == 0) putc ('0', f); else { char *p = buf + sizeof (buf); do *--p = "0123456789abcdef"[value % 16]; while ((value /= 16) != 0); *--p = 'x'; *--p = '0'; fwrite (p, 1, buf + sizeof (buf) - p, f); } }If the number is small, which is the common case, this will iterate just small number of items instead of always 16 times.
Thanks for the explanation, I measured your version and is indeed faster for the common case, so I'll be using it.
Anyway, generally, I wonder if replacing lots of fprintf calls won't lead to less readable and maintainable code, if many of the fprintfs will need to be replaced e.g. by two separate calls (one fwrite, one puthexl or similar). Plus, what I said on IRC, regarding transformation of fprintf calls to fwrite if there are no %s in the format string, we should leave that to the host compiler. It actually already does such transformations for fprintf, but in this case we have fprintf_unlocked due to system.h macros, and that isn't optimized by gcc into fwrite_unlocked. That IMHO should be fixed on the host gcc side though.
You're probably right, it's just that for starters I'm looking into the easy stuff to optimise, the low hanging fruit. I think it's gonna be much harder to implement these optimisations into some optimising pass of the compiler itself, and for now I don't even know where to look at for this, I'll probably check it later.
Thanks, Dimitris
