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