Commit | Line | Data |
---|---|---|
9e7f0924 MD |
1 | #ifndef _BABELTRACE_BITFIELD_H |
2 | #define _BABELTRACE_BITFIELD_H | |
3 | ||
4 | /* | |
792d46f3 | 5 | * Copyright 2010-2019 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
9e7f0924 MD |
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. | |
d2428e87 MD |
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. | |
9e7f0924 MD |
24 | */ |
25 | ||
9f3fdbc6 MD |
26 | #include <stdint.h> /* C99 5.2.4.2 Numerical limits */ |
27 | #include <limits.h> /* C99 5.2.4.2 Numerical limits */ | |
00ed25b1 | 28 | #include <stdbool.h> /* C99 7.16 bool type */ |
2ae57758 | 29 | #include <lttng/ust-endian.h> /* Non-standard BIG_ENDIAN, LITTLE_ENDIAN, BYTE_ORDER */ |
9e7f0924 | 30 | |
792d46f3 MD |
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 | |
9e7f0924 | 43 | |
00ed25b1 MD |
44 | /* |
45 | * _bt_is_signed_type() willingly generates comparison of unsigned | |
46 | * expression < 0, which is always false. Silence compiler warnings. | |
3f6807bf JR |
47 | * GCC versions lower than 4.6.0 do not accept diagnostic pragma inside |
48 | * functions. | |
00ed25b1 | 49 | */ |
3f6807bf | 50 | #if defined (__GNUC__) && (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600 |
00ed25b1 MD |
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 | |
b6e1397d | 64 | # define _BT_DIAG_IGNORE_TYPE_LIMITS |
00ed25b1 MD |
65 | #endif |
66 | ||
72914f82 | 67 | #define _bt_is_signed_type(type) ((type) -1 < (type) 0) |
9e7f0924 | 68 | |
792d46f3 MD |
69 | /* |
70 | * Produce a build-time error if the condition `cond` is non-zero. | |
71 | * Evaluates as a size_t expression. | |
72 | */ | |
f6d8019e SM |
73 | #ifdef __cplusplus |
74 | #define _BT_BUILD_ASSERT(cond) ([]{static_assert((cond), "");}, 0) | |
75 | #else | |
792d46f3 MD |
76 | #define _BT_BUILD_ASSERT(cond) \ |
77 | sizeof(struct { int f:(2 * !!(cond) - 1); }) | |
f6d8019e | 78 | #endif |
792d46f3 MD |
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) | |
9e7f0924 MD |
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 | ||
52594aef | 228 | #define _bt_bitfield_write_le(ptr, type, start, length, v) \ |
9e7f0924 | 229 | do { \ |
52594aef MD |
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" */ \ | |
9e7f0924 | 237 | \ |
52594aef | 238 | if (!_length) \ |
9e7f0924 MD |
239 | break; \ |
240 | \ | |
52594aef MD |
241 | _end = _start + _length; \ |
242 | _start_unit = _start / _ts; \ | |
243 | _end_unit = (_end + (_ts - 1)) / _ts; \ | |
9e7f0924 MD |
244 | \ |
245 | /* Trim v high bits */ \ | |
52594aef MD |
246 | if (_length < sizeof(_v) * CHAR_BIT) \ |
247 | _v &= _bt_make_mask(__typeof__(_v), _length); \ | |
9e7f0924 MD |
248 | \ |
249 | /* We can now append v with a simple "or", shift it piece-wise */ \ | |
52594aef MD |
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; \ | |
9e7f0924 MD |
259 | break; \ |
260 | } \ | |
52594aef MD |
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++; \ | |
9e7f0924 | 271 | } \ |
52594aef MD |
272 | for (; _this_unit < _end_unit - 1; _this_unit++) { \ |
273 | _ptr[_this_unit] = (type) _v; \ | |
274 | _bt_safe_rshift(_v, _ts); \ | |
275 | _start += _ts; \ | |
9e7f0924 | 276 | } \ |
52594aef MD |
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; \ | |
9e7f0924 | 283 | } else \ |
52594aef | 284 | _ptr[_this_unit] = (type) _v; \ |
9e7f0924 MD |
285 | } while (0) |
286 | ||
52594aef | 287 | #define _bt_bitfield_write_be(ptr, type, start, length, v) \ |
9e7f0924 | 288 | do { \ |
52594aef MD |
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" */ \ | |
9e7f0924 | 296 | \ |
52594aef | 297 | if (!_length) \ |
9e7f0924 MD |
298 | break; \ |
299 | \ | |
52594aef MD |
300 | _end = _start + _length; \ |
301 | _start_unit = _start / _ts; \ | |
302 | _end_unit = (_end + (_ts - 1)) / _ts; \ | |
9e7f0924 MD |
303 | \ |
304 | /* Trim v high bits */ \ | |
52594aef MD |
305 | if (_length < sizeof(_v) * CHAR_BIT) \ |
306 | _v &= _bt_make_mask(__typeof__(_v), _length); \ | |
9e7f0924 MD |
307 | \ |
308 | /* We can now append v with a simple "or", shift it piece-wise */ \ | |
52594aef MD |
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; \ | |
9e7f0924 MD |
318 | break; \ |
319 | } \ | |
52594aef MD |
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--; \ | |
9e7f0924 | 330 | } \ |
52594aef MD |
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; \ | |
9e7f0924 | 335 | } \ |
52594aef MD |
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; \ | |
9e7f0924 | 342 | } else \ |
52594aef | 343 | _ptr[_this_unit] = (type) _v; \ |
9e7f0924 MD |
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 | ||
9f3fdbc6 | 352 | #if (BYTE_ORDER == LITTLE_ENDIAN) |
9e7f0924 | 353 | |
52594aef MD |
354 | #define bt_bitfield_write(ptr, type, start, length, v) \ |
355 | _bt_bitfield_write_le(ptr, type, start, length, v) | |
9e7f0924 | 356 | |
52594aef MD |
357 | #define bt_bitfield_write_le(ptr, type, start, length, v) \ |
358 | _bt_bitfield_write_le(ptr, type, start, length, v) | |
792d46f3 | 359 | |
52594aef MD |
360 | #define bt_bitfield_write_be(ptr, type, start, length, v) \ |
361 | _bt_bitfield_write_be(ptr, unsigned char, start, length, v) | |
9e7f0924 | 362 | |
9f3fdbc6 | 363 | #elif (BYTE_ORDER == BIG_ENDIAN) |
9e7f0924 | 364 | |
52594aef MD |
365 | #define bt_bitfield_write(ptr, type, start, length, v) \ |
366 | _bt_bitfield_write_be(ptr, type, start, length, v) | |
9e7f0924 | 367 | |
52594aef MD |
368 | #define bt_bitfield_write_le(ptr, type, start, length, v) \ |
369 | _bt_bitfield_write_le(ptr, unsigned char, start, length, v) | |
792d46f3 | 370 | |
52594aef MD |
371 | #define bt_bitfield_write_be(ptr, type, start, length, v) \ |
372 | _bt_bitfield_write_be(ptr, type, start, length, v) | |
9e7f0924 MD |
373 | |
374 | #else /* (BYTE_ORDER == PDP_ENDIAN) */ | |
375 | ||
376 | #error "Byte order not supported" | |
377 | ||
378 | #endif | |
379 | ||
52594aef | 380 | #define _bt_bitfield_read_le(ptr, type, start, length, vptr) \ |
9e7f0924 | 381 | do { \ |
52594aef MD |
382 | __typeof__(*(vptr)) *_vptr = (vptr); \ |
383 | __typeof__(*_vptr) _v; \ | |
f6d8019e | 384 | type *_ptr = (type *) (ptr); \ |
52594aef MD |
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; \ | |
9e7f0924 | 391 | \ |
52594aef MD |
392 | if (!_length) { \ |
393 | *_vptr = 0; \ | |
9e7f0924 MD |
394 | break; \ |
395 | } \ | |
396 | \ | |
52594aef MD |
397 | _end = _start + _length; \ |
398 | _start_unit = _start / _ts; \ | |
399 | _end_unit = (_end + (_ts - 1)) / _ts; \ | |
9e7f0924 | 400 | \ |
52594aef | 401 | _this_unit = _end_unit - 1; \ |
00ed25b1 MD |
402 | _BT_DIAG_PUSH \ |
403 | _BT_DIAG_IGNORE_TYPE_LIMITS \ | |
52594aef | 404 | _is_signed_type = _bt_is_signed_type(__typeof__(_v)); \ |
00ed25b1 | 405 | _BT_DIAG_POP \ |
52594aef MD |
406 | if (_is_signed_type \ |
407 | && (_ptr[_this_unit] & _bt_lshift((type) 1, (_end % _ts ? _end % _ts : _ts) - 1))) \ | |
408 | _v = ~(__typeof__(_v)) 0; \ | |
9e7f0924 | 409 | else \ |
52594aef MD |
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; \ | |
9e7f0924 | 417 | } \ |
52594aef MD |
418 | _bt_safe_lshift(_v, _end - _start); \ |
419 | _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \ | |
420 | *_vptr = _v; \ | |
9e7f0924 MD |
421 | break; \ |
422 | } \ | |
52594aef MD |
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--; \ | |
9e7f0924 | 432 | } \ |
52594aef MD |
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; \ | |
9e7f0924 | 437 | } \ |
52594aef MD |
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); \ | |
9e7f0924 | 445 | } else { \ |
52594aef MD |
446 | _bt_safe_lshift(_v, _ts); \ |
447 | _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \ | |
9e7f0924 | 448 | } \ |
52594aef | 449 | *_vptr = _v; \ |
9e7f0924 MD |
450 | } while (0) |
451 | ||
52594aef | 452 | #define _bt_bitfield_read_be(ptr, type, start, length, vptr) \ |
9e7f0924 | 453 | do { \ |
52594aef MD |
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; \ | |
9e7f0924 | 463 | \ |
52594aef MD |
464 | if (!_length) { \ |
465 | *_vptr = 0; \ | |
9e7f0924 MD |
466 | break; \ |
467 | } \ | |
468 | \ | |
52594aef MD |
469 | _end = _start + _length; \ |
470 | _start_unit = _start / _ts; \ | |
471 | _end_unit = (_end + (_ts - 1)) / _ts; \ | |
9e7f0924 | 472 | \ |
52594aef | 473 | _this_unit = _start_unit; \ |
00ed25b1 MD |
474 | _BT_DIAG_PUSH \ |
475 | _BT_DIAG_IGNORE_TYPE_LIMITS \ | |
52594aef | 476 | _is_signed_type = _bt_is_signed_type(__typeof__(_v)); \ |
00ed25b1 | 477 | _BT_DIAG_POP \ |
52594aef MD |
478 | if (_is_signed_type \ |
479 | && (_ptr[_this_unit] & _bt_lshift((type) 1, _ts - (_start % _ts) - 1))) \ | |
480 | _v = ~(__typeof__(_v)) 0; \ | |
9e7f0924 | 481 | else \ |
52594aef MD |
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; \ | |
9e7f0924 | 489 | } \ |
52594aef MD |
490 | _bt_safe_lshift(_v, _end - _start); \ |
491 | _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _cmask); \ | |
492 | *_vptr = _v; \ | |
9e7f0924 MD |
493 | break; \ |
494 | } \ | |
52594aef MD |
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++; \ | |
9e7f0924 | 504 | } \ |
52594aef MD |
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; \ | |
9e7f0924 | 509 | } \ |
52594aef MD |
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); \ | |
9e7f0924 | 517 | } else { \ |
52594aef MD |
518 | _bt_safe_lshift(_v, _ts); \ |
519 | _v |= _bt_cast_value_to_unsigned_type(__typeof__(_v), _ptr[_this_unit]); \ | |
9e7f0924 | 520 | } \ |
52594aef | 521 | *_vptr = _v; \ |
9e7f0924 MD |
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 | ||
9f3fdbc6 | 530 | #if (BYTE_ORDER == LITTLE_ENDIAN) |
9e7f0924 | 531 | |
52594aef MD |
532 | #define bt_bitfield_read(ptr, type, start, length, vptr) \ |
533 | _bt_bitfield_read_le(ptr, type, start, length, vptr) | |
9e7f0924 | 534 | |
52594aef MD |
535 | #define bt_bitfield_read_le(ptr, type, start, length, vptr) \ |
536 | _bt_bitfield_read_le(ptr, type, start, length, vptr) | |
792d46f3 | 537 | |
52594aef MD |
538 | #define bt_bitfield_read_be(ptr, type, start, length, vptr) \ |
539 | _bt_bitfield_read_be(ptr, unsigned char, start, length, vptr) | |
9e7f0924 | 540 | |
9f3fdbc6 | 541 | #elif (BYTE_ORDER == BIG_ENDIAN) |
9e7f0924 | 542 | |
52594aef MD |
543 | #define bt_bitfield_read(ptr, type, start, length, vptr) \ |
544 | _bt_bitfield_read_be(ptr, type, start, length, vptr) | |
9e7f0924 | 545 | |
52594aef MD |
546 | #define bt_bitfield_read_le(ptr, type, start, length, vptr) \ |
547 | _bt_bitfield_read_le(ptr, unsigned char, start, length, vptr) | |
792d46f3 | 548 | |
52594aef MD |
549 | #define bt_bitfield_read_be(ptr, type, start, length, vptr) \ |
550 | _bt_bitfield_read_be(ptr, type, start, length, vptr) | |
9e7f0924 | 551 | |
9f3fdbc6 | 552 | #else /* (BYTE_ORDER == PDP_ENDIAN) */ |
9e7f0924 MD |
553 | |
554 | #error "Byte order not supported" | |
555 | ||
556 | #endif | |
557 | ||
558 | #endif /* _BABELTRACE_BITFIELD_H */ |