1 /* SPDX-License-Identifier: MIT
3 * Copyright 2010-2019 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 #ifndef _BABELTRACE_BITFIELD_H
7 #define _BABELTRACE_BITFIELD_H
9 #include <linux/types.h>
10 #include <lttng-endian.h>
17 * This header strictly follows the C99 standard, except for use of the
18 * compiler-specific __typeof__.
22 * This bitfield header requires the compiler representation of signed
23 * integers to be two's complement.
26 #error "bitfield.h requires the compiler representation of signed integers to be two's complement."
30 * _bt_is_signed_type() willingly generates comparison of unsigned
31 * expression < 0, which is always false. Silence compiler warnings.
32 * GCC versions lower than 4.6.0 do not accept diagnostic pragma inside
35 #if defined(__GNUC__) && (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
36 # define _BT_DIAG_PUSH _Pragma("GCC diagnostic push")
37 # define _BT_DIAG_POP _Pragma("GCC diagnostic pop")
39 # define _BT_DIAG_STRINGIFY_1(x) #x
40 # define _BT_DIAG_STRINGIFY(x) _BT_DIAG_STRINGIFY_1(x)
42 # define _BT_DIAG_IGNORE(option) \
43 _Pragma(_BT_DIAG_STRINGIFY(GCC diagnostic ignored option))
44 # define _BT_DIAG_IGNORE_TYPE_LIMITS _BT_DIAG_IGNORE("-Wtype-limits")
46 # define _BT_DIAG_PUSH
48 # define _BT_DIAG_IGNORE
49 # define _BT_DIAG_IGNORE_TYPE_LIMITS
52 #define _bt_is_signed_type(type) ((type) -1 < (type) 0)
55 * Produce a build-time error if the condition `cond` is non-zero.
56 * Evaluates as a size_t expression.
58 #define _BT_BUILD_ASSERT(cond) \
59 sizeof(struct { int f:(2 * !!(cond) - 1); })
62 * Cast value `v` to an unsigned integer of the same size as `v`.
64 #define _bt_cast_value_to_unsigned(v) \
65 (sizeof(v) == sizeof(uint8_t) ? (uint8_t) (v) : \
66 sizeof(v) == sizeof(uint16_t) ? (uint16_t) (v) : \
67 sizeof(v) == sizeof(uint32_t) ? (uint32_t) (v) : \
68 sizeof(v) == sizeof(uint64_t) ? (uint64_t) (v) : \
69 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
72 * Cast value `v` to an unsigned integer type of the size of type `type`
73 * *without* sign-extension.
75 * The unsigned cast ensures that we're not shifting a negative value,
76 * which is undefined in C. However, this limits the maximum type size
77 * of `type` to 64-bit. Generate a compile-time error if the size of
78 * `type` is larger than 64-bit.
80 #define _bt_cast_value_to_unsigned_type(type, v) \
81 (sizeof(type) == sizeof(uint8_t) ? \
82 (uint8_t) _bt_cast_value_to_unsigned(v) : \
83 sizeof(type) == sizeof(uint16_t) ? \
84 (uint16_t) _bt_cast_value_to_unsigned(v) : \
85 sizeof(type) == sizeof(uint32_t) ? \
86 (uint32_t) _bt_cast_value_to_unsigned(v) : \
87 sizeof(type) == sizeof(uint64_t) ? \
88 (uint64_t) _bt_cast_value_to_unsigned(v) : \
89 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
92 * _bt_fill_mask evaluates to a "type" integer with all bits set.
94 #define _bt_fill_mask(type) ((type) ~(type) 0)
97 * Left shift a value `v` of `shift` bits.
99 * The type of `v` can be signed or unsigned integer.
100 * The value of `shift` must be less than the size of `v` (in bits),
101 * otherwise the behavior is undefined.
102 * Evaluates to the result of the shift operation.
104 * According to the C99 standard, left shift of a left hand-side signed
105 * type is undefined if it has a negative value or if the result cannot
106 * be represented in the result type. This bitfield header discards the
107 * bits that are left-shifted beyond the result type representation,
108 * which is the behavior of an unsigned type left shift operation.
109 * Therefore, always perform left shift on an unsigned type.
111 * This macro should not be used if `shift` can be greater or equal than
112 * the bitwidth of `v`. See `_bt_safe_lshift`.
114 #define _bt_lshift(v, shift) \
115 ((__typeof__(v)) (_bt_cast_value_to_unsigned(v) << (shift)))
118 * Generate a mask of type `type` with the `length` least significant bits
119 * cleared, and the most significant bits set.
121 #define _bt_make_mask_complement(type, length) \
122 _bt_lshift(_bt_fill_mask(type), length)
125 * Generate a mask of type `type` with the `length` least significant bits
126 * set, and the most significant bits cleared.
128 #define _bt_make_mask(type, length) \
129 ((type) ~_bt_make_mask_complement(type, length))
132 * Right shift a value `v` of `shift` bits.
134 * The type of `v` can be signed or unsigned integer.
135 * The value of `shift` must be less than the size of `v` (in bits),
136 * otherwise the behavior is undefined.
137 * Evaluates to the result of the shift operation.
139 * According to the C99 standard, right shift of a left hand-side signed
140 * type which has a negative value is implementation defined. This
141 * bitfield header relies on the right shift implementation carrying the
142 * sign bit. If the compiler implementation has a different behavior,
143 * emulate carrying the sign bit.
145 * This macro should not be used if `shift` can be greater or equal than
146 * the bitwidth of `v`. See `_bt_safe_rshift`.
148 #if ((-1 >> 1) == -1)
149 #define _bt_rshift(v, shift) ((v) >> (shift))
151 #define _bt_rshift(v, shift) \
152 ((__typeof__(v)) ((_bt_cast_value_to_unsigned(v) >> (shift)) | \
153 ((v) < 0 ? _bt_make_mask_complement(__typeof__(v), \
154 sizeof(v) * CHAR_BIT - (shift)) : 0)))
158 * Right shift a signed or unsigned integer with `shift` value being an
159 * arbitrary number of bits. `v` is modified by this macro. The shift
160 * is transformed into a sequence of `_nr_partial_shifts` consecutive
161 * shift operations, each of a number of bits smaller than the bitwidth
162 * of `v`, ending with a shift of the number of left over bits.
164 #define _bt_safe_rshift(v, shift) \
166 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
167 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
169 for (; _nr_partial_shifts; _nr_partial_shifts--) \
170 (v) = _bt_rshift(v, sizeof(v) * CHAR_BIT - 1); \
171 (v) = _bt_rshift(v, _leftover_bits); \
175 * Left shift a signed or unsigned integer with `shift` value being an
176 * arbitrary number of bits. `v` is modified by this macro. The shift
177 * is transformed into a sequence of `_nr_partial_shifts` consecutive
178 * shift operations, each of a number of bits smaller than the bitwidth
179 * of `v`, ending with a shift of the number of left over bits.
181 #define _bt_safe_lshift(v, shift) \
183 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
184 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
186 for (; _nr_partial_shifts; _nr_partial_shifts--) \
187 (v) = _bt_lshift(v, sizeof(v) * CHAR_BIT - 1); \
188 (v) = _bt_lshift(v, _leftover_bits); \
192 * bt_bitfield_write - write integer to a bitfield in native endianness
194 * Save integer to the bitfield, which starts at the "start" bit, has "len"
196 * The inside of a bitfield is from high bits to low bits.
197 * Uses native endianness.
198 * For unsigned "v", pad MSB with 0 if bitfield is larger than v.
199 * For signed "v", sign-extend v if bitfield is larger than v.
201 * On little endian, bytes are placed from the less significant to the most
202 * significant. Also, consecutive bitfields are placed from lower bits to higher
205 * On big endian, bytes are places from most significant to less significant.
206 * Also, consecutive bitfields are placed from higher to lower bits.
209 #define _bt_bitfield_write_le(ptr, type, start, length, v) \
211 __typeof__(v) _v = (v); \
212 type *_ptr = (void *) (ptr); \
213 unsigned long _start = (start), _length = (length); \
214 type _mask, _cmask; \
215 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
216 unsigned long _start_unit, _end_unit, _this_unit; \
217 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
222 _end = _start + _length; \
223 _start_unit = _start / _ts; \
224 _end_unit = (_end + (_ts - 1)) / _ts; \
226 /* Trim v high bits */ \
227 if (_length < sizeof(_v) * CHAR_BIT) \
228 _v &= _bt_make_mask(__typeof__(_v), _length); \
230 /* We can now append v with a simple "or", shift it piece-wise */ \
231 _this_unit = _start_unit; \
232 if (_start_unit == _end_unit - 1) { \
233 _mask = _bt_make_mask(type, _start % _ts); \
235 _mask |= _bt_make_mask_complement(type, _end % _ts); \
236 _cmask = _bt_lshift((type) (_v), _start % _ts); \
238 _ptr[_this_unit] &= _mask; \
239 _ptr[_this_unit] |= _cmask; \
242 if (_start % _ts) { \
243 _cshift = _start % _ts; \
244 _mask = _bt_make_mask(type, _cshift); \
245 _cmask = _bt_lshift((type) (_v), _cshift); \
247 _ptr[_this_unit] &= _mask; \
248 _ptr[_this_unit] |= _cmask; \
249 _bt_safe_rshift(_v, _ts - _cshift); \
250 _start += _ts - _cshift; \
253 for (; _this_unit < _end_unit - 1; _this_unit++) { \
254 _ptr[_this_unit] = (type) _v; \
255 _bt_safe_rshift(_v, _ts); \
259 _mask = _bt_make_mask_complement(type, _end % _ts); \
260 _cmask = (type) _v; \
262 _ptr[_this_unit] &= _mask; \
263 _ptr[_this_unit] |= _cmask; \
265 _ptr[_this_unit] = (type) _v; \
268 #define _bt_bitfield_write_be(ptr, type, start, length, v) \
270 __typeof__(v) _v = (v); \
271 type *_ptr = (void *) (ptr); \
272 unsigned long _start = (start), _length = (length); \
273 type _mask, _cmask; \
274 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
275 unsigned long _start_unit, _end_unit, _this_unit; \
276 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
281 _end = _start + _length; \
282 _start_unit = _start / _ts; \
283 _end_unit = (_end + (_ts - 1)) / _ts; \
285 /* Trim v high bits */ \
286 if (_length < sizeof(_v) * CHAR_BIT) \
287 _v &= _bt_make_mask(__typeof__(_v), _length); \
289 /* We can now append v with a simple "or", shift it piece-wise */ \
290 _this_unit = _end_unit - 1; \
291 if (_start_unit == _end_unit - 1) { \
292 _mask = _bt_make_mask(type, (_ts - (_end % _ts)) % _ts); \
294 _mask |= _bt_make_mask_complement(type, _ts - (_start % _ts)); \
295 _cmask = _bt_lshift((type) (_v), (_ts - (_end % _ts)) % _ts); \
297 _ptr[_this_unit] &= _mask; \
298 _ptr[_this_unit] |= _cmask; \
302 _cshift = _end % _ts; \
303 _mask = _bt_make_mask(type, _ts - _cshift); \
304 _cmask = _bt_lshift((type) (_v), _ts - _cshift); \
306 _ptr[_this_unit] &= _mask; \
307 _ptr[_this_unit] |= _cmask; \
308 _bt_safe_rshift(_v, _cshift); \
312 for (; (long) _this_unit >= (long) _start_unit + 1; _this_unit--) { \
313 _ptr[_this_unit] = (type) _v; \
314 _bt_safe_rshift(_v, _ts); \
317 if (_start % _ts) { \
318 _mask = _bt_make_mask_complement(type, _ts - (_start % _ts)); \
319 _cmask = (type) _v; \
321 _ptr[_this_unit] &= _mask; \
322 _ptr[_this_unit] |= _cmask; \
324 _ptr[_this_unit] = (type) _v; \
328 * bt_bitfield_write - write integer to a bitfield in native endianness
329 * bt_bitfield_write_le - write integer to a bitfield in little endian
330 * bt_bitfield_write_be - write integer to a bitfield in big endian
333 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
335 #define bt_bitfield_write(ptr, type, start, length, v) \
336 _bt_bitfield_write_le(ptr, type, start, length, v)
338 #define bt_bitfield_write_le(ptr, type, start, length, v) \
339 _bt_bitfield_write_le(ptr, type, start, length, v)
341 #define bt_bitfield_write_be(ptr, type, start, length, v) \
342 _bt_bitfield_write_be(ptr, unsigned char, start, length, v)
344 #elif (__BYTE_ORDER == __BIG_ENDIAN)
346 #define bt_bitfield_write(ptr, type, start, length, v) \
347 _bt_bitfield_write_be(ptr, type, start, length, v)
349 #define bt_bitfield_write_le(ptr, type, start, length, v) \
350 _bt_bitfield_write_le(ptr, unsigned char, start, length, v)
352 #define bt_bitfield_write_be(ptr, type, start, length, v) \
353 _bt_bitfield_write_be(ptr, type, start, length, v)
355 #else /* (__BYTE_ORDER == __PDP_ENDIAN) */
357 #error "Byte order not supported"
361 #define _bt_bitfield_read_le(ptr, type, start, length, vptr) \
363 __typeof__(*(vptr)) *_vptr = (vptr); \
364 __typeof__(*_vptr) _v; \
365 type *_ptr = (void *) (ptr); \
366 unsigned long _start = (start), _length = (length); \
367 type _mask, _cmask; \
368 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
369 unsigned long _start_unit, _end_unit, _this_unit; \
370 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
371 bool _is_signed_type; \
378 _end = _start + _length; \
379 _start_unit = _start / _ts; \
380 _end_unit = (_end + (_ts - 1)) / _ts; \
382 _this_unit = _end_unit - 1; \
384 _BT_DIAG_IGNORE_TYPE_LIMITS \
385 _is_signed_type = _bt_is_signed_type(__typeof__(_v)); \
387 if (_is_signed_type \
388 && (_ptr[_this_unit] & _bt_lshift((type) 1, (_end % _ts ? _end % _ts : _ts) - 1))) \
389 _v = ~(__typeof__(_v)) 0; \
392 if (_start_unit == _end_unit - 1) { \
393 _cmask = _ptr[_this_unit]; \
394 _cmask = _bt_rshift(_cmask, _start % _ts); \
395 if ((_end - _start) % _ts) { \
396 _mask = _bt_make_mask(type, _end - _start); \
399 _bt_safe_lshift(_v, _end - _start); \
400 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
405 _cshift = _end % _ts; \
406 _mask = _bt_make_mask(type, _cshift); \
407 _cmask = _ptr[_this_unit]; \
409 _bt_safe_lshift(_v, _cshift); \
410 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
414 for (; (long) _this_unit >= (long) _start_unit + 1; _this_unit--) { \
415 _bt_safe_lshift(_v, _ts); \
416 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
419 if (_start % _ts) { \
420 _mask = _bt_make_mask(type, _ts - (_start % _ts)); \
421 _cmask = _ptr[_this_unit]; \
422 _cmask = _bt_rshift(_cmask, _start % _ts); \
424 _bt_safe_lshift(_v, _ts - (_start % _ts)); \
425 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
427 _bt_safe_lshift(_v, _ts); \
428 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
433 #define _bt_bitfield_read_be(ptr, type, start, length, vptr) \
435 __typeof__(*(vptr)) *_vptr = (vptr); \
436 __typeof__(*_vptr) _v; \
437 type *_ptr = (void *) (ptr); \
438 unsigned long _start = (start), _length = (length); \
439 type _mask, _cmask; \
440 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
441 unsigned long _start_unit, _end_unit, _this_unit; \
442 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
443 bool _is_signed_type; \
450 _end = _start + _length; \
451 _start_unit = _start / _ts; \
452 _end_unit = (_end + (_ts - 1)) / _ts; \
454 _this_unit = _start_unit; \
456 _BT_DIAG_IGNORE_TYPE_LIMITS \
457 _is_signed_type = _bt_is_signed_type(__typeof__(_v)); \
459 if (_is_signed_type \
460 && (_ptr[_this_unit] & _bt_lshift((type) 1, _ts - (_start % _ts) - 1))) \
461 _v = ~(__typeof__(_v)) 0; \
464 if (_start_unit == _end_unit - 1) { \
465 _cmask = _ptr[_this_unit]; \
466 _cmask = _bt_rshift(_cmask, (_ts - (_end % _ts)) % _ts); \
467 if ((_end - _start) % _ts) { \
468 _mask = _bt_make_mask(type, _end - _start); \
471 _bt_safe_lshift(_v, _end - _start); \
472 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
476 if (_start % _ts) { \
477 _cshift = _start % _ts; \
478 _mask = _bt_make_mask(type, _ts - _cshift); \
479 _cmask = _ptr[_this_unit]; \
481 _bt_safe_lshift(_v, _ts - _cshift); \
482 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
483 _start += _ts - _cshift; \
486 for (; _this_unit < _end_unit - 1; _this_unit++) { \
487 _bt_safe_lshift(_v, _ts); \
488 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
492 _mask = _bt_make_mask(type, _end % _ts); \
493 _cmask = _ptr[_this_unit]; \
494 _cmask = _bt_rshift(_cmask, _ts - (_end % _ts)); \
496 _bt_safe_lshift(_v, _end % _ts); \
497 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
499 _bt_safe_lshift(_v, _ts); \
500 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
506 * bt_bitfield_read - read integer from a bitfield in native endianness
507 * bt_bitfield_read_le - read integer from a bitfield in little endian
508 * bt_bitfield_read_be - read integer from a bitfield in big endian
511 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
513 #define bt_bitfield_read(ptr, type, start, length, vptr) \
514 _bt_bitfield_read_le(ptr, type, start, length, vptr)
516 #define bt_bitfield_read_le(ptr, type, start, length, vptr) \
517 _bt_bitfield_read_le(ptr, type, start, length, vptr)
519 #define bt_bitfield_read_be(ptr, type, start, length, vptr) \
520 _bt_bitfield_read_be(ptr, unsigned char, start, length, vptr)
522 #elif (__BYTE_ORDER == __BIG_ENDIAN)
524 #define bt_bitfield_read(ptr, type, start, length, vptr) \
525 _bt_bitfield_read_be(ptr, type, start, length, vptr)
527 #define bt_bitfield_read_le(ptr, type, start, length, vptr) \
528 _bt_bitfield_read_le(ptr, unsigned char, start, length, vptr)
530 #define bt_bitfield_read_be(ptr, type, start, length, vptr) \
531 _bt_bitfield_read_be(ptr, type, start, length, vptr)
533 #else /* (__BYTE_ORDER == __PDP_ENDIAN) */
535 #error "Byte order not supported"
539 #endif /* _BABELTRACE_BITFIELD_H */
This page took 0.03971 seconds and 4 git commands to generate.