[En-Nut-Discussion] Byte select macros

Uwe Bonnes bon at elektron.ikp.physik.tu-darmstadt.de
Mon Nov 22 13:25:08 CET 2010


Hello,

my AVR program has lots of code like
                  canFrame.byte[0] = owi_temps[0] & 0xff;
                  canFrame.byte[1] = owi_temps[0] >>   8;

where uint16_t or greater is assembled into an can packet of bytes.

Above code is translated into 
 3661 1bfa 8091 0000            lds r24,owi_temps
 3662 1bfe 9091 0000            lds r25,(owi_temps)+1
 3663 1c02 8B8F                 std Y+27,r24
 3664 1c04 8091 0000            lds r24,owi_temps
 3665 1c08 9091 0000            lds r25,(owi_temps)+1
 3666 1c0c 9C8F                 std Y+28,r25

were one load command is done useless, as the loaded register value is never
used, at least with (avr-)gcc version 4.3.3 (GCC).

While 
{
        uint8_t * p = owi_temps;
        canFrame.byte[0] = p[0];
        canFrame.byte[1] = p[1];
...
does the job as expected, this is machine dependant and the code gets less
readable.

Some macros like
#define b0(x) (*((uint8_t*)((&x))+0))
#define b1(x) (*((uint8_t*)((&x))+1))
#define b2(x) (*((uint8_t*)((&x))+2))
#define b3(x) (*((uint8_t*)((&x))+3))

                  canFrame.byte[0] = b0(owi_temps[0]);
                  canFrame.byte[1] = b1(owi_temps[0]);

would come handy and if the macros are machine depentant, theywould do the
right job with the ethernut code ported to a different machine typ.

I don't see macros like these, either because I look at the wrong place or
because they are none.

What about adding macros like these to the Ethernut package?

Thanks        

-- 
Uwe Bonnes                bon at elektron.ikp.physik.tu-darmstadt.de

Institut fuer Kernphysik  Schlossgartenstrasse 9  64289 Darmstadt
--------- Tel. 06151 162516 -------- Fax. 06151 164321 ----------



More information about the En-Nut-Discussion mailing list