[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: [avr-gcc-list] -fshort-enums are not so short
From: |
Ben Mann |
Subject: |
RE: [avr-gcc-list] -fshort-enums are not so short |
Date: |
Wed, 24 Nov 2004 20:22:29 +0800 |
I "suppose" I assume that it is related to the size of int which is 16 bits
in AVR, however I never thought twice about it - I always assumed it implied
"short" was 16-bits, as in the "short" data type.. Theoretically it could be
changeable using -mint8, but this option has the intriguing comment in the
avr-libc manual:
"Note that this is not really supported by avr-libc, so it should normally
not be used."
I had a similar problem with something like this:
unsigned char a = something;
switch(a) {
case 1:
foo();
break;
case 2:
bar();
break;
}
Which sets a to (say) r24/r25 with r25=0, then does a 2-byte comparison on
each value.
For my speed-critical state machine I was a little miffed.
I ended up "happy" with
unsigned char a = something;
if (0)
else if (a == 1) goto CASE_1;
else if (a == 2) goto CASE_2;
and you can imagine the rest - the resulting jump table is much smaller when
you have to iterate a large number of cases.
The solution seemed quick and easy (though dirty) and once again I'd be keen
to see the outcome of your query.
Ben Mann
-----Original Message-----
From: address@hidden [mailto:address@hidden
On Behalf Of Bernard Fouché
Sent: Wednesday, 24 November 2004 7:58 PM
To: address@hidden
Subject: [avr-gcc-list] -fshort-enums are not so short
Hi.
Compiling with gcc 3.4.2 with -Os -fshort-enums, it appears that enums are
processed on 16 bits and not 8 when they could be on 8 bits. (when looking
at 'man gcc' it is said that this option will make the compiler use the
smallest possible type of int to process the enum, I assume that for the avr
it is 8 bits)
C code:
enum Foo { FOO_1, FOO_2 } Foo;
uint8_t v;
main()
{
switch(Foo){
case FOO_1:
v=1;
break;
case FOO_2:
v=2;
break;
}
}
compilation result:
000000ca <main>:
enum Foo { FOO_1, FOO_2 } Foo;
uint8_t v;
main()
{
ca: cf ef ldi r28, 0xFF ; 255
cc: d0 e1 ldi r29, 0x10 ; 16
ce: de bf out 0x3e, r29 ; 62
d0: cd bf out 0x3d, r28 ; 61
switch(Foo){
d2: 80 91 02 01 lds r24, 0x0102
d6: 99 27 eor r25, r25
d8: 00 97 sbiw r24, 0x00 ; 0
da: 19 f0 breq .+6 ; 0xe2
dc: 01 97 sbiw r24, 0x01 ; 1
de: 19 f0 breq .+6 ; 0xe6
e0: 05 c0 rjmp .+10 ; 0xec
case FOO_1:
v=1;
e2: 81 e0 ldi r24, 0x01 ; 1
e4: 01 c0 rjmp .+2 ; 0xe8
break;
case FOO_2:
v=2;
e6: 82 e0 ldi r24, 0x02 ; 2
e8: 80 93 01 01 sts 0x0101, r24
break;
}
}
...
_______________________________________________
avr-gcc-list mailing list
address@hidden http://www.avr1.org/mailman/listinfo/avr-gcc-list