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