Re: Checking endianess in compile time
- From: "Stephen Sprunk" <stephen@xxxxxxxxxx>
- Date: Mon, 6 Jun 2005 15:06:07 -0500
"jlara" <jlara@xxxxxxxxxx> wrote in message
news:1118078684.464089.93370@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Having been stung by the same problem twice, I would like to
> automatically check if the bitfields are regarded properly by the
> compiler. For example, if I define the following structure:
>
> typedef struct
> {
> unsigned int : 4;
> unsigned int tffca : 1;
> unsigned int tsfrz : 1;
> unsigned int tswai : 1;
> unsigned int ten : 1;
> }tTSCR1;
>
> Is there a way to check if the compiler considers bitfield ten to be
> the most significant bit?
>
> If there is, I would like to have the compiler give an error message (
> #error ) and abort.
Since you care about the exact representation of the bitfield, which is
difficult if not impossible to discover during preprocessing, might I
suggest simply using a char and some bitmasks (hidden behind macros) to
do the same thing?
/* warning, untested code */
#define SET_TFFCA(r) do { (*r) |= 0x10; } while(0)
#define CLR_TFFCA(r) do { (*r) &= 0xef; } while (0)
#define SET_TSFRZ(r) do { (*r) |= 0x20; } while (0)
#define CLR_TSFRZ(r) do { (*r) &= 0xdf; } while (0)
#define SET_TSWAI(r) do { (*r) |= 0x40; } while(0)
#define CLR_TSWAI(r) do { (*r) &= 0xbf; } while (0)
#define SET_TEN(r) do { (*r) |= 0x80; } while(0)
#define CLR_TEN(r) do { (*r) &= 0x7f; } while (0)
And, of course, you'll need to check that CHAR_BIT == 8.
Then you can do:
char *hwreg = (location);
SET_TFFCA(hwreg);
CLR_TEN(hwreg);
S
--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov
.
- Follow-Ups:
- Re: Checking endianess in compile time
- From: Lawrence Kirby
- Re: Checking endianess in compile time
- References:
- Checking endianess in compile time
- From: jlara
- Checking endianess in compile time
- Prev by Date: Re: Computation slow with float than double.
- Next by Date: Re: Computation slow with float than double.
- Previous by thread: Re: Checking endianess in compile time
- Next by thread: Re: Checking endianess in compile time
- Index(es):
Relevant Pages
|