byteswap.h
Go to the documentation of this file.00001 #ifndef ETHERBOOT_BYTESWAP_H
00002 #define ETHERBOOT_BYTESWAP_H
00003
00004 FILE_LICENCE ( GPL2_OR_LATER );
00005
00006 #include "endian.h"
00007 #include "bits/byteswap.h"
00008
00009 #define __bswap_constant_16(x) \
00010 ((uint16_t)((((uint16_t)(x) & 0x00ff) << 8) | \
00011 (((uint16_t)(x) & 0xff00) >> 8)))
00012
00013 #define __bswap_constant_32(x) \
00014 ((uint32_t)((((uint32_t)(x) & 0x000000ffU) << 24) | \
00015 (((uint32_t)(x) & 0x0000ff00U) << 8) | \
00016 (((uint32_t)(x) & 0x00ff0000U) >> 8) | \
00017 (((uint32_t)(x) & 0xff000000U) >> 24)))
00018
00019 #define __bswap_constant_64(x) \
00020 ((uint64_t)((((uint64_t)(x) & 0x00000000000000ffULL) << 56) | \
00021 (((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \
00022 (((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
00023 (((uint64_t)(x) & 0x00000000ff000000ULL) << 8) | \
00024 (((uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \
00025 (((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \
00026 (((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
00027 (((uint64_t)(x) & 0xff00000000000000ULL) >> 56)))
00028
00029 #define __bswap_16(x) \
00030 ((uint16_t)(__builtin_constant_p(x) ? \
00031 __bswap_constant_16(x) : \
00032 __bswap_variable_16(x)))
00033
00034 #define __bswap_32(x) \
00035 ((uint32_t)(__builtin_constant_p(x) ? \
00036 __bswap_constant_32(x) : \
00037 __bswap_variable_32(x)))
00038
00039 #define __bswap_64(x) \
00040 ((uint64_t)(__builtin_constant_p(x) ? \
00041 __bswap_constant_64(x) : \
00042 __bswap_variable_64(x)))
00043
00044 #if __BYTE_ORDER == __LITTLE_ENDIAN
00045 #include "little_bswap.h"
00046 #endif
00047 #if __BYTE_ORDER == __BIG_ENDIAN
00048 #include "big_bswap.h"
00049 #endif
00050
00051
00052 #define swap64(x) __bswap_64(x)
00053 #define swap32(x) __bswap_32(x)
00054 #define swap16(x) __bswap_16(x)
00055 #define bswap_64(x) __bswap_64(x)
00056 #define bswap_32(x) __bswap_32(x)
00057 #define bswap_16(x) __bswap_16(x)
00058
00059 #endif