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.
59 #define _BT_BUILD_ASSERT(cond) ([]{static_assert((cond), "");}, 0)
61 #define _BT_BUILD_ASSERT(cond) \
62 sizeof(struct { int f:(2 * !!(cond) - 1); })
66 * Cast value `v` to an unsigned integer of the same size as `v`.
68 #define _bt_cast_value_to_unsigned(v) \
69 (sizeof(v) == sizeof(uint8_t) ? (uint8_t) (v) : \
70 sizeof(v) == sizeof(uint16_t) ? (uint16_t) (v) : \
71 sizeof(v) == sizeof(uint32_t) ? (uint32_t) (v) : \
72 sizeof(v) == sizeof(uint64_t) ? (uint64_t) (v) : \
73 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
76 * Cast value `v` to an unsigned integer type of the size of type `type`
77 * *without* sign-extension.
79 * The unsigned cast ensures that we're not shifting a negative value,
80 * which is undefined in C. However, this limits the maximum type size
81 * of `type` to 64-bit. Generate a compile-time error if the size of
82 * `type` is larger than 64-bit.
84 #define _bt_cast_value_to_unsigned_type(type, v) \
85 (sizeof(type) == sizeof(uint8_t) ? \
86 (uint8_t) _bt_cast_value_to_unsigned(v) : \
87 sizeof(type) == sizeof(uint16_t) ? \
88 (uint16_t) _bt_cast_value_to_unsigned(v) : \
89 sizeof(type) == sizeof(uint32_t) ? \
90 (uint32_t) _bt_cast_value_to_unsigned(v) : \
91 sizeof(type) == sizeof(uint64_t) ? \
92 (uint64_t) _bt_cast_value_to_unsigned(v) : \
93 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
96 * _bt_fill_mask evaluates to a "type" integer with all bits set.
98 #define _bt_fill_mask(type) ((type) ~(type) 0)
101 * Left shift a value `v` of `shift` bits.
103 * The type of `v` can be signed or unsigned integer.
104 * The value of `shift` must be less than the size of `v` (in bits),
105 * otherwise the behavior is undefined.
106 * Evaluates to the result of the shift operation.
108 * According to the C99 standard, left shift of a left hand-side signed
109 * type is undefined if it has a negative value or if the result cannot
110 * be represented in the result type. This bitfield header discards the
111 * bits that are left-shifted beyond the result type representation,
112 * which is the behavior of an unsigned type left shift operation.
113 * Therefore, always perform left shift on an unsigned type.
115 * This macro should not be used if `shift` can be greater or equal than
116 * the bitwidth of `v`. See `_bt_safe_lshift`.
118 #define _bt_lshift(v, shift) \
119 ((__typeof__(v)) (_bt_cast_value_to_unsigned(v) << (shift)))
122 * Generate a mask of type `type` with the `length` least significant bits
123 * cleared, and the most significant bits set.
125 #define _bt_make_mask_complement(type, length) \
126 _bt_lshift(_bt_fill_mask(type), length)
129 * Generate a mask of type `type` with the `length` least significant bits
130 * set, and the most significant bits cleared.
132 #define _bt_make_mask(type, length) \
133 ((type) ~_bt_make_mask_complement(type, length))
136 * Right shift a value `v` of `shift` bits.
138 * The type of `v` can be signed or unsigned integer.
139 * The value of `shift` must be less than the size of `v` (in bits),
140 * otherwise the behavior is undefined.
141 * Evaluates to the result of the shift operation.
143 * According to the C99 standard, right shift of a left hand-side signed
144 * type which has a negative value is implementation defined. This
145 * bitfield header relies on the right shift implementation carrying the
146 * sign bit. If the compiler implementation has a different behavior,
147 * emulate carrying the sign bit.
149 * This macro should not be used if `shift` can be greater or equal than
150 * the bitwidth of `v`. See `_bt_safe_rshift`.
152 #if ((-1 >> 1) == -1)
153 #define _bt_rshift(v, shift) ((v) >> (shift))
155 #define _bt_rshift(v, shift) \
156 ((__typeof__(v)) ((_bt_cast_value_to_unsigned(v) >> (shift)) | \
157 ((v) < 0 ? _bt_make_mask_complement(__typeof__(v), \
158 sizeof(v) * CHAR_BIT - (shift)) : 0)))
162 * Right shift a signed or unsigned integer with `shift` value being an
163 * arbitrary number of bits. `v` is modified by this macro. The shift
164 * is transformed into a sequence of `_nr_partial_shifts` consecutive
165 * shift operations, each of a number of bits smaller than the bitwidth
166 * of `v`, ending with a shift of the number of left over bits.
168 #define _bt_safe_rshift(v, shift) \
170 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
171 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
173 for (; _nr_partial_shifts; _nr_partial_shifts--) \
174 (v) = _bt_rshift(v, sizeof(v) * CHAR_BIT - 1); \
175 (v) = _bt_rshift(v, _leftover_bits); \
179 * Left shift a signed or unsigned integer with `shift` value being an
180 * arbitrary number of bits. `v` is modified by this macro. The shift
181 * is transformed into a sequence of `_nr_partial_shifts` consecutive
182 * shift operations, each of a number of bits smaller than the bitwidth
183 * of `v`, ending with a shift of the number of left over bits.
185 #define _bt_safe_lshift(v, shift) \
187 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
188 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
190 for (; _nr_partial_shifts; _nr_partial_shifts--) \
191 (v) = _bt_lshift(v, sizeof(v) * CHAR_BIT - 1); \
192 (v) = _bt_lshift(v, _leftover_bits); \
196 * bt_bitfield_write - write integer to a bitfield in native endianness
198 * Save integer to the bitfield, which starts at the "start" bit, has "len"
200 * The inside of a bitfield is from high bits to low bits.
201 * Uses native endianness.
202 * For unsigned "v", pad MSB with 0 if bitfield is larger than v.
203 * For signed "v", sign-extend v if bitfield is larger than v.
205 * On little endian, bytes are placed from the less significant to the most
206 * significant. Also, consecutive bitfields are placed from lower bits to higher
209 * On big endian, bytes are places from most significant to less significant.
210 * Also, consecutive bitfields are placed from higher to lower bits.
213 #define _bt_bitfield_write_le(ptr, type, start, length, v) \
215 __typeof__(v) _v = (v); \
216 type *_ptr = (void *) (ptr); \
217 unsigned long _start = (start), _length = (length); \
218 type _mask, _cmask; \
219 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
220 unsigned long _start_unit, _end_unit, _this_unit; \
221 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
226 _end = _start + _length; \
227 _start_unit = _start / _ts; \
228 _end_unit = (_end + (_ts - 1)) / _ts; \
230 /* Trim v high bits */ \
231 if (_length < sizeof(_v) * CHAR_BIT) \
232 _v &= _bt_make_mask(__typeof__(_v), _length); \
234 /* We can now append v with a simple "or", shift it piece-wise */ \
235 _this_unit = _start_unit; \
236 if (_start_unit == _end_unit - 1) { \
237 _mask = _bt_make_mask(type, _start % _ts); \
239 _mask |= _bt_make_mask_complement(type, _end % _ts); \
240 _cmask = _bt_lshift((type) (_v), _start % _ts); \
242 _ptr[_this_unit] &= _mask; \
243 _ptr[_this_unit] |= _cmask; \
246 if (_start % _ts) { \
247 _cshift = _start % _ts; \
248 _mask = _bt_make_mask(type, _cshift); \
249 _cmask = _bt_lshift((type) (_v), _cshift); \
251 _ptr[_this_unit] &= _mask; \
252 _ptr[_this_unit] |= _cmask; \
253 _bt_safe_rshift(_v, _ts - _cshift); \
254 _start += _ts - _cshift; \
257 for (; _this_unit < _end_unit - 1; _this_unit++) { \
258 _ptr[_this_unit] = (type) _v; \
259 _bt_safe_rshift(_v, _ts); \
263 _mask = _bt_make_mask_complement(type, _end % _ts); \
264 _cmask = (type) _v; \
266 _ptr[_this_unit] &= _mask; \
267 _ptr[_this_unit] |= _cmask; \
269 _ptr[_this_unit] = (type) _v; \
272 #define _bt_bitfield_write_be(ptr, type, start, length, v) \
274 __typeof__(v) _v = (v); \
275 type *_ptr = (void *) (ptr); \
276 unsigned long _start = (start), _length = (length); \
277 type _mask, _cmask; \
278 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
279 unsigned long _start_unit, _end_unit, _this_unit; \
280 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
285 _end = _start + _length; \
286 _start_unit = _start / _ts; \
287 _end_unit = (_end + (_ts - 1)) / _ts; \
289 /* Trim v high bits */ \
290 if (_length < sizeof(_v) * CHAR_BIT) \
291 _v &= _bt_make_mask(__typeof__(_v), _length); \
293 /* We can now append v with a simple "or", shift it piece-wise */ \
294 _this_unit = _end_unit - 1; \
295 if (_start_unit == _end_unit - 1) { \
296 _mask = _bt_make_mask(type, (_ts - (_end % _ts)) % _ts); \
298 _mask |= _bt_make_mask_complement(type, _ts - (_start % _ts)); \
299 _cmask = _bt_lshift((type) (_v), (_ts - (_end % _ts)) % _ts); \
301 _ptr[_this_unit] &= _mask; \
302 _ptr[_this_unit] |= _cmask; \
306 _cshift = _end % _ts; \
307 _mask = _bt_make_mask(type, _ts - _cshift); \
308 _cmask = _bt_lshift((type) (_v), _ts - _cshift); \
310 _ptr[_this_unit] &= _mask; \
311 _ptr[_this_unit] |= _cmask; \
312 _bt_safe_rshift(_v, _cshift); \
316 for (; (long) _this_unit >= (long) _start_unit + 1; _this_unit--) { \
317 _ptr[_this_unit] = (type) _v; \
318 _bt_safe_rshift(_v, _ts); \
321 if (_start % _ts) { \
322 _mask = _bt_make_mask_complement(type, _ts - (_start % _ts)); \
323 _cmask = (type) _v; \
325 _ptr[_this_unit] &= _mask; \
326 _ptr[_this_unit] |= _cmask; \
328 _ptr[_this_unit] = (type) _v; \
332 * bt_bitfield_write - write integer to a bitfield in native endianness
333 * bt_bitfield_write_le - write integer to a bitfield in little endian
334 * bt_bitfield_write_be - write integer to a bitfield in big endian
337 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
339 #define bt_bitfield_write(ptr, type, start, length, v) \
340 _bt_bitfield_write_le(ptr, type, start, length, v)
342 #define bt_bitfield_write_le(ptr, type, start, length, v) \
343 _bt_bitfield_write_le(ptr, type, start, length, v)
345 #define bt_bitfield_write_be(ptr, type, start, length, v) \
346 _bt_bitfield_write_be(ptr, unsigned char, start, length, v)
348 #elif (__BYTE_ORDER == __BIG_ENDIAN)
350 #define bt_bitfield_write(ptr, type, start, length, v) \
351 _bt_bitfield_write_be(ptr, type, start, length, v)
353 #define bt_bitfield_write_le(ptr, type, start, length, v) \
354 _bt_bitfield_write_le(ptr, unsigned char, start, length, v)
356 #define bt_bitfield_write_be(ptr, type, start, length, v) \
357 _bt_bitfield_write_be(ptr, type, start, length, v)
359 #else /* (__BYTE_ORDER == __PDP_ENDIAN) */
361 #error "Byte order not supported"
365 #define _bt_bitfield_read_le(ptr, type, start, length, vptr) \
367 __typeof__(*(vptr)) *_vptr = (vptr); \
368 __typeof__(*_vptr) _v; \
369 type *_ptr = (type *) (ptr); \
370 unsigned long _start = (start), _length = (length); \
371 type _mask, _cmask; \
372 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
373 unsigned long _start_unit, _end_unit, _this_unit; \
374 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
375 bool _is_signed_type; \
382 _end = _start + _length; \
383 _start_unit = _start / _ts; \
384 _end_unit = (_end + (_ts - 1)) / _ts; \
386 _this_unit = _end_unit - 1; \
388 _BT_DIAG_IGNORE_TYPE_LIMITS \
389 _is_signed_type = _bt_is_signed_type(__typeof__(_v)); \
391 if (_is_signed_type \
392 && (_ptr[_this_unit] & _bt_lshift((type) 1, (_end % _ts ? _end % _ts : _ts) - 1))) \
393 _v = ~(__typeof__(_v)) 0; \
396 if (_start_unit == _end_unit - 1) { \
397 _cmask = _ptr[_this_unit]; \
398 _cmask = _bt_rshift(_cmask, _start % _ts); \
399 if ((_end - _start) % _ts) { \
400 _mask = _bt_make_mask(type, _end - _start); \
403 _bt_safe_lshift(_v, _end - _start); \
404 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
409 _cshift = _end % _ts; \
410 _mask = _bt_make_mask(type, _cshift); \
411 _cmask = _ptr[_this_unit]; \
413 _bt_safe_lshift(_v, _cshift); \
414 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
418 for (; (long) _this_unit >= (long) _start_unit + 1; _this_unit--) { \
419 _bt_safe_lshift(_v, _ts); \
420 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
423 if (_start % _ts) { \
424 _mask = _bt_make_mask(type, _ts - (_start % _ts)); \
425 _cmask = _ptr[_this_unit]; \
426 _cmask = _bt_rshift(_cmask, _start % _ts); \
428 _bt_safe_lshift(_v, _ts - (_start % _ts)); \
429 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
431 _bt_safe_lshift(_v, _ts); \
432 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
437 #define _bt_bitfield_read_be(ptr, type, start, length, vptr) \
439 __typeof__(*(vptr)) *_vptr = (vptr); \
440 __typeof__(*_vptr) _v; \
441 type *_ptr = (void *) (ptr); \
442 unsigned long _start = (start), _length = (length); \
443 type _mask, _cmask; \
444 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
445 unsigned long _start_unit, _end_unit, _this_unit; \
446 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
447 bool _is_signed_type; \
454 _end = _start + _length; \
455 _start_unit = _start / _ts; \
456 _end_unit = (_end + (_ts - 1)) / _ts; \
458 _this_unit = _start_unit; \
460 _BT_DIAG_IGNORE_TYPE_LIMITS \
461 _is_signed_type = _bt_is_signed_type(__typeof__(_v)); \
463 if (_is_signed_type \
464 && (_ptr[_this_unit] & _bt_lshift((type) 1, _ts - (_start % _ts) - 1))) \
465 _v = ~(__typeof__(_v)) 0; \
468 if (_start_unit == _end_unit - 1) { \
469 _cmask = _ptr[_this_unit]; \
470 _cmask = _bt_rshift(_cmask, (_ts - (_end % _ts)) % _ts); \
471 if ((_end - _start) % _ts) { \
472 _mask = _bt_make_mask(type, _end - _start); \
475 _bt_safe_lshift(_v, _end - _start); \
476 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
480 if (_start % _ts) { \
481 _cshift = _start % _ts; \
482 _mask = _bt_make_mask(type, _ts - _cshift); \
483 _cmask = _ptr[_this_unit]; \
485 _bt_safe_lshift(_v, _ts - _cshift); \
486 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
487 _start += _ts - _cshift; \
490 for (; _this_unit < _end_unit - 1; _this_unit++) { \
491 _bt_safe_lshift(_v, _ts); \
492 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
496 _mask = _bt_make_mask(type, _end % _ts); \
497 _cmask = _ptr[_this_unit]; \
498 _cmask = _bt_rshift(_cmask, _ts - (_end % _ts)); \
500 _bt_safe_lshift(_v, _end % _ts); \
501 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
503 _bt_safe_lshift(_v, _ts); \
504 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
510 * bt_bitfield_read - read integer from a bitfield in native endianness
511 * bt_bitfield_read_le - read integer from a bitfield in little endian
512 * bt_bitfield_read_be - read integer from a bitfield in big endian
515 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
517 #define bt_bitfield_read(ptr, type, start, length, vptr) \
518 _bt_bitfield_read_le(ptr, type, start, length, vptr)
520 #define bt_bitfield_read_le(ptr, type, start, length, vptr) \
521 _bt_bitfield_read_le(ptr, type, start, length, vptr)
523 #define bt_bitfield_read_be(ptr, type, start, length, vptr) \
524 _bt_bitfield_read_be(ptr, unsigned char, start, length, vptr)
526 #elif (__BYTE_ORDER == __BIG_ENDIAN)
528 #define bt_bitfield_read(ptr, type, start, length, vptr) \
529 _bt_bitfield_read_be(ptr, type, start, length, vptr)
531 #define bt_bitfield_read_le(ptr, type, start, length, vptr) \
532 _bt_bitfield_read_le(ptr, unsigned char, start, length, vptr)
534 #define bt_bitfield_read_be(ptr, type, start, length, vptr) \
535 _bt_bitfield_read_be(ptr, type, start, length, vptr)
537 #else /* (__BYTE_ORDER == __PDP_ENDIAN) */
539 #error "Byte order not supported"
543 #endif /* _BABELTRACE_BITFIELD_H */
This page took 0.094065 seconds and 4 git commands to generate.