[En-Nut-Discussion] Byte select macros

Bernd Walter enut at cicely.de
Mon Nov 22 17:51:53 CET 2010


On Mon, Nov 22, 2010 at 01:25:08PM +0100, Uwe Bonnes wrote:
> 
> 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.

This is quite normal coding style.

> 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.

This sounds like a compiler optimization bug to me, or you didn't enable
optimization at all.
Normaly a -O2 or -Os should catch those simple things.
I would be pretty surprised if gcc won't catch this typical case, so my
first assumption is a usage error and you should verify your compiler
args.

> 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]);

Sounds pretty useless to me, since it is something the compiler
is expected to optimize.

> 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.

Because noone missed them so far.

> 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 ----------
> _______________________________________________
> http://lists.egnite.de/mailman/listinfo/en-nut-discussion

-- 
B.Walter <bernd at bwct.de> http://www.bwct.de
Modbus/TCP Ethernet I/O Baugruppen, ARM basierte FreeBSD Rechner uvm.



More information about the En-Nut-Discussion mailing list