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