Silence compiler "always false comparison" warning
[babeltrace.git] / include / babeltrace / bitfield-internal.h
CommitLineData
d79865b9
MD
1#ifndef _BABELTRACE_BITFIELD_H
2#define _BABELTRACE_BITFIELD_H
6dc2ca62
MD
3
4/*
7f140ae8 5 * Copyright 2010-2019 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6dc2ca62
MD
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
c462e188
MD
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
6dc2ca62
MD
24 */
25
26#include <stdint.h> /* C99 5.2.4.2 Numerical limits */
fd2c04ed 27#include <stdbool.h> /* C99 7.16 bool type */
3d9990ac 28#include <babeltrace/compat/limits-internal.h> /* C99 5.2.4.2 Numerical limits */
3d9990ac 29#include <babeltrace/endian-internal.h> /* Non-standard BIG_ENDIAN, LITTLE_ENDIAN, BYTE_ORDER */
6dc2ca62 30
7f140ae8
MD
31/*
32 * This header strictly follows the C99 standard, except for use of the
33 * compiler-specific __typeof__.
34 */
35
36/*
37 * This bitfield header requires the compiler representation of signed
38 * integers to be two's complement.
39 */
40#if (-1 != ~0)
41#error "bitfield.h requires the compiler representation of signed integers to be two's complement."
42#endif
08228826 43
fd2c04ed
MD
44/*
45 * _bt_is_signed_type() willingly generates comparison of unsigned
46 * expression < 0, which is always false. Silence compiler warnings.
47 */
48#ifdef __GNUC__
49# define _BT_DIAG_PUSH _Pragma("GCC diagnostic push")
50# define _BT_DIAG_POP _Pragma("GCC diagnostic pop")
51
52# define _BT_DIAG_STRINGIFY_1(x) #x
53# define _BT_DIAG_STRINGIFY(x) _BT_DIAG_STRINGIFY_1(x)
54
55# define _BT_DIAG_IGNORE(option) \
56 _Pragma(_BT_DIAG_STRINGIFY(GCC diagnostic ignored option))
57# define _BT_DIAG_IGNORE_TYPE_LIMITS _BT_DIAG_IGNORE("-Wtype-limits")
58#else
59# define _BT_DIAG_PUSH
60# define _BT_DIAG_POP
61# define _BT_DIAG_IGNORE
62#endif
63
cca767be 64#define _bt_is_signed_type(type) ((type) -1 < (type) 0)
08228826 65
a2d16120 66/*
7f140ae8
MD
67 * Produce a build-time error if the condition `cond` is non-zero.
68 * Evaluates as a size_t expression.
69 */
70#define _BT_BUILD_ASSERT(cond) \
71 sizeof(struct { int f:(2 * !!(cond) - 1); })
72
73/*
74 * Cast value `v` to an unsigned integer of the same size as `v`.
75 */
76#define _bt_cast_value_to_unsigned(v) \
77 (sizeof(v) == sizeof(uint8_t) ? (uint8_t) (v) : \
78 sizeof(v) == sizeof(uint16_t) ? (uint16_t) (v) : \
79 sizeof(v) == sizeof(uint32_t) ? (uint32_t) (v) : \
80 sizeof(v) == sizeof(uint64_t) ? (uint64_t) (v) : \
81 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
82
83/*
84 * Cast value `v` to an unsigned integer type of the size of type `type`
85 * *without* sign-extension.
86 *
87 * The unsigned cast ensures that we're not shifting a negative value,
88 * which is undefined in C. However, this limits the maximum type size
89 * of `type` to 64-bit. Generate a compile-time error if the size of
90 * `type` is larger than 64-bit.
91 */
92#define _bt_cast_value_to_unsigned_type(type, v) \
93 (sizeof(type) == sizeof(uint8_t) ? \
94 (uint8_t) _bt_cast_value_to_unsigned(v) : \
95 sizeof(type) == sizeof(uint16_t) ? \
96 (uint16_t) _bt_cast_value_to_unsigned(v) : \
97 sizeof(type) == sizeof(uint32_t) ? \
98 (uint32_t) _bt_cast_value_to_unsigned(v) : \
99 sizeof(type) == sizeof(uint64_t) ? \
100 (uint64_t) _bt_cast_value_to_unsigned(v) : \
101 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
102
103/*
104 * _bt_fill_mask evaluates to a "type" integer with all bits set.
105 */
106#define _bt_fill_mask(type) ((type) ~(type) 0)
107
108/*
109 * Left shift a value `v` of `shift` bits.
110 *
111 * The type of `v` can be signed or unsigned integer.
112 * The value of `shift` must be less than the size of `v` (in bits),
113 * otherwise the behavior is undefined.
114 * Evaluates to the result of the shift operation.
115 *
116 * According to the C99 standard, left shift of a left hand-side signed
117 * type is undefined if it has a negative value or if the result cannot
118 * be represented in the result type. This bitfield header discards the
119 * bits that are left-shifted beyond the result type representation,
120 * which is the behavior of an unsigned type left shift operation.
121 * Therefore, always perform left shift on an unsigned type.
122 *
123 * This macro should not be used if `shift` can be greater or equal than
124 * the bitwidth of `v`. See `_bt_safe_lshift`.
125 */
126#define _bt_lshift(v, shift) \
127 ((__typeof__(v)) (_bt_cast_value_to_unsigned(v) << (shift)))
128
129/*
130 * Generate a mask of type `type` with the `length` least significant bits
131 * cleared, and the most significant bits set.
a2d16120 132 */
7f140ae8
MD
133#define _bt_make_mask_complement(type, length) \
134 _bt_lshift(_bt_fill_mask(type), length)
08228826 135
7f140ae8
MD
136/*
137 * Generate a mask of type `type` with the `length` least significant bits
138 * set, and the most significant bits cleared.
139 */
140#define _bt_make_mask(type, length) \
141 ((type) ~_bt_make_mask_complement(type, length))
142
143/*
144 * Right shift a value `v` of `shift` bits.
145 *
146 * The type of `v` can be signed or unsigned integer.
147 * The value of `shift` must be less than the size of `v` (in bits),
148 * otherwise the behavior is undefined.
149 * Evaluates to the result of the shift operation.
150 *
151 * According to the C99 standard, right shift of a left hand-side signed
152 * type which has a negative value is implementation defined. This
153 * bitfield header relies on the right shift implementation carrying the
154 * sign bit. If the compiler implementation has a different behavior,
155 * emulate carrying the sign bit.
156 *
157 * This macro should not be used if `shift` can be greater or equal than
158 * the bitwidth of `v`. See `_bt_safe_rshift`.
159 */
160#if ((-1 >> 1) == -1)
161#define _bt_rshift(v, shift) ((v) >> (shift))
162#else
163#define _bt_rshift(v, shift) \
164 ((__typeof__(v)) ((_bt_cast_value_to_unsigned(v) >> (shift)) | \
165 ((v) < 0 ? _bt_make_mask_complement(__typeof__(v), \
166 sizeof(v) * CHAR_BIT - (shift)) : 0)))
167#endif
168
169/*
170 * Right shift a signed or unsigned integer with `shift` value being an
171 * arbitrary number of bits. `v` is modified by this macro. The shift
172 * is transformed into a sequence of `_nr_partial_shifts` consecutive
173 * shift operations, each of a number of bits smaller than the bitwidth
174 * of `v`, ending with a shift of the number of left over bits.
175 */
176#define _bt_safe_rshift(v, shift) \
177do { \
178 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
179 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
180 \
181 for (; _nr_partial_shifts; _nr_partial_shifts--) \
182 (v) = _bt_rshift(v, sizeof(v) * CHAR_BIT - 1); \
183 (v) = _bt_rshift(v, _leftover_bits); \
184} while (0)
185
186/*
187 * Left shift a signed or unsigned integer with `shift` value being an
188 * arbitrary number of bits. `v` is modified by this macro. The shift
189 * is transformed into a sequence of `_nr_partial_shifts` consecutive
190 * shift operations, each of a number of bits smaller than the bitwidth
191 * of `v`, ending with a shift of the number of left over bits.
192 */
193#define _bt_safe_lshift(v, shift) \
194do { \
195 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
196 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
197 \
198 for (; _nr_partial_shifts; _nr_partial_shifts--) \
199 (v) = _bt_lshift(v, sizeof(v) * CHAR_BIT - 1); \
200 (v) = _bt_lshift(v, _leftover_bits); \
201} while (0)
a2d16120 202
6dc2ca62 203/*
d79865b9 204 * bt_bitfield_write - write integer to a bitfield in native endianness
6dc2ca62
MD
205 *
206 * Save integer to the bitfield, which starts at the "start" bit, has "len"
207 * bits.
208 * The inside of a bitfield is from high bits to low bits.
209 * Uses native endianness.
210 * For unsigned "v", pad MSB with 0 if bitfield is larger than v.
211 * For signed "v", sign-extend v if bitfield is larger than v.
212 *
213 * On little endian, bytes are placed from the less significant to the most
214 * significant. Also, consecutive bitfields are placed from lower bits to higher
215 * bits.
216 *
217 * On big endian, bytes are places from most significant to less significant.
218 * Also, consecutive bitfields are placed from higher to lower bits.
219 */
220
5f61bf21 221#define _bt_bitfield_write_le(_ptr, type, _start, _length, _v) \
6dc2ca62 222do { \
7f140ae8 223 __typeof__(_v) __v = (_v); \
9128428b 224 type *__ptr = (void *) (_ptr); \
5f61bf21 225 unsigned long __start = (_start), __length = (_length); \
47e0f2e2
MD
226 type mask, cmask; \
227 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
6dc2ca62
MD
228 unsigned long start_unit, end_unit, this_unit; \
229 unsigned long end, cshift; /* cshift is "complement shift" */ \
6dc2ca62 230 \
5f61bf21 231 if (!__length) \
6dc2ca62
MD
232 break; \
233 \
5f61bf21
MD
234 end = __start + __length; \
235 start_unit = __start / ts; \
6dc2ca62
MD
236 end_unit = (end + (ts - 1)) / ts; \
237 \
238 /* Trim v high bits */ \
5f61bf21 239 if (__length < sizeof(__v) * CHAR_BIT) \
7f140ae8 240 __v &= _bt_make_mask(__typeof__(__v), __length); \
6dc2ca62
MD
241 \
242 /* We can now append v with a simple "or", shift it piece-wise */ \
243 this_unit = start_unit; \
244 if (start_unit == end_unit - 1) { \
7f140ae8 245 mask = _bt_make_mask(type, __start % ts); \
6dc2ca62 246 if (end % ts) \
7f140ae8
MD
247 mask |= _bt_make_mask_complement(type, end % ts); \
248 cmask = _bt_lshift((type) (__v), __start % ts); \
08228826 249 cmask &= ~mask; \
5f61bf21
MD
250 __ptr[this_unit] &= mask; \
251 __ptr[this_unit] |= cmask; \
6dc2ca62
MD
252 break; \
253 } \
5f61bf21
MD
254 if (__start % ts) { \
255 cshift = __start % ts; \
7f140ae8
MD
256 mask = _bt_make_mask(type, cshift); \
257 cmask = _bt_lshift((type) (__v), cshift); \
08228826 258 cmask &= ~mask; \
5f61bf21
MD
259 __ptr[this_unit] &= mask; \
260 __ptr[this_unit] |= cmask; \
7f140ae8 261 _bt_safe_rshift(__v, ts - cshift); \
5f61bf21 262 __start += ts - cshift; \
6dc2ca62
MD
263 this_unit++; \
264 } \
265 for (; this_unit < end_unit - 1; this_unit++) { \
5f61bf21 266 __ptr[this_unit] = (type) __v; \
7f140ae8 267 _bt_safe_rshift(__v, ts); \
5f61bf21 268 __start += ts; \
6dc2ca62
MD
269 } \
270 if (end % ts) { \
7f140ae8 271 mask = _bt_make_mask_complement(type, end % ts); \
5f61bf21 272 cmask = (type) __v; \
08228826 273 cmask &= ~mask; \
5f61bf21
MD
274 __ptr[this_unit] &= mask; \
275 __ptr[this_unit] |= cmask; \
6dc2ca62 276 } else \
5f61bf21 277 __ptr[this_unit] = (type) __v; \
6dc2ca62
MD
278} while (0)
279
5f61bf21 280#define _bt_bitfield_write_be(_ptr, type, _start, _length, _v) \
6dc2ca62 281do { \
7f140ae8 282 __typeof__(_v) __v = (_v); \
9128428b 283 type *__ptr = (void *) (_ptr); \
5f61bf21 284 unsigned long __start = (_start), __length = (_length); \
47e0f2e2
MD
285 type mask, cmask; \
286 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
6dc2ca62
MD
287 unsigned long start_unit, end_unit, this_unit; \
288 unsigned long end, cshift; /* cshift is "complement shift" */ \
6dc2ca62 289 \
5f61bf21 290 if (!__length) \
6dc2ca62
MD
291 break; \
292 \
5f61bf21
MD
293 end = __start + __length; \
294 start_unit = __start / ts; \
6dc2ca62
MD
295 end_unit = (end + (ts - 1)) / ts; \
296 \
297 /* Trim v high bits */ \
5f61bf21 298 if (__length < sizeof(__v) * CHAR_BIT) \
7f140ae8 299 __v &= _bt_make_mask(__typeof__(__v), __length); \
6dc2ca62
MD
300 \
301 /* We can now append v with a simple "or", shift it piece-wise */ \
302 this_unit = end_unit - 1; \
303 if (start_unit == end_unit - 1) { \
7f140ae8 304 mask = _bt_make_mask(type, (ts - (end % ts)) % ts); \
5f61bf21 305 if (__start % ts) \
7f140ae8
MD
306 mask |= _bt_make_mask_complement(type, ts - (__start % ts)); \
307 cmask = _bt_lshift((type) (__v), (ts - (end % ts)) % ts); \
08228826 308 cmask &= ~mask; \
5f61bf21
MD
309 __ptr[this_unit] &= mask; \
310 __ptr[this_unit] |= cmask; \
6dc2ca62
MD
311 break; \
312 } \
313 if (end % ts) { \
314 cshift = end % ts; \
7f140ae8
MD
315 mask = _bt_make_mask(type, ts - cshift); \
316 cmask = _bt_lshift((type) (__v), ts - cshift); \
08228826 317 cmask &= ~mask; \
5f61bf21
MD
318 __ptr[this_unit] &= mask; \
319 __ptr[this_unit] |= cmask; \
7f140ae8 320 _bt_safe_rshift(__v, cshift); \
6dc2ca62
MD
321 end -= cshift; \
322 this_unit--; \
323 } \
08228826 324 for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \
5f61bf21 325 __ptr[this_unit] = (type) __v; \
7f140ae8 326 _bt_safe_rshift(__v, ts); \
6dc2ca62
MD
327 end -= ts; \
328 } \
5f61bf21 329 if (__start % ts) { \
7f140ae8 330 mask = _bt_make_mask_complement(type, ts - (__start % ts)); \
5f61bf21 331 cmask = (type) __v; \
08228826 332 cmask &= ~mask; \
5f61bf21
MD
333 __ptr[this_unit] &= mask; \
334 __ptr[this_unit] |= cmask; \
6dc2ca62 335 } else \
5f61bf21 336 __ptr[this_unit] = (type) __v; \
6dc2ca62
MD
337} while (0)
338
339/*
d79865b9
MD
340 * bt_bitfield_write - write integer to a bitfield in native endianness
341 * bt_bitfield_write_le - write integer to a bitfield in little endian
342 * bt_bitfield_write_be - write integer to a bitfield in big endian
6dc2ca62
MD
343 */
344
345#if (BYTE_ORDER == LITTLE_ENDIAN)
346
47e0f2e2
MD
347#define bt_bitfield_write(ptr, type, _start, _length, _v) \
348 _bt_bitfield_write_le(ptr, type, _start, _length, _v)
6dc2ca62 349
47e0f2e2
MD
350#define bt_bitfield_write_le(ptr, type, _start, _length, _v) \
351 _bt_bitfield_write_le(ptr, type, _start, _length, _v)
a2d16120 352
47e0f2e2
MD
353#define bt_bitfield_write_be(ptr, type, _start, _length, _v) \
354 _bt_bitfield_write_be(ptr, unsigned char, _start, _length, _v)
6dc2ca62
MD
355
356#elif (BYTE_ORDER == BIG_ENDIAN)
357
47e0f2e2
MD
358#define bt_bitfield_write(ptr, type, _start, _length, _v) \
359 _bt_bitfield_write_be(ptr, type, _start, _length, _v)
6dc2ca62 360
47e0f2e2
MD
361#define bt_bitfield_write_le(ptr, type, _start, _length, _v) \
362 _bt_bitfield_write_le(ptr, unsigned char, _start, _length, _v)
a2d16120 363
47e0f2e2
MD
364#define bt_bitfield_write_be(ptr, type, _start, _length, _v) \
365 _bt_bitfield_write_be(ptr, type, _start, _length, _v)
6dc2ca62
MD
366
367#else /* (BYTE_ORDER == PDP_ENDIAN) */
368
369#error "Byte order not supported"
370
371#endif
372
5f61bf21 373#define _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
6dc2ca62 374do { \
7f140ae8
MD
375 __typeof__(*(_vptr)) *__vptr = (_vptr); \
376 __typeof__(*__vptr) __v; \
9128428b 377 type *__ptr = (void *) (_ptr); \
5f61bf21 378 unsigned long __start = (_start), __length = (_length); \
47e0f2e2
MD
379 type mask, cmask; \
380 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
6dc2ca62
MD
381 unsigned long start_unit, end_unit, this_unit; \
382 unsigned long end, cshift; /* cshift is "complement shift" */ \
fd2c04ed 383 bool is_signed_type; \
08228826 384 \
5f61bf21
MD
385 if (!__length) { \
386 *__vptr = 0; \
08228826
MD
387 break; \
388 } \
389 \
5f61bf21
MD
390 end = __start + __length; \
391 start_unit = __start / ts; \
08228826 392 end_unit = (end + (ts - 1)) / ts; \
6a7b3345
MD
393 \
394 this_unit = end_unit - 1; \
fd2c04ed
MD
395 _BT_DIAG_PUSH \
396 _BT_DIAG_IGNORE_TYPE_LIMITS \
397 is_signed_type = _bt_is_signed_type(__typeof__(__v)); \
398 _BT_DIAG_POP \
399 if (is_signed_type \
7f140ae8
MD
400 && (__ptr[this_unit] & _bt_lshift((type) 1, (end % ts ? end % ts : ts) - 1))) \
401 __v = ~(__typeof__(__v)) 0; \
6a7b3345 402 else \
5f61bf21 403 __v = 0; \
6a7b3345 404 if (start_unit == end_unit - 1) { \
5f61bf21 405 cmask = __ptr[this_unit]; \
7f140ae8 406 cmask = _bt_rshift(cmask, __start % ts); \
5f61bf21 407 if ((end - __start) % ts) { \
7f140ae8 408 mask = _bt_make_mask(type, end - __start); \
6a7b3345
MD
409 cmask &= mask; \
410 } \
7f140ae8
MD
411 _bt_safe_lshift(__v, end - __start); \
412 __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), cmask); \
5f61bf21 413 *__vptr = __v; \
6a7b3345
MD
414 break; \
415 } \
416 if (end % ts) { \
417 cshift = end % ts; \
7f140ae8 418 mask = _bt_make_mask(type, cshift); \
5f61bf21 419 cmask = __ptr[this_unit]; \
6a7b3345 420 cmask &= mask; \
7f140ae8
MD
421 _bt_safe_lshift(__v, cshift); \
422 __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), cmask); \
6a7b3345
MD
423 end -= cshift; \
424 this_unit--; \
425 } \
08228826 426 for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \
7f140ae8
MD
427 _bt_safe_lshift(__v, ts); \
428 __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), __ptr[this_unit]); \
6a7b3345
MD
429 end -= ts; \
430 } \
5f61bf21 431 if (__start % ts) { \
7f140ae8 432 mask = _bt_make_mask(type, ts - (__start % ts)); \
5f61bf21 433 cmask = __ptr[this_unit]; \
7f140ae8 434 cmask = _bt_rshift(cmask, __start % ts); \
6a7b3345 435 cmask &= mask; \
7f140ae8
MD
436 _bt_safe_lshift(__v, ts - (__start % ts)); \
437 __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), cmask); \
6a7b3345 438 } else { \
7f140ae8
MD
439 _bt_safe_lshift(__v, ts); \
440 __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), __ptr[this_unit]); \
6a7b3345 441 } \
5f61bf21 442 *__vptr = __v; \
08228826
MD
443} while (0)
444
5f61bf21 445#define _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
08228826 446do { \
7f140ae8
MD
447 __typeof__(*(_vptr)) *__vptr = (_vptr); \
448 __typeof__(*__vptr) __v; \
9128428b 449 type *__ptr = (void *) (_ptr); \
5f61bf21 450 unsigned long __start = (_start), __length = (_length); \
47e0f2e2
MD
451 type mask, cmask; \
452 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
08228826
MD
453 unsigned long start_unit, end_unit, this_unit; \
454 unsigned long end, cshift; /* cshift is "complement shift" */ \
fd2c04ed 455 bool is_signed_type; \
6dc2ca62 456 \
5f61bf21
MD
457 if (!__length) { \
458 *__vptr = 0; \
6dc2ca62 459 break; \
08228826 460 } \
6dc2ca62 461 \
5f61bf21
MD
462 end = __start + __length; \
463 start_unit = __start / ts; \
6dc2ca62 464 end_unit = (end + (ts - 1)) / ts; \
6a7b3345 465 \
08228826 466 this_unit = start_unit; \
fd2c04ed
MD
467 _BT_DIAG_PUSH \
468 _BT_DIAG_IGNORE_TYPE_LIMITS \
469 is_signed_type = _bt_is_signed_type(__typeof__(__v)); \
470 _BT_DIAG_POP \
471 if (is_signed_type \
7f140ae8
MD
472 && (__ptr[this_unit] & _bt_lshift((type) 1, ts - (__start % ts) - 1))) \
473 __v = ~(__typeof__(__v)) 0; \
6a7b3345 474 else \
5f61bf21 475 __v = 0; \
6a7b3345 476 if (start_unit == end_unit - 1) { \
5f61bf21 477 cmask = __ptr[this_unit]; \
7f140ae8 478 cmask = _bt_rshift(cmask, (ts - (end % ts)) % ts); \
5f61bf21 479 if ((end - __start) % ts) { \
7f140ae8 480 mask = _bt_make_mask(type, end - __start); \
6a7b3345
MD
481 cmask &= mask; \
482 } \
7f140ae8
MD
483 _bt_safe_lshift(__v, end - __start); \
484 __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), cmask); \
5f61bf21 485 *__vptr = __v; \
6a7b3345
MD
486 break; \
487 } \
5f61bf21
MD
488 if (__start % ts) { \
489 cshift = __start % ts; \
7f140ae8 490 mask = _bt_make_mask(type, ts - cshift); \
5f61bf21 491 cmask = __ptr[this_unit]; \
6a7b3345 492 cmask &= mask; \
7f140ae8
MD
493 _bt_safe_lshift(__v, ts - cshift); \
494 __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), cmask); \
5f61bf21 495 __start += ts - cshift; \
6a7b3345
MD
496 this_unit++; \
497 } \
498 for (; this_unit < end_unit - 1; this_unit++) { \
7f140ae8
MD
499 _bt_safe_lshift(__v, ts); \
500 __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), __ptr[this_unit]); \
5f61bf21 501 __start += ts; \
6a7b3345
MD
502 } \
503 if (end % ts) { \
7f140ae8 504 mask = _bt_make_mask(type, end % ts); \
5f61bf21 505 cmask = __ptr[this_unit]; \
7f140ae8 506 cmask = _bt_rshift(cmask, ts - (end % ts)); \
6a7b3345 507 cmask &= mask; \
7f140ae8
MD
508 _bt_safe_lshift(__v, end % ts); \
509 __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), cmask); \
6a7b3345 510 } else { \
7f140ae8
MD
511 _bt_safe_lshift(__v, ts); \
512 __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), __ptr[this_unit]); \
6a7b3345 513 } \
5f61bf21 514 *__vptr = __v; \
6dc2ca62
MD
515} while (0)
516
6dc2ca62 517/*
d79865b9
MD
518 * bt_bitfield_read - read integer from a bitfield in native endianness
519 * bt_bitfield_read_le - read integer from a bitfield in little endian
520 * bt_bitfield_read_be - read integer from a bitfield in big endian
6dc2ca62
MD
521 */
522
08228826
MD
523#if (BYTE_ORDER == LITTLE_ENDIAN)
524
5f61bf21
MD
525#define bt_bitfield_read(_ptr, type, _start, _length, _vptr) \
526 _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr)
08228826 527
5f61bf21
MD
528#define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
529 _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr)
a2d16120 530
5f61bf21
MD
531#define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
532 _bt_bitfield_read_be(_ptr, unsigned char, _start, _length, _vptr)
08228826
MD
533
534#elif (BYTE_ORDER == BIG_ENDIAN)
535
5f61bf21
MD
536#define bt_bitfield_read(_ptr, type, _start, _length, _vptr) \
537 _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr)
08228826 538
5f61bf21
MD
539#define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
540 _bt_bitfield_read_le(_ptr, unsigned char, _start, _length, _vptr)
a2d16120 541
5f61bf21
MD
542#define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
543 _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr)
08228826
MD
544
545#else /* (BYTE_ORDER == PDP_ENDIAN) */
546
547#error "Byte order not supported"
548
549#endif
6dc2ca62 550
d79865b9 551#endif /* _BABELTRACE_BITFIELD_H */
This page took 0.079438 seconds and 4 git commands to generate.