LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-21-2005, 04:32 PM   #1
JCipriani
Member
 
Registered: Aug 2005
Location: Pittsburgh, PA, USA
Distribution: Redhat 9, OS X 10.4.x, Win2K
Posts: 85

Rep: Reputation: 15
Determining system endianness.


Is there a 100% guaranteed standardized super duper way to determine if a system is big endian or not at compile time? Any precompiler macros that are defined on all big endian systems and on no little endian systems or anything like that? Every time I check I feel like it's a hack, with things like:
Code:
#if __LINUX__
#  if BIG_ENDIAN_____YEAH
#    if POWERPC
#      define BIG_ENDIAN 1
#    endif
#    if NOT_REALLY_LINUX_AFTERALL
#      define BIG_ENDIAN (!NOT_BIG_ENDIAN)
#    endif
#  endf
#elif (defined(MAC) && !defined(WINDOWS) && defined(WINDOWS) && (OMG == 5))
#  define BIG_ENDIAN (defined(MAC) && defined(LINUX) && defined(SOLARIS))
#elif LITTLE_ENDIAN
#  define BIG_ENDIAN (5 * 3 - 15) / M_PI
#endif
Well, obviously I made that up. But I can't figure out if there's a standard, compiler-independent way to check endianness at compile time. I always come up with stuff that works but it always feels so dirty and unreliable.

Thanks,
Jason

Last edited by JCipriani; 09-21-2005 at 04:34 PM.
 
Old 09-21-2005, 04:49 PM   #2
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
I don't know of any pre-compiler defines offhand, but a function like this should work:

Code:
bool IsLittleEndian()
{
    int i = 0x00000001;
    char* p = (char*)&i;

    return p[0] == 1;
}
 
Old 09-21-2005, 04:58 PM   #3
JCipriani
Member
 
Registered: Aug 2005
Location: Pittsburgh, PA, USA
Distribution: Redhat 9, OS X 10.4.x, Win2K
Posts: 85

Original Poster
Rep: Reputation: 15
Yeah, but I want to avoid runtime checking just so I can avoid branching in inner loops that may or may not need to perform byteswapping. I have found, though, that using code like this actually works quite well with optimizations turned on:

Code:
if (0x00FF & *(unsigned short *)"a")
  /* do little endian stuff */
else
  /* do big endian stuff */
Because the condition is constant and the compiler will optimize the branch away. But I don't particularly like that method.
 
Old 09-21-2005, 07:25 PM   #4
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
There are many ways, but the best one is to compile a program that defines the system's endianness in a file like config.h and #include it in your source.

http://www.unixpapa.com/incnote/byteorder.html

I've seen a header somewhere that tests for many processor and define its endianness.
 
Old 09-21-2005, 07:40 PM   #5
JCipriani
Member
 
Registered: Aug 2005
Location: Pittsburgh, PA, USA
Distribution: Redhat 9, OS X 10.4.x, Win2K
Posts: 85

Original Poster
Rep: Reputation: 15
Thanks, primo. That link was helpful. I was pretty sure that generating headers at compile time would be the best way to go but I was hoping there would be a cleaner way. It's too bad there's no standard defines or anything like that, it seems like something that would have been useful. Oh well.

Jason
 
Old 09-22-2005, 04:17 PM   #6
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
Code:
/*
 ---------------------------------------------------------------------------
 Copyright (c) 2003, Dr Brian Gladman, Worcester, UK.   All rights reserved.

 LICENSE TERMS

 The free distribution and use of this software in both source and binary
 form is allowed (with or without changes) provided that:

   1. distributions of this source code include the above copyright
      notice, this list of conditions and the following disclaimer;

   2. distributions in binary form include the above copyright
      notice, this list of conditions and the following disclaimer
      in the documentation and/or other associated materials;

   3. the copyright holder's name is not used to endorse products
      built using this software without specific written permission.

 ALTERNATIVELY, provided that this notice is retained in full, this product
 may be distributed under the terms of the GNU General Public License (GPL),
 in which case the provisions of the GPL apply INSTEAD OF those given above.

 DISCLAIMER

 This software is provided 'as is' with no explicit or implied warranties
 in respect of its properties, including, but not limited to, correctness
 and/or fitness for purpose.
 ---------------------------------------------------------------------------
 Issue 01/08/2005

*/

