Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

endianness.h

Go to the documentation of this file.
00001 /* 
00002  * $Id: endianness.h,v 1.2 2008/07/01 21:56:06 andrei Exp $
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 /*
00019  *  endianness compile and runtime  tests
00020  * 
00021  * History:
00022  * --------
00023  *  2008-06-13  created by andrei
00024  */
00025 
00026 /*
00027  * Defines:
00028  *    __IS_LITTLE_ENDIAN if the system is little endian and
00029  *    __IS_BIG_ENDIAN    if it's big endian
00030  * Function/macros:
00031  *     is_big_endian()  - runtime test for big endian
00032  *     is_little_endian() - runtime test for little endian
00033  *     endianness_sanity_check() - returns 0 if the compile time
00034  *                                  detected endianness corresponds to
00035  *                                  the runtime detected one and -1 on 
00036  *                                  error (recommended action: bail out)
00037  */
00038 /*
00039  * Implementation notes:
00040  * Endian macro names/tests for different OSes:
00041  * linux:  __BYTE_ORDER == __LITTLE_ENDIAN | __BIG_ENDIAN
00042  *           BYTE_ORDER == LITTLE_ENDIAN | BIG_ENDIAN
00043  * bsd:     _BYTE_ORDER == _LITTLE_ENDIAN | _BIG_ENDIAN
00044  *           BYTE_ORDER == LITTLE_ENDIAN | BIG_ENDIAN
00045  * solaris: _LITTLE_ENDIAN | _BIG_ENDIAN
00046  *
00047  * Include file for the endian macros:
00048  * linux: <endian.h> (glibc), <sys/param.h>
00049  * bsd:   <sys/param.h>, <sys/endian.h>
00050  * solaris: <sys/param.h>
00051  *
00052  * Note: BIG_ENDIAN, LITTLE_ENDIAN, _BIG_ENDIAN, _LITTLE_ENDIAN cannot be 
00053  *       used always,  some OSes define both of them for BYTE_ORDER use
00054  *       (e.g. linux defines both BIG_ENDIAN & LITTLE_ENDIAN, bsds define
00055  *          _BIG_ENDIAN, _LITTLE_ENDIAN, BIG_ENDIAN, LITTLE_ENDIAN)
00056  *
00057  */
00058 
00059 
00060 #ifndef _endianness_h
00061 #define _endianness_h
00062 
00063 /* use bsd includes: they work everywhere */
00064 #include <sys/types.h>
00065 #include <sys/param.h>
00066 
00067 
00068 
00069 extern int _endian_test_int;
00070 
00071 /* returns 1 for little endian, 0 for big endian */
00072 #define endian_test()           (*(char*)&_endian_test_int==1)
00073 #define is_big_endian()         (!endian_test())
00074 #define is_little_endian()      endian_test()
00075 
00076 
00077 extern int endianness_sanity_check();
00078 
00079 /* detect compile time endianess */
00080 #if defined __BYTE_ORDER && defined __LITTLE_ENDIAN && defined __BIG_ENDIAN
00081 /* linux */
00082 #if __BYTE_ORDER == __LITTLE_ENDIAN && ! defined __IS_LITTLE_ENDIAN
00083         #define __IS_LITTLE_ENDIAN 0x01020304
00084 #endif
00085 #if __BYTE_ORDER == __BIG_ENDIAN && ! defined __IS_BIG_ENDIAN
00086         #define __IS_BIG_ENDIAN 0x01020304
00087 #endif
00088 #elif defined _BYTE_ORDER && defined _LITTLE_ENDIAN && defined _BIG_ENDIAN
00089 /* bsd */
00090 #if _BYTE_ORDER == _LITTLE_ENDIAN && ! defined __IS_LITTLE_ENDIAN
00091         #define __IS_LITTLE_ENDIAN 0x01020304
00092 #endif
00093 #if _BYTE_ORDER == _BIG_ENDIAN && ! defined __IS_BIG_ENDIAN
00094         #define __IS_BIG_ENDIAN 0x01020304
00095 #endif
00096 #elif defined BYTE_ORDER && defined LITTLE_ENDIAN && defined BIG_ENDIAN
00097 /* bsd old/deprecated */
00098 #if BYTE_ORDER == LITTLE_ENDIAN && ! defined __IS_LITTLE_ENDIAN
00099         #define __IS_LITTLE_ENDIAN 0x01020304
00100 #endif
00101 #if BYTE_ORDER == BIG_ENDIAN && ! defined __IS_BIG_ENDIAN
00102         #define __IS_BIG_ENDIAN 0x01020304
00103 #endif
00104 #elif !(defined _LITTLE_ENDIAN && defined _BIG_ENDIAN) && \
00105                 (defined _LITTLE_ENDIAN || defined _BIG_ENDIAN)
00106 /* OSes that don't define BYTE_ORDER (sanity check above makes sure
00107  *   little & big endian are not defined in the same time )*/
00108 /* solaris */
00109 #if defined _LITTLE_ENDIAN && !defined __IS_LITTLE_ENDIAN
00110         #define __IS_LITTLE_ENDIAN 0x01020304
00111 #endif
00112 #if defined _BIG_ENDIAN && !defined __IS_BIG_ENDIAN
00113         #define __IS_BIG_ENDIAN 0x04030201
00114 #endif
00115 #elif !(defined LITTLE_ENDIAN && defined BIG_ENDIAN) && \
00116                 (defined LITTLE_ENDIAN || defined BIG_ENDIAN)
00117 /* OSes that don't define BYTE_ORDER (sanity check above makes sure
00118  *   little & big endian are not defined in the same time )*/
00119 #if defined LITTLE_ENDIAN && !defined __IS_LITTLE_ENDIAN
00120         #define __IS_LITTLE_ENDIAN 0x01020304
00121 #endif
00122 #if defined BIG_ENDIAN && !defined __IS_BIG_ENDIAN
00123         #define __IS_BIG_ENDIAN 0x04030201
00124 #endif
00125 
00126 #else
00127 #error could not detect endianess
00128 #endif
00129 
00130 #if !defined __IS_LITTLE_ENDIAN && !defined __IS_BIG_ENDIAN
00131 #error BUG: could not detect endianess
00132 #endif
00133 
00134 #if defined __IS_LITTLE_ENDIAN && defined __IS_BIG_ENDIAN
00135 #error BUG: both little & big endian detected in the same time
00136 #endif
00137 
00138 
00139 #endif /* _endianness_h */
00140 

Generated on Tue Sep 7 04:15:51 2010 for SIPExpressRouter by  doxygen 1.3.9.1