42521f2e3f6741826ea9a74cc5d49a89786559fc
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.
34 # define _BT_DIAG_PUSH _Pragma("GCC diagnostic push")
35 # define _BT_DIAG_POP _Pragma("GCC diagnostic pop")
37 # define _BT_DIAG_STRINGIFY_1(x) #x
38 # define _BT_DIAG_STRINGIFY(x) _BT_DIAG_STRINGIFY_1(x)
40 # define _BT_DIAG_IGNORE(option) \
41 _Pragma(_BT_DIAG_STRINGIFY(GCC diagnostic ignored option))
42 # define _BT_DIAG_IGNORE_TYPE_LIMITS _BT_DIAG_IGNORE("-Wtype-limits")
44 # define _BT_DIAG_PUSH
46 # define _BT_DIAG_IGNORE
49 #define _bt_is_signed_type(type) ((type) -1 < (type) 0)
52 * Produce a build-time error if the condition `cond` is non-zero.
53 * Evaluates as a size_t expression.
55 #define _BT_BUILD_ASSERT(cond) \
56 sizeof(struct { int f:(2 * !!(cond) - 1); })
59 * Cast value `v` to an unsigned integer of the same size as `v`.
61 #define _bt_cast_value_to_unsigned(v) \
62 (sizeof(v) == sizeof(uint8_t) ? (uint8_t) (v) : \
63 sizeof(v) == sizeof(uint16_t) ? (uint16_t) (v) : \
64 sizeof(v) == sizeof(uint32_t) ? (uint32_t) (v) : \
65 sizeof(v) == sizeof(uint64_t) ? (uint64_t) (v) : \
66 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
69 * Cast value `v` to an unsigned integer type of the size of type `type`
70 * *without* sign-extension.
72 * The unsigned cast ensures that we're not shifting a negative value,
73 * which is undefined in C. However, this limits the maximum type size
74 * of `type` to 64-bit. Generate a compile-time error if the size of
75 * `type` is larger than 64-bit.
77 #define _bt_cast_value_to_unsigned_type(type, v) \
78 (sizeof(type) == sizeof(uint8_t) ? \
79 (uint8_t) _bt_cast_value_to_unsigned(v) : \
80 sizeof(type) == sizeof(uint16_t) ? \
81 (uint16_t) _bt_cast_value_to_unsigned(v) : \
82 sizeof(type) == sizeof(uint32_t) ? \
83 (uint32_t) _bt_cast_value_to_unsigned(v) : \
84 sizeof(type) == sizeof(uint64_t) ? \
85 (uint64_t) _bt_cast_value_to_unsigned(v) : \
86 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
89 * _bt_fill_mask evaluates to a "type" integer with all bits set.
91 #define _bt_fill_mask(type) ((type) ~(type) 0)
94 * Left shift a value `v` of `shift` bits.
96 * The type of `v` can be signed or unsigned integer.
97 * The value of `shift` must be less than the size of `v` (in bits),
98 * otherwise the behavior is undefined.
99 * Evaluates to the result of the shift operation.
101 * According to the C99 standard, left shift of a left hand-side signed
102 * type is undefined if it has a negative value or if the result cannot
103 * be represented in the result type. This bitfield header discards the
104 * bits that are left-shifted beyond the result type representation,
105 * which is the behavior of an unsigned type left shift operation.
106 * Therefore, always perform left shift on an unsigned type.
108 * This macro should not be used if `shift` can be greater or equal than
109 * the bitwidth of `v`. See `_bt_safe_lshift`.
111 #define _bt_lshift(v, shift) \
112 ((__typeof__(v)) (_bt_cast_value_to_unsigned(v) << (shift)))
115 * Generate a mask of type `type` with the `length` least significant bits
116 * cleared, and the most significant bits set.
118 #define _bt_make_mask_complement(type, length) \
119 _bt_lshift(_bt_fill_mask(type), length)
122 * Generate a mask of type `type` with the `length` least significant bits
123 * set, and the most significant bits cleared.
125 #define _bt_make_mask(type, length) \
126 ((type) ~_bt_make_mask_complement(type, length))
129 * Right shift a value `v` of `shift` bits.
131 * The type of `v` can be signed or unsigned integer.
132 * The value of `shift` must be less than the size of `v` (in bits),
133 * otherwise the behavior is undefined.
134 * Evaluates to the result of the shift operation.
136 * According to the C99 standard, right shift of a left hand-side signed
137 * type which has a negative value is implementation defined. This
138 * bitfield header relies on the right shift implementation carrying the
139 * sign bit. If the compiler implementation has a different behavior,
140 * emulate carrying the sign bit.
142 * This macro should not be used if `shift` can be greater or equal than
143 * the bitwidth of `v`. See `_bt_safe_rshift`.
145 #if ((-1 >> 1) == -1)
146 #define _bt_rshift(v, shift) ((v) >> (shift))
148 #define _bt_rshift(v, shift) \
149 ((__typeof__(v)) ((_bt_cast_value_to_unsigned(v) >> (shift)) | \
150 ((v) < 0 ? _bt_make_mask_complement(__typeof__(v), \
151 sizeof(v) * CHAR_BIT - (shift)) : 0)))
155 * Right 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_rshift(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_rshift(v, sizeof(v) * CHAR_BIT - 1); \
168 (v) = _bt_rshift(v, _leftover_bits); \
172 * Left shift a signed or unsigned integer with `shift` value being an
173 * arbitrary number of bits. `v` is modified by this macro. The shift
174 * is transformed into a sequence of `_nr_partial_shifts` consecutive
175 * shift operations, each of a number of bits smaller than the bitwidth
176 * of `v`, ending with a shift of the number of left over bits.
178 #define _bt_safe_lshift(v, shift) \
180 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
181 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
183 for (; _nr_partial_shifts; _nr_partial_shifts--) \
184 (v) = _bt_lshift(v, sizeof(v) * CHAR_BIT - 1); \
185 (v) = _bt_lshift(v, _leftover_bits); \
189 * bt_bitfield_write - write integer to a bitfield in native endianness
191 * Save integer to the bitfield, which starts at the "start" bit, has "len"
193 * The inside of a bitfield is from high bits to low bits.
194 * Uses native endianness.
195 * For unsigned "v", pad MSB with 0 if bitfield is larger than v.
196 * For signed "v", sign-extend v if bitfield is larger than v.
198 * On little endian, bytes are placed from the less significant to the most
199 * significant. Also, consecutive bitfields are placed from lower bits to higher
202 * On big endian, bytes are places from most significant to less significant.
203 * Also, consecutive bitfields are placed from higher to lower bits.
206 #define _bt_bitfield_write_le(_ptr, type, _start, _length, _v) \
208 __typeof__(_v) __v = (_v); \
209 type *__ptr = (void *) (_ptr); \
210 unsigned long __start = (_start), __length = (_length); \
212 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
213 unsigned long start_unit, end_unit, this_unit; \
214 unsigned long end, cshift; /* cshift is "complement shift" */ \
219 end = __start + __length; \
220 start_unit = __start / ts; \
221 end_unit = (end + (ts - 1)) / ts; \
223 /* Trim v high bits */ \
224 if (__length < sizeof(__v) * CHAR_BIT) \
225 __v &= _bt_make_mask(__typeof__(__v), __length); \
227 /* We can now append v with a simple "or", shift it piece-wise */ \
228 this_unit = start_unit; \
229 if (start_unit == end_unit - 1) { \
230 mask = _bt_make_mask(type, __start % ts); \
232 mask |= _bt_make_mask_complement(type, end % ts); \
233 cmask = _bt_lshift((type) (__v), __start % ts); \
235 __ptr[this_unit] &= mask; \
236 __ptr[this_unit] |= cmask; \
239 if (__start % ts) { \
240 cshift = __start % ts; \
241 mask = _bt_make_mask(type, cshift); \
242 cmask = _bt_lshift((type) (__v), cshift); \
244 __ptr[this_unit] &= mask; \
245 __ptr[this_unit] |= cmask; \
246 _bt_safe_rshift(__v, ts - cshift); \
247 __start += ts - cshift; \
250 for (; this_unit < end_unit - 1; this_unit++) { \
251 __ptr[this_unit] = (type) __v; \
252 _bt_safe_rshift(__v, ts); \
256 mask = _bt_make_mask_complement(type, end % ts); \
257 cmask = (type) __v; \
259 __ptr[this_unit] &= mask; \
260 __ptr[this_unit] |= cmask; \
262 __ptr[this_unit] = (type) __v; \
265 #define _bt_bitfield_write_be(_ptr, type, _start, _length, _v) \
267 __typeof__(_v) __v = (_v); \
268 type *__ptr = (void *) (_ptr); \
269 unsigned long __start = (_start), __length = (_length); \
271 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
272 unsigned long start_unit, end_unit, this_unit; \
273 unsigned long end, cshift; /* cshift is "complement shift" */ \
278 end = __start + __length; \
279 start_unit = __start / ts; \
280 end_unit = (end + (ts - 1)) / ts; \
282 /* Trim v high bits */ \
283 if (__length < sizeof(__v) * CHAR_BIT) \
284 __v &= _bt_make_mask(__typeof__(__v), __length); \
286 /* We can now append v with a simple "or", shift it piece-wise */ \
287 this_unit = end_unit - 1; \
288 if (start_unit == end_unit - 1) { \
289 mask = _bt_make_mask(type, (ts - (end % ts)) % ts); \
291 mask |= _bt_make_mask_complement(type, ts - (__start % ts)); \
292 cmask = _bt_lshift((type) (__v), (ts - (end % ts)) % ts); \
294 __ptr[this_unit] &= mask; \
295 __ptr[this_unit] |= cmask; \
300 mask = _bt_make_mask(type, ts - cshift); \
301 cmask = _bt_lshift((type) (__v), ts - cshift); \
303 __ptr[this_unit] &= mask; \
304 __ptr[this_unit] |= cmask; \
305 _bt_safe_rshift(__v, cshift); \
309 for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \
310 __ptr[this_unit] = (type) __v; \
311 _bt_safe_rshift(__v, ts); \
314 if (__start % ts) { \
315 mask = _bt_make_mask_complement(type, ts - (__start % ts)); \
316 cmask = (type) __v; \
318 __ptr[this_unit] &= mask; \
319 __ptr[this_unit] |= cmask; \
321 __ptr[this_unit] = (type) __v; \
325 * bt_bitfield_write - write integer to a bitfield in native endianness
326 * bt_bitfield_write_le - write integer to a bitfield in little endian
327 * bt_bitfield_write_be - write integer to a bitfield in big endian
330 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
332 #define bt_bitfield_write(ptr, type, _start, _length, _v) \
333 _bt_bitfield_write_le(ptr, type, _start, _length, _v)
335 #define bt_bitfield_write_le(ptr, type, _start, _length, _v) \
336 _bt_bitfield_write_le(ptr, type, _start, _length, _v)
338 #define bt_bitfield_write_be(ptr, type, _start, _length, _v) \
339 _bt_bitfield_write_be(ptr, unsigned char, _start, _length, _v)
341 #elif (__BYTE_ORDER == __BIG_ENDIAN)
343 #define bt_bitfield_write(ptr, type, _start, _length, _v) \
344 _bt_bitfield_write_be(ptr, type, _start, _length, _v)
346 #define bt_bitfield_write_le(ptr, type, _start, _length, _v) \
347 _bt_bitfield_write_le(ptr, unsigned char, _start, _length, _v)
349 #define bt_bitfield_write_be(ptr, type, _start, _length, _v) \
350 _bt_bitfield_write_be(ptr, type, _start, _length, _v)
352 #else /* (__BYTE_ORDER == __PDP_ENDIAN) */
354 #error "Byte order not supported"
358 #define _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
360 __typeof__(*(_vptr)) *__vptr = (_vptr); \
361 __typeof__(*__vptr) __v; \
362 type *__ptr = (void *) (_ptr); \
363 unsigned long __start = (_start), __length = (_length); \
365 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
366 unsigned long start_unit, end_unit, this_unit; \
367 unsigned long end, cshift; /* cshift is "complement shift" */ \
368 bool is_signed_type; \
375 end = __start + __length; \
376 start_unit = __start / ts; \
377 end_unit = (end + (ts - 1)) / ts; \
379 this_unit = end_unit - 1; \
381 _BT_DIAG_IGNORE_TYPE_LIMITS \
382 is_signed_type = _bt_is_signed_type(__typeof__(__v)); \
385 && (__ptr[this_unit] & _bt_lshift((type) 1, (end % ts ? end % ts : ts) - 1))) \
386 __v = ~(__typeof__(__v)) 0; \
389 if (start_unit == end_unit - 1) { \
390 cmask = __ptr[this_unit]; \
391 cmask = _bt_rshift(cmask, __start % ts); \
392 if ((end - __start) % ts) { \
393 mask = _bt_make_mask(type, end - __start); \
396 _bt_safe_lshift(__v, end - __start); \
397 __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), cmask); \
403 mask = _bt_make_mask(type, cshift); \
404 cmask = __ptr[this_unit]; \
406 _bt_safe_lshift(__v, cshift); \
407 __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), cmask); \
411 for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \
412 _bt_safe_lshift(__v, ts); \
413 __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), __ptr[this_unit]); \
416 if (__start % ts) { \
417 mask = _bt_make_mask(type, ts - (__start % ts)); \
418 cmask = __ptr[this_unit]; \
419 cmask = _bt_rshift(cmask, __start % ts); \
421 _bt_safe_lshift(__v, ts - (__start % ts)); \
422 __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), cmask); \
424 _bt_safe_lshift(__v, ts); \
425 __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), __ptr[this_unit]); \
430 #define _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
432 __typeof__(*(_vptr)) *__vptr = (_vptr); \
433 __typeof__(*__vptr) __v; \
434 type *__ptr = (void *) (_ptr); \
435 unsigned long __start = (_start), __length = (_length); \
437 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
438 unsigned long start_unit, end_unit, this_unit; \
439 unsigned long end, cshift; /* cshift is "complement shift" */ \
440 bool is_signed_type; \
447 end = __start + __length; \
448 start_unit = __start / ts; \
449 end_unit = (end + (ts - 1)) / ts; \
451 this_unit = start_unit; \
453 _BT_DIAG_IGNORE_TYPE_LIMITS \
454 is_signed_type = _bt_is_signed_type(__typeof__(__v)); \
457 && (__ptr[this_unit] & _bt_lshift((type) 1, ts - (__start % ts) - 1))) \
458 __v = ~(__typeof__(__v)) 0; \
461 if (start_unit == end_unit - 1) { \
462 cmask = __ptr[this_unit]; \
463 cmask = _bt_rshift(cmask, (ts - (end % ts)) % ts); \
464 if ((end - __start) % ts) { \
465 mask = _bt_make_mask(type, end - __start); \
468 _bt_safe_lshift(__v, end - __start); \
469 __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), cmask); \
473 if (__start % ts) { \
474 cshift = __start % ts; \
475 mask = _bt_make_mask(type, ts - cshift); \
476 cmask = __ptr[this_unit]; \
478 _bt_safe_lshift(__v, ts - cshift); \
479 __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), cmask); \
480 __start += ts - cshift; \
483 for (; this_unit < end_unit - 1; this_unit++) { \
484 _bt_safe_lshift(__v, ts); \
485 __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), __ptr[this_unit]); \
489 mask = _bt_make_mask(type, end % ts); \
490 cmask = __ptr[this_unit]; \
491 cmask = _bt_rshift(cmask, ts - (end % ts)); \
493 _bt_safe_lshift(__v, end % ts); \
494 __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), cmask); \
496 _bt_safe_lshift(__v, ts); \
497 __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), __ptr[this_unit]); \
503 * bt_bitfield_read - read integer from a bitfield in native endianness
504 * bt_bitfield_read_le - read integer from a bitfield in little endian
505 * bt_bitfield_read_be - read integer from a bitfield in big endian
508 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
510 #define bt_bitfield_read(_ptr, type, _start, _length, _vptr) \
511 _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr)
513 #define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
514 _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr)
516 #define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
517 _bt_bitfield_read_be(_ptr, unsigned char, _start, _length, _vptr)
519 #elif (__BYTE_ORDER == __BIG_ENDIAN)
521 #define bt_bitfield_read(_ptr, type, _start, _length, _vptr) \
522 _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr)
524 #define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
525 _bt_bitfield_read_le(_ptr, unsigned char, _start, _length, _vptr)
527 #define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
528 _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr)
530 #else /* (__BYTE_ORDER == __PDP_ENDIAN) */
532 #error "Byte order not supported"
536 #endif /* _BABELTRACE_BITFIELD_H */
This page took 0.040953 seconds and 3 git commands to generate.