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