| 1 | #ifndef _BABELTRACE_BITFIELD_H |
| 2 | #define _BABELTRACE_BITFIELD_H |
| 3 | |
| 4 | /* |
| 5 | * BabelTrace |
| 6 | * |
| 7 | * Bitfields read/write functions. |
| 8 | * |
| 9 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 10 | * |
| 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 27 | * SOFTWARE. |
| 28 | */ |
| 29 | |
| 30 | #include <stdint.h> /* C99 5.2.4.2 Numerical limits */ |
| 31 | #include <babeltrace/compat/limits-internal.h> /* C99 5.2.4.2 Numerical limits */ |
| 32 | #include <assert.h> |
| 33 | #include <babeltrace/endian-internal.h> /* Non-standard BIG_ENDIAN, LITTLE_ENDIAN, BYTE_ORDER */ |
| 34 | |
| 35 | /* We can't shift a int from 32 bit, >> 32 and << 32 on int is undefined */ |
| 36 | #define _bt_piecewise_rshift(_v, _shift) \ |
| 37 | ({ \ |
| 38 | typeof(_v) ___v = (_v); \ |
| 39 | typeof(_shift) ___shift = (_shift); \ |
| 40 | unsigned long sb = (___shift) / (sizeof(___v) * CHAR_BIT - 1); \ |
| 41 | unsigned long final = (___shift) % (sizeof(___v) * CHAR_BIT - 1); \ |
| 42 | \ |
| 43 | for (; sb; sb--) \ |
| 44 | ___v >>= sizeof(___v) * CHAR_BIT - 1; \ |
| 45 | ___v >>= final; \ |
| 46 | }) |
| 47 | |
| 48 | #define _bt_piecewise_lshift(_v, _shift) \ |
| 49 | ({ \ |
| 50 | typeof(_v) ___v = (_v); \ |
| 51 | typeof(_shift) ___shift = (_shift); \ |
| 52 | unsigned long sb = (___shift) / (sizeof(___v) * CHAR_BIT - 1); \ |
| 53 | unsigned long final = (___shift) % (sizeof(___v) * CHAR_BIT - 1); \ |
| 54 | \ |
| 55 | for (; sb; sb--) \ |
| 56 | ___v <<= sizeof(___v) * CHAR_BIT - 1; \ |
| 57 | ___v <<= final; \ |
| 58 | }) |
| 59 | |
| 60 | #define _bt_is_signed_type(type) ((type) -1 < (type) 0) |
| 61 | |
| 62 | /* |
| 63 | * NOTE: The cast to (uint64_t) below ensures that we're not casting a |
| 64 | * negative value, which is undefined in C. However, this limits the |
| 65 | * maximum type size of `type` and `v` to 64-bit. The |
| 66 | * _bt_check_max_64bit() is used to check that the users of this header |
| 67 | * do not use types with a size greater than 64-bit. |
| 68 | */ |
| 69 | #define _bt_unsigned_cast(type, v) \ |
| 70 | ({ \ |
| 71 | (sizeof(v) < sizeof(type)) ? \ |
| 72 | ((type) (v)) & ((type) (~(~(uint64_t) 0 << (sizeof(v) * CHAR_BIT)))) : \ |
| 73 | (type) (v); \ |
| 74 | }) |
| 75 | |
| 76 | #define _bt_check_max_64bit(type) \ |
| 77 | char _max_64bit_assertion[sizeof(type) <= sizeof(uint64_t) ? 1 : -1] __attribute__((unused)) |
| 78 | |
| 79 | /* |
| 80 | * bt_bitfield_write - write integer to a bitfield in native endianness |
| 81 | * |
| 82 | * Save integer to the bitfield, which starts at the "start" bit, has "len" |
| 83 | * bits. |
| 84 | * The inside of a bitfield is from high bits to low bits. |
| 85 | * Uses native endianness. |
| 86 | * For unsigned "v", pad MSB with 0 if bitfield is larger than v. |
| 87 | * For signed "v", sign-extend v if bitfield is larger than v. |
| 88 | * |
| 89 | * On little endian, bytes are placed from the less significant to the most |
| 90 | * significant. Also, consecutive bitfields are placed from lower bits to higher |
| 91 | * bits. |
| 92 | * |
| 93 | * On big endian, bytes are places from most significant to less significant. |
| 94 | * Also, consecutive bitfields are placed from higher to lower bits. |
| 95 | */ |
| 96 | |
| 97 | #define _bt_bitfield_write_le(_ptr, type, _start, _length, _v) \ |
| 98 | do { \ |
| 99 | typeof(_v) __v = (_v); \ |
| 100 | type *__ptr = (void *) (_ptr); \ |
| 101 | unsigned long __start = (_start), __length = (_length); \ |
| 102 | type mask, cmask; \ |
| 103 | unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \ |
| 104 | unsigned long start_unit, end_unit, this_unit; \ |
| 105 | unsigned long end, cshift; /* cshift is "complement shift" */ \ |
| 106 | \ |
| 107 | if (!__length) \ |
| 108 | break; \ |
| 109 | \ |
| 110 | end = __start + __length; \ |
| 111 | start_unit = __start / ts; \ |
| 112 | end_unit = (end + (ts - 1)) / ts; \ |
| 113 | \ |
| 114 | /* Trim v high bits */ \ |
| 115 | if (__length < sizeof(__v) * CHAR_BIT) \ |
| 116 | __v &= ~((~(typeof(__v)) 0) << __length); \ |
| 117 | \ |
| 118 | /* We can now append v with a simple "or", shift it piece-wise */ \ |
| 119 | this_unit = start_unit; \ |
| 120 | if (start_unit == end_unit - 1) { \ |
| 121 | mask = ~((~(type) 0) << (__start % ts)); \ |
| 122 | if (end % ts) \ |
| 123 | mask |= (~(type) 0) << (end % ts); \ |
| 124 | cmask = (type) __v << (__start % ts); \ |
| 125 | cmask &= ~mask; \ |
| 126 | __ptr[this_unit] &= mask; \ |
| 127 | __ptr[this_unit] |= cmask; \ |
| 128 | break; \ |
| 129 | } \ |
| 130 | if (__start % ts) { \ |
| 131 | cshift = __start % ts; \ |
| 132 | mask = ~((~(type) 0) << cshift); \ |
| 133 | cmask = (type) __v << cshift; \ |
| 134 | cmask &= ~mask; \ |
| 135 | __ptr[this_unit] &= mask; \ |
| 136 | __ptr[this_unit] |= cmask; \ |
| 137 | __v = _bt_piecewise_rshift(__v, ts - cshift); \ |
| 138 | __start += ts - cshift; \ |
| 139 | this_unit++; \ |
| 140 | } \ |
| 141 | for (; this_unit < end_unit - 1; this_unit++) { \ |
| 142 | __ptr[this_unit] = (type) __v; \ |
| 143 | __v = _bt_piecewise_rshift(__v, ts); \ |
| 144 | __start += ts; \ |
| 145 | } \ |
| 146 | if (end % ts) { \ |
| 147 | mask = (~(type) 0) << (end % ts); \ |
| 148 | cmask = (type) __v; \ |
| 149 | cmask &= ~mask; \ |
| 150 | __ptr[this_unit] &= mask; \ |
| 151 | __ptr[this_unit] |= cmask; \ |
| 152 | } else \ |
| 153 | __ptr[this_unit] = (type) __v; \ |
| 154 | } while (0) |
| 155 | |
| 156 | #define _bt_bitfield_write_be(_ptr, type, _start, _length, _v) \ |
| 157 | do { \ |
| 158 | typeof(_v) __v = (_v); \ |
| 159 | type *__ptr = (void *) (_ptr); \ |
| 160 | unsigned long __start = (_start), __length = (_length); \ |
| 161 | type mask, cmask; \ |
| 162 | unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \ |
| 163 | unsigned long start_unit, end_unit, this_unit; \ |
| 164 | unsigned long end, cshift; /* cshift is "complement shift" */ \ |
| 165 | \ |
| 166 | if (!__length) \ |
| 167 | break; \ |
| 168 | \ |
| 169 | end = __start + __length; \ |
| 170 | start_unit = __start / ts; \ |
| 171 | end_unit = (end + (ts - 1)) / ts; \ |
| 172 | \ |
| 173 | /* Trim v high bits */ \ |
| 174 | if (__length < sizeof(__v) * CHAR_BIT) \ |
| 175 | __v &= ~((~(typeof(__v)) 0) << __length); \ |
| 176 | \ |
| 177 | /* We can now append v with a simple "or", shift it piece-wise */ \ |
| 178 | this_unit = end_unit - 1; \ |
| 179 | if (start_unit == end_unit - 1) { \ |
| 180 | mask = ~((~(type) 0) << ((ts - (end % ts)) % ts)); \ |
| 181 | if (__start % ts) \ |
| 182 | mask |= (~((type) 0)) << (ts - (__start % ts)); \ |
| 183 | cmask = (type) __v << ((ts - (end % ts)) % ts); \ |
| 184 | cmask &= ~mask; \ |
| 185 | __ptr[this_unit] &= mask; \ |
| 186 | __ptr[this_unit] |= cmask; \ |
| 187 | break; \ |
| 188 | } \ |
| 189 | if (end % ts) { \ |
| 190 | cshift = end % ts; \ |
| 191 | mask = ~((~(type) 0) << (ts - cshift)); \ |
| 192 | cmask = (type) __v << (ts - cshift); \ |
| 193 | cmask &= ~mask; \ |
| 194 | __ptr[this_unit] &= mask; \ |
| 195 | __ptr[this_unit] |= cmask; \ |
| 196 | __v = _bt_piecewise_rshift(__v, cshift); \ |
| 197 | end -= cshift; \ |
| 198 | this_unit--; \ |
| 199 | } \ |
| 200 | for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \ |
| 201 | __ptr[this_unit] = (type) __v; \ |
| 202 | __v = _bt_piecewise_rshift(__v, ts); \ |
| 203 | end -= ts; \ |
| 204 | } \ |
| 205 | if (__start % ts) { \ |
| 206 | mask = (~(type) 0) << (ts - (__start % ts)); \ |
| 207 | cmask = (type) __v; \ |
| 208 | cmask &= ~mask; \ |
| 209 | __ptr[this_unit] &= mask; \ |
| 210 | __ptr[this_unit] |= cmask; \ |
| 211 | } else \ |
| 212 | __ptr[this_unit] = (type) __v; \ |
| 213 | } while (0) |
| 214 | |
| 215 | /* |
| 216 | * bt_bitfield_write - write integer to a bitfield in native endianness |
| 217 | * bt_bitfield_write_le - write integer to a bitfield in little endian |
| 218 | * bt_bitfield_write_be - write integer to a bitfield in big endian |
| 219 | */ |
| 220 | |
| 221 | #if (BYTE_ORDER == LITTLE_ENDIAN) |
| 222 | |
| 223 | #define bt_bitfield_write(ptr, type, _start, _length, _v) \ |
| 224 | _bt_bitfield_write_le(ptr, type, _start, _length, _v) |
| 225 | |
| 226 | #define bt_bitfield_write_le(ptr, type, _start, _length, _v) \ |
| 227 | _bt_bitfield_write_le(ptr, type, _start, _length, _v) |
| 228 | |
| 229 | #define bt_bitfield_write_be(ptr, type, _start, _length, _v) \ |
| 230 | _bt_bitfield_write_be(ptr, unsigned char, _start, _length, _v) |
| 231 | |
| 232 | #elif (BYTE_ORDER == BIG_ENDIAN) |
| 233 | |
| 234 | #define bt_bitfield_write(ptr, type, _start, _length, _v) \ |
| 235 | _bt_bitfield_write_be(ptr, type, _start, _length, _v) |
| 236 | |
| 237 | #define bt_bitfield_write_le(ptr, type, _start, _length, _v) \ |
| 238 | _bt_bitfield_write_le(ptr, unsigned char, _start, _length, _v) |
| 239 | |
| 240 | #define bt_bitfield_write_be(ptr, type, _start, _length, _v) \ |
| 241 | _bt_bitfield_write_be(ptr, type, _start, _length, _v) |
| 242 | |
| 243 | #else /* (BYTE_ORDER == PDP_ENDIAN) */ |
| 244 | |
| 245 | #error "Byte order not supported" |
| 246 | |
| 247 | #endif |
| 248 | |
| 249 | #define _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \ |
| 250 | do { \ |
| 251 | typeof(*(_vptr)) *__vptr = (_vptr); \ |
| 252 | typeof(*__vptr) __v; \ |
| 253 | type *__ptr = (void *) (_ptr); \ |
| 254 | unsigned long __start = (_start), __length = (_length); \ |
| 255 | type mask, cmask; \ |
| 256 | unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \ |
| 257 | unsigned long start_unit, end_unit, this_unit; \ |
| 258 | unsigned long end, cshift; /* cshift is "complement shift" */ \ |
| 259 | \ |
| 260 | { _bt_check_max_64bit(type); } \ |
| 261 | { _bt_check_max_64bit(typeof(*_vptr)); } \ |
| 262 | { _bt_check_max_64bit(typeof(*_ptr)); } \ |
| 263 | \ |
| 264 | if (!__length) { \ |
| 265 | *__vptr = 0; \ |
| 266 | break; \ |
| 267 | } \ |
| 268 | \ |
| 269 | end = __start + __length; \ |
| 270 | start_unit = __start / ts; \ |
| 271 | end_unit = (end + (ts - 1)) / ts; \ |
| 272 | \ |
| 273 | this_unit = end_unit - 1; \ |
| 274 | if (_bt_is_signed_type(typeof(__v)) \ |
| 275 | && (__ptr[this_unit] & ((type) 1 << ((end % ts ? : ts) - 1)))) \ |
| 276 | __v = ~(typeof(__v)) 0; \ |
| 277 | else \ |
| 278 | __v = 0; \ |
| 279 | if (start_unit == end_unit - 1) { \ |
| 280 | cmask = __ptr[this_unit]; \ |
| 281 | cmask >>= (__start % ts); \ |
| 282 | if ((end - __start) % ts) { \ |
| 283 | mask = ~((~(type) 0) << (end - __start)); \ |
| 284 | cmask &= mask; \ |
| 285 | } \ |
| 286 | __v = _bt_piecewise_lshift(__v, end - __start); \ |
| 287 | __v |= _bt_unsigned_cast(typeof(__v), cmask); \ |
| 288 | *__vptr = __v; \ |
| 289 | break; \ |
| 290 | } \ |
| 291 | if (end % ts) { \ |
| 292 | cshift = end % ts; \ |
| 293 | mask = ~((~(type) 0) << cshift); \ |
| 294 | cmask = __ptr[this_unit]; \ |
| 295 | cmask &= mask; \ |
| 296 | __v = _bt_piecewise_lshift(__v, cshift); \ |
| 297 | __v |= _bt_unsigned_cast(typeof(__v), cmask); \ |
| 298 | end -= cshift; \ |
| 299 | this_unit--; \ |
| 300 | } \ |
| 301 | for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \ |
| 302 | __v = _bt_piecewise_lshift(__v, ts); \ |
| 303 | __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\ |
| 304 | end -= ts; \ |
| 305 | } \ |
| 306 | if (__start % ts) { \ |
| 307 | mask = ~((~(type) 0) << (ts - (__start % ts))); \ |
| 308 | cmask = __ptr[this_unit]; \ |
| 309 | cmask >>= (__start % ts); \ |
| 310 | cmask &= mask; \ |
| 311 | __v = _bt_piecewise_lshift(__v, ts - (__start % ts)); \ |
| 312 | __v |= _bt_unsigned_cast(typeof(__v), cmask); \ |
| 313 | } else { \ |
| 314 | __v = _bt_piecewise_lshift(__v, ts); \ |
| 315 | __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\ |
| 316 | } \ |
| 317 | *__vptr = __v; \ |
| 318 | } while (0) |
| 319 | |
| 320 | #define _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \ |
| 321 | do { \ |
| 322 | typeof(*(_vptr)) *__vptr = (_vptr); \ |
| 323 | typeof(*__vptr) __v; \ |
| 324 | type *__ptr = (void *) (_ptr); \ |
| 325 | unsigned long __start = (_start), __length = (_length); \ |
| 326 | type mask, cmask; \ |
| 327 | unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \ |
| 328 | unsigned long start_unit, end_unit, this_unit; \ |
| 329 | unsigned long end, cshift; /* cshift is "complement shift" */ \ |
| 330 | \ |
| 331 | { _bt_check_max_64bit(type); } \ |
| 332 | { _bt_check_max_64bit(typeof(*_vptr)); } \ |
| 333 | { _bt_check_max_64bit(typeof(*_ptr)); } \ |
| 334 | \ |
| 335 | if (!__length) { \ |
| 336 | *__vptr = 0; \ |
| 337 | break; \ |
| 338 | } \ |
| 339 | \ |
| 340 | end = __start + __length; \ |
| 341 | start_unit = __start / ts; \ |
| 342 | end_unit = (end + (ts - 1)) / ts; \ |
| 343 | \ |
| 344 | this_unit = start_unit; \ |
| 345 | if (_bt_is_signed_type(typeof(__v)) \ |
| 346 | && (__ptr[this_unit] & ((type) 1 << (ts - (__start % ts) - 1)))) \ |
| 347 | __v = ~(typeof(__v)) 0; \ |
| 348 | else \ |
| 349 | __v = 0; \ |
| 350 | if (start_unit == end_unit - 1) { \ |
| 351 | cmask = __ptr[this_unit]; \ |
| 352 | cmask >>= (ts - (end % ts)) % ts; \ |
| 353 | if ((end - __start) % ts) { \ |
| 354 | mask = ~((~(type) 0) << (end - __start)); \ |
| 355 | cmask &= mask; \ |
| 356 | } \ |
| 357 | __v = _bt_piecewise_lshift(__v, end - __start); \ |
| 358 | __v |= _bt_unsigned_cast(typeof(__v), cmask); \ |
| 359 | *__vptr = __v; \ |
| 360 | break; \ |
| 361 | } \ |
| 362 | if (__start % ts) { \ |
| 363 | cshift = __start % ts; \ |
| 364 | mask = ~((~(type) 0) << (ts - cshift)); \ |
| 365 | cmask = __ptr[this_unit]; \ |
| 366 | cmask &= mask; \ |
| 367 | __v = _bt_piecewise_lshift(__v, ts - cshift); \ |
| 368 | __v |= _bt_unsigned_cast(typeof(__v), cmask); \ |
| 369 | __start += ts - cshift; \ |
| 370 | this_unit++; \ |
| 371 | } \ |
| 372 | for (; this_unit < end_unit - 1; this_unit++) { \ |
| 373 | __v = _bt_piecewise_lshift(__v, ts); \ |
| 374 | __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\ |
| 375 | __start += ts; \ |
| 376 | } \ |
| 377 | if (end % ts) { \ |
| 378 | mask = ~((~(type) 0) << (end % ts)); \ |
| 379 | cmask = __ptr[this_unit]; \ |
| 380 | cmask >>= ts - (end % ts); \ |
| 381 | cmask &= mask; \ |
| 382 | __v = _bt_piecewise_lshift(__v, end % ts); \ |
| 383 | __v |= _bt_unsigned_cast(typeof(__v), cmask); \ |
| 384 | } else { \ |
| 385 | __v = _bt_piecewise_lshift(__v, ts); \ |
| 386 | __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\ |
| 387 | } \ |
| 388 | *__vptr = __v; \ |
| 389 | } while (0) |
| 390 | |
| 391 | /* |
| 392 | * bt_bitfield_read - read integer from a bitfield in native endianness |
| 393 | * bt_bitfield_read_le - read integer from a bitfield in little endian |
| 394 | * bt_bitfield_read_be - read integer from a bitfield in big endian |
| 395 | */ |
| 396 | |
| 397 | #if (BYTE_ORDER == LITTLE_ENDIAN) |
| 398 | |
| 399 | #define bt_bitfield_read(_ptr, type, _start, _length, _vptr) \ |
| 400 | _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) |
| 401 | |
| 402 | #define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \ |
| 403 | _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) |
| 404 | |
| 405 | #define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \ |
| 406 | _bt_bitfield_read_be(_ptr, unsigned char, _start, _length, _vptr) |
| 407 | |
| 408 | #elif (BYTE_ORDER == BIG_ENDIAN) |
| 409 | |
| 410 | #define bt_bitfield_read(_ptr, type, _start, _length, _vptr) \ |
| 411 | _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) |
| 412 | |
| 413 | #define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \ |
| 414 | _bt_bitfield_read_le(_ptr, unsigned char, _start, _length, _vptr) |
| 415 | |
| 416 | #define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \ |
| 417 | _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) |
| 418 | |
| 419 | #else /* (BYTE_ORDER == PDP_ENDIAN) */ |
| 420 | |
| 421 | #error "Byte order not supported" |
| 422 | |
| 423 | #endif |
| 424 | |
| 425 | #endif /* _BABELTRACE_BITFIELD_H */ |