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