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