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