avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Initilizing complex const arrays : syntax ?


From: Dave Hansen
Subject: Re: [avr-gcc-list] Initilizing complex const arrays : syntax ?
Date: Mon, 19 Sep 2005 10:19:41 -0400

From: David Kelly <address@hidden>
[...]

    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






reply via email to

[Prev in Thread] Current Thread [Next in Thread]