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