1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | /* * File: EndianUtilities.h * * Copyright (c) Freescale Semiconductor, Inc. All rights reserved. * See included license file for license details. */ #if !defined(_EndianUtilities_h_) #define _EndianUtilities_h_ //! \name Swap macros //! These macros always swap the data. //@{ //! Byte swap 16-bit value. #define _BYTESWAP16(value) \ (((((uint16_t)value)<<8) & 0xFF00) | \ ((((uint16_t)value)>>8) & 0x00FF)) //! Byte swap 32-bit value. #define _BYTESWAP32(value) \ (((((uint32_t)value)<<24) & 0xFF000000) | \ ((((uint32_t)value)<< 8) & 0x00FF0000) | \ ((((uint32_t)value)>> 8) & 0x0000FF00) | \ ((((uint32_t)value)>>24) & 0x000000FF)) //! Byte swap 64-bit value. #define _BYTESWAP64(value) \ (((((uint64_t)value)<<56) & 0xFF00000000000000ULL) | \ ((((uint64_t)value)<<40) & 0x00FF000000000000ULL) | \ ((((uint64_t)value)<<24) & 0x0000FF0000000000ULL) | \ ((((uint64_t)value)<< 8) & 0x000000FF00000000ULL) | \ ((((uint64_t)value)>> 8) & 0x00000000FF000000ULL) | \ ((((uint64_t)value)>>24) & 0x0000000000FF0000ULL) | \ ((((uint64_t)value)>>40) & 0x000000000000FF00ULL) | \ ((((uint64_t)value)>>56) & 0x00000000000000FFULL)) //@} //! \name Inline swap functions //@{ inline uint16_t _swap_u16(uint16_t value) { return _BYTESWAP16(value); } inline int16_t _swap_s16(int16_t value) { return (int16_t)_BYTESWAP16((uint16_t)value); } inline uint32_t _swap_u32(uint32_t value) { return _BYTESWAP32(value); } inline int32_t _swap_s32(int32_t value) { return (int32_t)_BYTESWAP32((uint32_t)value); } inline uint64_t _swap_u64(uint64_t value) { return _BYTESWAP64(value); } inline int64_t _swap_s64(int64_t value) { return (uint64_t)_BYTESWAP64((uint64_t)value); } //@} #if defined(__LITTLE_ENDIAN__) /* little endian host */ #define ENDIAN_BIG_TO_HOST_U16(value) (_swap_u16(value)) #define ENDIAN_HOST_TO_BIG_U16(value) (_swap_u16(value)) #define ENDIAN_BIG_TO_HOST_S16(value) (_swap_s16(value)) #define ENDIAN_HOST_TO_BIG_S16(value) (_swap_s16(value)) #define ENDIAN_BIG_TO_HOST_U32(value) (_swap_u32(value)) #define ENDIAN_HOST_TO_BIG_U32(value) (_swap_u32(value)) #define ENDIAN_BIG_TO_HOST_S32(value) (_swap_s32(value)) #define ENDIAN_HOST_TO_BIG_S32(value) (_swap_s32(value)) #define ENDIAN_BIG_TO_HOST_U64(value) (_swap_u64(value)) #define ENDIAN_HOST_TO_BIG_U64(value) (_swap_u64(value)) #define ENDIAN_BIG_TO_HOST_S64(value) (_swap_s64(value)) #define ENDIAN_HOST_TO_BIG_S64(value) (_swap_s64(value)) /* no-ops */ #define ENDIAN_LITTLE_TO_HOST_U16(value) (value) #define ENDIAN_HOST_TO_LITTLE_U16(value) (value) #define ENDIAN_LITTLE_TO_HOST_S16(value) (value) #define ENDIAN_HOST_TO_LITTLE_S16(value) (value) #define ENDIAN_LITTLE_TO_HOST_U32(value) (value) #define ENDIAN_HOST_TO_LITTLE_U32(value) (value) #define ENDIAN_LITTLE_TO_HOST_S32(value) (value) #define ENDIAN_HOST_TO_LITTLE_S32(value) (value) #define ENDIAN_LITTLE_TO_HOST_U64(value) (value) #define ENDIAN_HOST_TO_LITTLE_U64(value) (value) #define ENDIAN_LITTLE_TO_HOST_S64(value) (value) #define ENDIAN_HOST_TO_LITTLE_S64(value) (value) #elif defined(__BIG_ENDIAN__) /* big endian host */ #define ENDIAN_LITTLE_TO_HOST_U16(value) (_swap_u16(value)) #define ENDIAN_HOST_TO_LITTLE_U16(value) (_swap_u16(value)) #define ENDIAN_LITTLE_TO_HOST_S16(value) (_swap_s16(value)) #define ENDIAN_HOST_TO_LITTLE_S16(value) (_swap_s16(value)) #define ENDIAN_LITTLE_TO_HOST_U32(value) (_swap_u32(value)) #define ENDIAN_HOST_TO_LITTLE_U32(value) (_swap_u32(value)) #define ENDIAN_LITTLE_TO_HOST_S32(value) (_swap_s32(value)) #define ENDIAN_HOST_TO_LITTLE_S32(value) (_swap_s32(value)) #define ENDIAN_LITTLE_TO_HOST_U64(value) (_swap_u64(value)) #define ENDIAN_HOST_TO_LITTLE_U64(value) (_swap_u64(value)) #define ENDIAN_LITTLE_TO_HOST_S64(value) (_swap_s64(value)) #define ENDIAN_HOST_TO_LITTLE_S64(value) (_swap_s64(value)) /* no-ops */ #define ENDIAN_BIG_TO_HOST_U16(value) (value) #define ENDIAN_HOST_TO_BIG_U16(value) (value) #define ENDIAN_BIG_TO_HOST_S16(value) (value) #define ENDIAN_HOST_TO_BIG_S16(value) (value) #define ENDIAN_BIG_TO_HOST_U32(value) (value) #define ENDIAN_HOST_TO_BIG_U32(value) (value) #define ENDIAN_BIG_TO_HOST_S32(value) (value) #define ENDIAN_HOST_TO_BIG_S32(value) (value) #define ENDIAN_BIG_TO_HOST_U64(value) (value) #define ENDIAN_HOST_TO_BIG_U64(value) (value) #define ENDIAN_BIG_TO_HOST_S64(value) (value) #define ENDIAN_HOST_TO_BIG_S64(value) (value) #endif #endif // _EndianUtilities_h_ |