#ifndef EDEFS_H
#define EDEFS_H
#if defined(__cplusplus)
extern "C"
{
#endif

#define IS_LITTLE_ENDIAN   1234 /* byte 0 is least significant (i386) */
#define IS_BIG_ENDIAN      4321 /* byte 0 is most significant (mc68k) */

#if defined(__GNUC__) || defined(__GNU_LIBRARY__)
#  if defined(__FreeBSD__) || defined(__OpenBSD__)
#    include <sys/endian.h>
#  elif defined( BSD ) && ( BSD >= 199103 ) || defined( __DJGPP__ ) || defined( __CYGWIN32__ )
#      include <machine/endian.h>
#  elif defined(__APPLE__)
#    if defined(__BIG_ENDIAN__) && !defined( BIG_ENDIAN )
#      define BIG_ENDIAN
#    elif defined(__LITTLE_ENDIAN__) && !defined( LITTLE_ENDIAN )
#      define LITTLE_ENDIAN
#    endif
#  elif !defined( __MINGW32__ )
#    include <endian.h>
#    if !defined(__BEOS__)
#      include <byteswap.h>
#    endif
#  endif
#endif

#if !defined(PLATFORM_BYTE_ORDER)
#  if defined(LITTLE_ENDIAN) || defined(BIG_ENDIAN)
#    if    defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN)
#      define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
#    elif !defined(LITTLE_ENDIAN) &&  defined(BIG_ENDIAN)
#      define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
#    elif defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN)
#      define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
#    elif defined(BYTE_ORDER) && (BYTE_ORDER == BIG_ENDIAN)
#      define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
#    endif
#  elif defined(_LITTLE_ENDIAN) || defined(_BIG_ENDIAN)
#    if    defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
#      define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
#    elif !defined(_LITTLE_ENDIAN) &&  defined(_BIG_ENDIAN)
#      define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
#    elif defined(_BYTE_ORDER) && (_BYTE_ORDER == _LITTLE_ENDIAN)
#      define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
#    elif defined(_BYTE_ORDER) && (_BYTE_ORDER == _BIG_ENDIAN)
#      define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
#   endif
#  elif defined(__LITTLE_ENDIAN__) || defined(__BIG_ENDIAN__)
#    if    defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
#      define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
#    elif !defined(__LITTLE_ENDIAN__) &&  defined(__BIG_ENDIAN__)
#      define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
#    elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __LITTLE_ENDIAN__)
#      define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
#    elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __BIG_ENDIAN__)
#      define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
#    endif
#  endif
#endif

/*  if the platform is still unknown, try to find its byte order    */
/*  from commonly used machine defines                              */

#if !defined(PLATFORM_BYTE_ORDER)

#if   defined( __alpha__ ) || defined( __alpha ) || defined( i386 )       || \
      defined( __i386__ )  || defined( _M_I86 )  || defined( _M_IX86 )    || \
      defined( __OS2__ )   || defined( sun386 )  || defined( __TURBOC__ ) || \
      defined( vax )       || defined( vms )     || defined( VMS )        || \
      defined( __VMS )
#  define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN

#elif defined( AMIGA )    || defined( applec )  || defined( __AS400__ )  || \
      defined( _CRAY )    || defined( __hppa )  || defined( __hp9000 )   || \
      defined( ibm370 )   || defined( mc68000 ) || defined( m68k )       || \
      defined( __MRC__ )  || defined( __MVS__ ) || defined( __MWERKS__ ) || \
      defined( sparc )    || defined( __sparc)  || defined( SYMANTEC_C ) || \
      defined( __TANDEM ) || defined( THINK_C ) || defined( __VMCMS__ )
#  define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN

#elif 0     /* **** EDIT HERE IF NECESSARY **** */
#  define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
#elif 0     /* **** EDIT HERE IF NECESSARY **** */
#  define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
#else
#  error Please edit edefs.h (lines 113 or 115) to set the platform byte order
#endif

#endif

#if defined(__cplusplus)
}
#endif
#endif
 
Old 09-22-2005, 04:24 PM   #7
JCipriani
Member
 
Registered: Aug 2005
Location: Pittsburgh, PA, USA
Distribution: Redhat 9, OS X 10.4.x, Win2K
Posts: 85

Original Poster
Rep: Reputation: 15
Good lord.

Well, thanks for digging that up.
 
Old 09-22-2005, 04:33 PM   #8
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Why not just use functions like htonl(), nltoh(), etc. to keep your data endian-independent?
 
Old 09-22-2005, 04:42 PM   #9
JCipriani
Member
 
Registered: Aug 2005
Location: Pittsburgh, PA, USA
Distribution: Redhat 9, OS X 10.4.x, Win2K
Posts: 85

Original Poster
Rep: Reputation: 15
I could, but see post #3. I want to avoid repeated runtime checks. Also, AFAIK, there's no 8-byte hton* function, so binary doubles are still an issue. The other problem with those functions is that network byte order is big-endian, but I usually use little endian machines so I might as well keep everything little-endian for the sake of efficiency.
 
Old 09-22-2005, 09:35 PM   #10
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
Quote:
Originally posted by itsme86
Why not just use functions like htonl(), nltoh(), etc. to keep your data endian-independent?
The problem comes defining structures with bit-fields. Take a look at <netinet/tcp.h> and <netinet/ip.h>
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sha-1 encryption and endianness sade Linux - Software 0 08-24-2004 11:16 AM
Determining what time power was pulled on Red Hat 8 system utmpwtmp Linux - General 4 08-21-2004 07:49 PM
Determining IP bluethundr Linux - Newbie 1 08-12-2003 12:16 AM
determining file system 24jedi Linux - General 2 07-31-2003 11:26 AM
pci support and endianness philipsyyy Linux - Software 0 10-17-2002 11:35 AM


All times are GMT -5. The time now is 09:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration