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