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."
44 #define _bt_is_signed_type(type) ((type) -1 < (type) 1)
47 * Produce a build-time error if the condition `cond` is non-zero.
48 * Evaluates as a size_t expression.
50 #define _BT_BUILD_ASSERT(cond) \
51 sizeof(struct { int f:(2 * !!(cond) - 1); })
54 * Cast value `v` to an unsigned integer of the same size as `v`.
56 #define _bt_cast_value_to_unsigned(v) \
57 (sizeof(v) == sizeof(uint8_t) ? (uint8_t) (v) : \
58 sizeof(v) == sizeof(uint16_t) ? (uint16_t) (v) : \
59 sizeof(v) == sizeof(uint32_t) ? (uint32_t) (v) : \
60 sizeof(v) == sizeof(uint64_t) ? (uint64_t) (v) : \
61 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
64 * Cast value `v` to an unsigned integer type of the size of type `type`
65 * *without* sign-extension.
67 * The unsigned cast ensures that we're not shifting a negative value,
68 * which is undefined in C. However, this limits the maximum type size
69 * of `type` to 64-bit. Generate a compile-time error if the size of
70 * `type` is larger than 64-bit.
72 #define _bt_cast_value_to_unsigned_type(type, v) \
73 (sizeof(type) == sizeof(uint8_t) ? \
74 (uint8_t) _bt_cast_value_to_unsigned(v) : \
75 sizeof(type) == sizeof(uint16_t) ? \
76 (uint16_t) _bt_cast_value_to_unsigned(v) : \
77 sizeof(type) == sizeof(uint32_t) ? \
78 (uint32_t) _bt_cast_value_to_unsigned(v) : \
79 sizeof(type) == sizeof(uint64_t) ? \
80 (uint64_t) _bt_cast_value_to_unsigned(v) : \
81 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
84 * _bt_fill_mask evaluates to a "type" integer with all bits set.
86 #define _bt_fill_mask(type) ((type) ~(type) 0)
89 * Left shift a value `v` of `shift` bits.
91 * The type of `v` can be signed or unsigned integer.
92 * The value of `shift` must be less than the size of `v` (in bits),
93 * otherwise the behavior is undefined.
94 * Evaluates to the result of the shift operation.
96 * According to the C99 standard, left shift of a left hand-side signed
97 * type is undefined if it has a negative value or if the result cannot
98 * be represented in the result type. This bitfield header discards the
99 * bits that are left-shifted beyond the result type representation,
100 * which is the behavior of an unsigned type left shift operation.
101 * Therefore, always perform left shift on an unsigned type.
103 * This macro should not be used if `shift` can be greater or equal than
104 * the bitwidth of `v`. See `_bt_safe_lshift`.
106 #define _bt_lshift(v, shift) \
107 ((__typeof__(v)) (_bt_cast_value_to_unsigned(v) << (shift)))
110 * Generate a mask of type `type` with the `length` least significant bits
111 * cleared, and the most significant bits set.
113 #define _bt_make_mask_complement(type, length) \
114 _bt_lshift(_bt_fill_mask(type), length)
117 * Generate a mask of type `type` with the `length` least significant bits
118 * set, and the most significant bits cleared.
120 #define _bt_make_mask(type, length) \
121 ((type) ~_bt_make_mask_complement(type, length))
124 * Right shift a value `v` of `shift` bits.
126 * The type of `v` can be signed or unsigned integer.
127 * The value of `shift` must be less than the size of `v` (in bits),
128 * otherwise the behavior is undefined.
129 * Evaluates to the result of the shift operation.
131 * According to the C99 standard, right shift of a left hand-side signed
132 * type which has a negative value is implementation defined. This
133 * bitfield header relies on the right shift implementation carrying the
134 * sign bit. If the compiler implementation has a different behavior,
135 * emulate carrying the sign bit.
137 * This macro should not be used if `shift` can be greater or equal than
138 * the bitwidth of `v`. See `_bt_safe_rshift`.
140 #if ((-1 >> 1) == -1)
141 #define _bt_rshift(v, shift) ((v) >> (shift))
143 #define _bt_rshift(v, shift) \
144 ((__typeof__(v)) ((_bt_cast_value_to_unsigned(v) >> (shift)) | \
145 ((v) < 0 ? _bt_make_mask_complement(__typeof__(v), \
146 sizeof(v) * CHAR_BIT - (shift)) : 0)))
150 * Right shift a signed or unsigned integer with `shift` value being an
151 * arbitrary number of bits. `v` is modified by this macro. The shift
152 * is transformed into a sequence of `_nr_partial_shifts` consecutive
153 * shift operations, each of a number of bits smaller than the bitwidth
154 * of `v`, ending with a shift of the number of left over bits.
156 #define _bt_safe_rshift(v, shift) \
158 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
159 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
161 for (; _nr_partial_shifts; _nr_partial_shifts--) \
162 (v) = _bt_rshift(v, sizeof(v) * CHAR_BIT - 1); \
163 (v) = _bt_rshift(v, _leftover_bits); \
167 * Left shift a signed or unsigned integer with `shift` value being an
168 * arbitrary number of bits. `v` is modified by this macro. The shift
169 * is transformed into a sequence of `_nr_partial_shifts` consecutive
170 * shift operations, each of a number of bits smaller than the bitwidth
171 * of `v`, ending with a shift of the number of left over bits.
173 #define _bt_safe_lshift(v, shift) \
175 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
176 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
178 for (; _nr_partial_shifts; _nr_partial_shifts--) \
179 (v) = _bt_lshift(v, sizeof(v) * CHAR_BIT - 1); \
180 (v) = _bt_lshift(v, _leftover_bits); \
184 * bt_bitfield_write - write integer to a bitfield in native endianness
186 * Save integer to the bitfield, which starts at the "start" bit, has "len"
188 * The inside of a bitfield is from high bits to low bits.
189 * Uses native endianness.
190 * For unsigned "v", pad MSB with 0 if bitfield is larger than v.
191 * For signed "v", sign-extend v if bitfield is larger than v.
193 * On little endian, bytes are placed from the less significant to the most
194 * significant. Also, consecutive bitfields are placed from lower bits to higher
197 * On big endian, bytes are places from most significant to less significant.
198 * Also, consecutive bitfields are placed from higher to lower bits.
201 #define _bt_bitfield_write_le(ptr, type, start, length, v) \
203 __typeof__(v) _v = (v); \
204 type *_ptr = (void *) (ptr); \
205 unsigned long _start = (start), _length = (length); \
206 type _mask, _cmask; \
207 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
208 unsigned long _start_unit, _end_unit, _this_unit; \
209 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
214 _end = _start + _length; \
215 _start_unit = _start / _ts; \
216 _end_unit = (_end + (_ts - 1)) / _ts; \
218 /* Trim v high bits */ \
219 if (_length < sizeof(_v) * CHAR_BIT) \
220 _v &= _bt_make_mask(__typeof__(_v), _length); \
222 /* We can now append v with a simple "or", shift it piece-wise */ \
223 _this_unit = _start_unit; \
224 if (_start_unit == _end_unit - 1) { \
225 _mask = _bt_make_mask(type, _start % _ts); \
227 _mask |= _bt_make_mask_complement(type, _end % _ts); \
228 _cmask = _bt_lshift((type) (_v), _start % _ts); \
230 _ptr[_this_unit] &= _mask; \
231 _ptr[_this_unit] |= _cmask; \
234 if (_start % _ts) { \
235 _cshift = _start % _ts; \
236 _mask = _bt_make_mask(type, _cshift); \
237 _cmask = _bt_lshift((type) (_v), _cshift); \
239 _ptr[_this_unit] &= _mask; \
240 _ptr[_this_unit] |= _cmask; \
241 _bt_safe_rshift(_v, _ts - _cshift); \
242 _start += _ts - _cshift; \
245 for (; _this_unit < _end_unit - 1; _this_unit++) { \
246 _ptr[_this_unit] = (type) _v; \
247 _bt_safe_rshift(_v, _ts); \
251 _mask = _bt_make_mask_complement(type, _end % _ts); \
252 _cmask = (type) _v; \
254 _ptr[_this_unit] &= _mask; \
255 _ptr[_this_unit] |= _cmask; \
257 _ptr[_this_unit] = (type) _v; \
260 #define _bt_bitfield_write_be(ptr, type, start, length, v) \
262 __typeof__(v) _v = (v); \
263 type *_ptr = (void *) (ptr); \
264 unsigned long _start = (start), _length = (length); \
265 type _mask, _cmask; \
266 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
267 unsigned long _start_unit, _end_unit, _this_unit; \
268 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
273 _end = _start + _length; \
274 _start_unit = _start / _ts; \
275 _end_unit = (_end + (_ts - 1)) / _ts; \
277 /* Trim v high bits */ \
278 if (_length < sizeof(_v) * CHAR_BIT) \
279 _v &= _bt_make_mask(__typeof__(_v), _length); \
281 /* We can now append v with a simple "or", shift it piece-wise */ \
282 _this_unit = _end_unit - 1; \
283 if (_start_unit == _end_unit - 1) { \
284 _mask = _bt_make_mask(type, (_ts - (_end % _ts)) % _ts); \
286 _mask |= _bt_make_mask_complement(type, _ts - (_start % _ts)); \
287 _cmask = _bt_lshift((type) (_v), (_ts - (_end % _ts)) % _ts); \
289 _ptr[_this_unit] &= _mask; \
290 _ptr[_this_unit] |= _cmask; \
294 _cshift = _end % _ts; \
295 _mask = _bt_make_mask(type, _ts - _cshift); \
296 _cmask = _bt_lshift((type) (_v), _ts - _cshift); \
298 _ptr[_this_unit] &= _mask; \
299 _ptr[_this_unit] |= _cmask; \
300 _bt_safe_rshift(_v, _cshift); \
304 for (; (long) _this_unit >= (long) _start_unit + 1; _this_unit--) { \
305 _ptr[_this_unit] = (type) _v; \
306 _bt_safe_rshift(_v, _ts); \
309 if (_start % _ts) { \
310 _mask = _bt_make_mask_complement(type, _ts - (_start % _ts)); \
311 _cmask = (type) _v; \
313 _ptr[_this_unit] &= _mask; \
314 _ptr[_this_unit] |= _cmask; \
316 _ptr[_this_unit] = (type) _v; \
320 * bt_bitfield_write - write integer to a bitfield in native endianness
321 * bt_bitfield_write_le - write integer to a bitfield in little endian
322 * bt_bitfield_write_be - write integer to a bitfield in big endian
325 #if (BYTE_ORDER == LITTLE_ENDIAN)
327 #define bt_bitfield_write(ptr, type, start, length, v) \
328 _bt_bitfield_write_le(ptr, type, start, length, v)
330 #define bt_bitfield_write_le(ptr, type, start, length, v) \
331 _bt_bitfield_write_le(ptr, type, start, length, v)
333 #define bt_bitfield_write_be(ptr, type, start, length, v) \
334 _bt_bitfield_write_be(ptr, unsigned char, start, length, v)
336 #elif (BYTE_ORDER == BIG_ENDIAN)
338 #define bt_bitfield_write(ptr, type, start, length, v) \
339 _bt_bitfield_write_be(ptr, type, start, length, v)
341 #define bt_bitfield_write_le(ptr, type, start, length, v) \
342 _bt_bitfield_write_le(ptr, unsigned char, start, length, v)
344 #define bt_bitfield_write_be(ptr, type, start, length, v) \
345 _bt_bitfield_write_be(ptr, type, start, length, v)
347 #else /* (BYTE_ORDER == PDP_ENDIAN) */
349 #error "Byte order not supported"
353 #define _bt_bitfield_read_le(ptr, type, start, length, vptr) \
355 __typeof__(*(vptr)) *_vptr = (vptr); \
356 __typeof__(*_vptr) _v; \
357 type *_ptr = (void *) (ptr); \
358 unsigned long _start = (start), _length = (length); \
359 type _mask, _cmask; \
360 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
361 unsigned long _start_unit, _end_unit, _this_unit; \
362 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
369 _end = _start + _length; \
370 _start_unit = _start / _ts; \
371 _end_unit = (_end + (_ts - 1)) / _ts; \
373 _this_unit = _end_unit - 1; \
374 if (_bt_is_signed_type(__typeof__(_v)) \
375 && (_ptr[_this_unit] & _bt_lshift((type) 1, (_end % _ts ? _end % _ts : _ts) - 1))) \
376 _v = ~(__typeof__(_v)) 0; \
379 if (_start_unit == _end_unit - 1) { \
380 _cmask = _ptr[_this_unit]; \
381 _cmask = _bt_rshift(_cmask, _start % _ts); \
382 if ((_end - _start) % _ts) { \
383 _mask = _bt_make_mask(type, _end - _start); \
386 _bt_safe_lshift(_v, _end - _start); \
387 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
392 _cshift = _end % _ts; \
393 _mask = _bt_make_mask(type, _cshift); \
394 _cmask = _ptr[_this_unit]; \
396 _bt_safe_lshift(_v, _cshift); \
397 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
401 for (; (long) _this_unit >= (long) _start_unit + 1; _this_unit--) { \
402 _bt_safe_lshift(_v, _ts); \
403 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
406 if (_start % _ts) { \
407 _mask = _bt_make_mask(type, _ts - (_start % _ts)); \
408 _cmask = _ptr[_this_unit]; \
409 _cmask = _bt_rshift(_cmask, _start % _ts); \
411 _bt_safe_lshift(_v, _ts - (_start % _ts)); \
412 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
414 _bt_safe_lshift(_v, _ts); \
415 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
420 #define _bt_bitfield_read_be(ptr, type, start, length, vptr) \
422 __typeof__(*(vptr)) *_vptr = (vptr); \
423 __typeof__(*_vptr) _v; \
424 type *_ptr = (void *) (ptr); \
425 unsigned long _start = (start), _length = (length); \
426 type _mask, _cmask; \
427 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
428 unsigned long _start_unit, _end_unit, _this_unit; \
429 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
436 _end = _start + _length; \
437 _start_unit = _start / _ts; \
438 _end_unit = (_end + (_ts - 1)) / _ts; \
440 _this_unit = _start_unit; \
441 if (_bt_is_signed_type(__typeof__(_v)) \
442 && (_ptr[_this_unit] & _bt_lshift((type) 1, _ts - (_start % _ts) - 1))) \
443 _v = ~(__typeof__(_v)) 0; \
446 if (_start_unit == _end_unit - 1) { \
447 _cmask = _ptr[_this_unit]; \
448 _cmask = _bt_rshift(_cmask, (_ts - (_end % _ts)) % _ts); \
449 if ((_end - _start) % _ts) { \
450 _mask = _bt_make_mask(type, _end - _start); \
453 _bt_safe_lshift(_v, _end - _start); \
454 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
458 if (_start % _ts) { \
459 _cshift = _start % _ts; \
460 _mask = _bt_make_mask(type, _ts - _cshift); \
461 _cmask = _ptr[_this_unit]; \
463 _bt_safe_lshift(_v, _ts - _cshift); \
464 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
465 _start += _ts - _cshift; \
468 for (; _this_unit < _end_unit - 1; _this_unit++) { \
469 _bt_safe_lshift(_v, _ts); \
470 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
474 _mask = _bt_make_mask(type, _end % _ts); \
475 _cmask = _ptr[_this_unit]; \
476 _cmask = _bt_rshift(_cmask, _ts - (_end % _ts)); \
478 _bt_safe_lshift(_v, _end % _ts); \
479 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
481 _bt_safe_lshift(_v, _ts); \
482 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
488 * bt_bitfield_read - read integer from a bitfield in native endianness
489 * bt_bitfield_read_le - read integer from a bitfield in little endian
490 * bt_bitfield_read_be - read integer from a bitfield in big endian
493 #if (BYTE_ORDER == LITTLE_ENDIAN)
495 #define bt_bitfield_read(ptr, type, start, length, vptr) \
496 _bt_bitfield_read_le(ptr, type, start, length, vptr)
498 #define bt_bitfield_read_le(ptr, type, start, length, vptr) \
499 _bt_bitfield_read_le(ptr, type, start, length, vptr)
501 #define bt_bitfield_read_be(ptr, type, start, length, vptr) \
502 _bt_bitfield_read_be(ptr, unsigned char, start, length, vptr)
504 #elif (BYTE_ORDER == BIG_ENDIAN)
506 #define bt_bitfield_read(ptr, type, start, length, vptr) \
507 _bt_bitfield_read_be(ptr, type, start, length, vptr)
509 #define bt_bitfield_read_le(ptr, type, start, length, vptr) \
510 _bt_bitfield_read_le(ptr, unsigned char, start, length, vptr)
512 #define bt_bitfield_read_be(ptr, type, start, length, vptr) \
513 _bt_bitfield_read_be(ptr, type, start, length, vptr)
515 #else /* (BYTE_ORDER == PDP_ENDIAN) */
517 #error "Byte order not supported"
521 #endif /* _BABELTRACE_BITFIELD_H */
This page took 0.039843 seconds and 4 git commands to generate.