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.
47 * GCC versions lower than 4.6.0 do not accept diagnostic pragma inside
50 #if defined (__GNUC__) && (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
51 # define _BT_DIAG_PUSH _Pragma("GCC diagnostic push")
52 # define _BT_DIAG_POP _Pragma("GCC diagnostic pop")
54 # define _BT_DIAG_STRINGIFY_1(x) #x
55 # define _BT_DIAG_STRINGIFY(x) _BT_DIAG_STRINGIFY_1(x)
57 # define _BT_DIAG_IGNORE(option) \
58 _Pragma(_BT_DIAG_STRINGIFY(GCC diagnostic ignored option))
59 # define _BT_DIAG_IGNORE_TYPE_LIMITS _BT_DIAG_IGNORE("-Wtype-limits")
61 # define _BT_DIAG_PUSH
63 # define _BT_DIAG_IGNORE
64 # define _BT_DIAG_IGNORE_TYPE_LIMITS
67 #define _bt_is_signed_type(type) ((type) -1 < (type) 0)
70 * Produce a build-time error if the condition `cond` is non-zero.
71 * Evaluates as a size_t expression.
74 #define _BT_BUILD_ASSERT(cond) ([]{static_assert((cond), "");}, 0)
76 #define _BT_BUILD_ASSERT(cond) \
77 sizeof(struct { int f:(2 * !!(cond) - 1); })
81 * Cast value `v` to an unsigned integer of the same size as `v`.
83 #define _bt_cast_value_to_unsigned(v) \
84 (sizeof(v) == sizeof(uint8_t) ? (uint8_t) (v) : \
85 sizeof(v) == sizeof(uint16_t) ? (uint16_t) (v) : \
86 sizeof(v) == sizeof(uint32_t) ? (uint32_t) (v) : \
87 sizeof(v) == sizeof(uint64_t) ? (uint64_t) (v) : \
88 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
91 * Cast value `v` to an unsigned integer type of the size of type `type`
92 * *without* sign-extension.
94 * The unsigned cast ensures that we're not shifting a negative value,
95 * which is undefined in C. However, this limits the maximum type size
96 * of `type` to 64-bit. Generate a compile-time error if the size of
97 * `type` is larger than 64-bit.
99 #define _bt_cast_value_to_unsigned_type(type, v) \
100 (sizeof(type) == sizeof(uint8_t) ? \
101 (uint8_t) _bt_cast_value_to_unsigned(v) : \
102 sizeof(type) == sizeof(uint16_t) ? \
103 (uint16_t) _bt_cast_value_to_unsigned(v) : \
104 sizeof(type) == sizeof(uint32_t) ? \
105 (uint32_t) _bt_cast_value_to_unsigned(v) : \
106 sizeof(type) == sizeof(uint64_t) ? \
107 (uint64_t) _bt_cast_value_to_unsigned(v) : \
108 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
111 * _bt_fill_mask evaluates to a "type" integer with all bits set.
113 #define _bt_fill_mask(type) ((type) ~(type) 0)
116 * Left shift a value `v` of `shift` bits.
118 * The type of `v` can be signed or unsigned integer.
119 * The value of `shift` must be less than the size of `v` (in bits),
120 * otherwise the behavior is undefined.
121 * Evaluates to the result of the shift operation.
123 * According to the C99 standard, left shift of a left hand-side signed
124 * type is undefined if it has a negative value or if the result cannot
125 * be represented in the result type. This bitfield header discards the
126 * bits that are left-shifted beyond the result type representation,
127 * which is the behavior of an unsigned type left shift operation.
128 * Therefore, always perform left shift on an unsigned type.
130 * This macro should not be used if `shift` can be greater or equal than
131 * the bitwidth of `v`. See `_bt_safe_lshift`.
133 #define _bt_lshift(v, shift) \
134 ((__typeof__(v)) (_bt_cast_value_to_unsigned(v) << (shift)))
137 * Generate a mask of type `type` with the `length` least significant bits
138 * cleared, and the most significant bits set.
140 #define _bt_make_mask_complement(type, length) \
141 _bt_lshift(_bt_fill_mask(type), length)
144 * Generate a mask of type `type` with the `length` least significant bits
145 * set, and the most significant bits cleared.
147 #define _bt_make_mask(type, length) \
148 ((type) ~_bt_make_mask_complement(type, length))
151 * Right shift a value `v` of `shift` bits.
153 * The type of `v` can be signed or unsigned integer.
154 * The value of `shift` must be less than the size of `v` (in bits),
155 * otherwise the behavior is undefined.
156 * Evaluates to the result of the shift operation.
158 * According to the C99 standard, right shift of a left hand-side signed
159 * type which has a negative value is implementation defined. This
160 * bitfield header relies on the right shift implementation carrying the
161 * sign bit. If the compiler implementation has a different behavior,
162 * emulate carrying the sign bit.
164 * This macro should not be used if `shift` can be greater or equal than
165 * the bitwidth of `v`. See `_bt_safe_rshift`.
167 #if ((-1 >> 1) == -1)
168 #define _bt_rshift(v, shift) ((v) >> (shift))
170 #define _bt_rshift(v, shift) \
171 ((__typeof__(v)) ((_bt_cast_value_to_unsigned(v) >> (shift)) | \
172 ((v) < 0 ? _bt_make_mask_complement(__typeof__(v), \
173 sizeof(v) * CHAR_BIT - (shift)) : 0)))
177 * Right shift a signed or unsigned integer with `shift` value being an
178 * arbitrary number of bits. `v` is modified by this macro. The shift
179 * is transformed into a sequence of `_nr_partial_shifts` consecutive
180 * shift operations, each of a number of bits smaller than the bitwidth
181 * of `v`, ending with a shift of the number of left over bits.
183 #define _bt_safe_rshift(v, shift) \
185 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
186 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
188 for (; _nr_partial_shifts; _nr_partial_shifts--) \
189 (v) = _bt_rshift(v, sizeof(v) * CHAR_BIT - 1); \
190 (v) = _bt_rshift(v, _leftover_bits); \
194 * Left shift a signed or unsigned integer with `shift` value being an
195 * arbitrary number of bits. `v` is modified by this macro. The shift
196 * is transformed into a sequence of `_nr_partial_shifts` consecutive
197 * shift operations, each of a number of bits smaller than the bitwidth
198 * of `v`, ending with a shift of the number of left over bits.
200 #define _bt_safe_lshift(v, shift) \
202 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
203 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
205 for (; _nr_partial_shifts; _nr_partial_shifts--) \
206 (v) = _bt_lshift(v, sizeof(v) * CHAR_BIT - 1); \
207 (v) = _bt_lshift(v, _leftover_bits); \
211 * bt_bitfield_write - write integer to a bitfield in native endianness
213 * Save integer to the bitfield, which starts at the "start" bit, has "len"
215 * The inside of a bitfield is from high bits to low bits.
216 * Uses native endianness.
217 * For unsigned "v", pad MSB with 0 if bitfield is larger than v.
218 * For signed "v", sign-extend v if bitfield is larger than v.
220 * On little endian, bytes are placed from the less significant to the most
221 * significant. Also, consecutive bitfields are placed from lower bits to higher
224 * On big endian, bytes are places from most significant to less significant.
225 * Also, consecutive bitfields are placed from higher to lower bits.
228 #define _bt_bitfield_write_le(ptr, type, start, length, v) \
230 __typeof__(v) _v = (v); \
231 type *_ptr = (void *) (ptr); \
232 unsigned long _start = (start), _length = (length); \
233 type _mask, _cmask; \
234 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
235 unsigned long _start_unit, _end_unit, _this_unit; \
236 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
241 _end = _start + _length; \
242 _start_unit = _start / _ts; \
243 _end_unit = (_end + (_ts - 1)) / _ts; \
245 /* Trim v high bits */ \
246 if (_length < sizeof(_v) * CHAR_BIT) \
247 _v &= _bt_make_mask(__typeof__(_v), _length); \
249 /* We can now append v with a simple "or", shift it piece-wise */ \
250 _this_unit = _start_unit; \
251 if (_start_unit == _end_unit - 1) { \
252 _mask = _bt_make_mask(type, _start % _ts); \
254 _mask |= _bt_make_mask_complement(type, _end % _ts); \
255 _cmask = _bt_lshift((type) (_v), _start % _ts); \
257 _ptr[_this_unit] &= _mask; \
258 _ptr[_this_unit] |= _cmask; \
261 if (_start % _ts) { \
262 _cshift = _start % _ts; \
263 _mask = _bt_make_mask(type, _cshift); \
264 _cmask = _bt_lshift((type) (_v), _cshift); \
266 _ptr[_this_unit] &= _mask; \
267 _ptr[_this_unit] |= _cmask; \
268 _bt_safe_rshift(_v, _ts - _cshift); \
269 _start += _ts - _cshift; \
272 for (; _this_unit < _end_unit - 1; _this_unit++) { \
273 _ptr[_this_unit] = (type) _v; \
274 _bt_safe_rshift(_v, _ts); \
278 _mask = _bt_make_mask_complement(type, _end % _ts); \
279 _cmask = (type) _v; \
281 _ptr[_this_unit] &= _mask; \
282 _ptr[_this_unit] |= _cmask; \
284 _ptr[_this_unit] = (type) _v; \
287 #define _bt_bitfield_write_be(ptr, type, start, length, v) \
289 __typeof__(v) _v = (v); \
290 type *_ptr = (void *) (ptr); \
291 unsigned long _start = (start), _length = (length); \
292 type _mask, _cmask; \
293 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
294 unsigned long _start_unit, _end_unit, _this_unit; \
295 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
300 _end = _start + _length; \
301 _start_unit = _start / _ts; \
302 _end_unit = (_end + (_ts - 1)) / _ts; \
304 /* Trim v high bits */ \
305 if (_length < sizeof(_v) * CHAR_BIT) \
306 _v &= _bt_make_mask(__typeof__(_v), _length); \
308 /* We can now append v with a simple "or", shift it piece-wise */ \
309 _this_unit = _end_unit - 1; \
310 if (_start_unit == _end_unit - 1) { \
311 _mask = _bt_make_mask(type, (_ts - (_end % _ts)) % _ts); \
313 _mask |= _bt_make_mask_complement(type, _ts - (_start % _ts)); \
314 _cmask = _bt_lshift((type) (_v), (_ts - (_end % _ts)) % _ts); \
316 _ptr[_this_unit] &= _mask; \
317 _ptr[_this_unit] |= _cmask; \
321 _cshift = _end % _ts; \
322 _mask = _bt_make_mask(type, _ts - _cshift); \
323 _cmask = _bt_lshift((type) (_v), _ts - _cshift); \
325 _ptr[_this_unit] &= _mask; \
326 _ptr[_this_unit] |= _cmask; \
327 _bt_safe_rshift(_v, _cshift); \
331 for (; (long) _this_unit >= (long) _start_unit + 1; _this_unit--) { \
332 _ptr[_this_unit] = (type) _v; \
333 _bt_safe_rshift(_v, _ts); \
336 if (_start % _ts) { \
337 _mask = _bt_make_mask_complement(type, _ts - (_start % _ts)); \
338 _cmask = (type) _v; \
340 _ptr[_this_unit] &= _mask; \
341 _ptr[_this_unit] |= _cmask; \
343 _ptr[_this_unit] = (type) _v; \
347 * bt_bitfield_write - write integer to a bitfield in native endianness
348 * bt_bitfield_write_le - write integer to a bitfield in little endian
349 * bt_bitfield_write_be - write integer to a bitfield in big endian
352 #if (BYTE_ORDER == LITTLE_ENDIAN)
354 #define bt_bitfield_write(ptr, type, start, length, v) \
355 _bt_bitfield_write_le(ptr, type, start, length, v)
357 #define bt_bitfield_write_le(ptr, type, start, length, v) \
358 _bt_bitfield_write_le(ptr, type, start, length, v)
360 #define bt_bitfield_write_be(ptr, type, start, length, v) \
361 _bt_bitfield_write_be(ptr, unsigned char, start, length, v)
363 #elif (BYTE_ORDER == BIG_ENDIAN)
365 #define bt_bitfield_write(ptr, type, start, length, v) \
366 _bt_bitfield_write_be(ptr, type, start, length, v)
368 #define bt_bitfield_write_le(ptr, type, start, length, v) \
369 _bt_bitfield_write_le(ptr, unsigned char, start, length, v)
371 #define bt_bitfield_write_be(ptr, type, start, length, v) \
372 _bt_bitfield_write_be(ptr, type, start, length, v)
374 #else /* (BYTE_ORDER == PDP_ENDIAN) */
376 #error "Byte order not supported"
380 #define _bt_bitfield_read_le(ptr, type, start, length, vptr) \
382 __typeof__(*(vptr)) *_vptr = (vptr); \
383 __typeof__(*_vptr) _v; \
384 type *_ptr = (type *) (ptr); \
385 unsigned long _start = (start), _length = (length); \
386 type _mask, _cmask; \
387 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
388 unsigned long _start_unit, _end_unit, _this_unit; \
389 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
390 bool _is_signed_type; \
397 _end = _start + _length; \
398 _start_unit = _start / _ts; \
399 _end_unit = (_end + (_ts - 1)) / _ts; \
401 _this_unit = _end_unit - 1; \
403 _BT_DIAG_IGNORE_TYPE_LIMITS \
404 _is_signed_type = _bt_is_signed_type(__typeof__(_v)); \
406 if (_is_signed_type \
407 && (_ptr[_this_unit] & _bt_lshift((type) 1, (_end % _ts ? _end % _ts : _ts) - 1))) \
408 _v = ~(__typeof__(_v)) 0; \
411 if (_start_unit == _end_unit - 1) { \
412 _cmask = _ptr[_this_unit]; \
413 _cmask = _bt_rshift(_cmask, _start % _ts); \
414 if ((_end - _start) % _ts) { \
415 _mask = _bt_make_mask(type, _end - _start); \
418 _bt_safe_lshift(_v, _end - _start); \
419 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
424 _cshift = _end % _ts; \
425 _mask = _bt_make_mask(type, _cshift); \
426 _cmask = _ptr[_this_unit]; \
428 _bt_safe_lshift(_v, _cshift); \
429 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
433 for (; (long) _this_unit >= (long) _start_unit + 1; _this_unit--) { \
434 _bt_safe_lshift(_v, _ts); \
435 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
438 if (_start % _ts) { \
439 _mask = _bt_make_mask(type, _ts - (_start % _ts)); \
440 _cmask = _ptr[_this_unit]; \
441 _cmask = _bt_rshift(_cmask, _start % _ts); \
443 _bt_safe_lshift(_v, _ts - (_start % _ts)); \
444 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
446 _bt_safe_lshift(_v, _ts); \
447 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
452 #define _bt_bitfield_read_be(ptr, type, start, length, vptr) \
454 __typeof__(*(vptr)) *_vptr = (vptr); \
455 __typeof__(*_vptr) _v; \
456 type *_ptr = (void *) (ptr); \
457 unsigned long _start = (start), _length = (length); \
458 type _mask, _cmask; \
459 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
460 unsigned long _start_unit, _end_unit, _this_unit; \
461 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
462 bool _is_signed_type; \
469 _end = _start + _length; \
470 _start_unit = _start / _ts; \
471 _end_unit = (_end + (_ts - 1)) / _ts; \
473 _this_unit = _start_unit; \
475 _BT_DIAG_IGNORE_TYPE_LIMITS \
476 _is_signed_type = _bt_is_signed_type(__typeof__(_v)); \
478 if (_is_signed_type \
479 && (_ptr[_this_unit] & _bt_lshift((type) 1, _ts - (_start % _ts) - 1))) \
480 _v = ~(__typeof__(_v)) 0; \
483 if (_start_unit == _end_unit - 1) { \
484 _cmask = _ptr[_this_unit]; \
485 _cmask = _bt_rshift(_cmask, (_ts - (_end % _ts)) % _ts); \
486 if ((_end - _start) % _ts) { \
487 _mask = _bt_make_mask(type, _end - _start); \
490 _bt_safe_lshift(_v, _end - _start); \
491 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
495 if (_start % _ts) { \
496 _cshift = _start % _ts; \
497 _mask = _bt_make_mask(type, _ts - _cshift); \
498 _cmask = _ptr[_this_unit]; \
500 _bt_safe_lshift(_v, _ts - _cshift); \
501 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
502 _start += _ts - _cshift; \
505 for (; _this_unit < _end_unit - 1; _this_unit++) { \
506 _bt_safe_lshift(_v, _ts); \
507 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
511 _mask = _bt_make_mask(type, _end % _ts); \
512 _cmask = _ptr[_this_unit]; \
513 _cmask = _bt_rshift(_cmask, _ts - (_end % _ts)); \
515 _bt_safe_lshift(_v, _end % _ts); \
516 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
518 _bt_safe_lshift(_v, _ts); \
519 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
525 * bt_bitfield_read - read integer from a bitfield in native endianness
526 * bt_bitfield_read_le - read integer from a bitfield in little endian
527 * bt_bitfield_read_be - read integer from a bitfield in big endian
530 #if (BYTE_ORDER == LITTLE_ENDIAN)
532 #define bt_bitfield_read(ptr, type, start, length, vptr) \
533 _bt_bitfield_read_le(ptr, type, start, length, vptr)
535 #define bt_bitfield_read_le(ptr, type, start, length, vptr) \
536 _bt_bitfield_read_le(ptr, type, start, length, vptr)
538 #define bt_bitfield_read_be(ptr, type, start, length, vptr) \
539 _bt_bitfield_read_be(ptr, unsigned char, start, length, vptr)
541 #elif (BYTE_ORDER == BIG_ENDIAN)
543 #define bt_bitfield_read(ptr, type, start, length, vptr) \
544 _bt_bitfield_read_be(ptr, type, start, length, vptr)
546 #define bt_bitfield_read_le(ptr, type, start, length, vptr) \
547 _bt_bitfield_read_le(ptr, unsigned char, start, length, vptr)
549 #define bt_bitfield_read_be(ptr, type, start, length, vptr) \
550 _bt_bitfield_read_be(ptr, type, start, length, vptr)
552 #else /* (BYTE_ORDER == PDP_ENDIAN) */
554 #error "Byte order not supported"
558 #endif /* _BABELTRACE_BITFIELD_H */
This page took 0.051729 seconds and 4 git commands to generate.