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