Coverage Report

Created: 2025-06-15 00:57

/src/cmp_tool/lib/common/byteorder.h
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * @file   byteorder.h
3
 * @author Armin Luntzer (armin.luntzer@univie.ac.at)
4
 * @date   2015
5
 *
6
 * @copyright GPLv2
7
 * This program is free software; you can redistribute it and/or modify it
8
 * under the terms and conditions of the GNU General Public License,
9
 * version 2, as published by the Free Software Foundation.
10
 *
11
 * This program is distributed in the hope it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14
 * more details.
15
 *
16
 * @brief This is a set of macros for consistent endianness conversion. They work
17
 *  for both little and big endian cpus.
18
 *
19
 * conversion of XX-bit integers (16- or 32-) between native CPU format
20
 * and little/big endian format:
21
 *  cpu_to_[bl]eXX(uintXX_t x)
22
 *  [bl]eXX_to_cpu(uintXX_t x)
23
 *
24
 * the same, but change in situ:
25
 *  cpu_to_[bl]eXXs(uintXX_t x)
26
 *  [bl]eXX_to_cpus(uintXX_t x)
27
 *
28
 *
29
 * This is based on the byte order macros from the linux kernel, see:
30
 * include/linux/byteorder/generic.h
31
 * include/uapi/linux/swab.h
32
 * include/uapi/linux/byteorder/big_endian.h
33
 * include/uapi/linux/byteorder/little_endian.h
34
 * by @author Linus Torvalds et al.
35
 *
36
 */
37
#ifndef BYTEORDER_H
38
#define BYTEORDER_H
39
40
#include <stdint.h>
41
42
#ifndef GCC_VERSION
43
#define GCC_VERSION (__GNUC__ * 10000   \
44
         + __GNUC_MINOR__ * 100 \
45
         + __GNUC_PATCHLEVEL__)
46
#endif
47
48
#ifdef __BIG_ENDIAN
49
#undef __BIG_ENDIAN
50
#endif
51
52
#ifdef __LITTLE_ENDIAN
53
#undef __LITTLE_ENDIAN
54
#endif
55
56
#ifndef __BIG_ENDIAN
57
#  if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
58
#    define __BIG_ENDIAN 4321
59
#  elif defined(__clang__) && defined(__BIG_ENDIAN__)
60
#    define __BIG_ENDIAN 4321
61
#  elif defined(__sparc__)
62
#    define __BIG_ENDIAN 4321
63
#  endif
64
#endif
65
66
#ifndef __LITTLE_ENDIAN
67
#  if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
68
#    define __LITTLE_ENDIAN 1234
69
#  elif defined(__clang__) & defined(__LITTLE_ENDIAN__)
70
#    define __LITTLE_ENDIAN 1234
71
#  elif defined(_MSC_VER) && (_M_AMD64 || _M_IX86)
72
#    define __LITTLE_ENDIAN 1234
73
#  elif defined(__i386__) || defined(__x86_64__)
74
#    define __LITTLE_ENDIAN 1234
75
#  endif
76
#endif
77
78
#if defined(__BIG_ENDIAN) == defined(__LITTLE_ENDIAN)
79
#error "Unknown byte order!"
80
#endif
81
82
83
0
#define ___constant_swab16(x) ((uint16_t)(      \
84
0
  (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) |    \
85
0
  (((uint16_t)(x) & (uint16_t)0xff00U) >> 8)))
86
87
0
#define ___constant_swab32(x) ((uint32_t)(      \
88
0
  (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) |  \
89
0
  (((uint32_t)(x) & (uint32_t)0x0000ff00UL) <<  8) |  \
90
0
  (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >>  8) |  \
91
0
  (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24)))
