__attribute__ ((mode(__byte__)))

  • Thread starter JeromeArbezGindre
  • Start date
J

JeromeArbezGindre

Hi,

I would like to map an enum on a byte,so here is my code:


enum my_enum {
A = 1,
B = 2,
} __attribute__ ((mode(__byte__))) ;

int is_A (enum my_enum my_val)
{
return (my_val == A) ;
}

When compiled, I have the following:

cc -c -o toto.o toto.c
toto.c: In function 'is_A':
toto.c:10: warning: comparison is always false due to limited range of
data type

I do not have any problem when i transfrom is_A :
int is_A (unsigned char my_val)
{
return (my_val == A) ;
}

So, muy question is :

Is there a correct way to declare an enum fo map it on a byte.

Thanks

Jérôme
 
M

Mark Bluemel

Hi,

I would like to map an enum on a byte,so here is my code:

I don't know quite what you mean by this, or why you feel it's necessary
enum my_enum {
A = 1,
B = 2,
} __attribute__ ((mode(__byte__))) ;


This, from the rapid websearch I've done, appears to be specific to GCC.
You'd do better posting to a GCC forum like gnu.gcc.help, I suspect.
 
E

Eric Sosman

Hi,

I would like to map an enum on a byte,so here is my code:


enum my_enum {
A = 1,
B = 2,
}

This much is C, ...
__attribute__ ((mode(__byte__))) ;

.... but this is some compiler-specific (gcc?) extension to C.
You'll need to check the compiler's documentation or find a
forum where that compiler is discussed to learn exactly what
this beyond-C stuff does and how to use it wisely.
[...]
Is there a correct way to declare an enum fo map it on a byte.

The only thing C guarantees is that the type "behind" the
enum will be some kind of an integer, whose range of values
includes all the named enum constants (A and B, in your code).
The compiler is free to choose the type, and is not required
to explain its choices. You can influence the compiler's choice
only in an indirect and limited way: For example, using a named
constant with a negative value prevents the compiler from choosing
an unsigned type. Other than that, though, there is no portable
way to control what the compiler uses as an underlying type.
 
J

JeromeArbezGindre

I don't know quite what you mean by this, or why you feel it's necessary

This enum have to be mapped on a hardware register.
This, from the rapid websearch I've done, appears to be specific to GCC.
You'd do better posting to a GCC forum like gnu.gcc.help, I suspect.

Sorry for the noise.

Thanks.
 
K

Keith Thompson

I would like to map an enum on a byte,so here is my code:


enum my_enum {
A = 1,
B = 2,
} __attribute__ ((mode(__byte__))) ;

int is_A (enum my_enum my_val)
{
return (my_val == A) ;
}

When compiled, I have the following:

cc -c -o toto.o toto.c
toto.c: In function 'is_A':
toto.c:10: warning: comparison is always false due to limited range of
data type

The code depends on a gcc extension. Consider whether you're using
that extension properly. I won't comment further here; try the gcc
documentation and/or gnu.gcc.help. (If you post to gnu.gcc.help, I
might respond in more detail there.)

[...]
Is there a correct way to declare an enum fo map it on a byte.

There's no portable way to do so.

But enums aren't really first-class types in C anyway. (We could
argue for days about what "first-class type" means. Let's not.)
There's a slightly ugly workaround that's really no uglier than using
enums directly.

If you want a type that's exactly one byte, use ``unsigned char''.
It's guaranteed to be exactly one byte with no padding bits (though a
byte isn't guaranteed to be only 8 bits).

A declaration
enum my_enum { A = 1, B = 2 };
declares a type "enum my_enum", but the constants A and B are not of
that type; they're actually of type int. This doesn't (usually) cause
any problems because integer and enumerated types can be freely
assigned to each other, with implicit conversion. <OT>I think C++
does this differently.</OT>

An "enum" declaration can be used to declare a series of constants
without declaring a named type. This is a fairly common trick used to
create named constants without using the preprocessor. The values are
limited to the range of type int, but that's not a problem in your
case.

Putting all this together, you can declare the type and the constants
to be used with that type separately:

typedef unsigned char byte;
enum { A = 1, B = 2 };

int is_A(byte my_val)
{
return my_val == A;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top