Do not use diagnostic pragma when GCC version is lower than 4.6.0
[babeltrace.git] / include / babeltrace / bitfield.h
CommitLineData
d79865b9
MD
1#ifndef _BABELTRACE_BITFIELD_H
2#define _BABELTRACE_BITFIELD_H
6dc2ca62
MD
3
4/*
c15e0603 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 */
51950aa0 27#include <stdbool.h> /* C99 7.16 bool type */
c15e0603 28#include <babeltrace/compat/limits.h> /* C99 5.2.4.2 Numerical limits */
9b0422a0 29#include <babeltrace/endian.h> /* Non-standard BIG_ENDIAN, LITTLE_ENDIAN, BYTE_ORDER */
6dc2ca62 30
c15e0603
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
51950aa0
MD
44/*
45 * _bt_is_signed_type() willingly generates comparison of unsigned
46 * expression < 0, which is always false. Silence compiler warnings.
ea4c6f4c
JR
47 * GCC versions lower than 4.6.0 do not accept diagnostic pragma inside
48 * functions.
51950aa0 49 */
ea4c6f4c 50#if defined (__GNUC__) && (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
51950aa0
MD
51# define _BT_DIAG_PUSH _Pragma("GCC diagnostic push")
52# define _BT_DIAG_POP _Pragma("GCC diagnostic pop")
53
54# define _BT_DIAG_STRINGIFY_1(x) #x
55# define _BT_DIAG_STRINGIFY(x) _BT_DIAG_STRINGIFY_1(x)
56
57# define _BT_DIAG_IGNORE(option) \
58 _Pragma(_BT_DIAG_STRINGIFY(GCC diagnostic ignored option))
59# define _BT_DIAG_IGNORE_TYPE_LIMITS _BT_DIAG_IGNORE("-Wtype-limits")
60#else
61# define _BT_DIAG_PUSH
62# define _BT_DIAG_POP
63# define _BT_DIAG_IGNORE
452c28dd 64# define _BT_DIAG_IGNORE_TYPE_LIMITS
51950aa0
MD
65#endif
66
cca767be 67#define _bt_is_signed_type(type) ((type) -1 < (type) 0)
08228826 68
c15e0603
MD
69/*
70 * Produce a build-time error if the condition `cond` is non-zero.
71 * Evaluates as a size_t expression.
72 */
73#define _BT_BUILD_ASSERT(cond) \
74 sizeof(struct { int f:(2 * !!(cond) - 1); })
75
76/*
77 * Cast value `v` to an unsigned integer of the same size as `v`.
78 */
79#define _bt_cast_value_to_unsigned(v) \
80 (sizeof(v) == sizeof(uint8_t) ? (uint8_t) (v) : \
81 sizeof(v) == sizeof(uint16_t) ? (uint16_t) (v) : \
82 sizeof(v) == sizeof(uint32_t) ? (uint32_t) (v) : \
83 sizeof(v) == sizeof(uint64_t) ? (uint64_t) (v) : \
84 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
85
86/*
87 * Cast value `v` to an unsigned integer type of the size of type `type`
88 * *without* sign-extension.
89 *
90 * The unsigned cast ensures that we're not shifting a negative value,
91 * which is undefined in C. However, this limits the maximum type size
92 * of `type` to 64-bit. Generate a compile-time error if the size of
93 * `type` is larger than 64-bit.
94 */
95#define _bt_cast_value_to_unsigned_type(type, v) \
96 (sizeof(type) == sizeof(uint8_t) ? \
97 (uint8_t) _bt_cast_value_to_unsigned(v) : \
98 sizeof(type) == sizeof(uint16_t) ? \
99 (uint16_t) _bt_cast_value_to_unsigned(v) : \
100 sizeof(type) == sizeof(uint32_t) ? \
101 (uint32_t) _bt_cast_value_to_unsigned(v) : \
102 sizeof(type) == sizeof(uint64_t) ? \
103 (uint64_t) _bt_cast_value_to_unsigned(v) : \
104 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
105
106/*
107 * _bt_fill_mask evaluates to a "type" integer with all bits set.
108 */
109#define _bt_fill_mask(type) ((type) ~(type) 0)
110
111/*
112 * Left shift a value `v` of `shift` bits.
113 *
114 * The type of `v` can be signed or unsigned integer.
115 * The value of `shift` must be less than the size of `v` (in bits),
116 * otherwise the behavior is undefined.
117 * Evaluates to the result of the shift operation.
118 *
119 * According to the C99 standard, left shift of a left hand-side signed
120 * type is undefined if it has a negative value or if the result cannot
121 * be represented in the result type. This bitfield header discards the
122 * bits that are left-shifted beyond the result type representation,
123 * which is the behavior of an unsigned type left shift operation.
124 * Therefore, always perform left shift on an unsigned type.
125 *
126 * This macro should not be used if `shift` can be greater or equal than
127 * the bitwidth of `v`. See `_bt_safe_lshift`.
128 */
129#define _bt_lshift(v, shift) \
130 ((__typeof__(v)) (_bt_cast_value_to_unsigned(v) << (shift)))
131
132/*
133 * Generate a mask of type `type` with the `length` least significant bits
134 * cleared, and the most significant bits set.
135 */
136#define _bt_make_mask_complement(type, length) \
137 _bt_lshift(_bt_fill_mask(type), length)
138
139/*
140 * Generate a mask of type `type` with the `length` least significant bits
141 * set, and the most significant bits cleared.
142 */
143#define _bt_make_mask(type, length) \
144 ((type) ~_bt_make_mask_complement(type, length))
145
146/*
147 * Right shift a value `v` of `shift` bits.
148 *
149 * The type of `v` can be signed or unsigned integer.
150 * The value of `shift` must be less than the size of `v` (in bits),
151 * otherwise the behavior is undefined.
152 * Evaluates to the result of the shift operation.
153 *
154 * According to the C99 standard, right shift of a left hand-side signed
155 * type which has a negative value is implementation defined. This
156 * bitfield header relies on the right shift implementation carrying the
157 * sign bit. If the compiler implementation has a different behavior,
158 * emulate carrying the sign bit.
159 *
160 * This macro should not be used if `shift` can be greater or equal than
161 * the bitwidth of `v`. See `_bt_safe_rshift`.
162 */
163#if ((-1 >> 1) == -1)
164#define _bt_rshift(v, shift) ((v) >> (shift))
165#else
166#define _bt_rshift(v, shift) \
167 ((__typeof__(v)) ((_bt_cast_value_to_unsigned(v) >> (shift)) | \
168 ((v) < 0 ? _bt_make_mask_complement(__typeof__(v), \
169 sizeof(v) * CHAR_BIT - (shift)) : 0)))
170#endif
171
172/*
173 * Right shift a signed or unsigned integer with `shift` value being an
174 * arbitrary number of bits. `v` is modified by this macro. The shift
175 * is transformed into a sequence of `_nr_partial_shifts` consecutive
176 * shift operations, each of a number of bits smaller than the bitwidth
177 * of `v`, ending with a shift of the number of left over bits.
178 */
179#define _bt_safe_rshift(v, shift) \
180do { \
181 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
182 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
183 \
184 for (; _nr_partial_shifts; _nr_partial_shifts--) \
185 (v) = _bt_rshift(v, sizeof(v) * CHAR_BIT - 1); \
186 (v) = _bt_rshift(v, _leftover_bits); \
187} while (0)
188
189/*
190 * Left shift a signed or unsigned integer with `shift` value being an
191 * arbitrary number of bits. `v` is modified by this macro. The shift
192 * is transformed into a sequence of `_nr_partial_shifts` consecutive
193 * shift operations, each of a number of bits smaller than the bitwidth
194 * of `v`, ending with a shift of the number of left over bits.
195 */
196#define _bt_safe_lshift(v, shift) \
197do { \
198 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
199 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
200 \
201 for (; _nr_partial_shifts; _nr_partial_shifts--) \
202 (v) = _bt_lshift(v, sizeof(v) * CHAR_BIT - 1); \
203 (v) = _bt_lshift(v, _leftover_bits); \
204} while (0)
08228826 205
6dc2ca62 206/*
d79865b9 207 * bt_bitfield_write - write integer to a bitfield in native endianness
6dc2ca62
MD
208 *
209 * Save integer to the bitfield, which starts at the "start" bit, has "len"
210 * bits.
211 * The inside of a bitfield is from high bits to low bits.
212 * Uses native endianness.
213 * For unsigned "v", pad MSB with 0 if bitfield is larger than v.
214 * For signed "v", sign-extend v if bitfield is larger than v.
215 *
216 * On little endian, bytes are placed from the less significant to the most
217 * significant. Also, consecutive bitfields are placed from lower bits to higher
218 * bits.
219 *
220 * On big endian, bytes are places from most significant to less significant.
221 * Also, consecutive bitfields are placed from higher to lower bits.
222 */
223
51950aa0 224#define _bt_bitfield_write_le(ptr, type, start, length, v) \
6dc2ca62 225do { \
51950aa0
MD
226 __typeof__(v) _v = (v); \
227 type *_ptr = (void *) (ptr); \
228 unsigned long _start = (start), _length = (length); \
229 type _mask, _cmask; \
230 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
231 unsigned long _start_unit, _end_unit, _this_unit; \
232 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
6dc2ca62 233 \
51950aa0 234 if (!_length) \
6dc2ca62
MD
235 break; \
236 \
51950aa0
MD
237 _end = _start + _length; \
238 _start_unit = _start / _ts; \
239 _end_unit = (_end + (_ts - 1)) / _ts; \
6dc2ca62
MD
240 \
241 /* Trim v high bits */ \
51950aa0
MD
242 if (_length < sizeof(_v) * CHAR_BIT) \
243 _v &= _bt_make_mask(__typeof__(_v), _length); \
6dc2ca62
MD
244 \
245 /* We can now append v with a simple "or", shift it piece-wise */ \
51950aa0
MD
246 _this_unit = _start_unit; \
247 if (_start_unit == _end_unit - 1) { \
248 _mask = _bt_make_mask(type, _start % _ts); \
249 if (_end % _ts) \
250 _mask |= _bt_make_mask_complement(type, _end % _ts); \
251 _cmask = _bt_lshift((type) (_v), _start % _ts); \
252 _cmask &= ~_mask; \
253 _ptr[_this_unit] &= _mask; \
254 _ptr[_this_unit] |= _cmask; \
6dc2ca62
MD
255 break; \
256 } \
51950aa0
MD
257 if (_start % _ts) { \
258 _cshift = _start % _ts; \
259 _mask = _bt_make_mask(type, _cshift); \
260 _cmask = _bt_lshift((type) (_v), _cshift); \
261 _cmask &= ~_mask; \
262 _ptr[_this_unit] &= _mask; \
263 _ptr[_this_unit] |= _cmask; \
264 _bt_safe_rshift(_v, _ts - _cshift); \
265 _start += _ts - _cshift; \
266 _this_unit++; \
6dc2ca62 267 } \
51950aa0
MD
268 for (; _this_unit < _end_unit - 1; _this_unit++) { \
269 _ptr[_this_unit] = (type) _v; \
270 _bt_safe_rshift(_v, _ts); \
271 _start += _ts; \
6dc2ca62 272 } \
51950aa0
MD
273 if (_end % _ts) { \
274 _mask = _bt_make_mask_complement(type, _end % _ts); \
275 _cmask = (type) _v; \
276 _cmask &= ~_mask; \
277 _ptr[_this_unit] &= _mask; \
278 _ptr[_this_unit] |= _cmask; \
6dc2ca62 279 } else \
51950aa0 280 _ptr[_this_unit] = (type) _v; \
6dc2ca62
MD
281} while (0)
282
51950aa0 283#define _bt_bitfield_write_be(ptr, type, start, length, v) \
6dc2ca62 284do { \
51950aa0
MD
285 __typeof__(v) _v = (v); \
286 type *_ptr = (void *) (ptr); \
287 unsigned long _start = (start), _length = (length); \
288 type _mask, _cmask; \
289 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
290 unsigned long _start_unit, _end_unit, _this_unit; \
291 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
6dc2ca62 292 \
51950aa0 293 if (!_length) \
6dc2ca62
MD
294 break; \
295 \
51950aa0
MD
296 _end = _start + _length; \
297 _start_unit = _start / _ts; \
298 _end_unit = (_end + (_ts - 1)) / _ts; \
6dc2ca62
MD
299 \
300 /* Trim v high bits */ \
51950aa0
MD
301 if (_length < sizeof(_v) * CHAR_BIT) \
302 _v &= _bt_make_mask(__typeof__(_v), _length); \
6dc2ca62
MD
303 \
304 /* We can now append v with a simple "or", shift it piece-wise */ \
51950aa0
MD
305 _this_unit = _end_unit - 1; \
306 if (_start_unit == _end_unit - 1) { \
307 _mask = _bt_make_mask(type, (_ts - (_end % _ts)) % _ts); \
308 if (_start % _ts) \
309 _mask |= _bt_make_mask_complement(type, _ts - (_start % _ts)); \
310 _cmask = _bt_lshift((type) (_v), (_ts - (_end % _ts)) % _ts); \
311 _cmask &= ~_mask; \
312 _ptr[_this_unit] &= _mask; \
313 _ptr[_this_unit] |= _cmask; \
6dc2ca62
MD
314 break; \
315 } \
51950aa0
MD
316 if (_end % _ts) { \
317 _cshift = _end % _ts; \
318 _mask = _bt_make_mask(type, _ts - _cshift); \
319 _cmask = _bt_lshift((type) (_v), _ts - _cshift); \
320 _cmask &= ~_mask; \
321 _ptr[_this_unit] &= _mask; \
322 _ptr[_this_unit] |= _cmask; \
323 _bt_safe_rshift(_v, _cshift); \
324 _end -= _cshift; \
325 _this_unit--; \
6dc2ca62 326 } \
51950aa0
MD
327 for (; (long) _this_unit >= (long) _start_unit + 1; _this_unit--) { \
328 _ptr[_this_unit] = (type) _v; \
329 _bt_safe_rshift(_v, _ts); \
330 _end -= _ts; \
6dc2ca62 331 } \
51950aa0
MD
332 if (_start % _ts) { \
333 _mask = _bt_make_mask_complement(type, _ts - (_start % _ts)); \
334 _cmask = (type) _v; \
335 _cmask &= ~_mask; \
336 _ptr[_this_unit] &= _mask; \
337 _ptr[_this_unit] |= _cmask; \
6dc2ca62 338 } else \
51950aa0 339 _ptr[_this_unit] = (type) _v; \
6dc2ca62
MD
340} while (0)
341
342/*
d79865b9
MD
343 * bt_bitfield_write - write integer to a bitfield in native endianness
344 * bt_bitfield_write_le - write integer to a bitfield in little endian
345 * bt_bitfield_write_be - write integer to a bitfield in big endian
6dc2ca62
MD
346 */
347
348#if (BYTE_ORDER == LITTLE_ENDIAN)
349
51950aa0
MD
350#define bt_bitfield_write(ptr, type, start, length, v) \
351 _bt_bitfield_write_le(ptr, type, start, length, v)
6dc2ca62 352
51950aa0
MD
353#define bt_bitfield_write_le(ptr, type, start, length, v) \
354 _bt_bitfield_write_le(ptr, type, start, length, v)
c15e0603 355
51950aa0
MD
356#define bt_bitfield_write_be(ptr, type, start, length, v) \
357 _bt_bitfield_write_be(ptr, unsigned char, start, length, v)
6dc2ca62
MD
358
359#elif (BYTE_ORDER == BIG_ENDIAN)
360
51950aa0
MD
361#define bt_bitfield_write(ptr, type, start, length, v) \
362 _bt_bitfield_write_be(ptr, type, start, length, v)
6dc2ca62 363
51950aa0
MD
364#define bt_bitfield_write_le(ptr, type, start, length, v) \
365 _bt_bitfield_write_le(ptr, unsigned char, start, length, v)
c15e0603 366
51950aa0
MD
367#define bt_bitfield_write_be(ptr, type, start, length, v) \
368 _bt_bitfield_write_be(ptr, type, start, length, v)
6dc2ca62
MD
369
370#else /* (BYTE_ORDER == PDP_ENDIAN) */
371
372#error "Byte order not supported"
373
374#endif
375
51950aa0 376#define _bt_bitfield_read_le(ptr, type, start, length, vptr) \
6dc2ca62 377do { \
51950aa0
MD
378 __typeof__(*(vptr)) *_vptr = (vptr); \
379 __typeof__(*_vptr) _v; \
380 type *_ptr = (void *) (ptr); \
381 unsigned long _start = (start), _length = (length); \
382 type _mask, _cmask; \
383 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
384 unsigned long _start_unit, _end_unit, _this_unit; \
385 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
386 bool _is_signed_type; \
08228826 387 \
51950aa0
MD
388 if (!_length) { \
389 *_vptr = 0; \
08228826
MD
390 break; \
391 } \
392 \
51950aa0
MD
393 _end = _start + _length; \
394 _start_unit = _start / _ts; \
395 _end_unit = (_end + (_ts - 1)) / _ts; \
6a7b3345 396 \
51950aa0
MD
397 _this_unit = _end_unit - 1; \
398 _BT_DIAG_PUSH \
399 _BT_DIAG_IGNORE_TYPE_LIMITS \
400 _is_signed_type = _bt_is_signed_type(__typeof__(_v)); \
401 _BT_DIAG_POP \
402 if (_is_signed_type \
403 && (_ptr[_this_unit] & _bt_lshift((type) 1, (_end % _ts ? _end % _ts : _ts) - 1))) \
404 _v = ~(__typeof__(_v)) 0; \
6a7b3345 405 else \
51950aa0
MD
406 _v = 0; \
407 if (_start_unit == _end_unit - 1) { \
408 _cmask = _ptr[_this_unit]; \
409 _cmask = _bt_rshift(_cmask, _start % _ts); \
410 if ((_end - _start) % _ts) { \
411 _mask = _bt_make_mask(type, _end - _start); \
412 _cmask &= _mask; \
6a7b3345 413 } \
51950aa0
MD
414 _bt_safe_lshift(_v, _end - _start); \
415 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
416 *_vptr = _v; \
6a7b3345
MD
417 break; \
418 } \
51950aa0
MD
419 if (_end % _ts) { \
420 _cshift = _end % _ts; \
421 _mask = _bt_make_mask(type, _cshift); \
422 _cmask = _ptr[_this_unit]; \
423 _cmask &= _mask; \
424 _bt_safe_lshift(_v, _cshift); \
425 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
426 _end -= _cshift; \
427 _this_unit--; \
6a7b3345 428 } \
51950aa0
MD
429 for (; (long) _this_unit >= (long) _start_unit + 1; _this_unit--) { \
430 _bt_safe_lshift(_v, _ts); \
431 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
432 _end -= _ts; \
6a7b3345 433 } \
51950aa0
MD
434 if (_start % _ts) { \
435 _mask = _bt_make_mask(type, _ts - (_start % _ts)); \
436 _cmask = _ptr[_this_unit]; \
437 _cmask = _bt_rshift(_cmask, _start % _ts); \
438 _cmask &= _mask; \
439 _bt_safe_lshift(_v, _ts - (_start % _ts)); \
440 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
6a7b3345 441 } else { \
51950aa0
MD
442 _bt_safe_lshift(_v, _ts); \
443 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
6a7b3345 444 } \
51950aa0 445 *_vptr = _v; \
08228826
MD
446} while (0)
447
51950aa0 448#define _bt_bitfield_read_be(ptr, type, start, length, vptr) \
08228826 449do { \
51950aa0
MD
450 __typeof__(*(vptr)) *_vptr = (vptr); \
451 __typeof__(*_vptr) _v; \
452 type *_ptr = (void *) (ptr); \
453 unsigned long _start = (start), _length = (length); \
454 type _mask, _cmask; \
455 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
456 unsigned long _start_unit, _end_unit, _this_unit; \
457 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
458 bool _is_signed_type; \
6dc2ca62 459 \
51950aa0
MD
460 if (!_length) { \
461 *_vptr = 0; \
6dc2ca62 462 break; \
08228826 463 } \
6dc2ca62 464 \
51950aa0
MD
465 _end = _start + _length; \
466 _start_unit = _start / _ts; \
467 _end_unit = (_end + (_ts - 1)) / _ts; \
6a7b3345 468 \
51950aa0
MD
469 _this_unit = _start_unit; \
470 _BT_DIAG_PUSH \
471 _BT_DIAG_IGNORE_TYPE_LIMITS \
472 _is_signed_type = _bt_is_signed_type(__typeof__(_v)); \
473 _BT_DIAG_POP \
474 if (_is_signed_type \
475 && (_ptr[_this_unit] & _bt_lshift((type) 1, _ts - (_start % _ts) - 1))) \
476 _v = ~(__typeof__(_v)) 0; \
6a7b3345 477 else \
51950aa0
MD
478 _v = 0; \
479 if (_start_unit == _end_unit - 1) { \
480 _cmask = _ptr[_this_unit]; \
481 _cmask = _bt_rshift(_cmask, (_ts - (_end % _ts)) % _ts); \
482 if ((_end - _start) % _ts) { \
483 _mask = _bt_make_mask(type, _end - _start); \
484 _cmask &= _mask; \
6a7b3345 485 } \
51950aa0
MD
486 _bt_safe_lshift(_v, _end - _start); \
487 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
488 *_vptr = _v; \
6a7b3345
MD
489 break; \
490 } \
51950aa0
MD
491 if (_start % _ts) { \
492 _cshift = _start % _ts; \
493 _mask = _bt_make_mask(type, _ts - _cshift); \
494 _cmask = _ptr[_this_unit]; \
495 _cmask &= _mask; \
496 _bt_safe_lshift(_v, _ts - _cshift); \
497 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
498 _start += _ts - _cshift; \
499 _this_unit++; \
6a7b3345 500 } \
51950aa0
MD
501 for (; _this_unit < _end_unit - 1; _this_unit++) { \
502 _bt_safe_lshift(_v, _ts); \
503 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
504 _start += _ts; \
6a7b3345 505 } \
51950aa0
MD
506 if (_end % _ts) { \
507 _mask = _bt_make_mask(type, _end % _ts); \
508 _cmask = _ptr[_this_unit]; \
509 _cmask = _bt_rshift(_cmask, _ts - (_end % _ts)); \
510 _cmask &= _mask; \
511 _bt_safe_lshift(_v, _end % _ts); \
512 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
6a7b3345 513 } else { \
51950aa0
MD
514 _bt_safe_lshift(_v, _ts); \
515 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
6a7b3345 516 } \
51950aa0 517 *_vptr = _v; \
6dc2ca62
MD
518} while (0)
519
6dc2ca62 520/*
d79865b9
MD
521 * bt_bitfield_read - read integer from a bitfield in native endianness
522 * bt_bitfield_read_le - read integer from a bitfield in little endian
523 * bt_bitfield_read_be - read integer from a bitfield in big endian
6dc2ca62
MD
524 */
525
08228826
MD
526#if (BYTE_ORDER == LITTLE_ENDIAN)
527
51950aa0
MD
528#define bt_bitfield_read(ptr, type, start, length, vptr) \
529 _bt_bitfield_read_le(ptr, type, start, length, vptr)
08228826 530
51950aa0
MD
531#define bt_bitfield_read_le(ptr, type, start, length, vptr) \
532 _bt_bitfield_read_le(ptr, type, start, length, vptr)
c15e0603 533
51950aa0
MD
534#define bt_bitfield_read_be(ptr, type, start, length, vptr) \
535 _bt_bitfield_read_be(ptr, unsigned char, start, length, vptr)
08228826
MD
536
537#elif (BYTE_ORDER == BIG_ENDIAN)
538
51950aa0
MD
539#define bt_bitfield_read(ptr, type, start, length, vptr) \
540 _bt_bitfield_read_be(ptr, type, start, length, vptr)
08228826 541
51950aa0
MD
542#define bt_bitfield_read_le(ptr, type, start, length, vptr) \
543 _bt_bitfield_read_le(ptr, unsigned char, start, length, vptr)
c15e0603 544
51950aa0
MD
545#define bt_bitfield_read_be(ptr, type, start, length, vptr) \
546 _bt_bitfield_read_be(ptr, type, start, length, vptr)
08228826
MD
547
548#else /* (BYTE_ORDER == PDP_ENDIAN) */
549
550#error "Byte order not supported"
551
552#endif
6dc2ca62 553
d79865b9 554#endif /* _BABELTRACE_BITFIELD_H */
This page took 0.056525 seconds and 4 git commands to generate.