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