Topic: portable method of determining endianness byte order
Author: Steve Clamage <stephen.clamage@sun.com>
Date: 2000/01/22 Raw View
Gavin Collings wrote:
>
> In article <387BA7E2.304D59C6@sun.com>,
> Steve Clamage <stephen.clamage@sun.com> wrote:
> >
> > This thread originally asked how to determine endianness at compile
> > time with portable code. You can't do it in general, even when
> > the architecture really is big- or little-endian.
>
> I'm sure I'll soon be put to rights, but what is wrong with comparing
> addresses, as in: -
>
> union
> {
> int i;
> char c[4];
> } u;
>
> const int addr_diff = (&u.c[0] - reinterpret_cast<char *>( &u.i ));
> const bool little_endian = ( addr_diff == 0 );
The address of i and the address of c are required to be the same.
(Every member of a union has the same address, by definition.)
The question is how the bits of an object stored in memory are
interpreted. In the example, let's assume that an int occupies 4
8-bit bytes. If we write u.i&255 we will always get the least-
significant 8 bits of i, but we don't know where in memory those
8 bits are located.
--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: ajm@virata.com (Andy Moreton)
Date: 2000/01/13 Raw View
In article <387BA7E2.304D59C6@sun.com>, Steve Clamage <stephen.clamage@sun.com> wrote:
>
>This thread originally asked how to determine endianness at compile
>time with portable code. You can't do it in general, even when
>the architecture really is big- or little-endian.
>
Indeed - especially if you are using a cross compiler....
AndyM
--
Andy Moreton
Virata Ltd http://www.virata.com/
Mount Pleasant House, Huntingdon Road, Cambridge CB3 0BL, UK
Tel: +44 1223 566919 Fax: +44 1223 566915
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Gavin Collings <gcollings@sperry-sun.co.uk>
Date: 2000/01/14 Raw View
In article <387BA7E2.304D59C6@sun.com>,
Steve Clamage <stephen.clamage@sun.com> wrote:
>
> This thread originally asked how to determine endianness at compile
> time with portable code. You can't do it in general, even when
> the architecture really is big- or little-endian.
I'm sure I'll soon be put to rights, but what is wrong with comparing
addresses, as in: -
union
{
int i;
char c[4];
} u;
const int addr_diff = (&u.c[0] - reinterpret_cast<char *>( &u.i ));
const bool little_endian = ( addr_diff == 0 );
I can imagine architectures where the address of an int isn't the
lowest addressed byte that it occupies, but do they exist in practice?
--
Gavin Collings
gcollings@sperry-sun.co.uk
Sent via Deja.com http://www.deja.com/
Before you buy.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "C. M. Heard/VVNET, Inc." <heard@vvnet.com>
Date: 2000/01/15 Raw View
Gavin Collings <gcollings@sperry-sun.co.uk> writes:
>In article <387BA7E2.304D59C6@sun.com>,
> Steve Clamage <stephen.clamage@sun.com> wrote:
>>
>> This thread originally asked how to determine endianness at compile
>> time with portable code. You can't do it in general, even when
>> the architecture really is big- or little-endian.
>
>I'm sure I'll soon be put to rights, but what is wrong with comparing
>addresses, as in: -
>
> union
> {
> int i;
> char c[4];
> } u;
>
> const int addr_diff = (&u.c[0] - reinterpret_cast<char *>( &u.i ));
> const bool little_endian = ( addr_diff == 0 );
>
>I can imagine architectures where the address of an int isn't the
>lowest addressed byte that it occupies, but do they exist in practice?
The problem with that construction is that it yields "true" for
little_endian on any architecture where the address of an int
is the lowest addressed byte that it occupies. That's true on
all big-endian architectures that I know of.
On the other hand, the following construction
union
{
int i;
char c[4];
} u = { { 0x1 } };
const bool little_endian = ( u.c[0] == 0x1 );
will work on many implementations (although this is not guaranteed by
the standard). The rub is that it leads to dynamic initialization of
little_endian, and so does not qualify as a compile-time technique.
Mike
--
C. M. Heard/VVNET, Inc.
heard@vvnet.com
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Marcus Barnes <marcus_aurelius@my-deja.com>
Date: 2000/01/11 Raw View
How about this? Or is it undefined to access a union in this way?
Since it's all constant it could be evaluated by the compiler right?
More complete byte level tests can be made for the VAX "middle endian"
if desired of course.
inline
bool
isBig ()
{
union
{
unsigned int word;
unsigned char bytes[ sizeof( unsigned int ) ];
}
const test = { 1 };
return test.bytes[ 0 ] == 0;
}
inline
bool
isLittle ()
{
union
{
unsigned int word;
unsigned char bytes[ sizeof( unsigned int ) ];
}
const test = { 1 };
return test.bytes[ 0 ] == 1;
}
--
Regards, Marcus
Sent via Deja.com http://www.deja.com/
Before you buy.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Steve Clamage <stephen.clamage@sun.com>
Date: 2000/01/11 Raw View
Marcus Barnes wrote:
>
> How about this? Or is it undefined to access a union in this way?
>
> Since it's all constant it could be evaluated by the compiler right?
> More complete byte level tests can be made for the VAX "middle endian"
> if desired of course.
>
> inline
> bool
> isBig ()
> {
> union
> {
> unsigned int word;
> unsigned char bytes[ sizeof( unsigned int ) ];
> }
> const test = { 1 };
>
> return test.bytes[ 0 ] == 0;
> }
>
To get useful compile-time evaluation for this purpose, you need an
"integral constant-expression". Such an expression cannot contain
function calls or the evaluation of array variables. If run-time
evaluation is adequate, you can use a variety of techniques to
figure out byte order.
Technically, the effects of storing in one component of a union and
reading a different component are undefined. But on most systems of
interest you get the expected result. On tagged architectures you
might get a hardware trap if you used this technique.
Of course, the concepts of big-endian and little-endian do not
apply to all architectures. For example, 4-byte integers on some
systems are not stored in either 1234 or 4321 order.
This thread originally asked how to determine endianness at compile
time with portable code. You can't do it in general, even when
the architecture really is big- or little-endian.
--
Steve Clamage, stephen.clamage@sun.com
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]