Re: Help: How to determine big/little endian?

daniels@cse.ogi.edu (Scott David Daniels)
Wed, 13 Sep 1995 20:51:34 GMT

          From comp.compilers

Related articles
[9 earlier articles]
Re: Help: How to determine big/little endian? cwf@research.att.com (Chris Fraser) (1995-08-25)
Re: Help: How to determine big/little endian? nr@cs.purdue.edu (1995-08-25)
Re: Help: How to determine big/little endian? doug@netcom.com (1995-08-26)
Re: Help: How to determine big/little endian? meissner@cygnus.com (Michael Meissner) (1995-09-01)
Re: Help: How to determine big/little endian? mab@wdl.loral.com (1995-09-01)
Re: Help: How to determine big/little endian? erik@kroete2.freinet.de (1995-09-03)
Re: Help: How to determine big/little endian? daniels@cse.ogi.edu (1995-09-13)
Re: Re: Help: How to determine big/little endian? robison@kai.com (Arch Robison) (1995-09-21)
Re: Help: How to determine big/little endian? wdavis@dw3f.ess.harris.com (1995-09-26)
| List of all articles for this month |

Newsgroups: comp.compilers
From: daniels@cse.ogi.edu (Scott David Daniels)
Keywords: C, architecture, comment
Organization: Oregon Graduate Institute (formerly OGC), Beaverton, OR
References: 95-08-120 95-09-008
Date: Wed, 13 Sep 1995 20:51:34 GMT

In describing his compiler (lcc)'s code to determine the host Endian-ness,
Chris Fraser (cwf@research.att.com) says that he uses:
...
union { char c; int i; } u;
u.i = 0; u.c = 0;
swap = (u.i == 1) != IR->little_endian;
...
I suspect this code is improved by using:
...
volatile union { char c; int i; } u;
u.i = 0; u.c = 0;
swap = (u.i == 1) != IR->little_endian;
...


So that the compiler may not decide to ``optimize'' this code into
...
union { char c; int i; } u;
u.i = 0; u.c = 0;
swap = (0 == 1) != IR->little_endian;
...
The fetch of an int from the union after storing a char is strictly
illegal. Since it is illegal, the compiler may assume the fetch must be
tied to a corresponding store, and the "u.i = 0;" strictly dominate the
fetch. So, my ``optimization'' should be a legal compiler optimization.
By making u volatile, each fetch and store must be accomplished by the
related sequence points, guaranteeing a strict order of
write u.i, write u.c, read u.i
Which is what we want.


-Scott David Daniels
daniels@cse.ogi.edu
[It occurs to me that changing the first 0 to 1 will also help a lot. -John]
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.