Re: Checking endianess in compile time



"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


.



Relevant Pages

  • Re: convert string of hex characters to char
    ... has type 'unsigned int' ... could produce this error message. ... It silences the compiler and the output is the same but I wanted to ...
    (comp.lang.c)
  • Checking endianess in compile time
    ... automatically check if the bitfields are regarded properly by the ... unsigned int tffca: 1; ... Is there a way to check if the compiler considers bitfield ten to be ... I would like to have the compiler give an error message ( ...
    (comp.lang.c)
  • STL std::inplace_merge and Visual 2008 performance
    ... Is there an obvious error on the compiler flags used? ... unsigned int ToRemoveNbr = 100; ... QueryPerformanceCounter(&timeBefore); ...
    (microsoft.public.vc.stl)
  • Re: register keyword
    ... "register" keyword is only a hint to the compiler. ... unsigned int Array_Sum(unsigned int * p_array, ... reading pattern and use specialized processor instructions. ...
    (comp.lang.c)
  • Re: Any good refs on dealing with tightly packed data in C#?
    ... :-) To access those bit fields, the compiler needs to generate tricky code for masking and shifting the values every time they're accessed. ... unsigned int d: 14; ... There's room for optimization, but this is the general idea: mask and shift to get the field, perform the operation, mask and shift again, logical-or with existing data. ...
    (microsoft.public.dotnet.languages.csharp)