| 1 | /* |
| 2 | * local version of endian.h - byte order defines |
| 3 | * |
| 4 | * Copyright 2007, Broadcom Corporation |
| 5 | * All Rights Reserved. |
| 6 | * |
| 7 | * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY |
| 8 | * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM |
| 9 | * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS |
| 10 | * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #ifndef _BCMENDIAN_H_ |
| 15 | #define _BCMENDIAN_H_ |
| 16 | |
| 17 | #include <typedefs.h> |
| 18 | |
| 19 | /* Byte swap a 16 bit value */ |
| 20 | #define BCMSWAP16(val) \ |
| 21 | ((uint16)(\ |
| 22 | (((uint16)(val) & (uint16)0x00ffU) << 8) | \ |
| 23 | (((uint16)(val) & (uint16)0xff00U) >> 8))) |
| 24 | |
| 25 | /* Byte swap a 32 bit value */ |
| 26 | #define BCMSWAP32(val) \ |
| 27 | ((uint32)(\ |
| 28 | (((uint32)(val) & (uint32)0x000000ffUL) << 24) | \ |
| 29 | (((uint32)(val) & (uint32)0x0000ff00UL) << 8) | \ |
| 30 | (((uint32)(val) & (uint32)0x00ff0000UL) >> 8) | \ |
| 31 | (((uint32)(val) & (uint32)0xff000000UL) >> 24))) |
| 32 | |
| 33 | /* 2 Byte swap a 32 bit value */ |
| 34 | #define BCMSWAP32BY16(val) \ |
| 35 | ((uint32)(\ |
| 36 | (((uint32)(val) & (uint32)0x0000ffffUL) << 16) | \ |
| 37 | (((uint32)(val) & (uint32)0xffff0000UL) >> 16))) |
| 38 | |
| 39 | |
| 40 | static INLINE uint16 |
| 41 | bcmswap16(uint16 val) |
| 42 | { |
| 43 | return BCMSWAP16(val); |
| 44 | } |
| 45 | |
| 46 | static INLINE uint32 |
| 47 | bcmswap32(uint32 val) |
| 48 | { |
| 49 | return BCMSWAP32(val); |
| 50 | } |
| 51 | |
| 52 | static INLINE uint32 |
| 53 | bcmswap32by16(uint32 val) |
| 54 | { |
| 55 | return BCMSWAP32BY16(val); |
| 56 | } |
| 57 | |
| 58 | /* buf - start of buffer of shorts to swap */ |
| 59 | /* len - byte length of buffer */ |
| 60 | static INLINE void |
| 61 | bcmswap16_buf(uint16 *buf, uint len) |
| 62 | { |
| 63 | len = len/2; |
| 64 | |
| 65 | while (len--) { |
| 66 | *buf = bcmswap16(*buf); |
| 67 | buf++; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | #ifndef hton16 |
| 72 | #ifndef IL_BIGENDIAN |
| 73 | #define HTON16(i) BCMSWAP16(i) |
| 74 | #define hton16(i) bcmswap16(i) |
| 75 | #define hton32(i) bcmswap32(i) |
| 76 | #define ntoh16(i) bcmswap16(i) |
| 77 | #define ntoh32(i) bcmswap32(i) |
| 78 | #define ltoh16(i) (i) |
| 79 | #define ltoh32(i) (i) |
| 80 | #define htol16(i) (i) |
| 81 | #define htol32(i) (i) |
| 82 | #else |
| 83 | #define HTON16(i) (i) |
| 84 | #define hton16(i) (i) |
| 85 | #define hton32(i) (i) |
| 86 | #define ntoh16(i) (i) |
| 87 | #define ntoh32(i) (i) |
| 88 | #define ltoh16(i) bcmswap16(i) |
| 89 | #define ltoh32(i) bcmswap32(i) |
| 90 | #define htol16(i) bcmswap16(i) |
| 91 | #define htol32(i) bcmswap32(i) |
| 92 | #endif /* IL_BIGENDIAN */ |
| 93 | #endif /* hton16 */ |
| 94 | |
| 95 | #ifndef IL_BIGENDIAN |
| 96 | #define ltoh16_buf(buf, i) |
| 97 | #define htol16_buf(buf, i) |
| 98 | #else |
| 99 | #define ltoh16_buf(buf, i) bcmswap16_buf((uint16*)buf, i) |
| 100 | #define htol16_buf(buf, i) bcmswap16_buf((uint16*)buf, i) |
| 101 | #endif /* IL_BIGENDIAN */ |
| 102 | |
| 103 | /* |
| 104 | * store 16-bit value to unaligned little endian byte array. |
| 105 | */ |
| 106 | static INLINE void |
| 107 | htol16_ua_store(uint16 val, uint8 *bytes) |
| 108 | { |
| 109 | bytes[0] = val&0xff; |
| 110 | bytes[1] = val>>8; |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * store 32-bit value to unaligned little endian byte array. |
| 115 | */ |
| 116 | static INLINE void |
| 117 | htol32_ua_store(uint32 val, uint8 *bytes) |
| 118 | { |
| 119 | bytes[0] = val&0xff; |
| 120 | bytes[1] = (val>>8)&0xff; |
| 121 | bytes[2] = (val>>16)&0xff; |
| 122 | bytes[3] = val>>24; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * store 16-bit value to unaligned network(big) endian byte array. |
| 127 | */ |
| 128 | static INLINE void |
| 129 | hton16_ua_store(uint16 val, uint8 *bytes) |
| 130 | { |
| 131 | bytes[1] = val&0xff; |
| 132 | bytes[0] = val>>8; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * store 32-bit value to unaligned network(big) endian byte array. |
| 137 | */ |
| 138 | static INLINE void |
| 139 | hton32_ua_store(uint32 val, uint8 *bytes) |
| 140 | { |
| 141 | bytes[3] = val&0xff; |
| 142 | bytes[2] = (val>>8)&0xff; |
| 143 | bytes[1] = (val>>16)&0xff; |
| 144 | bytes[0] = val>>24; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * load 16-bit value from unaligned little endian byte array. |
| 149 | */ |
| 150 | static INLINE uint16 |
| 151 | ltoh16_ua(void *bytes) |
| 152 | { |
| 153 | return (((uint8*)bytes)[1]<<8)+((uint8 *)bytes)[0]; |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * load 32-bit value from unaligned little endian byte array. |
| 158 | */ |
| 159 | static INLINE uint32 |
| 160 | ltoh32_ua(void *bytes) |
| 161 | { |
| 162 | return (((uint8*)bytes)[3]<<24)+(((uint8*)bytes)[2]<<16)+ |
| 163 | (((uint8*)bytes)[1]<<8)+((uint8*)bytes)[0]; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * load 16-bit value from unaligned big(network) endian byte array. |
| 168 | */ |
| 169 | static INLINE uint16 |
| 170 | ntoh16_ua(void *bytes) |
| 171 | { |
| 172 | return (((uint8*)bytes)[0]<<8)+((uint8*)bytes)[1]; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * load 32-bit value from unaligned big(network) endian byte array. |
| 177 | */ |
| 178 | static INLINE uint32 |
| 179 | ntoh32_ua(void *bytes) |
| 180 | { |
| 181 | return (((uint8*)bytes)[0]<<24)+(((uint8*)bytes)[1]<<16)+ |
| 182 | (((uint8*)bytes)[2]<<8)+((uint8*)bytes)[3]; |
| 183 | } |
| 184 | |
| 185 | #define ltoh_ua(ptr) (\ |
| 186 | sizeof(*(ptr)) == sizeof(uint8) ? *(uint8 *)ptr : \ |
| 187 | sizeof(*(ptr)) == sizeof(uint16) ? (((uint8 *)ptr)[1]<<8)+((uint8 *)ptr)[0] : \ |
| 188 | (((uint8 *)ptr)[3]<<24)+(((uint8 *)ptr)[2]<<16)+(((uint8 *)ptr)[1]<<8)+((uint8 *)ptr)[0] \ |
| 189 | ) |
| 190 | |
| 191 | #define ntoh_ua(ptr) (\ |
| 192 | sizeof(*(ptr)) == sizeof(uint8) ? *(uint8 *)ptr : \ |
| 193 | sizeof(*(ptr)) == sizeof(uint16) ? (((uint8 *)ptr)[0]<<8)+((uint8 *)ptr)[1] : \ |
| 194 | (((uint8 *)ptr)[0]<<24)+(((uint8 *)ptr)[1]<<16)+(((uint8 *)ptr)[2]<<8)+((uint8 *)ptr)[3] \ |
| 195 | ) |
| 196 | |
| 197 | #endif /* _BCMENDIAN_H_ */ |
| 198 | |