92
93
0
#define ___constant_swab64(x) ((uint64_t)(        \
94
0
  (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \
95
0
  (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
96
0
  (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
97
0
  (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) <<  8) | \
98
0
  (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >>  8) | \
99
0
  (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
100
0
  (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
101
0
  (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56)))
102
103
#if GCC_VERSION >= 40400 || defined(__clang__)
104
#define __HAVE_BUILTIN_BSWAP32__
105
#define __HAVE_BUILTIN_BSWAP64__
106
#endif
107
#if GCC_VERSION >= 40800 || defined(__clang__)
108
#define __HAVE_BUILTIN_BSWAP16__
109
#endif /* USE_BUILTIN_BSWAP */
110
111
112
static __inline __attribute__((const)) uint16_t __fswab16(uint16_t val)
113
140k
{
114
140k
#ifdef __HAVE_BUILTIN_BSWAP16__
115
140k
  return __builtin_bswap16(val);
116
#else
117
  return ___constant_swab16(val);
118
#endif
119
140k
}
cmp_icu.c:__fswab16
Line
Count
Source
113
7.37k
{
114
7.37k
#ifdef __HAVE_BUILTIN_BSWAP16__
115
7.37k
  return __builtin_bswap16(val);
116
#else
117
  return ___constant_swab16(val);
118
#endif
119
7.37k
}
cmp_data_types.c:__fswab16
Line
Count
Source
113
129k
{
114
129k
#ifdef __HAVE_BUILTIN_BSWAP16__
115
129k
  return __builtin_bswap16(val);
116
#else
117
  return ___constant_swab16(val);
118
#endif
119
129k
}
cmp_entity.c:__fswab16
Line
Count
Source
113
3.10k
{
114
3.10k
#ifdef __HAVE_BUILTIN_BSWAP16__
115
3.10k
  return __builtin_bswap16(val);
116
#else
117
  return ___constant_swab16(val);
118
#endif
119
3.10k
}
120
121
122
static __inline __attribute__((const)) uint32_t __fswab32(uint32_t val)
123
305k
{
124
305k
#ifdef __HAVE_BUILTIN_BSWAP32__
125
305k
  return __builtin_bswap32(val);
126
#else
127
  return ___constant_swab32(val);
128
#endif
129
305k
}
cmp_icu.c:__fswab32
Line
Count
Source
123
247k
{
124
247k
#ifdef __HAVE_BUILTIN_BSWAP32__
125
247k
  return __builtin_bswap32(val);
126
#else
127
  return ___constant_swab32(val);
128
#endif
129
247k
}
cmp_data_types.c:__fswab32
Line
Count
Source
123
54.9k
{
124
54.9k
#ifdef __HAVE_BUILTIN_BSWAP32__
125
54.9k
  return __builtin_bswap32(val);
126
#else
127
  return ___constant_swab32(val);
128
#endif
129
54.9k
}
cmp_entity.c:__fswab32
Line
Count
Source
123
3.32k
{
124
3.32k
#ifdef __HAVE_BUILTIN_BSWAP32__
125
3.32k
  return __builtin_bswap32(val);
126
#else
127
  return ___constant_swab32(val);
128
#endif
129
3.32k
}
130
131
132
static __inline __attribute__((const)) uint64_t __fswab64(uint64_t val)
133
402
{
134
402
#ifdef __HAVE_BUILTIN_BSWAP64__
135
402
  return __builtin_bswap64(val);
136
#else
137
  return ___constant_swab64(val);
138
#endif
139
402
}
Unexecuted instantiation: cmp_icu.c:__fswab64
Unexecuted instantiation: cmp_data_types.c:__fswab64
cmp_entity.c:__fswab64
Line
Count
Source
133
402
{
134
402
#ifdef __HAVE_BUILTIN_BSWAP64__
135
402
  return __builtin_bswap64(val);
136
#else
137
  return ___constant_swab64(val);
138
#endif
139
402
}
140
141
/**
142
 * @brief return a byteswapped 16-bit value
143
 * @param x value to byteswap
144
 */
145
146
#define __swab16(x)       \
147
140k
  (__builtin_constant_p((uint16_t)(x)) ?  \
148
140k
  ___constant_swab16(x) :     \
149
140k
  __fswab16(x))
150
151
152
/**
153
 * @brief return a byteswapped 32-bit value
154
 * @param x a value to byteswap
155
 */
156
157
#define __swab32(x)       \
158
305k
  (__builtin_constant_p((uint32_t)(x)) ?  \
159
305k
  ___constant_swab32(x) :     \
160
305k
  __fswab32(x))
161
162
163
/**
164
 * @brief return a byteswapped 64-bit value
165
 * @param x a value to byteswap
166
 */
167
168
#define __swab64(x)       \
169
402
  (__builtin_constant_p((uint64_t)(x)) ?  \
170
402
  ___constant_swab64(x) :     \
171
402
  __fswab64(x))
172
173
/**
174
 * @brief return a byteswapped 16-bit value from a pointer
175
 * @param p a pointer to a naturally-aligned 16-bit value
176
 */
177
static __inline uint16_t __swab16p(const uint16_t *p)
178
0
{
179
0
  return __swab16(*p);
180
0
}
Unexecuted instantiation: cmp_icu.c:__swab16p
Unexecuted instantiation: cmp_data_types.c:__swab16p
Unexecuted instantiation: cmp_entity.c:__swab16p
181
182
183
/**
184
 * @brief return a byteswapped 32-bit value from a pointer
185
 * @param p a pointer to a naturally-aligned 32-bit value
186
 */
187
static __inline uint32_t __swab32p(const uint32_t *p)
188
0
{
189
0
  return __swab32(*p);
190
0
}
Unexecuted instantiation: cmp_icu.c:__swab32p
Unexecuted instantiation: cmp_data_types.c:__swab32p
Unexecuted instantiation: cmp_entity.c:__swab32p
191
192
193
/**
194
 * @brief return a byteswapped 64-bit value from a pointer
195
 * @param p a pointer to a naturally-aligned 64-bit value
196
 */
197
static __inline uint64_t __swab64p(const uint64_t *p)
198
0
{
199
0
  return __swab64(*p);
200
0
}
Unexecuted instantiation: cmp_icu.c:__swab64p
Unexecuted instantiation: cmp_data_types.c:__swab64p
Unexecuted instantiation: cmp_entity.c:__swab64p
201
202
203
/**
204
 * @brief byteswap a 16-bit value in-place
205
 * @param p a pointer to a naturally-aligned 16-bit value
206
 */
207
208
static __inline void __swab16s(uint16_t *p)
209
0
{
210
0
  *p = __swab16p(p);
211
0
}
Unexecuted instantiation: cmp_icu.c:__swab16s
Unexecuted instantiation: cmp_data_types.c:__swab16s
Unexecuted instantiation: cmp_entity.c:__swab16s
212
213
214
/**
215
 * @brief byteswap a 32-bit value in-place
216
 * @param p a pointer to a naturally-aligned 32-bit value
217
 */
218
219
static __inline void __swab32s(uint32_t *p)
220
0
{
221
0
  *p = __swab32p(p);
222
0
}
Unexecuted instantiation: cmp_icu.c:__swab32s
Unexecuted instantiation: cmp_data_types.c:__swab32s
Unexecuted instantiation: cmp_entity.c:__swab32s
223
224
225
/**
226
 * @brief byteswap a 64-bit value in-place
227
 * @param p a pointer to a naturally-aligned 64-bit value
228
 */
229
230
static __inline void __swab64s(uint64_t *p)
231
0
{
232
0
  *p = __swab64p(p);
233
0
}
Unexecuted instantiation: cmp_icu.c:__swab64s
Unexecuted instantiation: cmp_data_types.c:__swab64s
Unexecuted instantiation: cmp_entity.c:__swab64s
234
235
236
#ifdef __BIG_ENDIAN
237
238
#define __cpu_to_le16(x)   ((uint16_t)__swab16((x)))
239
#define __cpu_to_le32(x)   ((uint32_t)__swab32((x)))
240
#define __cpu_to_le64(x)   ((uint64_t)__swab64((x)))
241
242
#define __cpu_to_le16s(x)  __swab16s((x))
243
#define __cpu_to_le32s(x)  __swab32s((x))
244
#define __cpu_to_le64s(x)  __swab64s((x))
245
246
#define __cpu_to_be16(x)   ((uint16_t)(x))
247
#define __cpu_to_be32(x)   ((uint32_t)(x))
248
#define __cpu_to_be64(x)   ((uint64_t)(x))
249
250
#define __cpu_to_be16s(x)  { (void)(x); }
251
#define __cpu_to_be32s(x)  { (void)(x); }
252
#define __cpu_to_be64s(x)  { (void)(x); }
253
254
255
256
#define __le16_to_cpu(x)   __swab16((uint16_t)(x))
257
#define __le32_to_cpu(x)   __swab32((uint32_t)(x))
258
#define __le64_to_cpu(x)   __swab64((uint64_t)(x))
259
260
#define __le16_to_cpus(x)  __swab16s((x))
261
#define __le32_to_cpus(x)  __swab32s((x))
262
#define __le64_to_cpus(x)  __swab64s((x))
263
264
#define __be16_to_cpu(x)   ((uint16_t)(x))
265
#define __be32_to_cpu(x)   ((uint32_t)(x))
266
#define __be64_to_cpu(x)   ((uint64_t)(x))
267
268
#define __be16_to_cpus(x)  { (void)(x); }
269
#define __be32_to_cpus(x)  { (void)(x); }
270
#define __be64_to_cpus(x)  { (void)(x); }
271
272
#endif /* __BIG_ENDIAN */
273
274
275
#ifdef __LITTLE_ENDIAN
276
277
#define __cpu_to_le16(x)   ((uint16_t)(x))
278
#define __cpu_to_le32(x)   ((uint32_t)(x))
279
#define __cpu_to_le64(x)   ((uint64_t)(x))
280
281
#define __cpu_to_le16s(x)  { (void)(x); }
282
#define __cpu_to_le32s(x)  { (void)(x); }
283
#define __cpu_to_le64s(x)  { (void)(x); }
284
285
10.4k
#define __cpu_to_be16(x)   ((uint16_t)__swab16((x)))
286
127k
#define __cpu_to_be32(x)   ((uint32_t)__swab32((x)))
287
402
#define __cpu_to_be64(x)   ((uint64_t)__swab64((x)))
288
289
#define __cpu_to_be16s(x)  __swab16s((x))
290
#define __cpu_to_be32s(x)  __swab32s((x))
291
#define __cpu_to_be64s(x)  __swab64s((x))
292
293
294
295
#define __le16_to_cpu(x)  ((uint16_t)(x))
296
#define __le32_to_cpu(x)  ((uint32_t)(x))
297
#define __le64_to_cpu(x)  ((uint64_t)(x))
298
299
#define __le64_to_cpus(x) { (void)(x); }
300
#define __le32_to_cpus(x) { (void)(x); }
301
#define __le16_to_cpus(x) { (void)(x); }
302
303
129k
#define __be16_to_cpu(x)  __swab16((uint16_t)(uint16_t)(x))
304
178k
#define __be32_to_cpu(x)  __swab32((uint32_t)(uint32_t)(x))
305
0
#define __be64_to_cpu(x)  __swab64((uint64_t)(uint64_t)(x))
306
307
#define __be16_to_cpus(x) __swab16s((x))
308
0
#define __be32_to_cpus(x) __swab32s((x))
309
#define __be64_to_cpus(x) __swab64s((x))
310
311
#endif /* __LITTLE_ENDIAN */
312
313
314
315
/** these are the conversion macros */
316
317
/** convert cpu order to little endian */
318
#define cpu_to_le16  __cpu_to_le16
319
#define cpu_to_le32  __cpu_to_le32
320
#define cpu_to_le64  __cpu_to_le64
321
322
/** in-place convert cpu order to little endian */
323
#define cpu_to_le16s __cpu_to_le16s
324
#define cpu_to_le32s __cpu_to_le32s
325
#define cpu_to_le64s __cpu_to_le64s
326
327
/** convert cpu order to big endian */
328
10.4k
#define cpu_to_be16  __cpu_to_be16
329
127k
#define cpu_to_be32  __cpu_to_be32
330
402
#define cpu_to_be64  __cpu_to_be64
331
332
/** in-place convert cpu order to big endian */
333
#define cpu_to_be16s __cpu_to_be16s
334
#define cpu_to_be32s __cpu_to_be32s
335
#define cpu_to_be64s __cpu_to_be64s
336
337
338
/* same, but in reverse */
339
340
/** convert little endian to cpu order*/
341
#define le16_to_cpu  __le16_to_cpu
342
#define le32_to_cpu  __le32_to_cpu
343
#define le64_to_cpu  __le64_to_cpu
344
345
/** in-place convert little endian to cpu order*/
346
#define le16_to_cpus __le16_to_cpus
347
#define le32_to_cpus __le32_to_cpus
348
#define le64_to_cpus __le64_to_cpus
349
350
/** convert big endian to cpu order*/
351
129k
#define be16_to_cpu  __be16_to_cpu
352
178k
#define be32_to_cpu  __be32_to_cpu
353
0
#define be64_to_cpu  __be64_to_cpu
354
355
/** in-place convert big endian to cpu order*/
356
#define be16_to_cpus __be16_to_cpus
357
0
#define be32_to_cpus __be32_to_cpus
358
#define be64_to_cpus __be64_to_cpus
359
360
361
362
#endif /* BYTEORDER_H */