1 #ifndef INCLUDED_ENDIAN_H 2 #define INCLUDED_ENDIAN_H 3 /* vim: set ts=8 sts=4 sw=4 tw=80 noet: */ 4 /*====================================================================== 5 Copyright (C) 2004,2005,2009,2012,2013 Walter Doekes 6 <walter+tthsum@wjd.nu> 7 This file is part of tthsum. 8 9 tthsum is free software: you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation, either version 3 of the License, or 12 (at your option) any later version. 13 14 tthsum is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with tthsum. If not, see <http://www.gnu.org/licenses/>. 21 ======================================================================*/ 22 23 /** 24 * Get byte order (endianness) for the host platform. 25 */ 26 27 /* Try to get BYTE_ORDER, BIG_ENDIAN and LITTLE_ENDIAN */ 28 #if defined(__linux) || defined(__GLIBC__) 29 # include <endian.h> 30 # ifndef BIG_ENDIAN 31 # define BIG_ENDIAN __BIG_ENDIAN 32 # endif 33 # ifndef LITTLE_ENDIAN 34 # define LITTLE_ENDIAN __LITTLE_ENDIAN 35 # endif 36 # ifndef BYTE_ORDER 37 # define BYTE_ORDER __BYTE_ORDER 38 # endif 39 #elif defined(__FreeBSD__) || defined (__NetBSD__) || defined(__OpenBSD__) 40 /* sys/types.h includes machine/endian.h */ 41 # include <sys/types.h> 42 # define LITTLE_ENDIAN _LITTLE_ENDIAN 43 # define BIG_ENDIAN _BIG_ENDIAN 44 #endif /* !__*BSD__ */ 45 46 /* Test the values and try to set them by hand on failure */ 47 #if LITTLE_ENDIAN == BIG_ENDIAN \ 48 || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN) \ 49 || !defined(BIG_ENDIAN) || !defined(LITTLE_ENDIAN) 50 # undef BYTE_ORDER 51 # undef BIG_ENDIAN 52 # undef LITTLE_ENDIAN 53 # define BIG_ENDIAN 4321 54 # define LITTLE_ENDIAN 1234 55 # if defined(__alpha) || defined(__i386__) || defined(__vax__) \ 56 || defined(_WIN32) 57 # define BYTE_ORDER LITTLE_ENDIAN 58 # else 59 # define BYTE_ORDER BIG_ENDIAN 60 # endif 61 #endif 62 63 #endif /* INCLUDED_ENDIAN_H */ 64