2
* local version of endian.h - byte order defines
4
* Copyright 2008, Broadcom Corporation
4
* Copyright (C) 2010, Broadcom Corporation
5
5
* All Rights Reserved.
7
* Unless you and Broadcom execute a separate written software license
8
* agreement governing use of this software, this software is licensed to you
9
* under the terms of the GNU General Public License version 2, available at
10
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html (the "GPL"), with the
11
* following added to such license:
12
* As a special exception, the copyright holders of this software give you
13
* permission to link this software with independent modules, regardless of the
14
* license terms of these independent modules, and to copy and distribute the
15
* resulting executable under terms of your choice, provided that you also meet,
16
* for each linked independent module, the terms and conditions of the license
17
* of that module. An independent module is a module which is not derived from
20
7
* THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
21
8
* KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
22
9
* SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
23
10
* FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
25
* $Id: bcmendian.h,v 1.31 2005/09/03 22:24:22 Exp $
12
* $Id: bcmendian.h,v 1.34 2008/09/17 22:52:25 Exp $
14
* This file by default provides proper behavior on little-endian architectures.
15
* On big-endian architectures, IL_BIGENDIAN should be defined.
28
18
#ifndef _BCMENDIAN_H_
29
19
#define _BCMENDIAN_H_
31
21
#include <typedefs.h>
33
23
#define BCMSWAP16(val) \
35
(((uint16)(val) & (uint16)0x00ffU) << 8) | \
36
(((uint16)(val) & (uint16)0xff00U) >> 8)))
24
((uint16)((((uint16)(val) & (uint16)0x00ffU) << 8) | \
25
(((uint16)(val) & (uint16)0xff00U) >> 8)))
38
27
#define BCMSWAP32(val) \
40
(((uint32)(val) & (uint32)0x000000ffUL) << 24) | \
41
(((uint32)(val) & (uint32)0x0000ff00UL) << 8) | \
42
(((uint32)(val) & (uint32)0x00ff0000UL) >> 8) | \
43
(((uint32)(val) & (uint32)0xff000000UL) >> 24)))
28
((uint32)((((uint32)(val) & (uint32)0x000000ffU) << 24) | \
29
(((uint32)(val) & (uint32)0x0000ff00U) << 8) | \
30
(((uint32)(val) & (uint32)0x00ff0000U) >> 8) | \
31
(((uint32)(val) & (uint32)0xff000000U) >> 24)))
45
33
#define BCMSWAP32BY16(val) \
47
(((uint32)(val) & (uint32)0x0000ffffUL) << 16) | \
48
(((uint32)(val) & (uint32)0xffff0000UL) >> 16)))
34
((uint32)((((uint32)(val) & (uint32)0x0000ffffU) << 16) | \
35
(((uint32)(val) & (uint32)0xffff0000U) >> 16)))
50
37
static INLINE uint16
51
38
bcmswap16(uint16 val)
80
67
#define HTON16(i) BCMSWAP16(i)
81
68
#define hton16(i) bcmswap16(i)
69
#define HTON32(i) BCMSWAP32(i)
82
70
#define hton32(i) bcmswap32(i)
71
#define NTOH16(i) BCMSWAP16(i)
83
72
#define ntoh16(i) bcmswap16(i)
73
#define NTOH32(i) BCMSWAP32(i)
84
74
#define ntoh32(i) bcmswap32(i)
85
76
#define ltoh16(i) (i)
86
78
#define ltoh32(i) (i)
87
80
#define htol16(i) (i)
88
82
#define htol32(i) (i)
95
89
htol16_ua_store(uint16 val, uint8 *bytes)
91
bytes[0] = val & 0xff;
101
95
static INLINE void
102
96
htol32_ua_store(uint32 val, uint8 *bytes)
105
bytes[1] = (val>>8)&0xff;
106
bytes[2] = (val>>16)&0xff;
98
bytes[0] = val & 0xff;
99
bytes[1] = (val >> 8) & 0xff;
100
bytes[2] = (val >> 16) & 0xff;
101
bytes[3] = val >> 24;
110
104
static INLINE void
111
105
hton16_ua_store(uint16 val, uint8 *bytes)
108
bytes[1] = val & 0xff;
117
111
static INLINE void
118
112
hton32_ua_store(uint32 val, uint8 *bytes)
121
bytes[2] = (val>>8)&0xff;
122
bytes[1] = (val>>16)&0xff;
127
ltoh16_ua(void *bytes)
129
return (((uint8*)bytes)[1]<<8)+((uint8 *)bytes)[0];
133
ltoh32_ua(void *bytes)
135
return (((uint8*)bytes)[3]<<24)+(((uint8*)bytes)[2]<<16)+
136
(((uint8*)bytes)[1]<<8)+((uint8*)bytes)[0];
140
ntoh16_ua(void *bytes)
142
return (((uint8*)bytes)[0]<<8)+((uint8*)bytes)[1];
146
ntoh32_ua(void *bytes)
148
return (((uint8*)bytes)[0]<<24)+(((uint8*)bytes)[1]<<16)+
149
(((uint8*)bytes)[2]<<8)+((uint8*)bytes)[3];
152
#define ltoh_ua(ptr) (\
153
sizeof(*(ptr)) == sizeof(uint8) ? *(uint8 *)ptr : \
154
sizeof(*(ptr)) == sizeof(uint16) ? (((uint8 *)ptr)[1]<<8)+((uint8 *)ptr)[0] : \
155
(((uint8 *)ptr)[3]<<24)+(((uint8 *)ptr)[2]<<16)+(((uint8 *)ptr)[1]<<8)+((uint8 *)ptr)[0] \
158
#define ntoh_ua(ptr) (\
159
sizeof(*(ptr)) == sizeof(uint8) ? *(uint8 *)ptr : \
160
sizeof(*(ptr)) == sizeof(uint16) ? (((uint8 *)ptr)[0]<<8)+((uint8 *)ptr)[1] : \
161
(((uint8 *)ptr)[0]<<24)+(((uint8 *)ptr)[1]<<16)+(((uint8 *)ptr)[2]<<8)+((uint8 *)ptr)[3] \
114
bytes[0] = val >> 24;
115
bytes[1] = (val >> 16) & 0xff;
116
bytes[2] = (val >> 8) & 0xff;
117
bytes[3] = val & 0xff;
120
#define _LTOH16_UA(cp) ((cp)[0] | ((cp)[1] << 8))
121
#define _LTOH32_UA(cp) ((cp)[0] | ((cp)[1] << 8) | ((cp)[2] << 16) | ((cp)[3] << 24))
122
#define _NTOH16_UA(cp) (((cp)[0] << 8) | (cp)[1])
123
#define _NTOH32_UA(cp) (((cp)[0] << 24) | ((cp)[1] << 16) | ((cp)[2] << 8) | (cp)[3])
126
ltoh16_ua(const void *bytes)
128
return _LTOH16_UA((const uint8 *)bytes);
132
ltoh32_ua(const void *bytes)
134
return _LTOH32_UA((const uint8 *)bytes);
138
ntoh16_ua(const void *bytes)
140
return _NTOH16_UA((const uint8 *)bytes);
144
ntoh32_ua(const void *bytes)
146
return _NTOH32_UA((const uint8 *)bytes);
149
#define ltoh_ua(ptr) \
150
(sizeof(*(ptr)) == sizeof(uint8) ? *(const uint8 *)ptr : \
151
sizeof(*(ptr)) == sizeof(uint16) ? _LTOH16_UA((const uint8 *)ptr) : \
152
sizeof(*(ptr)) == sizeof(uint32) ? _LTOH32_UA((const uint8 *)ptr) : \
155
#define ntoh_ua(ptr) \
156
(sizeof(*(ptr)) == sizeof(uint8) ? *(const uint8 *)ptr : \
157
sizeof(*(ptr)) == sizeof(uint16) ? _NTOH16_UA((const uint8 *)ptr) : \
158
sizeof(*(ptr)) == sizeof(uint32) ? _NTOH32_UA((const uint8 *)ptr) : \