92f1c2c988bcd0d2bfe3aa9aa137a0b5d6f8d272
1 #ifndef _BABELTRACE_BITFIELD_H
2 #define _BABELTRACE_BITFIELD_H
5 * Copyright 2010-2019 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #include <stdint.h> /* C99 5.2.4.2 Numerical limits */
27 #include <limits.h> /* C99 5.2.4.2 Numerical limits */
28 #include <stdbool.h> /* C99 7.16 bool type */
29 #include <lttng/ust-endian.h> /* Non-standard BIG_ENDIAN, LITTLE_ENDIAN, BYTE_ORDER */
32 * This header strictly follows the C99 standard, except for use of the
33 * compiler-specific __typeof__.
37 * This bitfield header requires the compiler representation of signed
38 * integers to be two's complement.
41 #error "bitfield.h requires the compiler representation of signed integers to be two's complement."
45 * _bt_is_signed_type() willingly generates comparison of unsigned
46 * expression < 0, which is always false. Silence compiler warnings.
49 # define _BT_DIAG_PUSH _Pragma("GCC diagnostic push")
50 # define _BT_DIAG_POP _Pragma("GCC diagnostic pop")
52 # define _BT_DIAG_STRINGIFY_1(x) #x
53 # define _BT_DIAG_STRINGIFY(x) _BT_DIAG_STRINGIFY_1(x)
55 # define _BT_DIAG_IGNORE(option) \
56 _Pragma(_BT_DIAG_STRINGIFY(GCC diagnostic ignored option))
57 # define _BT_DIAG_IGNORE_TYPE_LIMITS _BT_DIAG_IGNORE("-Wtype-limits")
59 # define _BT_DIAG_PUSH
61 # define _BT_DIAG_IGNORE
62 # define _BT_DIAG_IGNORE_TYPE_LIMITS
65 #define _bt_is_signed_type(type) ((type) -1 < (type) 0)
68 * Produce a build-time error if the condition `cond` is non-zero.
69 * Evaluates as a size_t expression.
71 #define _BT_BUILD_ASSERT(cond) \
72 sizeof(struct { int f:(2 * !!(cond) - 1); })
75 * Cast value `v` to an unsigned integer of the same size as `v`.
77 #define _bt_cast_value_to_unsigned(v) \
78 (sizeof(v) == sizeof(uint8_t) ? (uint8_t) (v) : \
79 sizeof(v) == sizeof(uint16_t) ? (uint16_t) (v) : \
80 sizeof(v) == sizeof(uint32_t) ? (uint32_t) (v) : \
81 sizeof(v) == sizeof(uint64_t) ? (uint64_t) (v) : \
82 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
85 * Cast value `v` to an unsigned integer type of the size of type `type`
86 * *without* sign-extension.
88 * The unsigned cast ensures that we're not shifting a negative value,
89 * which is undefined in C. However, this limits the maximum type size
90 * of `type` to 64-bit. Generate a compile-time error if the size of
91 * `type` is larger than 64-bit.
93 #define _bt_cast_value_to_unsigned_type(type, v) \
94 (sizeof(type) == sizeof(uint8_t) ? \
95 (uint8_t) _bt_cast_value_to_unsigned(v) : \
96 sizeof(type) == sizeof(uint16_t) ? \
97 (uint16_t) _bt_cast_value_to_unsigned(v) : \
98 sizeof(type) == sizeof(uint32_t) ? \
99 (uint32_t) _bt_cast_value_to_unsigned(v) : \
100 sizeof(type) == sizeof(uint64_t) ? \
101 (uint64_t) _bt_cast_value_to_unsigned(v) : \
102 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
105 * _bt_fill_mask evaluates to a "type" integer with all bits set.
107 #define _bt_fill_mask(type) ((type) ~(type) 0)
110 * Left shift a value `v` of `shift` bits.
112 * The type of `v` can be signed or unsigned integer.
113 * The value of `shift` must be less than the size of `v` (in bits),
114 * otherwise the behavior is undefined.
115 * Evaluates to the result of the shift operation.
117 * According to the C99 standard, left shift of a left hand-side signed
118 * type is undefined if it has a negative value or if the result cannot
119 * be represented in the result type. This bitfield header discards the
120 * bits that are left-shifted beyond the result type representation,
121 * which is the behavior of an unsigned type left shift operation.
122 * Therefore, always perform left shift on an unsigned type.
124 * This macro should not be used if `shift` can be greater or equal than
125 * the bitwidth of `v`. See `_bt_safe_lshift`.
127 #define _bt_lshift(v, shift) \
128 ((__typeof__(v)) (_bt_cast_value_to_unsigned(v) << (shift)))
131 * Generate a mask of type `type` with the `length` least significant bits
132 * cleared, and the most significant bits set.
134 #define _bt_make_mask_complement(type, length) \
135 _bt_lshift(_bt_fill_mask(type), length)
138 * Generate a mask of type `type` with the `length` least significant bits
139 * set, and the most significant bits cleared.
141 #define _bt_make_mask(type, length) \
142 ((type) ~_bt_make_mask_complement(type, length))
145 * Right shift a value `v` of `shift` bits.
147 * The type of `v` can be signed or unsigned integer.
148 * The value of `shift` must be less than the size of `v` (in bits),
149 * otherwise the behavior is undefined.
150 * Evaluates to the result of the shift operation.
152 * According to the C99 standard, right shift of a left hand-side signed
153 * type which has a negative value is implementation defined. This
154 * bitfield header relies on the right shift implementation carrying the
155 * sign bit. If the compiler implementation has a different behavior,
156 * emulate carrying the sign bit.
158 * This macro should not be used if `shift` can be greater or equal than
159 * the bitwidth of `v`. See `_bt_safe_rshift`.
161 #if ((-1 >> 1) == -1)
162 #define _bt_rshift(v, shift) ((v) >> (shift))
164 #define _bt_rshift(v, shift) \
165 ((__typeof__(v)) ((_bt_cast_value_to_unsigned(v) >> (shift)) | \
166 ((v) < 0 ? _bt_make_mask_complement(__typeof__(v), \
167 sizeof(v) * CHAR_BIT - (shift)) : 0)))
171 * Right shift a signed or unsigned integer with `shift` value being an
172 * arbitrary number of bits. `v` is modified by this macro. The shift
173 * is transformed into a sequence of `_nr_partial_shifts` consecutive
174 * shift operations, each of a number of bits smaller than the bitwidth
175 * of `v`, ending with a shift of the number of left over bits.
177 #define _bt_safe_rshift(v, shift) \
179 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
180 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
182 for (; _nr_partial_shifts; _nr_partial_shifts--) \
183 (v) = _bt_rshift(v, sizeof(v) * CHAR_BIT - 1); \
184 (v) = _bt_rshift(v, _leftover_bits); \
188 * Left shift a signed or unsigned integer with `shift` value being an
189 * arbitrary number of bits. `v` is modified by this macro. The shift
190 * is transformed into a sequence of `_nr_partial_shifts` consecutive
191 * shift operations, each of a number of bits smaller than the bitwidth
192 * of `v`, ending with a shift of the number of left over bits.
194 #define _bt_safe_lshift(v, shift) \
196 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
197 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
199 for (; _nr_partial_shifts; _nr_partial_shifts--) \
200 (v) = _bt_lshift(v, sizeof(v) * CHAR_BIT - 1); \
201 (v) = _bt_lshift(v, _leftover_bits); \
205 * bt_bitfield_write - write integer to a bitfield in native endianness
207 * Save integer to the bitfield, which starts at the "start" bit, has "len"
209 * The inside of a bitfield is from high bits to low bits.
210 * Uses native endianness.
211 * For unsigned "v", pad MSB with 0 if bitfield is larger than v.
212 * For signed "v", sign-extend v if bitfield is larger than v.
214 * On little endian, bytes are placed from the less significant to the most
215 * significant. Also, consecutive bitfields are placed from lower bits to higher
218 * On big endian, bytes are places from most significant to less significant.
219 * Also, consecutive bitfields are placed from higher to lower bits.
222 #define _bt_bitfield_write_le(ptr, type, start, length, v) \
224 __typeof__(v) _v = (v); \
225 type *_ptr = (void *) (ptr); \
226 unsigned long _start = (start), _length = (length); \
227 type _mask, _cmask; \
228 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
229 unsigned long _start_unit, _end_unit, _this_unit; \
230 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
235 _end = _start + _length; \
236 _start_unit = _start / _ts; \
237 _end_unit = (_end + (_ts - 1)) / _ts; \
239 /* Trim v high bits */ \
240 if (_length < sizeof(_v) * CHAR_BIT) \
241 _v &= _bt_make_mask(__typeof__(_v), _length); \
243 /* We can now append v with a simple "or", shift it piece-wise */ \
244 _this_unit = _start_unit; \
245 if (_start_unit == _end_unit - 1) { \
246 _mask = _bt_make_mask(type, _start % _ts); \
248 _mask |= _bt_make_mask_complement(type, _end % _ts); \
249 _cmask = _bt_lshift((type) (_v), _start % _ts); \
251 _ptr[_this_unit] &= _mask; \
252 _ptr[_this_unit] |= _cmask; \
255 if (_start % _ts) { \
256 _cshift = _start % _ts; \
257 _mask = _bt_make_mask(type, _cshift); \
258 _cmask = _bt_lshift((type) (_v), _cshift); \
260 _ptr[_this_unit] &= _mask; \
261 _ptr[_this_unit] |= _cmask; \
262 _bt_safe_rshift(_v, _ts - _cshift); \
263 _start += _ts - _cshift; \
266 for (; _this_unit < _end_unit - 1; _this_unit++) { \
267 _ptr[_this_unit] = (type) _v; \
268 _bt_safe_rshift(_v, _ts); \
272 _mask = _bt_make_mask_complement(type, _end % _ts); \
273 _cmask = (type) _v; \
275 _ptr[_this_unit] &= _mask; \
276 _ptr[_this_unit] |= _cmask; \
278 _ptr[_this_unit] = (type) _v; \
281 #define _bt_bitfield_write_be(ptr, type, start, length, v) \
283 __typeof__(v) _v = (v); \
284 type *_ptr = (void *) (ptr); \
285 unsigned long _start = (start), _length = (length); \
286 type _mask, _cmask; \
287 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
288 unsigned long _start_unit, _end_unit, _this_unit; \
289 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
294 _end = _start + _length; \
295 _start_unit = _start / _ts; \
296 _end_unit = (_end + (_ts - 1)) / _ts; \
298 /* Trim v high bits */ \
299 if (_length < sizeof(_v) * CHAR_BIT) \
300 _v &= _bt_make_mask(__typeof__(_v), _length); \
302 /* We can now append v with a simple "or", shift it piece-wise */ \
303 _this_unit = _end_unit - 1; \
304 if (_start_unit == _end_unit - 1) { \
305 _mask = _bt_make_mask(type, (_ts - (_end % _ts)) % _ts); \
307 _mask |= _bt_make_mask_complement(type, _ts - (_start % _ts)); \
308 _cmask = _bt_lshift((type) (_v), (_ts - (_end % _ts)) % _ts); \
310 _ptr[_this_unit] &= _mask; \
311 _ptr[_this_unit] |= _cmask; \
315 _cshift = _end % _ts; \
316 _mask = _bt_make_mask(type, _ts - _cshift); \
317 _cmask = _bt_lshift((type) (_v), _ts - _cshift); \
319 _ptr[_this_unit] &= _mask; \
320 _ptr[_this_unit] |= _cmask; \
321 _bt_safe_rshift(_v, _cshift); \
325 for (; (long) _this_unit >= (long) _start_unit + 1; _this_unit--) { \
326 _ptr[_this_unit] = (type) _v; \
327 _bt_safe_rshift(_v, _ts); \
330 if (_start % _ts) { \
331 _mask = _bt_make_mask_complement(type, _ts - (_start % _ts)); \
332 _cmask = (type) _v; \
334 _ptr[_this_unit] &= _mask; \
335 _ptr[_this_unit] |= _cmask; \
337 _ptr[_this_unit] = (type) _v; \
341 * bt_bitfield_write - write integer to a bitfield in native endianness
342 * bt_bitfield_write_le - write integer to a bitfield in little endian
343 * bt_bitfield_write_be - write integer to a bitfield in big endian
346 #if (BYTE_ORDER == LITTLE_ENDIAN)
348 #define bt_bitfield_write(ptr, type, start, length, v) \
349 _bt_bitfield_write_le(ptr, type, start, length, v)
351 #define bt_bitfield_write_le(ptr, type, start, length, v) \
352 _bt_bitfield_write_le(ptr, type, start, length, v)
354 #define bt_bitfield_write_be(ptr, type, start, length, v) \
355 _bt_bitfield_write_be(ptr, unsigned char, start, length, v)
357 #elif (BYTE_ORDER == BIG_ENDIAN)
359 #define bt_bitfield_write(ptr, type, start, length, v) \
360 _bt_bitfield_write_be(ptr, type, start, length, v)
362 #define bt_bitfield_write_le(ptr, type, start, length, v) \
363 _bt_bitfield_write_le(ptr, unsigned char, start, length, v)
365 #define bt_bitfield_write_be(ptr, type, start, length, v) \
366 _bt_bitfield_write_be(ptr, type, start, length, v)
368 #else /* (BYTE_ORDER == PDP_ENDIAN) */
370 #error "Byte order not supported"
374 #define _bt_bitfield_read_le(ptr, type, start, length, vptr) \
376 __typeof__(*(vptr)) *_vptr = (vptr); \
377 __typeof__(*_vptr) _v; \
378 type *_ptr = (void *) (ptr); \
379 unsigned long _start = (start), _length = (length); \
380 type _mask, _cmask; \
381 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
382 unsigned long _start_unit, _end_unit, _this_unit; \
383 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
384 bool _is_signed_type; \
391 _end = _start + _length; \
392 _start_unit = _start / _ts; \
393 _end_unit = (_end + (_ts - 1)) / _ts; \
395 _this_unit = _end_unit - 1; \
397 _BT_DIAG_IGNORE_TYPE_LIMITS \
398 _is_signed_type = _bt_is_signed_type(__typeof__(_v)); \
400 if (_is_signed_type \
401 && (_ptr[_this_unit] & _bt_lshift((type) 1, (_end % _ts ? _end % _ts : _ts) - 1))) \
402 _v = ~(__typeof__(_v)) 0; \
405 if (_start_unit == _end_unit - 1) { \
406 _cmask = _ptr[_this_unit]; \
407 _cmask = _bt_rshift(_cmask, _start % _ts); \
408 if ((_end - _start) % _ts) { \
409 _mask = _bt_make_mask(type, _end - _start); \
412 _bt_safe_lshift(_v, _end - _start); \
413 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
418 _cshift = _end % _ts; \
419 _mask = _bt_make_mask(type, _cshift); \
420 _cmask = _ptr[_this_unit]; \
422 _bt_safe_lshift(_v, _cshift); \
423 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
427 for (; (long) _this_unit >= (long) _start_unit + 1; _this_unit--) { \
428 _bt_safe_lshift(_v, _ts); \
429 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
432 if (_start % _ts) { \
433 _mask = _bt_make_mask(type, _ts - (_start % _ts)); \
434 _cmask = _ptr[_this_unit]; \
435 _cmask = _bt_rshift(_cmask, _start % _ts); \
437 _bt_safe_lshift(_v, _ts - (_start % _ts)); \
438 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
440 _bt_safe_lshift(_v, _ts); \
441 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
446 #define _bt_bitfield_read_be(ptr, type, start, length, vptr) \
448 __typeof__(*(vptr)) *_vptr = (vptr); \
449 __typeof__(*_vptr) _v; \
450 type *_ptr = (void *) (ptr); \
451 unsigned long _start = (start), _length = (length); \
452 type _mask, _cmask; \
453 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
454 unsigned long _start_unit, _end_unit, _this_unit; \
455 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
456 bool _is_signed_type; \
463 _end = _start + _length; \
464 _start_unit = _start / _ts; \
465 _end_unit = (_end + (_ts - 1)) / _ts; \
467 _this_unit = _start_unit; \
469 _BT_DIAG_IGNORE_TYPE_LIMITS \
470 _is_signed_type = _bt_is_signed_type(__typeof__(_v)); \
472 if (_is_signed_type \
473 && (_ptr[_this_unit] & _bt_lshift((type) 1, _ts - (_start % _ts) - 1))) \
474 _v = ~(__typeof__(_v)) 0; \
477 if (_start_unit == _end_unit - 1) { \
478 _cmask = _ptr[_this_unit]; \
479 _cmask = _bt_rshift(_cmask, (_ts - (_end % _ts)) % _ts); \
480 if ((_end - _start) % _ts) { \
481 _mask = _bt_make_mask(type, _end - _start); \
484 _bt_safe_lshift(_v, _end - _start); \
485 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
489 if (_start % _ts) { \
490 _cshift = _start % _ts; \
491 _mask = _bt_make_mask(type, _ts - _cshift); \
492 _cmask = _ptr[_this_unit]; \
494 _bt_safe_lshift(_v, _ts - _cshift); \
495 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
496 _start += _ts - _cshift; \
499 for (; _this_unit < _end_unit - 1; _this_unit++) { \
500 _bt_safe_lshift(_v, _ts); \
501 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
505 _mask = _bt_make_mask(type, _end % _ts); \
506 _cmask = _ptr[_this_unit]; \
507 _cmask = _bt_rshift(_cmask, _ts - (_end % _ts)); \
509 _bt_safe_lshift(_v, _end % _ts); \
510 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
512 _bt_safe_lshift(_v, _ts); \
513 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
519 * bt_bitfield_read - read integer from a bitfield in native endianness
520 * bt_bitfield_read_le - read integer from a bitfield in little endian
521 * bt_bitfield_read_be - read integer from a bitfield in big endian
524 #if (BYTE_ORDER == LITTLE_ENDIAN)
526 #define bt_bitfield_read(ptr, type, start, length, vptr) \
527 _bt_bitfield_read_le(ptr, type, start, length, vptr)
529 #define bt_bitfield_read_le(ptr, type, start, length, vptr) \
530 _bt_bitfield_read_le(ptr, type, start, length, vptr)
532 #define bt_bitfield_read_be(ptr, type, start, length, vptr) \
533 _bt_bitfield_read_be(ptr, unsigned char, start, length, vptr)
535 #elif (BYTE_ORDER == BIG_ENDIAN)
537 #define bt_bitfield_read(ptr, type, start, length, vptr) \
538 _bt_bitfield_read_be(ptr, type, start, length, vptr)
540 #define bt_bitfield_read_le(ptr, type, start, length, vptr) \
541 _bt_bitfield_read_le(ptr, unsigned char, start, length, vptr)
543 #define bt_bitfield_read_be(ptr, type, start, length, vptr) \
544 _bt_bitfield_read_be(ptr, type, start, length, vptr)
546 #else /* (BYTE_ORDER == PDP_ENDIAN) */
548 #error "Byte order not supported"
552 #endif /* _BABELTRACE_BITFIELD_H */
This page took 0.041091 seconds and 4 git commands to generate.