2 * SPDX-License-Identifier: MIT
4 * Copyright (C) 2010-2019 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 #ifndef _UST_COMMON_BITFIELD_H
8 #define _UST_COMMON_BITFIELD_H
10 #include <stdint.h> /* C99 5.2.4.2 Numerical limits */
11 #include <limits.h> /* C99 5.2.4.2 Numerical limits */
12 #include <stdbool.h> /* C99 7.16 bool type */
13 #include <lttng/ust-endian.h> /* Non-standard BIG_ENDIAN, LITTLE_ENDIAN, BYTE_ORDER */
16 * This header strictly follows the C99 standard, except for use of the
17 * compiler-specific __typeof__.
21 * This bitfield header requires the compiler representation of signed
22 * integers to be two's complement.
25 #error "bitfield.h requires the compiler representation of signed integers to be two's complement."
28 #define _bt_is_signed_type(type) ((type) -1 < (type) 1)
31 * Produce a build-time error if the condition `cond` is non-zero.
32 * Evaluates as a size_t expression.
35 #define _BT_BUILD_ASSERT(cond) ([]{static_assert((cond), "");}, 0)
37 #define _BT_BUILD_ASSERT(cond) \
38 sizeof(struct { int f:(2 * !!(cond) - 1); })
42 * Cast value `v` to an unsigned integer of the same size as `v`.
44 #define _bt_cast_value_to_unsigned(v) \
45 (sizeof(v) == sizeof(uint8_t) ? (uint8_t) (v) : \
46 sizeof(v) == sizeof(uint16_t) ? (uint16_t) (v) : \
47 sizeof(v) == sizeof(uint32_t) ? (uint32_t) (v) : \
48 sizeof(v) == sizeof(uint64_t) ? (uint64_t) (v) : \
49 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
52 * Cast value `v` to an unsigned integer type of the size of type `type`
53 * *without* sign-extension.
55 * The unsigned cast ensures that we're not shifting a negative value,
56 * which is undefined in C. However, this limits the maximum type size
57 * of `type` to 64-bit. Generate a compile-time error if the size of
58 * `type` is larger than 64-bit.
60 #define _bt_cast_value_to_unsigned_type(type, v) \
61 (sizeof(type) == sizeof(uint8_t) ? \
62 (uint8_t) _bt_cast_value_to_unsigned(v) : \
63 sizeof(type) == sizeof(uint16_t) ? \
64 (uint16_t) _bt_cast_value_to_unsigned(v) : \
65 sizeof(type) == sizeof(uint32_t) ? \
66 (uint32_t) _bt_cast_value_to_unsigned(v) : \
67 sizeof(type) == sizeof(uint64_t) ? \
68 (uint64_t) _bt_cast_value_to_unsigned(v) : \
69 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
72 * _bt_fill_mask evaluates to a "type" integer with all bits set.
74 #define _bt_fill_mask(type) ((type) ~(type) 0)
77 * Left shift a value `v` of `shift` bits.
79 * The type of `v` can be signed or unsigned integer.
80 * The value of `shift` must be less than the size of `v` (in bits),
81 * otherwise the behavior is undefined.
82 * Evaluates to the result of the shift operation.
84 * According to the C99 standard, left shift of a left hand-side signed
85 * type is undefined if it has a negative value or if the result cannot
86 * be represented in the result type. This bitfield header discards the
87 * bits that are left-shifted beyond the result type representation,
88 * which is the behavior of an unsigned type left shift operation.
89 * Therefore, always perform left shift on an unsigned type.
91 * This macro should not be used if `shift` can be greater or equal than
92 * the bitwidth of `v`. See `_bt_safe_lshift`.
94 #define _bt_lshift(v, shift) \
95 ((__typeof__(v)) (_bt_cast_value_to_unsigned(v) << (shift)))
98 * Generate a mask of type `type` with the `length` least significant bits
99 * cleared, and the most significant bits set.
101 #define _bt_make_mask_complement(type, length) \
102 _bt_lshift(_bt_fill_mask(type), length)
105 * Generate a mask of type `type` with the `length` least significant bits
106 * set, and the most significant bits cleared.
108 #define _bt_make_mask(type, length) \
109 ((type) ~_bt_make_mask_complement(type, length))
112 * Right shift a value `v` of `shift` bits.
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.
119 * According to the C99 standard, right shift of a left hand-side signed
120 * type which has a negative value is implementation defined. This
121 * bitfield header relies on the right shift implementation carrying the
122 * sign bit. If the compiler implementation has a different behavior,
123 * emulate carrying the sign bit.
125 * This macro should not be used if `shift` can be greater or equal than
126 * the bitwidth of `v`. See `_bt_safe_rshift`.
128 #if ((-1 >> 1) == -1)
129 #define _bt_rshift(v, shift) ((v) >> (shift))
131 #define _bt_rshift(v, shift) \
132 ((__typeof__(v)) ((_bt_cast_value_to_unsigned(v) >> (shift)) | \
133 ((v) < 0 ? _bt_make_mask_complement(__typeof__(v), \
134 sizeof(v) * CHAR_BIT - (shift)) : 0)))
138 * Right shift a signed or unsigned integer with `shift` value being an
139 * arbitrary number of bits. `v` is modified by this macro. The shift
140 * is transformed into a sequence of `_nr_partial_shifts` consecutive
141 * shift operations, each of a number of bits smaller than the bitwidth
142 * of `v`, ending with a shift of the number of left over bits.
144 #define _bt_safe_rshift(v, shift) \
146 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
147 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
149 for (; _nr_partial_shifts; _nr_partial_shifts--) \
150 (v) = _bt_rshift(v, sizeof(v) * CHAR_BIT - 1); \
151 (v) = _bt_rshift(v, _leftover_bits); \
155 * Left shift a signed or unsigned integer with `shift` value being an
156 * arbitrary number of bits. `v` is modified by this macro. The shift
157 * is transformed into a sequence of `_nr_partial_shifts` consecutive
158 * shift operations, each of a number of bits smaller than the bitwidth
159 * of `v`, ending with a shift of the number of left over bits.
161 #define _bt_safe_lshift(v, shift) \
163 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
164 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
166 for (; _nr_partial_shifts; _nr_partial_shifts--) \
167 (v) = _bt_lshift(v, sizeof(v) * CHAR_BIT - 1); \
168 (v) = _bt_lshift(v, _leftover_bits); \
172 * bt_bitfield_write - write integer to a bitfield in native endianness
174 * Save integer to the bitfield, which starts at the "start" bit, has "len"
176 * The inside of a bitfield is from high bits to low bits.
177 * Uses native endianness.
178 * For unsigned "v", pad MSB with 0 if bitfield is larger than v.
179 * For signed "v", sign-extend v if bitfield is larger than v.
181 * On little endian, bytes are placed from the less significant to the most
182 * significant. Also, consecutive bitfields are placed from lower bits to higher
185 * On big endian, bytes are places from most significant to less significant.
186 * Also, consecutive bitfields are placed from higher to lower bits.
189 #define _bt_bitfield_write_le(ptr, type, start, length, v) \
191 __typeof__(v) _v = (v); \
192 type *_ptr = (void *) (ptr); \
193 unsigned long _start = (start), _length = (length); \
194 type _mask, _cmask; \
195 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
196 unsigned long _start_unit, _end_unit, _this_unit; \
197 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
202 _end = _start + _length; \
203 _start_unit = _start / _ts; \
204 _end_unit = (_end + (_ts - 1)) / _ts; \
206 /* Trim v high bits */ \
207 if (_length < sizeof(_v) * CHAR_BIT) \
208 _v &= _bt_make_mask(__typeof__(_v), _length); \
210 /* We can now append v with a simple "or", shift it piece-wise */ \
211 _this_unit = _start_unit; \
212 if (_start_unit == _end_unit - 1) { \
213 _mask = _bt_make_mask(type, _start % _ts); \
215 _mask |= _bt_make_mask_complement(type, _end % _ts); \
216 _cmask = _bt_lshift((type) (_v), _start % _ts); \
218 _ptr[_this_unit] &= _mask; \
219 _ptr[_this_unit] |= _cmask; \
222 if (_start % _ts) { \
223 _cshift = _start % _ts; \
224 _mask = _bt_make_mask(type, _cshift); \
225 _cmask = _bt_lshift((type) (_v), _cshift); \
227 _ptr[_this_unit] &= _mask; \
228 _ptr[_this_unit] |= _cmask; \
229 _bt_safe_rshift(_v, _ts - _cshift); \
230 _start += _ts - _cshift; \
233 for (; _this_unit < _end_unit - 1; _this_unit++) { \
234 _ptr[_this_unit] = (type) _v; \
235 _bt_safe_rshift(_v, _ts); \
239 _mask = _bt_make_mask_complement(type, _end % _ts); \
240 _cmask = (type) _v; \
242 _ptr[_this_unit] &= _mask; \
243 _ptr[_this_unit] |= _cmask; \
245 _ptr[_this_unit] = (type) _v; \
248 #define _bt_bitfield_write_be(ptr, type, start, length, v) \
250 __typeof__(v) _v = (v); \
251 type *_ptr = (void *) (ptr); \
252 unsigned long _start = (start), _length = (length); \
253 type _mask, _cmask; \
254 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
255 unsigned long _start_unit, _end_unit, _this_unit; \
256 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
261 _end = _start + _length; \
262 _start_unit = _start / _ts; \
263 _end_unit = (_end + (_ts - 1)) / _ts; \
265 /* Trim v high bits */ \
266 if (_length < sizeof(_v) * CHAR_BIT) \
267 _v &= _bt_make_mask(__typeof__(_v), _length); \
269 /* We can now append v with a simple "or", shift it piece-wise */ \
270 _this_unit = _end_unit - 1; \
271 if (_start_unit == _end_unit - 1) { \
272 _mask = _bt_make_mask(type, (_ts - (_end % _ts)) % _ts); \
274 _mask |= _bt_make_mask_complement(type, _ts - (_start % _ts)); \
275 _cmask = _bt_lshift((type) (_v), (_ts - (_end % _ts)) % _ts); \
277 _ptr[_this_unit] &= _mask; \
278 _ptr[_this_unit] |= _cmask; \
282 _cshift = _end % _ts; \
283 _mask = _bt_make_mask(type, _ts - _cshift); \
284 _cmask = _bt_lshift((type) (_v), _ts - _cshift); \
286 _ptr[_this_unit] &= _mask; \
287 _ptr[_this_unit] |= _cmask; \
288 _bt_safe_rshift(_v, _cshift); \
292 for (; (long) _this_unit >= (long) _start_unit + 1; _this_unit--) { \
293 _ptr[_this_unit] = (type) _v; \
294 _bt_safe_rshift(_v, _ts); \
297 if (_start % _ts) { \
298 _mask = _bt_make_mask_complement(type, _ts - (_start % _ts)); \
299 _cmask = (type) _v; \
301 _ptr[_this_unit] &= _mask; \
302 _ptr[_this_unit] |= _cmask; \
304 _ptr[_this_unit] = (type) _v; \
308 * bt_bitfield_write - write integer to a bitfield in native endianness
309 * bt_bitfield_write_le - write integer to a bitfield in little endian
310 * bt_bitfield_write_be - write integer to a bitfield in big endian
313 #if (BYTE_ORDER == LITTLE_ENDIAN)
315 #define bt_bitfield_write(ptr, type, start, length, v) \
316 _bt_bitfield_write_le(ptr, type, start, length, v)
318 #define bt_bitfield_write_le(ptr, type, start, length, v) \
319 _bt_bitfield_write_le(ptr, type, start, length, v)
321 #define bt_bitfield_write_be(ptr, type, start, length, v) \
322 _bt_bitfield_write_be(ptr, unsigned char, start, length, v)
324 #elif (BYTE_ORDER == BIG_ENDIAN)
326 #define bt_bitfield_write(ptr, type, start, length, v) \
327 _bt_bitfield_write_be(ptr, type, start, length, v)
329 #define bt_bitfield_write_le(ptr, type, start, length, v) \
330 _bt_bitfield_write_le(ptr, unsigned char, start, length, v)
332 #define bt_bitfield_write_be(ptr, type, start, length, v) \
333 _bt_bitfield_write_be(ptr, type, start, length, v)
335 #else /* (BYTE_ORDER == PDP_ENDIAN) */
337 #error "Byte order not supported"
341 #define _bt_bitfield_read_le(ptr, type, start, length, vptr) \
343 __typeof__(*(vptr)) *_vptr = (vptr); \
344 __typeof__(*_vptr) _v; \
345 type *_ptr = (type *) (ptr); \
346 unsigned long _start = (start), _length = (length); \
347 type _mask, _cmask; \
348 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
349 unsigned long _start_unit, _end_unit, _this_unit; \
350 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
357 _end = _start + _length; \
358 _start_unit = _start / _ts; \
359 _end_unit = (_end + (_ts - 1)) / _ts; \
361 _this_unit = _end_unit - 1; \
362 if (_bt_is_signed_type(__typeof__(_v)) \
363 && (_ptr[_this_unit] & _bt_lshift((type) 1, (_end % _ts ? _end % _ts : _ts) - 1))) \
364 _v = ~(__typeof__(_v)) 0; \
367 if (_start_unit == _end_unit - 1) { \
368 _cmask = _ptr[_this_unit]; \
369 _cmask = _bt_rshift(_cmask, _start % _ts); \
370 if ((_end - _start) % _ts) { \
371 _mask = _bt_make_mask(type, _end - _start); \
374 _bt_safe_lshift(_v, _end - _start); \
375 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
380 _cshift = _end % _ts; \
381 _mask = _bt_make_mask(type, _cshift); \
382 _cmask = _ptr[_this_unit]; \
384 _bt_safe_lshift(_v, _cshift); \
385 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
389 for (; (long) _this_unit >= (long) _start_unit + 1; _this_unit--) { \
390 _bt_safe_lshift(_v, _ts); \
391 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
394 if (_start % _ts) { \
395 _mask = _bt_make_mask(type, _ts - (_start % _ts)); \
396 _cmask = _ptr[_this_unit]; \
397 _cmask = _bt_rshift(_cmask, _start % _ts); \
399 _bt_safe_lshift(_v, _ts - (_start % _ts)); \
400 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
402 _bt_safe_lshift(_v, _ts); \
403 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
408 #define _bt_bitfield_read_be(ptr, type, start, length, vptr) \
410 __typeof__(*(vptr)) *_vptr = (vptr); \
411 __typeof__(*_vptr) _v; \
412 type *_ptr = (void *) (ptr); \
413 unsigned long _start = (start), _length = (length); \
414 type _mask, _cmask; \
415 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
416 unsigned long _start_unit, _end_unit, _this_unit; \
417 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
424 _end = _start + _length; \
425 _start_unit = _start / _ts; \
426 _end_unit = (_end + (_ts - 1)) / _ts; \
428 _this_unit = _start_unit; \
429 if (_bt_is_signed_type(__typeof__(_v)) \
430 && (_ptr[_this_unit] & _bt_lshift((type) 1, _ts - (_start % _ts) - 1))) \
431 _v = ~(__typeof__(_v)) 0; \
434 if (_start_unit == _end_unit - 1) { \
435 _cmask = _ptr[_this_unit]; \
436 _cmask = _bt_rshift(_cmask, (_ts - (_end % _ts)) % _ts); \
437 if ((_end - _start) % _ts) { \
438 _mask = _bt_make_mask(type, _end - _start); \
441 _bt_safe_lshift(_v, _end - _start); \
442 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
446 if (_start % _ts) { \
447 _cshift = _start % _ts; \
448 _mask = _bt_make_mask(type, _ts - _cshift); \
449 _cmask = _ptr[_this_unit]; \
451 _bt_safe_lshift(_v, _ts - _cshift); \
452 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
453 _start += _ts - _cshift; \
456 for (; _this_unit < _end_unit - 1; _this_unit++) { \
457 _bt_safe_lshift(_v, _ts); \
458 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
462 _mask = _bt_make_mask(type, _end % _ts); \
463 _cmask = _ptr[_this_unit]; \
464 _cmask = _bt_rshift(_cmask, _ts - (_end % _ts)); \
466 _bt_safe_lshift(_v, _end % _ts); \
467 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
469 _bt_safe_lshift(_v, _ts); \
470 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
476 * bt_bitfield_read - read integer from a bitfield in native endianness
477 * bt_bitfield_read_le - read integer from a bitfield in little endian
478 * bt_bitfield_read_be - read integer from a bitfield in big endian
481 #if (BYTE_ORDER == LITTLE_ENDIAN)
483 #define bt_bitfield_read(ptr, type, start, length, vptr) \
484 _bt_bitfield_read_le(ptr, type, start, length, vptr)
486 #define bt_bitfield_read_le(ptr, type, start, length, vptr) \
487 _bt_bitfield_read_le(ptr, type, start, length, vptr)
489 #define bt_bitfield_read_be(ptr, type, start, length, vptr) \
490 _bt_bitfield_read_be(ptr, unsigned char, start, length, vptr)
492 #elif (BYTE_ORDER == BIG_ENDIAN)
494 #define bt_bitfield_read(ptr, type, start, length, vptr) \
495 _bt_bitfield_read_be(ptr, type, start, length, vptr)
497 #define bt_bitfield_read_le(ptr, type, start, length, vptr) \
498 _bt_bitfield_read_le(ptr, unsigned char, start, length, vptr)
500 #define bt_bitfield_read_be(ptr, type, start, length, vptr) \
501 _bt_bitfield_read_be(ptr, type, start, length, vptr)
503 #else /* (BYTE_ORDER == PDP_ENDIAN) */
505 #error "Byte order not supported"
509 #endif /* _BABELTRACE_BITFIELD_H */
This page took 0.039788 seconds and 4 git commands to generate.