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.
35 #define _BT_BUILD_ASSERT(cond) \
36 sizeof(struct { int f:(2 * !!(cond) - 1); })
39 * Cast value `v` to an unsigned integer of the same size as `v`.
41 #define _bt_cast_value_to_unsigned(v) \
42 (sizeof(v) == sizeof(uint8_t) ? (uint8_t) (v) : \
43 sizeof(v) == sizeof(uint16_t) ? (uint16_t) (v) : \
44 sizeof(v) == sizeof(uint32_t) ? (uint32_t) (v) : \
45 sizeof(v) == sizeof(uint64_t) ? (uint64_t) (v) : \
46 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
49 * Cast value `v` to an unsigned integer type of the size of type `type`
50 * *without* sign-extension.
52 * The unsigned cast ensures that we're not shifting a negative value,
53 * which is undefined in C. However, this limits the maximum type size
54 * of `type` to 64-bit. Generate a compile-time error if the size of
55 * `type` is larger than 64-bit.
57 #define _bt_cast_value_to_unsigned_type(type, v) \
58 (sizeof(type) == sizeof(uint8_t) ? \
59 (uint8_t) _bt_cast_value_to_unsigned(v) : \
60 sizeof(type) == sizeof(uint16_t) ? \
61 (uint16_t) _bt_cast_value_to_unsigned(v) : \
62 sizeof(type) == sizeof(uint32_t) ? \
63 (uint32_t) _bt_cast_value_to_unsigned(v) : \
64 sizeof(type) == sizeof(uint64_t) ? \
65 (uint64_t) _bt_cast_value_to_unsigned(v) : \
66 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
69 * _bt_fill_mask evaluates to a "type" integer with all bits set.
71 #define _bt_fill_mask(type) ((type) ~(type) 0)
74 * Left shift a value `v` of `shift` bits.
76 * The type of `v` can be signed or unsigned integer.
77 * The value of `shift` must be less than the size of `v` (in bits),
78 * otherwise the behavior is undefined.
79 * Evaluates to the result of the shift operation.
81 * According to the C99 standard, left shift of a left hand-side signed
82 * type is undefined if it has a negative value or if the result cannot
83 * be represented in the result type. This bitfield header discards the
84 * bits that are left-shifted beyond the result type representation,
85 * which is the behavior of an unsigned type left shift operation.
86 * Therefore, always perform left shift on an unsigned type.
88 * This macro should not be used if `shift` can be greater or equal than
89 * the bitwidth of `v`. See `_bt_safe_lshift`.
91 #define _bt_lshift(v, shift) \
92 ((__typeof__(v)) (_bt_cast_value_to_unsigned(v) << (shift)))
95 * Generate a mask of type `type` with the `length` least significant bits
96 * cleared, and the most significant bits set.
98 #define _bt_make_mask_complement(type, length) \
99 _bt_lshift(_bt_fill_mask(type), length)
102 * Generate a mask of type `type` with the `length` least significant bits
103 * set, and the most significant bits cleared.
105 #define _bt_make_mask(type, length) \
106 ((type) ~_bt_make_mask_complement(type, length))
109 * Right shift a value `v` of `shift` bits.
111 * The type of `v` can be signed or unsigned integer.
112 * The value of `shift` must be less than the size of `v` (in bits),
113 * otherwise the behavior is undefined.
114 * Evaluates to the result of the shift operation.
116 * According to the C99 standard, right shift of a left hand-side signed
117 * type which has a negative value is implementation defined. This
118 * bitfield header relies on the right shift implementation carrying the
119 * sign bit. If the compiler implementation has a different behavior,
120 * emulate carrying the sign bit.
122 * This macro should not be used if `shift` can be greater or equal than
123 * the bitwidth of `v`. See `_bt_safe_rshift`.
125 #if ((-1 >> 1) == -1)
126 #define _bt_rshift(v, shift) ((v) >> (shift))
128 #define _bt_rshift(v, shift) \
129 ((__typeof__(v)) ((_bt_cast_value_to_unsigned(v) >> (shift)) | \
130 ((v) < 0 ? _bt_make_mask_complement(__typeof__(v), \
131 sizeof(v) * CHAR_BIT - (shift)) : 0)))
135 * Right shift a signed or unsigned integer with `shift` value being an
136 * arbitrary number of bits. `v` is modified by this macro. The shift
137 * is transformed into a sequence of `_nr_partial_shifts` consecutive
138 * shift operations, each of a number of bits smaller than the bitwidth
139 * of `v`, ending with a shift of the number of left over bits.
141 #define _bt_safe_rshift(v, shift) \
143 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
144 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
146 for (; _nr_partial_shifts; _nr_partial_shifts--) \
147 (v) = _bt_rshift(v, sizeof(v) * CHAR_BIT - 1); \
148 (v) = _bt_rshift(v, _leftover_bits); \
152 * Left shift a signed or unsigned integer with `shift` value being an
153 * arbitrary number of bits. `v` is modified by this macro. The shift
154 * is transformed into a sequence of `_nr_partial_shifts` consecutive
155 * shift operations, each of a number of bits smaller than the bitwidth
156 * of `v`, ending with a shift of the number of left over bits.
158 #define _bt_safe_lshift(v, shift) \
160 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
161 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
163 for (; _nr_partial_shifts; _nr_partial_shifts--) \
164 (v) = _bt_lshift(v, sizeof(v) * CHAR_BIT - 1); \
165 (v) = _bt_lshift(v, _leftover_bits); \
169 * bt_bitfield_write - write integer to a bitfield in native endianness
171 * Save integer to the bitfield, which starts at the "start" bit, has "len"
173 * The inside of a bitfield is from high bits to low bits.
174 * Uses native endianness.
175 * For unsigned "v", pad MSB with 0 if bitfield is larger than v.
176 * For signed "v", sign-extend v if bitfield is larger than v.
178 * On little endian, bytes are placed from the less significant to the most
179 * significant. Also, consecutive bitfields are placed from lower bits to higher
182 * On big endian, bytes are places from most significant to less significant.
183 * Also, consecutive bitfields are placed from higher to lower bits.
186 #define _bt_bitfield_write_le(ptr, type, start, length, v) \
188 __typeof__(v) _v = (v); \
189 type *_ptr = (void *) (ptr); \
190 unsigned long _start = (start), _length = (length); \
191 type _mask, _cmask; \
192 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
193 unsigned long _start_unit, _end_unit, _this_unit; \
194 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
199 _end = _start + _length; \
200 _start_unit = _start / _ts; \
201 _end_unit = (_end + (_ts - 1)) / _ts; \
203 /* Trim v high bits */ \
204 if (_length < sizeof(_v) * CHAR_BIT) \
205 _v &= _bt_make_mask(__typeof__(_v), _length); \
207 /* We can now append v with a simple "or", shift it piece-wise */ \
208 _this_unit = _start_unit; \
209 if (_start_unit == _end_unit - 1) { \
210 _mask = _bt_make_mask(type, _start % _ts); \
212 _mask |= _bt_make_mask_complement(type, _end % _ts); \
213 _cmask = _bt_lshift((type) (_v), _start % _ts); \
215 _ptr[_this_unit] &= _mask; \
216 _ptr[_this_unit] |= _cmask; \
219 if (_start % _ts) { \
220 _cshift = _start % _ts; \
221 _mask = _bt_make_mask(type, _cshift); \
222 _cmask = _bt_lshift((type) (_v), _cshift); \
224 _ptr[_this_unit] &= _mask; \
225 _ptr[_this_unit] |= _cmask; \
226 _bt_safe_rshift(_v, _ts - _cshift); \
227 _start += _ts - _cshift; \
230 for (; _this_unit < _end_unit - 1; _this_unit++) { \
231 _ptr[_this_unit] = (type) _v; \
232 _bt_safe_rshift(_v, _ts); \
236 _mask = _bt_make_mask_complement(type, _end % _ts); \
237 _cmask = (type) _v; \
239 _ptr[_this_unit] &= _mask; \
240 _ptr[_this_unit] |= _cmask; \
242 _ptr[_this_unit] = (type) _v; \
245 #define _bt_bitfield_write_be(ptr, type, start, length, v) \
247 __typeof__(v) _v = (v); \
248 type *_ptr = (void *) (ptr); \
249 unsigned long _start = (start), _length = (length); \
250 type _mask, _cmask; \
251 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
252 unsigned long _start_unit, _end_unit, _this_unit; \
253 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
258 _end = _start + _length; \
259 _start_unit = _start / _ts; \
260 _end_unit = (_end + (_ts - 1)) / _ts; \
262 /* Trim v high bits */ \
263 if (_length < sizeof(_v) * CHAR_BIT) \
264 _v &= _bt_make_mask(__typeof__(_v), _length); \
266 /* We can now append v with a simple "or", shift it piece-wise */ \
267 _this_unit = _end_unit - 1; \
268 if (_start_unit == _end_unit - 1) { \
269 _mask = _bt_make_mask(type, (_ts - (_end % _ts)) % _ts); \
271 _mask |= _bt_make_mask_complement(type, _ts - (_start % _ts)); \
272 _cmask = _bt_lshift((type) (_v), (_ts - (_end % _ts)) % _ts); \
274 _ptr[_this_unit] &= _mask; \
275 _ptr[_this_unit] |= _cmask; \
279 _cshift = _end % _ts; \
280 _mask = _bt_make_mask(type, _ts - _cshift); \
281 _cmask = _bt_lshift((type) (_v), _ts - _cshift); \
283 _ptr[_this_unit] &= _mask; \
284 _ptr[_this_unit] |= _cmask; \
285 _bt_safe_rshift(_v, _cshift); \
289 for (; (long) _this_unit >= (long) _start_unit + 1; _this_unit--) { \
290 _ptr[_this_unit] = (type) _v; \
291 _bt_safe_rshift(_v, _ts); \
294 if (_start % _ts) { \
295 _mask = _bt_make_mask_complement(type, _ts - (_start % _ts)); \
296 _cmask = (type) _v; \
298 _ptr[_this_unit] &= _mask; \
299 _ptr[_this_unit] |= _cmask; \
301 _ptr[_this_unit] = (type) _v; \
305 * bt_bitfield_write - write integer to a bitfield in native endianness
306 * bt_bitfield_write_le - write integer to a bitfield in little endian
307 * bt_bitfield_write_be - write integer to a bitfield in big endian
310 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
312 #define bt_bitfield_write(ptr, type, start, length, v) \
313 _bt_bitfield_write_le(ptr, type, start, length, v)
315 #define bt_bitfield_write_le(ptr, type, start, length, v) \
316 _bt_bitfield_write_le(ptr, type, start, length, v)
318 #define bt_bitfield_write_be(ptr, type, start, length, v) \
319 _bt_bitfield_write_be(ptr, unsigned char, start, length, v)
321 #elif (__BYTE_ORDER == __BIG_ENDIAN)
323 #define bt_bitfield_write(ptr, type, start, length, v) \
324 _bt_bitfield_write_be(ptr, type, start, length, v)
326 #define bt_bitfield_write_le(ptr, type, start, length, v) \
327 _bt_bitfield_write_le(ptr, unsigned char, start, length, v)
329 #define bt_bitfield_write_be(ptr, type, start, length, v) \
330 _bt_bitfield_write_be(ptr, type, start, length, v)
332 #else /* (__BYTE_ORDER == __PDP_ENDIAN) */
334 #error "Byte order not supported"
338 #define _bt_bitfield_read_le(ptr, type, start, length, vptr) \
340 __typeof__(*(vptr)) *_vptr = (vptr); \
341 __typeof__(*_vptr) _v; \
342 type *_ptr = (void *) (ptr); \
343 unsigned long _start = (start), _length = (length); \
344 type _mask, _cmask; \
345 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
346 unsigned long _start_unit, _end_unit, _this_unit; \
347 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
354 _end = _start + _length; \
355 _start_unit = _start / _ts; \
356 _end_unit = (_end + (_ts - 1)) / _ts; \
358 _this_unit = _end_unit - 1; \
359 if (_bt_is_signed_type(__typeof__(_v)) \
360 && (_ptr[_this_unit] & _bt_lshift((type) 1, (_end % _ts ? _end % _ts : _ts) - 1))) \
361 _v = ~(__typeof__(_v)) 0; \
364 if (_start_unit == _end_unit - 1) { \
365 _cmask = _ptr[_this_unit]; \
366 _cmask = _bt_rshift(_cmask, _start % _ts); \
367 if ((_end - _start) % _ts) { \
368 _mask = _bt_make_mask(type, _end - _start); \
371 _bt_safe_lshift(_v, _end - _start); \
372 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
377 _cshift = _end % _ts; \
378 _mask = _bt_make_mask(type, _cshift); \
379 _cmask = _ptr[_this_unit]; \
381 _bt_safe_lshift(_v, _cshift); \
382 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
386 for (; (long) _this_unit >= (long) _start_unit + 1; _this_unit--) { \
387 _bt_safe_lshift(_v, _ts); \
388 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
391 if (_start % _ts) { \
392 _mask = _bt_make_mask(type, _ts - (_start % _ts)); \
393 _cmask = _ptr[_this_unit]; \
394 _cmask = _bt_rshift(_cmask, _start % _ts); \
396 _bt_safe_lshift(_v, _ts - (_start % _ts)); \
397 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
399 _bt_safe_lshift(_v, _ts); \
400 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
405 #define _bt_bitfield_read_be(ptr, type, start, length, vptr) \
407 __typeof__(*(vptr)) *_vptr = (vptr); \
408 __typeof__(*_vptr) _v; \
409 type *_ptr = (void *) (ptr); \
410 unsigned long _start = (start), _length = (length); \
411 type _mask, _cmask; \
412 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
413 unsigned long _start_unit, _end_unit, _this_unit; \
414 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
421 _end = _start + _length; \
422 _start_unit = _start / _ts; \
423 _end_unit = (_end + (_ts - 1)) / _ts; \
425 _this_unit = _start_unit; \
426 if (_bt_is_signed_type(__typeof__(_v)) \
427 && (_ptr[_this_unit] & _bt_lshift((type) 1, _ts - (_start % _ts) - 1))) \
428 _v = ~(__typeof__(_v)) 0; \
431 if (_start_unit == _end_unit - 1) { \
432 _cmask = _ptr[_this_unit]; \
433 _cmask = _bt_rshift(_cmask, (_ts - (_end % _ts)) % _ts); \
434 if ((_end - _start) % _ts) { \
435 _mask = _bt_make_mask(type, _end - _start); \
438 _bt_safe_lshift(_v, _end - _start); \
439 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
443 if (_start % _ts) { \
444 _cshift = _start % _ts; \
445 _mask = _bt_make_mask(type, _ts - _cshift); \
446 _cmask = _ptr[_this_unit]; \
448 _bt_safe_lshift(_v, _ts - _cshift); \
449 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
450 _start += _ts - _cshift; \
453 for (; _this_unit < _end_unit - 1; _this_unit++) { \
454 _bt_safe_lshift(_v, _ts); \
455 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
459 _mask = _bt_make_mask(type, _end % _ts); \
460 _cmask = _ptr[_this_unit]; \
461 _cmask = _bt_rshift(_cmask, _ts - (_end % _ts)); \
463 _bt_safe_lshift(_v, _end % _ts); \
464 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
466 _bt_safe_lshift(_v, _ts); \
467 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
473 * bt_bitfield_read - read integer from a bitfield in native endianness
474 * bt_bitfield_read_le - read integer from a bitfield in little endian
475 * bt_bitfield_read_be - read integer from a bitfield in big endian
478 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
480 #define bt_bitfield_read(ptr, type, start, length, vptr) \
481 _bt_bitfield_read_le(ptr, type, start, length, vptr)
483 #define bt_bitfield_read_le(ptr, type, start, length, vptr) \
484 _bt_bitfield_read_le(ptr, type, start, length, vptr)
486 #define bt_bitfield_read_be(ptr, type, start, length, vptr) \
487 _bt_bitfield_read_be(ptr, unsigned char, start, length, vptr)
489 #elif (__BYTE_ORDER == __BIG_ENDIAN)
491 #define bt_bitfield_read(ptr, type, start, length, vptr) \
492 _bt_bitfield_read_be(ptr, type, start, length, vptr)
494 #define bt_bitfield_read_le(ptr, type, start, length, vptr) \
495 _bt_bitfield_read_le(ptr, unsigned char, start, length, vptr)
497 #define bt_bitfield_read_be(ptr, type, start, length, vptr) \
498 _bt_bitfield_read_be(ptr, type, start, length, vptr)
500 #else /* (__BYTE_ORDER == __PDP_ENDIAN) */
502 #error "Byte order not supported"
506 #endif /* _BABELTRACE_BITFIELD_H */
This page took 0.040059 seconds and 5 git commands to generate.