• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • Directories
  • File List
  • Globals

endianness.h

Go to the documentation of this file.
00001 /* 
00002  * $Id$
00003  * 
00004  * Copyright (C) 2008 iptelorg GmbH
00005  *
00006  * Permission to use, copy, modify, and distribute this software for any
00007  * purpose with or without fee is hereby granted, provided that the above
00008  * copyright notice and this permission notice appear in all copies.
00009  *
00010  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00011  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00012  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00013  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00014  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00015  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00016  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00017  */
00018 
00058 #ifndef _endianness_h
00059 #define _endianness_h
00060 
00061 /* use bsd includes: they work everywhere */
00062 #include <sys/types.h>
00063 #include <sys/param.h>
00064 
00065 
00066 
00067 extern int _endian_test_int;
00068 
00069 /* returns 1 for little endian, 0 for big endian */
00070 #define endian_test()           (*(char*)&_endian_test_int==1)
00071 #define is_big_endian()         (!endian_test())
00072 #define is_little_endian()      endian_test()
00073 
00074 
00075 extern int endianness_sanity_check(void);
00076 
00077 /* detect compile time endianess */
00078 #if defined __BYTE_ORDER && defined __LITTLE_ENDIAN && defined __BIG_ENDIAN
00079 /* linux */
00080 #if __BYTE_ORDER == __LITTLE_ENDIAN && ! defined __IS_LITTLE_ENDIAN
00081         #define __IS_LITTLE_ENDIAN 0x01020304
00082 #endif
00083 #if __BYTE_ORDER == __BIG_ENDIAN && ! defined __IS_BIG_ENDIAN
00084         #define __IS_BIG_ENDIAN 0x01020304
00085 #endif
00086 #elif defined _BYTE_ORDER && defined _LITTLE_ENDIAN && defined _BIG_ENDIAN
00087 /* bsd */
00088 #if _BYTE_ORDER == _LITTLE_ENDIAN && ! defined __IS_LITTLE_ENDIAN
00089         #define __IS_LITTLE_ENDIAN 0x01020304
00090 #endif
00091 #if _BYTE_ORDER == _BIG_ENDIAN && ! defined __IS_BIG_ENDIAN
00092         #define __IS_BIG_ENDIAN 0x01020304
00093 #endif
00094 #elif defined BYTE_ORDER && defined LITTLE_ENDIAN && defined BIG_ENDIAN
00095 /* bsd old/deprecated */
00096 #if BYTE_ORDER == LITTLE_ENDIAN && ! defined __IS_LITTLE_ENDIAN
00097         #define __IS_LITTLE_ENDIAN 0x01020304
00098 #endif
00099 #if BYTE_ORDER == BIG_ENDIAN && ! defined __IS_BIG_ENDIAN
00100         #define __IS_BIG_ENDIAN 0x01020304
00101 #endif
00102 #elif !(defined _LITTLE_ENDIAN && defined _BIG_ENDIAN) && \
00103                 (defined _LITTLE_ENDIAN || defined _BIG_ENDIAN)
00104 /* OSes that don't define BYTE_ORDER (sanity check above makes sure
00105  *   little & big endian are not defined in the same time )*/
00106 /* solaris */
00107 #if defined _LITTLE_ENDIAN && !defined __IS_LITTLE_ENDIAN
00108         #define __IS_LITTLE_ENDIAN 0x01020304
00109 #endif
00110 #if defined _BIG_ENDIAN && !defined __IS_BIG_ENDIAN
00111         #define __IS_BIG_ENDIAN 0x04030201
00112 #endif
00113 #elif !(defined LITTLE_ENDIAN && defined BIG_ENDIAN) && \
00114                 (defined LITTLE_ENDIAN || defined BIG_ENDIAN)
00115 /* OSes that don't define BYTE_ORDER (sanity check above makes sure
00116  *   little & big endian are not defined in the same time )*/
00117 #if defined LITTLE_ENDIAN && !defined __IS_LITTLE_ENDIAN
00118         #define __IS_LITTLE_ENDIAN 0x01020304
00119 #endif
00120 #if defined BIG_ENDIAN && !defined __IS_BIG_ENDIAN
00121         #define __IS_BIG_ENDIAN 0x04030201
00122 #endif
00123 
00124 #else
00125 #error could not detect endianess
00126 #endif
00127 
00128 #if !defined __IS_LITTLE_ENDIAN && !defined __IS_BIG_ENDIAN
00129 #error BUG: could not detect endianess
00130 #endif
00131 
00132 #if defined __IS_LITTLE_ENDIAN && defined __IS_BIG_ENDIAN
00133 #error BUG: both little & big endian detected in the same time
00134 #endif
00135 
00136 
00137 #endif /* _endianness_h */
00138 

Generated on Tue May 22 2012 13:10:07 for SIP Router by  doxygen 1.7.1