aboutsummaryrefslogtreecommitdiff
path: root/include/odp_byteorder.h
blob: e0f7a17f229ef3ac3c1c3aab59177b9fff80aeaa (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* Copyright (c) 2014, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */


/**
 * @file
 *
 * ODP byteorder
 */

#ifndef ODP_BYTEORDER_H_
#define ODP_BYTEORDER_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <endian.h>
#include <odp_std_types.h>
#include <odp_compiler.h>

#ifndef __BYTE_ORDER
#error __BYTE_ORDER not defined!
#endif

#ifndef __BIG_ENDIAN
#error __BIG_ENDIAN not defined!
#endif

#ifndef __LITTLE_ENDIAN
#error __LITTLE_ENDIAN not defined!
#endif

/** Big endian byte order */
#define ODP_BIG_ENDIAN    __BIG_ENDIAN

/** Little endian byte order */
#define ODP_LITTLE_ENDIAN __LITTLE_ENDIAN

/** Selected byte order */
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define ODP_BYTE_ORDER ODP_LITTLE_ENDIAN
#elif __BYTE_ORDER == __BIG_ENDIAN
#define ODP_BYTE_ORDER ODP_BIG_ENDIAN
#endif


/* for use with type checkers such as sparse */
#ifdef __CHECKER__
/** @internal bitwise attribute */
#define __odp_bitwise	__attribute__((bitwise))
/** @internal force attribute */
#define __odp_force     __attribute__((force))
#else
/** @internal bitwise attribute */
#define __odp_bitwise
/** @internal force attribute */
#define __odp_force
#endif


typedef uint16_t __odp_bitwise	uint16le_t; /**< unsigned 16bit little endian */
typedef uint16_t __odp_bitwise	uint16be_t; /**< unsigned 16bit big endian */

typedef uint32_t __odp_bitwise	uint32le_t; /**< unsigned 32bit little endian */
typedef uint32_t __odp_bitwise	uint32be_t; /**< unsigned 32bit big endian */

typedef uint64_t __odp_bitwise	uint64le_t; /**< unsigned 64bit little endian */
typedef uint64_t __odp_bitwise	uint64be_t; /**< unsigned 64bit big endian */

typedef uint16_t __odp_bitwise  uint16sum_t; /**< unsigned 16bit bitwise */
typedef uint32_t __odp_bitwise  uint32sum_t; /**< unsigned 32bit bitwise */
/*
 * Big Endian -> CPU byte order:
 */

/**
 * Convert 16bit big endian to cpu native uint16_t
 * @param be16  big endian 16bit
 * @return  cpu native uint16_t
 */
static inline uint16_t odp_be_to_cpu_16(uint16be_t be16)
{
#if ODP_BYTE_ORDER == ODP_LITTLE_ENDIAN
	return __odp_builtin_bswap16((__odp_force uint16_t)be16);
#else
	return (__odp_force uint16_t)be16;
#endif
}

/**
 * Convert 32bit big endian to cpu native uint32_t
 * @param be32  big endian 32bit
 * @return  cpu native uint32_t
 */
static inline uint32_t odp_be_to_cpu_32(uint32be_t be32)
{
#if ODP_BYTE_ORDER == ODP_LITTLE_ENDIAN
	return __builtin_bswap32((__odp_force uint32_t)be32);
#else
	return (__odp_force uint32_t)be32;
#endif
}

/**
 * Convert 64bit big endian to cpu native uint64_t
 * @param be64  big endian 64bit
 * @return  cpu native uint64_t
 */
static inline uint64_t odp_be_to_cpu_64(uint64be_t be64)
{
#if ODP_BYTE_ORDER == ODP_LITTLE_ENDIAN
	return __builtin_bswap64((__odp_force uint64_t)be64);
#else
	return (__odp_force uint64_t)be64;
#endif
}


/*
 * CPU byte order -> Big Endian:
 */

/**
 * Convert cpu native uint16_t to 16bit big endian
 * @param cpu16  uint16_t in cpu native format
 * @return  big endian 16bit
 */
static inline uint16be_t odp_cpu_to_be_16(uint16_t cpu16)
{
#if ODP_BYTE_ORDER == ODP_LITTLE_ENDIAN
	return (__odp_force uint16be_t)__odp_builtin_bswap16(cpu16);
#else
	return (__odp_force uint16be_t)cpu16;
#endif
}

/**
 * Convert cpu native uint32_t to 32bit big endian
 * @param cpu32  uint32_t in cpu native format
 * @return  big endian 32bit
 */
static inline uint32be_t odp_cpu_to_be_32(uint32_t cpu32)
{
#if ODP_BYTE_ORDER == ODP_LITTLE_ENDIAN
	return (__odp_force uint32be_t)__builtin_bswap32(cpu32);
#else
	return (__odp_force uint32be_t)cpu32;
#endif
}

/**
 * Convert cpu native uint64_t to 64bit big endian
 * @param cpu64  uint64_t in cpu native format
 * @return  big endian 64bit
 */
static inline uint64be_t odp_cpu_to_be_64(uint64_t cpu64)
{
#if ODP_BYTE_ORDER == ODP_LITTLE_ENDIAN
	return (__odp_force uint64be_t)__builtin_bswap64(cpu64);
#else
	return (__odp_force uint64be_t)cpu64;
#endif
}


/*
 * Little Endian -> CPU byte order:
 */

/**
 * Convert 16bit little endian to cpu native uint16_t
 * @param le16  little endian 16bit
 * @return  cpu native uint16_t
 */
static inline uint16_t odp_le_to_cpu_16(uint16le_t le16)
{
#if ODP_BYTE_ORDER == ODP_LITTLE_ENDIAN
	return (__odp_force uint16_t)le16;
#else
	return __odp_builtin_bswap16((__odp_force uint16_t)le16);
#endif
}

/**
 * Convert 32bit little endian to cpu native uint32_t
 * @param le32  little endian 32bit
 * @return  cpu native uint32_t
 */
static inline uint32_t odp_le_to_cpu_32(uint32le_t le32)
{
#if ODP_BYTE_ORDER == ODP_LITTLE_ENDIAN
	return (__odp_force uint32_t)le32;
#else
	return __builtin_bswap32((__odp_force uint32_t)le32);
#endif
}

/**
 * Convert 64bit little endian to cpu native uint64_t
 * @param le64  little endian 64bit
 * @return  cpu native uint64_t
 */
static inline uint64_t odp_le_to_cpu_64(uint64le_t le64)
{
#if ODP_BYTE_ORDER == ODP_LITTLE_ENDIAN
	return (__odp_force uint64_t)le64;
#else
	return __builtin_bswap64((__odp_force uint64_t)le64);
#endif
}


/*
 * CPU byte order -> Little Endian:
 */

/**
 * Convert cpu native uint16_t to 16bit little endian
 * @param cpu16  uint16_t in cpu native format
 * @return  little endian 16bit
 */
static inline uint16le_t odp_cpu_to_le_16(uint16_t cpu16)
{
#if ODP_BYTE_ORDER == ODP_LITTLE_ENDIAN
	return (__odp_force uint16le_t)cpu16;
#else
	return (__odp_force uint16le_t)__odp_builtin_bswap16(cpu16);
#endif
}

/**
 * Convert cpu native uint32_t to 32bit little endian
 * @param cpu32  uint32_t in cpu native format
 * @return  little endian 32bit
 */
static inline uint32le_t odp_cpu_to_le_32(uint32_t cpu32)
{
#if ODP_BYTE_ORDER == ODP_LITTLE_ENDIAN
	return (__odp_force uint32le_t)cpu32;
#else
	return (__odp_force uint32le_t)__builtin_bswap32(cpu32);
#endif
}

/**
 * Convert cpu native uint64_t to 64bit little endian
 * @param cpu64  uint64_t in cpu native format
 * @return  little endian 64bit
 */
static inline uint64le_t odp_cpu_to_le_64(uint64_t cpu64)
{
#if ODP_BYTE_ORDER == ODP_LITTLE_ENDIAN
	return (__odp_force uint64le_t)cpu64;
#else
	return (__odp_force uint64le_t)__builtin_bswap64(cpu64);
#endif
}


#ifdef __cplusplus
}
#endif

#endif