From: David Kelly <[EMAIL PROTECTED]>
[...]

    char buffer[4];
    uint8_t i;
    strncpy_P( buffer, PGM_P("000"), 3 );
    i = 2;
    while(x) {
        buffer[i--] = x%10 + '0';
        x = x/10;
    }
    putchar(buffer[0]);
    putchar('.');
    putchar(buffer[1]);
    putchar(buffer[2]);

In the above I find myself wishing for the FORTH /mod operator. Avr- gcc calls exactly the same routine for division or modulo and stores the desired result. In assembly we could store both but I don't see how to get at both from C. Is possible the compiler could optimize it but plain -O didn't in recent tests.

Try something like

  #include <stdlib.h>
  ...
  div_t qr;

    while(x) {
        qr = div(x,10);
        buffer[i--] = qr.rem + '0';
        x = qr.quot;
    }

I'm not certain this is optimal. I haven't looked closely, but it looks like it should be. It certainly has the opportunity to be.

Regards,
  -=Dave




_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.org
http://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Reply via email to