| 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 | |
| 22 | #include <stdint.h> /* C99 5.2.4.2 Numerical limits */ |
| 23 | #include <limits.h> /* C99 5.2.4.2 Numerical limits */ |
| 24 | #include <endian.h> /* Non-standard BIG_ENDIAN, LITTLE_ENDIAN, BYTE_ORDER */ |
| 25 | #include <assert.h> |
| 26 | |
| 27 | /* We can't shift a int from 32 bit, >> 32 and << 32 on int is undefined */ |
| 28 | #define _bt_piecewise_rshift(_v, _shift) \ |
| 29 | ({ \ |
| 30 | typeof(_v) ___v = (_v); \ |
| 31 | typeof(_shift) ___shift = (_shift); \ |
| 32 | unsigned long sb = (___shift) / (sizeof(___v) * CHAR_BIT - 1); \ |
| 33 | unsigned long final = (___shift) % (sizeof(___v) * CHAR_BIT - 1); \ |
| 34 | \ |
| 35 | for (; sb; sb--) \ |
| 36 | ___v >>= sizeof(___v) * CHAR_BIT - 1; \ |
| 37 | ___v >>= final; \ |
| 38 | }) |
| 39 | |
| 40 | #define _bt_piecewise_lshift(_v, _shift) \ |
| 41 | ({ \ |
| 42 | typeof(_v) ___v = (_v); \ |
| 43 | typeof(_shift) ___shift = (_shift); \ |
| 44 | unsigned long sb = (___shift) / (sizeof(___v) * CHAR_BIT - 1); \ |
| 45 | unsigned long final = (___shift) % (sizeof(___v) * CHAR_BIT - 1); \ |
| 46 | \ |
| 47 | for (; sb; sb--) \ |
| 48 | ___v <<= sizeof(___v) * CHAR_BIT - 1; \ |
| 49 | ___v <<= final; \ |
| 50 | }) |
| 51 | |
| 52 | #define _bt_is_signed_type(type) (((type)(-1)) < 0) |
| 53 | |
| 54 | #define _bt_unsigned_cast(type, v) \ |
| 55 | ({ \ |
| 56 | (sizeof(v) < sizeof(type)) ? \ |
| 57 | ((type) (v)) & (~(~(type) 0 << (sizeof(v) * CHAR_BIT))) : \ |
| 58 | (type) (v); \ |
| 59 | }) |
| 60 | |
| 61 | /* |
| 62 | * bt_bitfield_write - write integer to a bitfield in native endianness |
| 63 | * |
| 64 | * Save integer to the bitfield, which starts at the "start" bit, has "len" |
| 65 | * bits. |
| 66 | * The inside of a bitfield is from high bits to low bits. |
| 67 | * Uses native endianness. |
| 68 | * For unsigned "v", pad MSB with 0 if bitfield is larger than v. |
| 69 | * For signed "v", sign-extend v if bitfield is larger than v. |
| 70 | * |
| 71 | * On little endian, bytes are placed from the less significant to the most |
| 72 | * significant. Also, consecutive bitfields are placed from lower bits to higher |
| 73 | * bits. |
| 74 | * |
| 75 | * On big endian, bytes are places from most significant to less significant. |
| 76 | * Also, consecutive bitfields are placed from higher to lower bits. |
| 77 | */ |
| 78 | |
| 79 | #define _bt_bitfield_write_le(_ptr, type, _start, _length, _v) \ |
| 80 | do { \ |
| 81 | typeof(_v) __v = (_v); \ |
| 82 | type *__ptr = (void *) (_ptr); \ |
| 83 | unsigned long __start = (_start), __length = (_length); \ |
| 84 | type mask, cmask; \ |
| 85 | unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \ |
| 86 | unsigned long start_unit, end_unit, this_unit; \ |
| 87 | unsigned long end, cshift; /* cshift is "complement shift" */ \ |
| 88 | \ |
| 89 | if (!__length) \ |
| 90 | break; \ |
| 91 | \ |
| 92 | end = __start + __length; \ |
| 93 | start_unit = __start / ts; \ |
| 94 | end_unit = (end + (ts - 1)) / ts; \ |
| 95 | \ |
| 96 | /* Trim v high bits */ \ |
| 97 | if (__length < sizeof(__v) * CHAR_BIT) \ |
| 98 | __v &= ~((~(typeof(__v)) 0) << __length); \ |
| 99 | \ |
| 100 | /* We can now append v with a simple "or", shift it piece-wise */ \ |
| 101 | this_unit = start_unit; \ |
| 102 | if (start_unit == end_unit - 1) { \ |
| 103 | mask = ~((~(type) 0) << (__start % ts)); \ |
| 104 | if (end % ts) \ |
| 105 | mask |= (~(type) 0) << (end % ts); \ |
| 106 | cmask = (type) __v << (__start % ts); \ |
| 107 | cmask &= ~mask; \ |
| 108 | __ptr[this_unit] &= mask; \ |
| 109 | __ptr[this_unit] |= cmask; \ |
| 110 | break; \ |
| 111 | } \ |
| 112 | if (__start % ts) { \ |
| 113 | cshift = __start % ts; \ |
| 114 | mask = ~((~(type) 0) << cshift); \ |
| 115 | cmask = (type) __v << cshift; \ |
| 116 | cmask &= ~mask; \ |
| 117 | __ptr[this_unit] &= mask; \ |
| 118 | __ptr[this_unit] |= cmask; \ |
| 119 | __v = _bt_piecewise_rshift(__v, ts - cshift); \ |
| 120 | __start += ts - cshift; \ |
| 121 | this_unit++; \ |
| 122 | } \ |
| 123 | for (; this_unit < end_unit - 1; this_unit++) { \ |
| 124 | __ptr[this_unit] = (type) __v; \ |
| 125 | __v = _bt_piecewise_rshift(__v, ts); \ |
| 126 | __start += ts; \ |
| 127 | } \ |
| 128 | if (end % ts) { \ |
| 129 | mask = (~(type) 0) << (end % ts); \ |
| 130 | cmask = (type) __v; \ |
| 131 | cmask &= ~mask; \ |
| 132 | __ptr[this_unit] &= mask; \ |
| 133 | __ptr[this_unit] |= cmask; \ |
| 134 | } else \ |
| 135 | __ptr[this_unit] = (type) __v; \ |
| 136 | } while (0) |
| 137 | |
| 138 | #define _bt_bitfield_write_be(_ptr, type, _start, _length, _v) \ |
| 139 | do { \ |
| 140 | typeof(_v) __v = (_v); \ |
| 141 | type *__ptr = (void *) (_ptr); \ |
| 142 | unsigned long __start = (_start), __length = (_length); \ |
| 143 | type mask, cmask; \ |
| 144 | unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \ |
| 145 | unsigned long start_unit, end_unit, this_unit; \ |
| 146 | unsigned long end, cshift; /* cshift is "complement shift" */ \ |
| 147 | \ |
| 148 | if (!__length) \ |
| 149 | break; \ |
| 150 | \ |
| 151 | end = __start + __length; \ |
| 152 | start_unit = __start / ts; \ |
| 153 | end_unit = (end + (ts - 1)) / ts; \ |
| 154 | \ |
| 155 | /* Trim v high bits */ \ |
| 156 | if (__length < sizeof(__v) * CHAR_BIT) \ |
| 157 | __v &= ~((~(typeof(__v)) 0) << __length); \ |
| 158 | \ |
| 159 | /* We can now append v with a simple "or", shift it piece-wise */ \ |
| 160 | this_unit = end_unit - 1; \ |
| 161 | if (start_unit == end_unit - 1) { \ |
| 162 | mask = ~((~(type) 0) << ((ts - (end % ts)) % ts)); \ |
| 163 | if (__start % ts) \ |
| 164 | mask |= (~((type) 0)) << (ts - (__start % ts)); \ |
| 165 | cmask = (type) __v << ((ts - (end % ts)) % ts); \ |
| 166 | cmask &= ~mask; \ |
| 167 | __ptr[this_unit] &= mask; \ |
| 168 | __ptr[this_unit] |= cmask; \ |
| 169 | break; \ |
| 170 | } \ |
| 171 | if (end % ts) { \ |
| 172 | cshift = end % ts; \ |
| 173 | mask = ~((~(type) 0) << (ts - cshift)); \ |
| 174 | cmask = (type) __v << (ts - cshift); \ |
| 175 | cmask &= ~mask; \ |
| 176 | __ptr[this_unit] &= mask; \ |
| 177 | __ptr[this_unit] |= cmask; \ |
| 178 | __v = _bt_piecewise_rshift(__v, cshift); \ |
| 179 | end -= cshift; \ |
| 180 | this_unit--; \ |
| 181 | } \ |
| 182 | for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \ |
| 183 | __ptr[this_unit] = (type) __v; \ |
| 184 | __v = _bt_piecewise_rshift(__v, ts); \ |
| 185 | end -= ts; \ |
| 186 | } \ |
| 187 | if (__start % ts) { \ |
| 188 | mask = (~(type) 0) << (ts - (__start % ts)); \ |
| 189 | cmask = (type) __v; \ |
| 190 | cmask &= ~mask; \ |
| 191 | __ptr[this_unit] &= mask; \ |
| 192 | __ptr[this_unit] |= cmask; \ |
| 193 | } else \ |
| 194 | __ptr[this_unit] = (type) __v; \ |
| 195 | } while (0) |
| 196 | |
| 197 | /* |
| 198 | * bt_bitfield_write - write integer to a bitfield in native endianness |
| 199 | * bt_bitfield_write_le - write integer to a bitfield in little endian |
| 200 | * bt_bitfield_write_be - write integer to a bitfield in big endian |
| 201 | */ |
| 202 | |
| 203 | #if (BYTE_ORDER == LITTLE_ENDIAN) |
| 204 | |
| 205 | #define bt_bitfield_write(ptr, type, _start, _length, _v) \ |
| 206 | _bt_bitfield_write_le(ptr, type, _start, _length, _v) |
| 207 | |
| 208 | #define bt_bitfield_write_le(ptr, type, _start, _length, _v) \ |
| 209 | _bt_bitfield_write_le(ptr, type, _start, _length, _v) |
| 210 | |
| 211 | #define bt_bitfield_write_be(ptr, type, _start, _length, _v) \ |
| 212 | _bt_bitfield_write_be(ptr, unsigned char, _start, _length, _v) |
| 213 | |
| 214 | #elif (BYTE_ORDER == BIG_ENDIAN) |
| 215 | |
| 216 | #define bt_bitfield_write(ptr, type, _start, _length, _v) \ |
| 217 | _bt_bitfield_write_be(ptr, type, _start, _length, _v) |
| 218 | |
| 219 | #define bt_bitfield_write_le(ptr, type, _start, _length, _v) \ |
| 220 | _bt_bitfield_write_le(ptr, unsigned char, _start, _length, _v) |
| 221 | |
| 222 | #define bt_bitfield_write_be(ptr, type, _start, _length, _v) \ |
| 223 | _bt_bitfield_write_be(ptr, type, _start, _length, _v) |
| 224 | |
| 225 | #else /* (BYTE_ORDER == PDP_ENDIAN) */ |
| 226 | |
| 227 | #error "Byte order not supported" |
| 228 | |
| 229 | #endif |
| 230 | |
| 231 | #define _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \ |
| 232 | do { \ |
| 233 | typeof(*(_vptr)) *__vptr = (_vptr); \ |
| 234 | typeof(*__vptr) __v; \ |
| 235 | type *__ptr = (void *) (_ptr); \ |
| 236 | unsigned long __start = (_start), __length = (_length); \ |
| 237 | type mask, cmask; \ |
| 238 | unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \ |
| 239 | unsigned long start_unit, end_unit, this_unit; \ |
| 240 | unsigned long end, cshift; /* cshift is "complement shift" */ \ |
| 241 | \ |
| 242 | if (!__length) { \ |
| 243 | *__vptr = 0; \ |
| 244 | break; \ |
| 245 | } \ |
| 246 | \ |
| 247 | end = __start + __length; \ |
| 248 | start_unit = __start / ts; \ |
| 249 | end_unit = (end + (ts - 1)) / ts; \ |
| 250 | \ |
| 251 | this_unit = end_unit - 1; \ |
| 252 | if (_bt_is_signed_type(typeof(__v)) \ |
| 253 | && (__ptr[this_unit] & ((type) 1 << ((end % ts ? : ts) - 1)))) \ |
| 254 | __v = ~(typeof(__v)) 0; \ |
| 255 | else \ |
| 256 | __v = 0; \ |
| 257 | if (start_unit == end_unit - 1) { \ |
| 258 | cmask = __ptr[this_unit]; \ |
| 259 | cmask >>= (__start % ts); \ |
| 260 | if ((end - __start) % ts) { \ |
| 261 | mask = ~((~(type) 0) << (end - __start)); \ |
| 262 | cmask &= mask; \ |
| 263 | } \ |
| 264 | __v = _bt_piecewise_lshift(__v, end - __start); \ |
| 265 | __v |= _bt_unsigned_cast(typeof(__v), cmask); \ |
| 266 | *__vptr = __v; \ |
| 267 | break; \ |
| 268 | } \ |
| 269 | if (end % ts) { \ |
| 270 | cshift = end % ts; \ |
| 271 | mask = ~((~(type) 0) << cshift); \ |
| 272 | cmask = __ptr[this_unit]; \ |
| 273 | cmask &= mask; \ |
| 274 | __v = _bt_piecewise_lshift(__v, cshift); \ |
| 275 | __v |= _bt_unsigned_cast(typeof(__v), cmask); \ |
| 276 | end -= cshift; \ |
| 277 | this_unit--; \ |
| 278 | } \ |
| 279 | for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \ |
| 280 | __v = _bt_piecewise_lshift(__v, ts); \ |
| 281 | __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\ |
| 282 | end -= ts; \ |
| 283 | } \ |
| 284 | if (__start % ts) { \ |
| 285 | mask = ~((~(type) 0) << (ts - (__start % ts))); \ |
| 286 | cmask = __ptr[this_unit]; \ |
| 287 | cmask >>= (__start % ts); \ |
| 288 | cmask &= mask; \ |
| 289 | __v = _bt_piecewise_lshift(__v, ts - (__start % ts)); \ |
| 290 | __v |= _bt_unsigned_cast(typeof(__v), cmask); \ |
| 291 | } else { \ |
| 292 | __v = _bt_piecewise_lshift(__v, ts); \ |
| 293 | __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\ |
| 294 | } \ |
| 295 | *__vptr = __v; \ |
| 296 | } while (0) |
| 297 | |
| 298 | #define _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \ |
| 299 | do { \ |
| 300 | typeof(*(_vptr)) *__vptr = (_vptr); \ |
| 301 | typeof(*__vptr) __v; \ |
| 302 | type *__ptr = (void *) (_ptr); \ |
| 303 | unsigned long __start = (_start), __length = (_length); \ |
| 304 | type mask, cmask; \ |
| 305 | unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \ |
| 306 | unsigned long start_unit, end_unit, this_unit; \ |
| 307 | unsigned long end, cshift; /* cshift is "complement shift" */ \ |
| 308 | \ |
| 309 | if (!__length) { \ |
| 310 | *__vptr = 0; \ |
| 311 | break; \ |
| 312 | } \ |
| 313 | \ |
| 314 | end = __start + __length; \ |
| 315 | start_unit = __start / ts; \ |
| 316 | end_unit = (end + (ts - 1)) / ts; \ |
| 317 | \ |
| 318 | this_unit = start_unit; \ |
| 319 | if (_bt_is_signed_type(typeof(__v)) \ |
| 320 | && (__ptr[this_unit] & ((type) 1 << (ts - (__start % ts) - 1)))) \ |
| 321 | __v = ~(typeof(__v)) 0; \ |
| 322 | else \ |
| 323 | __v = 0; \ |
| 324 | if (start_unit == end_unit - 1) { \ |
| 325 | cmask = __ptr[this_unit]; \ |
| 326 | cmask >>= (ts - (end % ts)) % ts; \ |
| 327 | if ((end - __start) % ts) { \ |
| 328 | mask = ~((~(type) 0) << (end - __start)); \ |
| 329 | cmask &= mask; \ |
| 330 | } \ |
| 331 | __v = _bt_piecewise_lshift(__v, end - __start); \ |
| 332 | __v |= _bt_unsigned_cast(typeof(__v), cmask); \ |
| 333 | *__vptr = __v; \ |
| 334 | break; \ |
| 335 | } \ |
| 336 | if (__start % ts) { \ |
| 337 | cshift = __start % ts; \ |
| 338 | mask = ~((~(type) 0) << (ts - cshift)); \ |
| 339 | cmask = __ptr[this_unit]; \ |
| 340 | cmask &= mask; \ |
| 341 | __v = _bt_piecewise_lshift(__v, ts - cshift); \ |
| 342 | __v |= _bt_unsigned_cast(typeof(__v), cmask); \ |
| 343 | __start += ts - cshift; \ |
| 344 | this_unit++; \ |
| 345 | } \ |
| 346 | for (; this_unit < end_unit - 1; this_unit++) { \ |
| 347 | __v = _bt_piecewise_lshift(__v, ts); \ |
| 348 | __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\ |
| 349 | __start += ts; \ |
| 350 | } \ |
| 351 | if (end % ts) { \ |
| 352 | mask = ~((~(type) 0) << (end % ts)); \ |
| 353 | cmask = __ptr[this_unit]; \ |
| 354 | cmask >>= ts - (end % ts); \ |
| 355 | cmask &= mask; \ |
| 356 | __v = _bt_piecewise_lshift(__v, end % ts); \ |
| 357 | __v |= _bt_unsigned_cast(typeof(__v), cmask); \ |
| 358 | } else { \ |
| 359 | __v = _bt_piecewise_lshift(__v, ts); \ |
| 360 | __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\ |
| 361 | } \ |
| 362 | *__vptr = __v; \ |
| 363 | } while (0) |
| 364 | |
| 365 | /* |
| 366 | * bt_bitfield_read - read integer from a bitfield in native endianness |
| 367 | * bt_bitfield_read_le - read integer from a bitfield in little endian |
| 368 | * bt_bitfield_read_be - read integer from a bitfield in big endian |
| 369 | */ |
| 370 | |
| 371 | #if (BYTE_ORDER == LITTLE_ENDIAN) |
| 372 | |
| 373 | #define bt_bitfield_read(_ptr, type, _start, _length, _vptr) \ |
| 374 | _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) |
| 375 | |
| 376 | #define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \ |
| 377 | _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) |
| 378 | |
| 379 | #define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \ |
| 380 | _bt_bitfield_read_be(_ptr, unsigned char, _start, _length, _vptr) |
| 381 | |
| 382 | #elif (BYTE_ORDER == BIG_ENDIAN) |
| 383 | |
| 384 | #define bt_bitfield_read(_ptr, type, _start, _length, _vptr) \ |
| 385 | _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) |
| 386 | |
| 387 | #define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \ |
| 388 | _bt_bitfield_read_le(_ptr, unsigned char, _start, _length, _vptr) |
| 389 | |
| 390 | #define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \ |
| 391 | _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) |
| 392 | |
| 393 | #else /* (BYTE_ORDER == PDP_ENDIAN) */ |
| 394 | |
| 395 | #error "Byte order not supported" |
| 396 | |
| 397 | #endif |
| 398 | |
| 399 | #endif /* _BABELTRACE_BITFIELD_H */ |