473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Checking endianess in compile time

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.

Thanks very much in advance.

Nov 14 '05 #1
7 2083
>Having been stung by the same problem twice, I would like to
automatically check if the bitfields are regarded properly by the
compiler.
Bitfields *ARE* regarded properly by the compiler.
You may need to have your expecter fixed, though.
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?
Maybe (but not at compile time). Is this portable? Make a union
of an int and your bitfields. Assign to the bitfields, then look
at the int. Ordinarily that's not portable, but since bitfields
are supposed to be placed in (one or more) ints, does that work
under the "common first member" rule?
If there is, I would like to have the compiler give an error message (
#error ) and abort.


You cannot access memory at the preprocessor level. Therefore, you
can't check endianness unless you've got some header file which
tells you the endianness (and might be lying).

Gordon L. Burditt
Nov 14 '05 #2
"jlara" <jl***@wowway.com> writes:
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.


I don't believe there's any way to check endianness at compilation
time. You can check it at run time. For example, declare a union of
the above struct and an unsigned char, check that sizeof(tTSCR1)
yields 1 and that CHAR_BIT==8, and for each named bit field, set the
unsigned char member to 0, set the bit field to 1, and check that the
unsigned char takes on the expected value. If any of these tests
fails, abort the program.

(Incidentally, "endianness" usually refers to the ordering of bytes
within a word; you're looking at bit order within a byte.)

If you don't want to perform this test every time the program runs,
you can incorporate it into your build procedure. Before building
your application, build and run a test program that performs the
appropriate tests; abort the build if the test program fails.
(Methods for doing this are off-topic; depending on your system, the
words "configure" and "Makefile" may provide a clue.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3
"jlara" <jl***@wowway.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
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
Nov 14 '05 #4
jlara wrote:

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.


The DS9000 system will site the 'ten' field anywhere from first to
last, dependant on the day of the week modulo some ramdom number.
In other words, if it matters, use another method, such as explicit
masks.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #5
On Mon, 06 Jun 2005 15:06:07 -0500, Stephen Sprunk wrote:
"jlara" <jl***@wowway.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
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:
I'm not sure what you mean by "properly". There are various choices a
compiler can legitimately make in allocating bits for bits-fields.
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?
There isn't even a guarantee that this will allocate 1 byte. Many
compilers will allocate a word (e.g. 32 bits) for this.
If there is, I would like to have the compiler give an error message (
#error ) and abort.
Why not just write code that works anyway? Bit-fields are designed to
create small integer objects, they aren't well suited to bit level layouts.
Use bit manipulations on, say, an unsigned char instead.
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)


do { } whole (0) is a useful construct in macros but not needed for single
expressions. It is simpler and clearer to write

#define SET_TFFCA(r) ((*r) |= 0x10)

etc.

Lawrence
Nov 14 '05 #6
1)
what do you all think a variable defined as
char c_var = 'a';
would be read in another environment?

i think it must be 'a' ,right?

2)
thus,
there is no need to talk about the problem of bit-field's endian.

3)
and we only need to know the architecture's endian mode.

Nov 14 '05 #7
Umh, what are you replying to? Even though I can see the
"upwards" message, I am not sure which part(s) your questions
belong.
Please quote a sensible amount of context so everybody knows
what you are referring to.

baumann@pan wrote:
1)
what do you all think a variable defined as
char c_var = 'a';
would be read in another environment?

i think it must be 'a' ,right?
Execution environment?
Translation environment?
Floating point environment?
Host environment?
Hosted/freestanding environment?
Environment in the sense of longjmp()/setjmp()?

The latter is the closest to making sense for me but probably
you are using environment in another sense.

2)
thus,
there is no need to talk about the problem of bit-field's endian.

3)
and we only need to know the architecture's endian mode.


I am still not sure what you are talking about but char and bitfields
have nothing to do with each other unless your implementation supports
char-bitfields as an extension.
Do you wish to replace bitfields by _unsigned_ char as suggested by
other people?
Maybe you really added a new aspect to the discussion but I
completely miss it.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Web Developer | last post by:
Hi, I come across the term "type checking" very often in my readings on C++, and have never heard it in Java. Besides the simplistic answer that it checks the "type", what more does it mean? ...
6
by: hantheman | last post by:
Is this a portable implementation? #if defined(BIG_ENDIAN) && !defined(LITTLE_ENDIAN) #define htons(A) (A) #define htonl(A) (A) #define ntohs(A) (A) #define ntohl(A) (A) #elif...
6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
3
by: Vinodh Kumar P | last post by:
Whenever I read any C++ literature I find the words "C++ is statically type checked".OK.Agreed. Is there any language that supports "Dynamic type checking"? In such a case any type can be assigned...
22
by: Qopit | last post by:
Hi there, I'm pretty new to Python and am trying to figure out how to get "will this code compile?"-like code checking. To me this is a pretty basic language/environment requirement, especially...
8
by: | last post by:
Well! Maybe I wrote this word incorrect but its not included im my e-dictionary! I have a structure with many int, short etc I execute this function: fread(&my_struct, sizeof(struct), 1,...
4
by: Dave Rahardja | last post by:
I have the following program that uses an array of chars to simulate a bit set: --------- // An out-of-bounds exception class BoundsException {}; template <int bits = 1> class Bitset
125
by: jacob navia | last post by:
We hear very often in this discussion group that bounds checking, or safety tests are too expensive to be used in C. Several researchers of UCSD have published an interesting paper about this...
11
by: Bryan Crouse | last post by:
I am looking a way to do error checking on a string at compile time, and if the string isn't the correct length have then have the compiler throw an error. I am working an embedded software that...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.