On Sat, Nov 28, 2009 at 11:43 PM, Shaun Jackman <[email protected]> wrote:
> When assigning a bool to a single bit of a bitfield located in the
> bit-addressable region of memory, better code is produced by
> if (flag)
> bitfield.bit = true;
> else
> bitfield.bit = false;
> than
> bitfield.bit = flag;
>
> I've included a short test and the assembler output by both forms.
> Should I file a bug suggesting a possible improvement here?
Yes, a bugreport is useful - but there might be a bug with this issue
already.
Richard.
> Cheers,
> Shaun
>
> #include <stdbool.h>
> #include <stdint.h>
>
> struct byte { uint8_t x0:1; uint8_t x1:1; uint8_t x2:1; uint8_t x3:1;
> uint8_t x4:1; uint8_t x5:1; uint8_t x6:1; uint8_t x7:1; };
>
> volatile struct byte *const porte = (void*)0x23;
>
> void set_flag_good(bool flag)
> {
> if (flag)
> porte->x6 = true;
> else
> porte->x6 = false;
> }
>
> void set_flag_bad(bool flag)
> {
> porte->x6 = flag;
> }
>
>
> 00000000 <set_flag_good>:
> 0: 88 23 and r24, r24
> 2: 01 f4 brne .+0 ; 0x4 <set_flag_good+0x4>
> 2: R_AVR_7_PCREL .text+0x8
> 4: 1e 98 cbi 0x03, 6 ; 3
> 6: 08 95 ret
> 8: 1e 9a sbi 0x03, 6 ; 3
> a: 08 95 ret
>
> 0000000c <set_flag_bad>:
> c: 81 70 andi r24, 0x01 ; 1
> e: 82 95 swap r24
> 10: 88 0f add r24, r24
> 12: 88 0f add r24, r24
> 14: 80 7c andi r24, 0xC0 ; 192
> 16: 93 b1 in r25, 0x03 ; 3
> 18: 9f 7b andi r25, 0xBF ; 191
> 1a: 98 2b or r25, r24
> 1c: 93 b9 out 0x03, r25 ; 3
> 1e: 08 95 ret
>