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."
29 #define _bt_is_signed_type(type) ((type) -1 < (type) 1)
32 * Produce a build-time error if the condition `cond` is non-zero.
33 * Evaluates as a size_t expression.
36 #define _BT_BUILD_ASSERT(cond) ([]{static_assert((cond), "");}, 0)
38 #define _BT_BUILD_ASSERT(cond) \
39 sizeof(struct { int f:(2 * !!(cond) - 1); })
43 * Cast value `v` to an unsigned integer of the same size as `v`.
45 #define _bt_cast_value_to_unsigned(v) \
46 (sizeof(v) == sizeof(uint8_t) ? (uint8_t) (v) : \
47 sizeof(v) == sizeof(uint16_t) ? (uint16_t) (v) : \
48 sizeof(v) == sizeof(uint32_t) ? (uint32_t) (v) : \
49 sizeof(v) == sizeof(uint64_t) ? (uint64_t) (v) : \
50 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
53 * Cast value `v` to an unsigned integer type of the size of type `type`
54 * *without* sign-extension.
56 * The unsigned cast ensures that we're not shifting a negative value,
57 * which is undefined in C. However, this limits the maximum type size
58 * of `type` to 64-bit. Generate a compile-time error if the size of
59 * `type` is larger than 64-bit.
61 #define _bt_cast_value_to_unsigned_type(type, v) \
62 (sizeof(type) == sizeof(uint8_t) ? \
63 (uint8_t) _bt_cast_value_to_unsigned(v) : \
64 sizeof(type) == sizeof(uint16_t) ? \
65 (uint16_t) _bt_cast_value_to_unsigned(v) : \
66 sizeof(type) == sizeof(uint32_t) ? \
67 (uint32_t) _bt_cast_value_to_unsigned(v) : \
68 sizeof(type) == sizeof(uint64_t) ? \
69 (uint64_t) _bt_cast_value_to_unsigned(v) : \
70 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
73 * _bt_fill_mask evaluates to a "type" integer with all bits set.
75 #define _bt_fill_mask(type) ((type) ~(type) 0)
78 * Left shift a value `v` of `shift` bits.
80 * The type of `v` can be signed or unsigned integer.
81 * The value of `shift` must be less than the size of `v` (in bits),
82 * otherwise the behavior is undefined.
83 * Evaluates to the result of the shift operation.
85 * According to the C99 standard, left shift of a left hand-side signed
86 * type is undefined if it has a negative value or if the result cannot
87 * be represented in the result type. This bitfield header discards the
88 * bits that are left-shifted beyond the result type representation,
89 * which is the behavior of an unsigned type left shift operation.
90 * Therefore, always perform left shift on an unsigned type.
92 * This macro should not be used if `shift` can be greater or equal than
93 * the bitwidth of `v`. See `_bt_safe_lshift`.
95 #define _bt_lshift(v, shift) \
96 ((__typeof__(v)) (_bt_cast_value_to_unsigned(v) << (shift)))
99 * Generate a mask of type `type` with the `length` least significant bits
100 * cleared, and the most significant bits set.
102 #define _bt_make_mask_complement(type, length) \
103 _bt_lshift(_bt_fill_mask(type), length)
106 * Generate a mask of type `type` with the `length` least significant bits
107 * set, and the most significant bits cleared.
109 #define _bt_make_mask(type, length) \
110 ((type) ~_bt_make_mask_complement(type, length))
113 * Right shift a value `v` of `shift` bits.
115 * The type of `v` can be signed or unsigned integer.
116 * The value of `shift` must be less than the size of `v` (in bits),
117 * otherwise the behavior is undefined.
118 * Evaluates to the result of the shift operation.
120 * According to the C99 standard, right shift of a left hand-side signed
121 * type which has a negative value is implementation defined. This
122 * bitfield header relies on the right shift implementation carrying the
123 * sign bit. If the compiler implementation has a different behavior,
124 * emulate carrying the sign bit.
126 * This macro should not be used if `shift` can be greater or equal than
127 * the bitwidth of `v`. See `_bt_safe_rshift`.
129 #if ((-1 >> 1) == -1)
130 #define _bt_rshift(v, shift) ((v) >> (shift))
132 #define _bt_rshift(v, shift) \
133 ((__typeof__(v)) ((_bt_cast_value_to_unsigned(v) >> (shift)) | \
134 ((v) < 0 ? _bt_make_mask_complement(__typeof__(v), \
135 sizeof(v) * CHAR_BIT - (shift)) : 0)))
139 * Right shift a signed or unsigned integer with `shift` value being an
140 * arbitrary number of bits. `v` is modified by this macro. The shift
141 * is transformed into a sequence of `_nr_partial_shifts` consecutive
142 * shift operations, each of a number of bits smaller than the bitwidth
143 * of `v`, ending with a shift of the number of left over bits.
145 #define _bt_safe_rshift(v, shift) \
147 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
148 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
150 for (; _nr_partial_shifts; _nr_partial_shifts--) \
151 (v) = _bt_rshift(v, sizeof(v) * CHAR_BIT - 1); \
152 (v) = _bt_rshift(v, _leftover_bits); \
156 * Left shift a signed or unsigned integer with `shift` value being an
157 * arbitrary number of bits. `v` is modified by this macro. The shift
158 * is transformed into a sequence of `_nr_partial_shifts` consecutive
159 * shift operations, each of a number of bits smaller than the bitwidth
160 * of `v`, ending with a shift of the number of left over bits.
162 #define _bt_safe_lshift(v, shift) \
164 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
165 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
167 for (; _nr_partial_shifts; _nr_partial_shifts--) \
168 (v) = _bt_lshift(v, sizeof(v) * CHAR_BIT - 1); \
169 (v) = _bt_lshift(v, _leftover_bits); \
173 * bt_bitfield_write - write integer to a bitfield in native endianness
175 * Save integer to the bitfield, which starts at the "start" bit, has "len"
177 * The inside of a bitfield is from high bits to low bits.
178 * Uses native endianness.
179 * For unsigned "v", pad MSB with 0 if bitfield is larger than v.
180 * For signed "v", sign-extend v if bitfield is larger than v.
182 * On little endian, bytes are placed from the less significant to the most
183 * significant. Also, consecutive bitfields are placed from lower bits to higher
186 * On big endian, bytes are places from most significant to less significant.
187 * Also, consecutive bitfields are placed from higher to lower bits.
190 #define _bt_bitfield_write_le(ptr, type, start, length, v) \
192 __typeof__(v) _v = (v); \
193 type *_ptr = (void *) (ptr); \
194 unsigned long _start = (start), _length = (length); \
195 type _mask, _cmask; \
196 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
197 unsigned long _start_unit, _end_unit, _this_unit; \
198 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
203 _end = _start + _length; \
204 _start_unit = _start / _ts; \
205 _end_unit = (_end + (_ts - 1)) / _ts; \
207 /* Trim v high bits */ \
208 if (_length < sizeof(_v) * CHAR_BIT) \
209 _v &= _bt_make_mask(__typeof__(_v), _length); \
211 /* We can now append v with a simple "or", shift it piece-wise */ \
212 _this_unit = _start_unit; \
213 if (_start_unit == _end_unit - 1) { \
214 _mask = _bt_make_mask(type, _start % _ts); \
216 _mask |= _bt_make_mask_complement(type, _end % _ts); \
217 _cmask = _bt_lshift((type) (_v), _start % _ts); \
219 _ptr[_this_unit] &= _mask; \
220 _ptr[_this_unit] |= _cmask; \
223 if (_start % _ts) { \
224 _cshift = _start % _ts; \
225 _mask = _bt_make_mask(type, _cshift); \
226 _cmask = _bt_lshift((type) (_v), _cshift); \
228 _ptr[_this_unit] &= _mask; \
229 _ptr[_this_unit] |= _cmask; \
230 _bt_safe_rshift(_v, _ts - _cshift); \
231 _start += _ts - _cshift; \
234 for (; _this_unit < _end_unit - 1; _this_unit++) { \
235 _ptr[_this_unit] = (type) _v; \
236 _bt_safe_rshift(_v, _ts); \
240 _mask = _bt_make_mask_complement(type, _end % _ts); \
241 _cmask = (type) _v; \
243 _ptr[_this_unit] &= _mask; \
244 _ptr[_this_unit] |= _cmask; \
246 _ptr[_this_unit] = (type) _v; \
249 #define _bt_bitfield_write_be(ptr, type, start, length, v) \
251 __typeof__(v) _v = (v); \
252 type *_ptr = (void *) (ptr); \
253 unsigned long _start = (start), _length = (length); \
254 type _mask, _cmask; \
255 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
256 unsigned long _start_unit, _end_unit, _this_unit; \
257 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
262 _end = _start + _length; \
263 _start_unit = _start / _ts; \
264 _end_unit = (_end + (_ts - 1)) / _ts; \
266 /* Trim v high bits */ \
267 if (_length < sizeof(_v) * CHAR_BIT) \
268 _v &= _bt_make_mask(__typeof__(_v), _length); \
270 /* We can now append v with a simple "or", shift it piece-wise */ \
271 _this_unit = _end_unit - 1; \
272 if (_start_unit == _end_unit - 1) { \
273 _mask = _bt_make_mask(type, (_ts - (_end % _ts)) % _ts); \
275 _mask |= _bt_make_mask_complement(type, _ts - (_start % _ts)); \
276 _cmask = _bt_lshift((type) (_v), (_ts - (_end % _ts)) % _ts); \
278 _ptr[_this_unit] &= _mask; \
279 _ptr[_this_unit] |= _cmask; \
283 _cshift = _end % _ts; \
284 _mask = _bt_make_mask(type, _ts - _cshift); \
285 _cmask = _bt_lshift((type) (_v), _ts - _cshift); \
287 _ptr[_this_unit] &= _mask; \
288 _ptr[_this_unit] |= _cmask; \
289 _bt_safe_rshift(_v, _cshift); \
293 for (; (long) _this_unit >= (long) _start_unit + 1; _this_unit--) { \
294 _ptr[_this_unit] = (type) _v; \
295 _bt_safe_rshift(_v, _ts); \
298 if (_start % _ts) { \
299 _mask = _bt_make_mask_complement(type, _ts - (_start % _ts)); \
300 _cmask = (type) _v; \
302 _ptr[_this_unit] &= _mask; \
303 _ptr[_this_unit] |= _cmask; \
305 _ptr[_this_unit] = (type) _v; \
309 * bt_bitfield_write - write integer to a bitfield in native endianness
310 * bt_bitfield_write_le - write integer to a bitfield in little endian
311 * bt_bitfield_write_be - write integer to a bitfield in big endian
314 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
316 #define bt_bitfield_write(ptr, type, start, length, v) \
317 _bt_bitfield_write_le(ptr, type, start, length, v)
319 #define bt_bitfield_write_le(ptr, type, start, length, v) \
320 _bt_bitfield_write_le(ptr, type, start, length, v)
322 #define bt_bitfield_write_be(ptr, type, start, length, v) \
323 _bt_bitfield_write_be(ptr, unsigned char, start, length, v)
325 #elif (__BYTE_ORDER == __BIG_ENDIAN)
327 #define bt_bitfield_write(ptr, type, start, length, v) \
328 _bt_bitfield_write_be(ptr, type, start, length, v)
330 #define bt_bitfield_write_le(ptr, type, start, length, v) \
331 _bt_bitfield_write_le(ptr, unsigned char, start, length, v)
333 #define bt_bitfield_write_be(ptr, type, start, length, v) \
334 _bt_bitfield_write_be(ptr, type, start, length, v)
336 #else /* (__BYTE_ORDER == __PDP_ENDIAN) */
338 #error "Byte order not supported"
342 #define _bt_bitfield_read_le(ptr, type, start, length, vptr) \
344 __typeof__(*(vptr)) *_vptr = (vptr); \
345 __typeof__(*_vptr) _v; \
346 type *_ptr = (type *) (ptr); \
347 unsigned long _start = (start), _length = (length); \
348 type _mask, _cmask; \
349 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
350 unsigned long _start_unit, _end_unit, _this_unit; \
351 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
358 _end = _start + _length; \
359 _start_unit = _start / _ts; \
360 _end_unit = (_end + (_ts - 1)) / _ts; \
362 _this_unit = _end_unit - 1; \
363 if (_bt_is_signed_type(__typeof__(_v)) \
364 && (_ptr[_this_unit] & _bt_lshift((type) 1, (_end % _ts ? _end % _ts : _ts) - 1))) \
365 _v = ~(__typeof__(_v)) 0; \
368 if (_start_unit == _end_unit - 1) { \
369 _cmask = _ptr[_this_unit]; \
370 _cmask = _bt_rshift(_cmask, _start % _ts); \
371 if ((_end - _start) % _ts) { \
372 _mask = _bt_make_mask(type, _end - _start); \
375 _bt_safe_lshift(_v, _end - _start); \
376 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
381 _cshift = _end % _ts; \
382 _mask = _bt_make_mask(type, _cshift); \
383 _cmask = _ptr[_this_unit]; \
385 _bt_safe_lshift(_v, _cshift); \
386 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
390 for (; (long) _this_unit >= (long) _start_unit + 1; _this_unit--) { \
391 _bt_safe_lshift(_v, _ts); \
392 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
395 if (_start % _ts) { \
396 _mask = _bt_make_mask(type, _ts - (_start % _ts)); \
397 _cmask = _ptr[_this_unit]; \
398 _cmask = _bt_rshift(_cmask, _start % _ts); \
400 _bt_safe_lshift(_v, _ts - (_start % _ts)); \
401 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
403 _bt_safe_lshift(_v, _ts); \
404 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
409 #define _bt_bitfield_read_be(ptr, type, start, length, vptr) \
411 __typeof__(*(vptr)) *_vptr = (vptr); \
412 __typeof__(*_vptr) _v; \
413 type *_ptr = (void *) (ptr); \
414 unsigned long _start = (start), _length = (length); \
415 type _mask, _cmask; \
416 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
417 unsigned long _start_unit, _end_unit, _this_unit; \
418 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
425 _end = _start + _length; \
426 _start_unit = _start / _ts; \
427 _end_unit = (_end + (_ts - 1)) / _ts; \
429 _this_unit = _start_unit; \
430 if (_bt_is_signed_type(__typeof__(_v)) \
431 && (_ptr[_this_unit] & _bt_lshift((type) 1, _ts - (_start % _ts) - 1))) \
432 _v = ~(__typeof__(_v)) 0; \
435 if (_start_unit == _end_unit - 1) { \
436 _cmask = _ptr[_this_unit]; \
437 _cmask = _bt_rshift(_cmask, (_ts - (_end % _ts)) % _ts); \
438 if ((_end - _start) % _ts) { \
439 _mask = _bt_make_mask(type, _end - _start); \
442 _bt_safe_lshift(_v, _end - _start); \
443 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
447 if (_start % _ts) { \
448 _cshift = _start % _ts; \
449 _mask = _bt_make_mask(type, _ts - _cshift); \
450 _cmask = _ptr[_this_unit]; \
452 _bt_safe_lshift(_v, _ts - _cshift); \
453 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
454 _start += _ts - _cshift; \
457 for (; _this_unit < _end_unit - 1; _this_unit++) { \
458 _bt_safe_lshift(_v, _ts); \
459 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
463 _mask = _bt_make_mask(type, _end % _ts); \
464 _cmask = _ptr[_this_unit]; \
465 _cmask = _bt_rshift(_cmask, _ts - (_end % _ts)); \
467 _bt_safe_lshift(_v, _end % _ts); \
468 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
470 _bt_safe_lshift(_v, _ts); \
471 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
477 * bt_bitfield_read - read integer from a bitfield in native endianness
478 * bt_bitfield_read_le - read integer from a bitfield in little endian
479 * bt_bitfield_read_be - read integer from a bitfield in big endian
482 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
484 #define bt_bitfield_read(ptr, type, start, length, vptr) \
485 _bt_bitfield_read_le(ptr, type, start, length, vptr)
487 #define bt_bitfield_read_le(ptr, type, start, length, vptr) \
488 _bt_bitfield_read_le(ptr, type, start, length, vptr)
490 #define bt_bitfield_read_be(ptr, type, start, length, vptr) \
491 _bt_bitfield_read_be(ptr, unsigned char, start, length, vptr)
493 #elif (__BYTE_ORDER == __BIG_ENDIAN)
495 #define bt_bitfield_read(ptr, type, start, length, vptr) \
496 _bt_bitfield_read_be(ptr, type, start, length, vptr)
498 #define bt_bitfield_read_le(ptr, type, start, length, vptr) \
499 _bt_bitfield_read_le(ptr, unsigned char, start, length, vptr)
501 #define bt_bitfield_read_be(ptr, type, start, length, vptr) \
502 _bt_bitfield_read_be(ptr, type, start, length, vptr)
504 #else /* (__BYTE_ORDER == __PDP_ENDIAN) */
506 #error "Byte order not supported"
510 #endif /* _BABELTRACE_BITFIELD_H */
This page took 0.044972 seconds and 4 git commands to generate.