endian.h

Go to the documentation of this file.
00001 /**
00002  *  Copyright (C) 2004-2005 Alo Sarv <madcat_@users.sourceforge.net>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00019 #ifndef __ENDIAN_H__
00020 #define __ENDIAN_H__
00021 
00022 /**
00023  * Defines global macros for byte-swapping. Whenever available, headers provided
00024  * by host platform are used, but falling back to generic code when none are
00025  * available.
00026  */
00027 
00028 #include <hn/osdep.h>
00029 
00030 #ifdef HAVE_LIBKERN_OSBYTEORDER_H
00031         #include <libkern/OSByteOrder.h>
00032         #define SWAP16_ALWAYS(x) OSSwapInt16(x)
00033         #define SWAP32_ALWAYS(x) OSSwapInt32(x)
00034         #define SWAP64_ALWAYS(x) OSSwapInt64(x)
00035 #elif defined(HAVE_BYTESWAP_H)
00036         #include <byteswap.h>
00037         #define SWAP16_ALWAYS(x) bswap_16(x)
00038         #define SWAP32_ALWAYS(x) bswap_32(x)
00039         #define SWAP64_ALWAYS(x) bswap_64(x)
00040 #else
00041         #define SWAP16_ALWAYS(val) (                              \
00042                 (((uint16_t) (val) & (uint16_t) 0x00ffU) << 8)  | \
00043                 (((uint16_t) (val) & (uint16_t) 0xff00U) >> 8))
00044 
00045         #define SWAP32_ALWAYS(val) (                                  \
00046                 (((uint32_t) (val) & (uint32_t) 0x000000ffU) << 24) | \
00047                 (((uint32_t) (val) & (uint32_t) 0x0000ff00U) <<  8) | \
00048                 (((uint32_t) (val) & (uint32_t) 0x00ff0000U) >>  8) | \
00049                 (((uint32_t) (val) & (uint32_t) 0xff000000U) >> 24))
00050 
00051         #define SWAP64_ALWAYS(val) (                                           \
00052                 (((uint64_t) (val) & (uint64_t) 0x00000000000000ffULL) << 56) |\
00053                 (((uint64_t) (val) & (uint64_t) 0x000000000000ff00ULL) << 40) |\
00054                 (((uint64_t) (val) & (uint64_t) 0x0000000000ff0000ULL) << 24) |\
00055                 (((uint64_t) (val) & (uint64_t) 0x00000000ff000000ULL) <<  8) |\
00056                 (((uint64_t) (val) & (uint64_t) 0x000000ff00000000ULL) >>  8) |\
00057                 (((uint64_t) (val) & (uint64_t) 0x0000ff0000000000ULL) >> 24) |\
00058                 (((uint64_t) (val) & (uint64_t) 0x00ff000000000000ULL) >> 40) |\
00059                 (((uint64_t) (val) & (uint64_t) 0xff00000000000000ULL) >> 56))
00060 #endif
00061 
00062 #if defined(__BIG_ENDIAN__)
00063         #define SWAP16_ON_BE(val) SWAP16_ALWAYS(val)
00064         #define SWAP32_ON_BE(val) SWAP32_ALWAYS(val)
00065         #define SWAP64_ON_BE(val) SWAP64_ALWAYS(val)
00066         #define SWAP16_ON_LE(val) (val)
00067         #define SWAP32_ON_LE(val) (val)
00068         #define SWAP64_ON_LE(val) (val)
00069 #else
00070         #define SWAP16_ON_BE(val) (val)
00071         #define SWAP32_ON_BE(val) (val)
00072         #define SWAP64_ON_BE(val) (val)
00073         #define SWAP16_ON_LE(val) SWAP16_ALWAYS(val)
00074         #define SWAP32_ON_LE(val) SWAP32_ALWAYS(val)
00075         #define SWAP64_ON_LE(val) SWAP64_ALWAYS(val)
00076 #endif
00077 
00078 #endif /* __ENDIAN_H__ */