Test for endian using MPL or PP(Wave?)
Hi, There is an application I use that always produces a little endian binary file. I have written a program to process this file. As my program can port to both little/big endian platforms I have a #define (LITTLE_ENDIAN_TARGET) acting as a flag for the necessary byte swapping that is needed on the big-endian platforms. I was wondering if there is a better way to do this, so I searched the Web & boost archives. I found the following code snippet in the boost archives. bool isLittleEndian() { int temp = 1; return (*(char*)&temp) != 0; } which is of some help (eliminating my #define). However, I was wondering if there is a PP or mpl way of testing for endian-ness at compile time. For example, I'd like to do something like the following: const bool IsLittleEndian = ..to be determined automatically..; I've got a gut feeling that I can't do the above at complile time, but maybe something like Wave may do the job? Thanks for any assistance in this matter. Mike
check out boost's template metaprogramming library...i won't guarantee anything, but you might be able to contort it to do what you need. http://boost.org/libs/mpl/doc/index.html -Ryan ------------------------------- "...real recognize real." -Rakim On Mon, 21 Apr 2003, Mike Wilson wrote:
Hi,
There is an application I use that always produces a little endian binary file. I have written a program to process this file. As my program can port to both little/big endian platforms I have a #define (LITTLE_ENDIAN_TARGET) acting as a flag for the necessary byte swapping that is needed on the big-endian platforms.
I was wondering if there is a better way to do this, so I searched the Web & boost archives. I found the following code snippet in the boost archives.
bool isLittleEndian() { int temp = 1; return (*(char*)&temp) != 0; }
which is of some help (eliminating my #define).
However, I was wondering if there is a PP or mpl way of testing for endian-ness at compile time.
For example, I'd like to do something like the following:
const bool IsLittleEndian = ..to be determined automatically..;
I've got a gut feeling that I can't do the above at complile time, but maybe something like Wave may do the job?
Thanks for any assistance in this matter.
Mike
Info: http://www.boost.org Wiki: http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl Unsubscribe: mailto:boost-users-unsubscribe@yahoogroups.com
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
Thanks for that - I'll look into it.
----- Original Message -----
From: "Ryan Barrett"
check out boost's template metaprogramming library...i won't guarantee anything, but you might be able to contort it to do what you need.
http://boost.org/libs/mpl/doc/index.html
-Ryan
------------------------------- "...real recognize real." -Rakim
On Mon, 21 Apr 2003, Mike Wilson wrote:
Hi,
There is an application I use that always produces a little endian binary file. I have written a program to process this file. As my program can port to both little/big endian platforms I have a #define (LITTLE_ENDIAN_TARGET) acting as a flag for the necessary byte swapping that is needed on the big-endian platforms.
I was wondering if there is a better way to do this, so I searched the Web & boost archives. I found the following code snippet in the boost archives.
bool isLittleEndian() { int temp = 1; return (*(char*)&temp) != 0; }
which is of some help (eliminating my #define).
However, I was wondering if there is a PP or mpl way of testing for endian-ness at compile time.
For example, I'd like to do something like the following:
const bool IsLittleEndian = ..to be determined automatically..;
I've got a gut feeling that I can't do the above at complile time, but maybe something like Wave may do the job?
Thanks for any assistance in this matter.
Mike
"Mike Wilson"
Thanks for that - I'll look into it.
----- Original Message ----- From: "Ryan Barrett"
Newsgroups: gmane.comp.lib.boost.user Sent: Monday, April 21, 2003 10:48 PM Subject: Re: Test for endian using MPL or PP(Wave?) check out boost's template metaprogramming library...i won't guarantee anything, but you might be able to contort it to do what you need.
Don't bother. Endian-ness is fundamentally a property of memory layout. You can't discover anything about the layout of bits in memory at compile-time (and certainly not using the preprocessor), since you can't dereference a pointer at compile-time. The only way to discover endian-ness is to write an integer type to memory and read it back out as a sequence of chars. Of course, some platforms supply headers which will tell you about their endian-ness... -- Dave Abrahams Boost Consulting www.boost-consulting.com
----- Original Message -----
From: "David Abrahams"
"Mike Wilson"
writes: Thanks for that - I'll look into it.
----- Original Message ----- From: "Ryan Barrett"
Newsgroups: gmane.comp.lib.boost.user Sent: Monday, April 21, 2003 10:48 PM Subject: Re: Test for endian using MPL or PP(Wave?) check out boost's template metaprogramming library...i won't guarantee anything, but you might be able to contort it to do what you need.
Don't bother. Endian-ness is fundamentally a property of memory layout. You can't discover anything about the layout of bits in memory at compile-time (and certainly not using the preprocessor), since you can't dereference a pointer at compile-time. The only way to discover endian-ness is to write an integer type to memory and read it back out as a sequence of chars. I thought so, but the doc was informative.
Of course, some platforms supply headers which will tell you about their endian-ness...
I think I saw a BOOST_BIG_ENDIAN_TARGET mentioned in the archives, but no such luck in my 1.29 sources. Thanks.
participants (4)
-
David Abrahams
-
Mike Wilson
-
Mike Wilson
-
Ryan Barrett