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 <stdbool.h> /* C99 7.16 bool type */
28 #include "compat/limits.h" /* C99 5.2.4.2 Numerical limits */
29 #include "compat/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.
73 #define _BT_BUILD_ASSERT(cond) \
74 sizeof(struct { int f:(2 * !!(cond) - 1); })
77 * Cast value `v` to an unsigned integer of the same size as `v`.
79 #define _bt_cast_value_to_unsigned(v) \
80 (sizeof(v) == sizeof(uint8_t) ? (uint8_t) (v) : \
81 sizeof(v) == sizeof(uint16_t) ? (uint16_t) (v) : \
82 sizeof(v) == sizeof(uint32_t) ? (uint32_t) (v) : \
83 sizeof(v) == sizeof(uint64_t) ? (uint64_t) (v) : \
84 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
87 * Cast value `v` to an unsigned integer type of the size of type `type`
88 * *without* sign-extension.
90 * The unsigned cast ensures that we're not shifting a negative value,
91 * which is undefined in C. However, this limits the maximum type size
92 * of `type` to 64-bit. Generate a compile-time error if the size of
93 * `type` is larger than 64-bit.
95 #define _bt_cast_value_to_unsigned_type(type, v) \
96 (sizeof(type) == sizeof(uint8_t) ? \
97 (uint8_t) _bt_cast_value_to_unsigned(v) : \
98 sizeof(type) == sizeof(uint16_t) ? \
99 (uint16_t) _bt_cast_value_to_unsigned(v) : \
100 sizeof(type) == sizeof(uint32_t) ? \
101 (uint32_t) _bt_cast_value_to_unsigned(v) : \
102 sizeof(type) == sizeof(uint64_t) ? \
103 (uint64_t) _bt_cast_value_to_unsigned(v) : \
104 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
107 * _bt_fill_mask evaluates to a "type" integer with all bits set.
109 #define _bt_fill_mask(type) ((type) ~(type) 0)
112 * Left 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, left shift of a left hand-side signed
120 * type is undefined if it has a negative value or if the result cannot
121 * be represented in the result type. This bitfield header discards the
122 * bits that are left-shifted beyond the result type representation,
123 * which is the behavior of an unsigned type left shift operation.
124 * Therefore, always perform left shift on an unsigned type.
126 * This macro should not be used if `shift` can be greater or equal than
127 * the bitwidth of `v`. See `_bt_safe_lshift`.
129 #define _bt_lshift(v, shift) \
130 ((__typeof__(v)) (_bt_cast_value_to_unsigned(v) << (shift)))
133 * Generate a mask of type `type` with the `length` least significant bits
134 * cleared, and the most significant bits set.
136 #define _bt_make_mask_complement(type, length) \
137 _bt_lshift(_bt_fill_mask(type), length)
140 * Generate a mask of type `type` with the `length` least significant bits
141 * set, and the most significant bits cleared.
143 #define _bt_make_mask(type, length) \
144 ((type) ~_bt_make_mask_complement(type, length))
147 * Right shift a value `v` of `shift` bits.
149 * The type of `v` can be signed or unsigned integer.
150 * The value of `shift` must be less than the size of `v` (in bits),
151 * otherwise the behavior is undefined.
152 * Evaluates to the result of the shift operation.
154 * According to the C99 standard, right shift of a left hand-side signed
155 * type which has a negative value is implementation defined. This
156 * bitfield header relies on the right shift implementation carrying the
157 * sign bit. If the compiler implementation has a different behavior,
158 * emulate carrying the sign bit.
160 * This macro should not be used if `shift` can be greater or equal than
161 * the bitwidth of `v`. See `_bt_safe_rshift`.
163 #if ((-1 >> 1) == -1)
164 #define _bt_rshift(v, shift) ((v) >> (shift))
166 #define _bt_rshift(v, shift) \
167 ((__typeof__(v)) ((_bt_cast_value_to_unsigned(v) >> (shift)) | \
168 ((v) < 0 ? _bt_make_mask_complement(__typeof__(v), \
169 sizeof(v) * CHAR_BIT - (shift)) : 0)))
173 * Right shift a signed or unsigned integer with `shift` value being an
174 * arbitrary number of bits. `v` is modified by this macro. The shift
175 * is transformed into a sequence of `_nr_partial_shifts` consecutive
176 * shift operations, each of a number of bits smaller than the bitwidth
177 * of `v`, ending with a shift of the number of left over bits.
179 #define _bt_safe_rshift(v, shift) \
181 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
182 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
184 for (; _nr_partial_shifts; _nr_partial_shifts--) \
185 (v) = _bt_rshift(v, sizeof(v) * CHAR_BIT - 1); \
186 (v) = _bt_rshift(v, _leftover_bits); \
190 * Left shift a signed or unsigned integer with `shift` value being an
191 * arbitrary number of bits. `v` is modified by this macro. The shift
192 * is transformed into a sequence of `_nr_partial_shifts` consecutive
193 * shift operations, each of a number of bits smaller than the bitwidth
194 * of `v`, ending with a shift of the number of left over bits.
196 #define _bt_safe_lshift(v, shift) \
198 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
199 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
201 for (; _nr_partial_shifts; _nr_partial_shifts--) \
202 (v) = _bt_lshift(v, sizeof(v) * CHAR_BIT - 1); \
203 (v) = _bt_lshift(v, _leftover_bits); \
207 * bt_bitfield_write - write integer to a bitfield in native endianness
209 * Save integer to the bitfield, which starts at the "start" bit, has "len"
211 * The inside of a bitfield is from high bits to low bits.
212 * Uses native endianness.
213 * For unsigned "v", pad MSB with 0 if bitfield is larger than v.
214 * For signed "v", sign-extend v if bitfield is larger than v.
216 * On little endian, bytes are placed from the less significant to the most
217 * significant. Also, consecutive bitfields are placed from lower bits to higher
220 * On big endian, bytes are places from most significant to less significant.
221 * Also, consecutive bitfields are placed from higher to lower bits.
224 #define _bt_bitfield_write_le(ptr, type, start, length, v) \
226 __typeof__(v) _v = (v); \
227 type *_ptr = (void *) (ptr); \
228 unsigned long _start = (start), _length = (length); \
229 type _mask, _cmask; \
230 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
231 unsigned long _start_unit, _end_unit, _this_unit; \
232 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
237 _end = _start + _length; \
238 _start_unit = _start / _ts; \
239 _end_unit = (_end + (_ts - 1)) / _ts; \
241 /* Trim v high bits */ \
242 if (_length < sizeof(_v) * CHAR_BIT) \
243 _v &= _bt_make_mask(__typeof__(_v), _length); \
245 /* We can now append v with a simple "or", shift it piece-wise */ \
246 _this_unit = _start_unit; \
247 if (_start_unit == _end_unit - 1) { \
248 _mask = _bt_make_mask(type, _start % _ts); \
250 _mask |= _bt_make_mask_complement(type, _end % _ts); \
251 _cmask = _bt_lshift((type) (_v), _start % _ts); \
253 _ptr[_this_unit] &= _mask; \
254 _ptr[_this_unit] |= _cmask; \
257 if (_start % _ts) { \
258 _cshift = _start % _ts; \
259 _mask = _bt_make_mask(type, _cshift); \
260 _cmask = _bt_lshift((type) (_v), _cshift); \
262 _ptr[_this_unit] &= _mask; \
263 _ptr[_this_unit] |= _cmask; \
264 _bt_safe_rshift(_v, _ts - _cshift); \
265 _start += _ts - _cshift; \
268 for (; _this_unit < _end_unit - 1; _this_unit++) { \
269 _ptr[_this_unit] = (type) _v; \
270 _bt_safe_rshift(_v, _ts); \
274 _mask = _bt_make_mask_complement(type, _end % _ts); \
275 _cmask = (type) _v; \
277 _ptr[_this_unit] &= _mask; \
278 _ptr[_this_unit] |= _cmask; \
280 _ptr[_this_unit] = (type) _v; \
283 #define _bt_bitfield_write_be(ptr, type, start, length, v) \
285 __typeof__(v) _v = (v); \
286 type *_ptr = (void *) (ptr); \
287 unsigned long _start = (start), _length = (length); \
288 type _mask, _cmask; \
289 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
290 unsigned long _start_unit, _end_unit, _this_unit; \
291 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
296 _end = _start + _length; \
297 _start_unit = _start / _ts; \
298 _end_unit = (_end + (_ts - 1)) / _ts; \
300 /* Trim v high bits */ \
301 if (_length < sizeof(_v) * CHAR_BIT) \
302 _v &= _bt_make_mask(__typeof__(_v), _length); \
304 /* We can now append v with a simple "or", shift it piece-wise */ \
305 _this_unit = _end_unit - 1; \
306 if (_start_unit == _end_unit - 1) { \
307 _mask = _bt_make_mask(type, (_ts - (_end % _ts)) % _ts); \
309 _mask |= _bt_make_mask_complement(type, _ts - (_start % _ts)); \
310 _cmask = _bt_lshift((type) (_v), (_ts - (_end % _ts)) % _ts); \
312 _ptr[_this_unit] &= _mask; \
313 _ptr[_this_unit] |= _cmask; \
317 _cshift = _end % _ts; \
318 _mask = _bt_make_mask(type, _ts - _cshift); \
319 _cmask = _bt_lshift((type) (_v), _ts - _cshift); \
321 _ptr[_this_unit] &= _mask; \
322 _ptr[_this_unit] |= _cmask; \
323 _bt_safe_rshift(_v, _cshift); \
327 for (; (long) _this_unit >= (long) _start_unit + 1; _this_unit--) { \
328 _ptr[_this_unit] = (type) _v; \
329 _bt_safe_rshift(_v, _ts); \
332 if (_start % _ts) { \
333 _mask = _bt_make_mask_complement(type, _ts - (_start % _ts)); \
334 _cmask = (type) _v; \
336 _ptr[_this_unit] &= _mask; \
337 _ptr[_this_unit] |= _cmask; \
339 _ptr[_this_unit] = (type) _v; \
343 * bt_bitfield_write - write integer to a bitfield in native endianness
344 * bt_bitfield_write_le - write integer to a bitfield in little endian
345 * bt_bitfield_write_be - write integer to a bitfield in big endian
348 #if (BYTE_ORDER == LITTLE_ENDIAN)
350 #define bt_bitfield_write(ptr, type, start, length, v) \
351 _bt_bitfield_write_le(ptr, type, start, length, v)
353 #define bt_bitfield_write_le(ptr, type, start, length, v) \
354 _bt_bitfield_write_le(ptr, type, start, length, v)
356 #define bt_bitfield_write_be(ptr, type, start, length, v) \
357 _bt_bitfield_write_be(ptr, unsigned char, start, length, v)
359 #elif (BYTE_ORDER == BIG_ENDIAN)
361 #define bt_bitfield_write(ptr, type, start, length, v) \
362 _bt_bitfield_write_be(ptr, type, start, length, v)
364 #define bt_bitfield_write_le(ptr, type, start, length, v) \
365 _bt_bitfield_write_le(ptr, unsigned char, start, length, v)
367 #define bt_bitfield_write_be(ptr, type, start, length, v) \
368 _bt_bitfield_write_be(ptr, type, start, length, v)
370 #else /* (BYTE_ORDER == PDP_ENDIAN) */
372 #error "Byte order not supported"
376 #define _bt_bitfield_read_le(ptr, type, start, length, vptr) \
378 __typeof__(*(vptr)) *_vptr = (vptr); \
379 __typeof__(*_vptr) _v; \
380 type *_ptr = (void *) (ptr); \
381 unsigned long _start = (start), _length = (length); \
382 type _mask, _cmask; \
383 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
384 unsigned long _start_unit, _end_unit, _this_unit; \
385 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
386 bool _is_signed_type; \
393 _end = _start + _length; \
394 _start_unit = _start / _ts; \
395 _end_unit = (_end + (_ts - 1)) / _ts; \
397 _this_unit = _end_unit - 1; \
399 _BT_DIAG_IGNORE_TYPE_LIMITS \
400 _is_signed_type = _bt_is_signed_type(__typeof__(_v)); \
402 if (_is_signed_type \
403 && (_ptr[_this_unit] & _bt_lshift((type) 1, (_end % _ts ? _end % _ts : _ts) - 1))) \
404 _v = ~(__typeof__(_v)) 0; \
407 if (_start_unit == _end_unit - 1) { \
408 _cmask = _ptr[_this_unit]; \
409 _cmask = _bt_rshift(_cmask, _start % _ts); \
410 if ((_end - _start) % _ts) { \
411 _mask = _bt_make_mask(type, _end - _start); \
414 _bt_safe_lshift(_v, _end - _start); \
415 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
420 _cshift = _end % _ts; \
421 _mask = _bt_make_mask(type, _cshift); \
422 _cmask = _ptr[_this_unit]; \
424 _bt_safe_lshift(_v, _cshift); \
425 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
429 for (; (long) _this_unit >= (long) _start_unit + 1; _this_unit--) { \
430 _bt_safe_lshift(_v, _ts); \
431 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
434 if (_start % _ts) { \
435 _mask = _bt_make_mask(type, _ts - (_start % _ts)); \
436 _cmask = _ptr[_this_unit]; \
437 _cmask = _bt_rshift(_cmask, _start % _ts); \
439 _bt_safe_lshift(_v, _ts - (_start % _ts)); \
440 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
442 _bt_safe_lshift(_v, _ts); \
443 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
448 #define _bt_bitfield_read_be(ptr, type, start, length, vptr) \
450 __typeof__(*(vptr)) *_vptr = (vptr); \
451 __typeof__(*_vptr) _v; \
452 type *_ptr = (void *) (ptr); \
453 unsigned long _start = (start), _length = (length); \
454 type _mask, _cmask; \
455 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
456 unsigned long _start_unit, _end_unit, _this_unit; \
457 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
458 bool _is_signed_type; \
465 _end = _start + _length; \
466 _start_unit = _start / _ts; \
467 _end_unit = (_end + (_ts - 1)) / _ts; \
469 _this_unit = _start_unit; \
471 _BT_DIAG_IGNORE_TYPE_LIMITS \
472 _is_signed_type = _bt_is_signed_type(__typeof__(_v)); \
474 if (_is_signed_type \
475 && (_ptr[_this_unit] & _bt_lshift((type) 1, _ts - (_start % _ts) - 1))) \
476 _v = ~(__typeof__(_v)) 0; \
479 if (_start_unit == _end_unit - 1) { \
480 _cmask = _ptr[_this_unit]; \
481 _cmask = _bt_rshift(_cmask, (_ts - (_end % _ts)) % _ts); \
482 if ((_end - _start) % _ts) { \
483 _mask = _bt_make_mask(type, _end - _start); \
486 _bt_safe_lshift(_v, _end - _start); \
487 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
491 if (_start % _ts) { \
492 _cshift = _start % _ts; \
493 _mask = _bt_make_mask(type, _ts - _cshift); \
494 _cmask = _ptr[_this_unit]; \
496 _bt_safe_lshift(_v, _ts - _cshift); \
497 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
498 _start += _ts - _cshift; \
501 for (; _this_unit < _end_unit - 1; _this_unit++) { \
502 _bt_safe_lshift(_v, _ts); \
503 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
507 _mask = _bt_make_mask(type, _end % _ts); \
508 _cmask = _ptr[_this_unit]; \
509 _cmask = _bt_rshift(_cmask, _ts - (_end % _ts)); \
511 _bt_safe_lshift(_v, _end % _ts); \
512 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
514 _bt_safe_lshift(_v, _ts); \
515 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
521 * bt_bitfield_read - read integer from a bitfield in native endianness
522 * bt_bitfield_read_le - read integer from a bitfield in little endian
523 * bt_bitfield_read_be - read integer from a bitfield in big endian
526 #if (BYTE_ORDER == LITTLE_ENDIAN)
528 #define bt_bitfield_read(ptr, type, start, length, vptr) \
529 _bt_bitfield_read_le(ptr, type, start, length, vptr)
531 #define bt_bitfield_read_le(ptr, type, start, length, vptr) \
532 _bt_bitfield_read_le(ptr, type, start, length, vptr)
534 #define bt_bitfield_read_be(ptr, type, start, length, vptr) \
535 _bt_bitfield_read_be(ptr, unsigned char, start, length, vptr)
537 #elif (BYTE_ORDER == BIG_ENDIAN)
539 #define bt_bitfield_read(ptr, type, start, length, vptr) \
540 _bt_bitfield_read_be(ptr, type, start, length, vptr)
542 #define bt_bitfield_read_le(ptr, type, start, length, vptr) \
543 _bt_bitfield_read_le(ptr, unsigned char, start, length, vptr)
545 #define bt_bitfield_read_be(ptr, type, start, length, vptr) \
546 _bt_bitfield_read_be(ptr, type, start, length, vptr)
548 #else /* (BYTE_ORDER == PDP_ENDIAN) */
550 #error "Byte order not supported"
554 #endif /* _BABELTRACE_BITFIELD_H */
This page took 0.040446 seconds and 4 git commands to generate.