7d5eaeaa068ddadb7631d9f88750453bc9aa47a2
[lttng-modules.git] / lib / bitfield.h
1 /* SPDX-License-Identifier: MIT
2 *
3 * Copyright 2010-2019 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 */
5
6 #ifndef _BABELTRACE_BITFIELD_H
7 #define _BABELTRACE_BITFIELD_H
8
9 #include <linux/types.h>
10 #include <lttng-endian.h>
11
12 #ifndef CHAR_BIT
13 #define CHAR_BIT 8
14 #endif
15
16 /*
17 * This header strictly follows the C99 standard, except for use of the
18 * compiler-specific __typeof__.
19 */
20
21 /*
22 * This bitfield header requires the compiler representation of signed
23 * integers to be two's complement.
24 */
25 #if (-1 != ~0)
26 #error "bitfield.h requires the compiler representation of signed integers to be two's complement."
27 #endif
28
29 /*
30 * _bt_is_signed_type() willingly generates comparison of unsigned
31 * expression < 0, which is always false. Silence compiler warnings.
32 * GCC versions lower than 4.6.0 do not accept diagnostic pragma inside
33 * functions.
34 */
35 #if defined(__GNUC__) && (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
36 # define _BT_DIAG_PUSH _Pragma("GCC diagnostic push")
37 # define _BT_DIAG_POP _Pragma("GCC diagnostic pop")
38
39 # define _BT_DIAG_STRINGIFY_1(x) #x
40 # define _BT_DIAG_STRINGIFY(x) _BT_DIAG_STRINGIFY_1(x)
41
42 # define _BT_DIAG_IGNORE(option) \
43 _Pragma(_BT_DIAG_STRINGIFY(GCC diagnostic ignored option))
44 # define _BT_DIAG_IGNORE_TYPE_LIMITS _BT_DIAG_IGNORE("-Wtype-limits")
45 #else
46 # define _BT_DIAG_PUSH
47 # define _BT_DIAG_POP
48 # define _BT_DIAG_IGNORE
49 # define _BT_DIAG_IGNORE_TYPE_LIMITS
50 #endif
51
52 #define _bt_is_signed_type(type) ((type) -1 < (type) 0)
53
54 /*
55 * Produce a build-time error if the condition `cond` is non-zero.
56 * Evaluates as a size_t expression.
57 */
58 #ifdef __cplusplus
59 #define _BT_BUILD_ASSERT(cond) ([]{static_assert((cond), "");}, 0)
60 #else
61 #define _BT_BUILD_ASSERT(cond) \
62 sizeof(struct { int f:(2 * !!(cond) - 1); })
63 #endif
64
65 /*
66 * Cast value `v` to an unsigned integer of the same size as `v`.
67 */
68 #define _bt_cast_value_to_unsigned(v) \
69 (sizeof(v) == sizeof(uint8_t) ? (uint8_t) (v) : \
70 sizeof(v) == sizeof(uint16_t) ? (uint16_t) (v) : \
71 sizeof(v) == sizeof(uint32_t) ? (uint32_t) (v) : \
72 sizeof(v) == sizeof(uint64_t) ? (uint64_t) (v) : \
73 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
74
75 /*
76 * Cast value `v` to an unsigned integer type of the size of type `type`
77 * *without* sign-extension.
78 *
79 * The unsigned cast ensures that we're not shifting a negative value,
80 * which is undefined in C. However, this limits the maximum type size
81 * of `type` to 64-bit. Generate a compile-time error if the size of
82 * `type` is larger than 64-bit.
83 */
84 #define _bt_cast_value_to_unsigned_type(type, v) \
85 (sizeof(type) == sizeof(uint8_t) ? \
86 (uint8_t) _bt_cast_value_to_unsigned(v) : \
87 sizeof(type) == sizeof(uint16_t) ? \
88 (uint16_t) _bt_cast_value_to_unsigned(v) : \
89 sizeof(type) == sizeof(uint32_t) ? \
90 (uint32_t) _bt_cast_value_to_unsigned(v) : \
91 sizeof(type) == sizeof(uint64_t) ? \
92 (uint64_t) _bt_cast_value_to_unsigned(v) : \
93 _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))
94
95 /*
96 * _bt_fill_mask evaluates to a "type" integer with all bits set.
97 */
98 #define _bt_fill_mask(type) ((type) ~(type) 0)
99
100 /*
101 * Left shift a value `v` of `shift` bits.
102 *
103 * The type of `v` can be signed or unsigned integer.
104 * The value of `shift` must be less than the size of `v` (in bits),
105 * otherwise the behavior is undefined.
106 * Evaluates to the result of the shift operation.
107 *
108 * According to the C99 standard, left shift of a left hand-side signed
109 * type is undefined if it has a negative value or if the result cannot
110 * be represented in the result type. This bitfield header discards the
111 * bits that are left-shifted beyond the result type representation,
112 * which is the behavior of an unsigned type left shift operation.
113 * Therefore, always perform left shift on an unsigned type.
114 *
115 * This macro should not be used if `shift` can be greater or equal than
116 * the bitwidth of `v`. See `_bt_safe_lshift`.
117 */
118 #define _bt_lshift(v, shift) \
119 ((__typeof__(v)) (_bt_cast_value_to_unsigned(v) << (shift)))
120
121 /*
122 * Generate a mask of type `type` with the `length` least significant bits
123 * cleared, and the most significant bits set.
124 */
125 #define _bt_make_mask_complement(type, length) \
126 _bt_lshift(_bt_fill_mask(type), length)
127
128 /*
129 * Generate a mask of type `type` with the `length` least significant bits
130 * set, and the most significant bits cleared.
131 */
132 #define _bt_make_mask(type, length) \
133 ((type) ~_bt_make_mask_complement(type, length))
134
135 /*
136 * Right shift a value `v` of `shift` bits.
137 *
138 * The type of `v` can be signed or unsigned integer.
139 * The value of `shift` must be less than the size of `v` (in bits),
140 * otherwise the behavior is undefined.
141 * Evaluates to the result of the shift operation.
142 *
143 * According to the C99 standard, right shift of a left hand-side signed
144 * type which has a negative value is implementation defined. This
145 * bitfield header relies on the right shift implementation carrying the
146 * sign bit. If the compiler implementation has a different behavior,
147 * emulate carrying the sign bit.
148 *
149 * This macro should not be used if `shift` can be greater or equal than
150 * the bitwidth of `v`. See `_bt_safe_rshift`.
151 */
152 #if ((-1 >> 1) == -1)
153 #define _bt_rshift(v, shift) ((v) >> (shift))
154 #else
155 #define _bt_rshift(v, shift) \
156 ((__typeof__(v)) ((_bt_cast_value_to_unsigned(v) >> (shift)) | \
157 ((v) < 0 ? _bt_make_mask_complement(__typeof__(v), \
158 sizeof(v) * CHAR_BIT - (shift)) : 0)))
159 #endif
160
161 /*
162 * Right shift a signed or unsigned integer with `shift` value being an
163 * arbitrary number of bits. `v` is modified by this macro. The shift
164 * is transformed into a sequence of `_nr_partial_shifts` consecutive
165 * shift operations, each of a number of bits smaller than the bitwidth
166 * of `v`, ending with a shift of the number of left over bits.
167 */
168 #define _bt_safe_rshift(v, shift) \
169 do { \
170 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
171 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
172 \
173 for (; _nr_partial_shifts; _nr_partial_shifts--) \
174 (v) = _bt_rshift(v, sizeof(v) * CHAR_BIT - 1); \
175 (v) = _bt_rshift(v, _leftover_bits); \
176 } while (0)
177
178 /*
179 * Left shift a signed or unsigned integer with `shift` value being an
180 * arbitrary number of bits. `v` is modified by this macro. The shift
181 * is transformed into a sequence of `_nr_partial_shifts` consecutive
182 * shift operations, each of a number of bits smaller than the bitwidth
183 * of `v`, ending with a shift of the number of left over bits.
184 */
185 #define _bt_safe_lshift(v, shift) \
186 do { \
187 unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \
188 unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \
189 \
190 for (; _nr_partial_shifts; _nr_partial_shifts--) \
191 (v) = _bt_lshift(v, sizeof(v) * CHAR_BIT - 1); \
192 (v) = _bt_lshift(v, _leftover_bits); \
193 } while (0)
194
195 /*
196 * bt_bitfield_write - write integer to a bitfield in native endianness
197 *
198 * Save integer to the bitfield, which starts at the "start" bit, has "len"
199 * bits.
200 * The inside of a bitfield is from high bits to low bits.
201 * Uses native endianness.
202 * For unsigned "v", pad MSB with 0 if bitfield is larger than v.
203 * For signed "v", sign-extend v if bitfield is larger than v.
204 *
205 * On little endian, bytes are placed from the less significant to the most
206 * significant. Also, consecutive bitfields are placed from lower bits to higher
207 * bits.
208 *
209 * On big endian, bytes are places from most significant to less significant.
210 * Also, consecutive bitfields are placed from higher to lower bits.
211 */
212
213 #define _bt_bitfield_write_le(ptr, type, start, length, v) \
214 do { \
215 __typeof__(v) _v = (v); \
216 type *_ptr = (void *) (ptr); \
217 unsigned long _start = (start), _length = (length); \
218 type _mask, _cmask; \
219 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
220 unsigned long _start_unit, _end_unit, _this_unit; \
221 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
222 \
223 if (!_length) \
224 break; \
225 \
226 _end = _start + _length; \
227 _start_unit = _start / _ts; \
228 _end_unit = (_end + (_ts - 1)) / _ts; \
229 \
230 /* Trim v high bits */ \
231 if (_length < sizeof(_v) * CHAR_BIT) \
232 _v &= _bt_make_mask(__typeof__(_v), _length); \
233 \
234 /* We can now append v with a simple "or", shift it piece-wise */ \
235 _this_unit = _start_unit; \
236 if (_start_unit == _end_unit - 1) { \
237 _mask = _bt_make_mask(type, _start % _ts); \
238 if (_end % _ts) \
239 _mask |= _bt_make_mask_complement(type, _end % _ts); \
240 _cmask = _bt_lshift((type) (_v), _start % _ts); \
241 _cmask &= ~_mask; \
242 _ptr[_this_unit] &= _mask; \
243 _ptr[_this_unit] |= _cmask; \
244 break; \
245 } \
246 if (_start % _ts) { \
247 _cshift = _start % _ts; \
248 _mask = _bt_make_mask(type, _cshift); \
249 _cmask = _bt_lshift((type) (_v), _cshift); \
250 _cmask &= ~_mask; \
251 _ptr[_this_unit] &= _mask; \
252 _ptr[_this_unit] |= _cmask; \
253 _bt_safe_rshift(_v, _ts - _cshift); \
254 _start += _ts - _cshift; \
255 _this_unit++; \
256 } \
257 for (; _this_unit < _end_unit - 1; _this_unit++) { \
258 _ptr[_this_unit] = (type) _v; \
259 _bt_safe_rshift(_v, _ts); \
260 _start += _ts; \
261 } \
262 if (_end % _ts) { \
263 _mask = _bt_make_mask_complement(type, _end % _ts); \
264 _cmask = (type) _v; \
265 _cmask &= ~_mask; \
266 _ptr[_this_unit] &= _mask; \
267 _ptr[_this_unit] |= _cmask; \
268 } else \
269 _ptr[_this_unit] = (type) _v; \
270 } while (0)
271
272 #define _bt_bitfield_write_be(ptr, type, start, length, v) \
273 do { \
274 __typeof__(v) _v = (v); \
275 type *_ptr = (void *) (ptr); \
276 unsigned long _start = (start), _length = (length); \
277 type _mask, _cmask; \
278 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
279 unsigned long _start_unit, _end_unit, _this_unit; \
280 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
281 \
282 if (!_length) \
283 break; \
284 \
285 _end = _start + _length; \
286 _start_unit = _start / _ts; \
287 _end_unit = (_end + (_ts - 1)) / _ts; \
288 \
289 /* Trim v high bits */ \
290 if (_length < sizeof(_v) * CHAR_BIT) \
291 _v &= _bt_make_mask(__typeof__(_v), _length); \
292 \
293 /* We can now append v with a simple "or", shift it piece-wise */ \
294 _this_unit = _end_unit - 1; \
295 if (_start_unit == _end_unit - 1) { \
296 _mask = _bt_make_mask(type, (_ts - (_end % _ts)) % _ts); \
297 if (_start % _ts) \
298 _mask |= _bt_make_mask_complement(type, _ts - (_start % _ts)); \
299 _cmask = _bt_lshift((type) (_v), (_ts - (_end % _ts)) % _ts); \
300 _cmask &= ~_mask; \
301 _ptr[_this_unit] &= _mask; \
302 _ptr[_this_unit] |= _cmask; \
303 break; \
304 } \
305 if (_end % _ts) { \
306 _cshift = _end % _ts; \
307 _mask = _bt_make_mask(type, _ts - _cshift); \
308 _cmask = _bt_lshift((type) (_v), _ts - _cshift); \
309 _cmask &= ~_mask; \
310 _ptr[_this_unit] &= _mask; \
311 _ptr[_this_unit] |= _cmask; \
312 _bt_safe_rshift(_v, _cshift); \
313 _end -= _cshift; \
314 _this_unit--; \
315 } \
316 for (; (long) _this_unit >= (long) _start_unit + 1; _this_unit--) { \
317 _ptr[_this_unit] = (type) _v; \
318 _bt_safe_rshift(_v, _ts); \
319 _end -= _ts; \
320 } \
321 if (_start % _ts) { \
322 _mask = _bt_make_mask_complement(type, _ts - (_start % _ts)); \
323 _cmask = (type) _v; \
324 _cmask &= ~_mask; \
325 _ptr[_this_unit] &= _mask; \
326 _ptr[_this_unit] |= _cmask; \
327 } else \
328 _ptr[_this_unit] = (type) _v; \
329 } while (0)
330
331 /*
332 * bt_bitfield_write - write integer to a bitfield in native endianness
333 * bt_bitfield_write_le - write integer to a bitfield in little endian
334 * bt_bitfield_write_be - write integer to a bitfield in big endian
335 */
336
337 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
338
339 #define bt_bitfield_write(ptr, type, start, length, v) \
340 _bt_bitfield_write_le(ptr, type, start, length, v)
341
342 #define bt_bitfield_write_le(ptr, type, start, length, v) \
343 _bt_bitfield_write_le(ptr, type, start, length, v)
344
345 #define bt_bitfield_write_be(ptr, type, start, length, v) \
346 _bt_bitfield_write_be(ptr, unsigned char, start, length, v)
347
348 #elif (__BYTE_ORDER == __BIG_ENDIAN)
349
350 #define bt_bitfield_write(ptr, type, start, length, v) \
351 _bt_bitfield_write_be(ptr, type, start, length, v)
352
353 #define bt_bitfield_write_le(ptr, type, start, length, v) \
354 _bt_bitfield_write_le(ptr, unsigned char, start, length, v)
355
356 #define bt_bitfield_write_be(ptr, type, start, length, v) \
357 _bt_bitfield_write_be(ptr, type, start, length, v)
358
359 #else /* (__BYTE_ORDER == __PDP_ENDIAN) */
360
361 #error "Byte order not supported"
362
363 #endif
364
365 #define _bt_bitfield_read_le(ptr, type, start, length, vptr) \
366 do { \
367 __typeof__(*(vptr)) *_vptr = (vptr); \
368 __typeof__(*_vptr) _v; \
369 type *_ptr = (type *) (ptr); \
370 unsigned long _start = (start), _length = (length); \
371 type _mask, _cmask; \
372 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
373 unsigned long _start_unit, _end_unit, _this_unit; \
374 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
375 bool _is_signed_type; \
376 \
377 if (!_length) { \
378 *_vptr = 0; \
379 break; \
380 } \
381 \
382 _end = _start + _length; \
383 _start_unit = _start / _ts; \
384 _end_unit = (_end + (_ts - 1)) / _ts; \
385 \
386 _this_unit = _end_unit - 1; \
387 _BT_DIAG_PUSH \
388 _BT_DIAG_IGNORE_TYPE_LIMITS \
389 _is_signed_type = _bt_is_signed_type(__typeof__(_v)); \
390 _BT_DIAG_POP \
391 if (_is_signed_type \
392 && (_ptr[_this_unit] & _bt_lshift((type) 1, (_end % _ts ? _end % _ts : _ts) - 1))) \
393 _v = ~(__typeof__(_v)) 0; \
394 else \
395 _v = 0; \
396 if (_start_unit == _end_unit - 1) { \
397 _cmask = _ptr[_this_unit]; \
398 _cmask = _bt_rshift(_cmask, _start % _ts); \
399 if ((_end - _start) % _ts) { \
400 _mask = _bt_make_mask(type, _end - _start); \
401 _cmask &= _mask; \
402 } \
403 _bt_safe_lshift(_v, _end - _start); \
404 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
405 *_vptr = _v; \
406 break; \
407 } \
408 if (_end % _ts) { \
409 _cshift = _end % _ts; \
410 _mask = _bt_make_mask(type, _cshift); \
411 _cmask = _ptr[_this_unit]; \
412 _cmask &= _mask; \
413 _bt_safe_lshift(_v, _cshift); \
414 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
415 _end -= _cshift; \
416 _this_unit--; \
417 } \
418 for (; (long) _this_unit >= (long) _start_unit + 1; _this_unit--) { \
419 _bt_safe_lshift(_v, _ts); \
420 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
421 _end -= _ts; \
422 } \
423 if (_start % _ts) { \
424 _mask = _bt_make_mask(type, _ts - (_start % _ts)); \
425 _cmask = _ptr[_this_unit]; \
426 _cmask = _bt_rshift(_cmask, _start % _ts); \
427 _cmask &= _mask; \
428 _bt_safe_lshift(_v, _ts - (_start % _ts)); \
429 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
430 } else { \
431 _bt_safe_lshift(_v, _ts); \
432 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
433 } \
434 *_vptr = _v; \
435 } while (0)
436
437 #define _bt_bitfield_read_be(ptr, type, start, length, vptr) \
438 do { \
439 __typeof__(*(vptr)) *_vptr = (vptr); \
440 __typeof__(*_vptr) _v; \
441 type *_ptr = (void *) (ptr); \
442 unsigned long _start = (start), _length = (length); \
443 type _mask, _cmask; \
444 unsigned long _ts = sizeof(type) * CHAR_BIT; /* type size */ \
445 unsigned long _start_unit, _end_unit, _this_unit; \
446 unsigned long _end, _cshift; /* _cshift is "complement shift" */ \
447 bool _is_signed_type; \
448 \
449 if (!_length) { \
450 *_vptr = 0; \
451 break; \
452 } \
453 \
454 _end = _start + _length; \
455 _start_unit = _start / _ts; \
456 _end_unit = (_end + (_ts - 1)) / _ts; \
457 \
458 _this_unit = _start_unit; \
459 _BT_DIAG_PUSH \
460 _BT_DIAG_IGNORE_TYPE_LIMITS \
461 _is_signed_type = _bt_is_signed_type(__typeof__(_v)); \
462 _BT_DIAG_POP \
463 if (_is_signed_type \
464 && (_ptr[_this_unit] & _bt_lshift((type) 1, _ts - (_start % _ts) - 1))) \
465 _v = ~(__typeof__(_v)) 0; \
466 else \
467 _v = 0; \
468 if (_start_unit == _end_unit - 1) { \
469 _cmask = _ptr[_this_unit]; \
470 _cmask = _bt_rshift(_cmask, (_ts - (_end % _ts)) % _ts); \
471 if ((_end - _start) % _ts) { \
472 _mask = _bt_make_mask(type, _end - _start); \
473 _cmask &= _mask; \
474 } \
475 _bt_safe_lshift(_v, _end - _start); \
476 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
477 *_vptr = _v; \
478 break; \
479 } \
480 if (_start % _ts) { \
481 _cshift = _start % _ts; \
482 _mask = _bt_make_mask(type, _ts - _cshift); \
483 _cmask = _ptr[_this_unit]; \
484 _cmask &= _mask; \
485 _bt_safe_lshift(_v, _ts - _cshift); \
486 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
487 _start += _ts - _cshift; \
488 _this_unit++; \
489 } \
490 for (; _this_unit < _end_unit - 1; _this_unit++) { \
491 _bt_safe_lshift(_v, _ts); \
492 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
493 _start += _ts; \
494 } \
495 if (_end % _ts) { \
496 _mask = _bt_make_mask(type, _end % _ts); \
497 _cmask = _ptr[_this_unit]; \
498 _cmask = _bt_rshift(_cmask, _ts - (_end % _ts)); \
499 _cmask &= _mask; \
500 _bt_safe_lshift(_v, _end % _ts); \
501 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \
502 } else { \
503 _bt_safe_lshift(_v, _ts); \
504 _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \
505 } \
506 *_vptr = _v; \
507 } while (0)
508
509 /*
510 * bt_bitfield_read - read integer from a bitfield in native endianness
511 * bt_bitfield_read_le - read integer from a bitfield in little endian
512 * bt_bitfield_read_be - read integer from a bitfield in big endian
513 */
514
515 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
516
517 #define bt_bitfield_read(ptr, type, start, length, vptr) \
518 _bt_bitfield_read_le(ptr, type, start, length, vptr)
519
520 #define bt_bitfield_read_le(ptr, type, start, length, vptr) \
521 _bt_bitfield_read_le(ptr, type, start, length, vptr)
522
523 #define bt_bitfield_read_be(ptr, type, start, length, vptr) \
524 _bt_bitfield_read_be(ptr, unsigned char, start, length, vptr)
525
526 #elif (__BYTE_ORDER == __BIG_ENDIAN)
527
528 #define bt_bitfield_read(ptr, type, start, length, vptr) \
529 _bt_bitfield_read_be(ptr, type, start, length, vptr)
530
531 #define bt_bitfield_read_le(ptr, type, start, length, vptr) \
532 _bt_bitfield_read_le(ptr, unsigned char, start, length, vptr)
533
534 #define bt_bitfield_read_be(ptr, type, start, length, vptr) \
535 _bt_bitfield_read_be(ptr, type, start, length, vptr)
536
537 #else /* (__BYTE_ORDER == __PDP_ENDIAN) */
538
539 #error "Byte order not supported"
540
541 #endif
542
543 #endif /* _BABELTRACE_BITFIELD_H */
This page took 0.038713 seconds and 3 git commands to generate.