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