1 #ifndef _BABELTRACE_BITFIELD_H
2 #define _BABELTRACE_BITFIELD_H
7 * Bitfields read/write functions.
9 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #include <stdint.h> /* C99 5.2.4.2 Numerical limits */
31 #include <babeltrace/compat/limits-internal.h> /* C99 5.2.4.2 Numerical limits */
32 #include <babeltrace/endian-internal.h> /* Non-standard BIG_ENDIAN, LITTLE_ENDIAN, BYTE_ORDER */
34 /* We can't shift a int from 32 bit, >> 32 and << 32 on int is undefined */
35 #define _bt_piecewise_rshift(_v, _shift) \
37 typeof(_v) ___v = (_v); \
38 typeof(_shift) ___shift = (_shift); \
39 unsigned long sb = (___shift) / (sizeof(___v) * CHAR_BIT - 1); \
40 unsigned long final = (___shift) % (sizeof(___v) * CHAR_BIT - 1); \
43 ___v >>= sizeof(___v) * CHAR_BIT - 1; \
47 #define _bt_piecewise_lshift(_v, _shift) \
49 typeof(_v) ___v = (_v); \
50 typeof(_shift) ___shift = (_shift); \
51 unsigned long sb = (___shift) / (sizeof(___v) * CHAR_BIT - 1); \
52 unsigned long final = (___shift) % (sizeof(___v) * CHAR_BIT - 1); \
55 ___v <<= sizeof(___v) * CHAR_BIT - 1; \
59 #define _bt_is_signed_type(type) ((type) -1 < (type) 0)
62 * NOTE: The cast to (uint64_t) below ensures that we're not casting a
63 * negative value, which is undefined in C. However, this limits the
64 * maximum type size of `type` and `v` to 64-bit. The
65 * _bt_check_max_64bit() is used to check that the users of this header
66 * do not use types with a size greater than 64-bit.
68 #define _bt_unsigned_cast(type, v) \
70 (sizeof(v) < sizeof(type)) ? \
71 ((type) (v)) & ((type) (~(~(uint64_t) 0 << (sizeof(v) * CHAR_BIT)))) : \
75 #define _bt_check_max_64bit(type) \
76 char _max_64bit_assertion[sizeof(type) <= sizeof(uint64_t) ? 1 : -1] __attribute__((unused))
79 * bt_bitfield_write - write integer to a bitfield in native endianness
81 * Save integer to the bitfield, which starts at the "start" bit, has "len"
83 * The inside of a bitfield is from high bits to low bits.
84 * Uses native endianness.
85 * For unsigned "v", pad MSB with 0 if bitfield is larger than v.
86 * For signed "v", sign-extend v if bitfield is larger than v.
88 * On little endian, bytes are placed from the less significant to the most
89 * significant. Also, consecutive bitfields are placed from lower bits to higher
92 * On big endian, bytes are places from most significant to less significant.
93 * Also, consecutive bitfields are placed from higher to lower bits.
96 #define _bt_bitfield_write_le(_ptr, type, _start, _length, _v) \
98 typeof(_v) __v = (_v); \
99 type *__ptr = (void *) (_ptr); \
100 unsigned long __start = (_start), __length = (_length); \
102 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
103 unsigned long start_unit, end_unit, this_unit; \
104 unsigned long end, cshift; /* cshift is "complement shift" */ \
109 end = __start + __length; \
110 start_unit = __start / ts; \
111 end_unit = (end + (ts - 1)) / ts; \
113 /* Trim v high bits */ \
114 if (__length < sizeof(__v) * CHAR_BIT) \
115 __v &= ~((~(typeof(__v)) 0) << __length); \
117 /* We can now append v with a simple "or", shift it piece-wise */ \
118 this_unit = start_unit; \
119 if (start_unit == end_unit - 1) { \
120 mask = ~((~(type) 0) << (__start % ts)); \
122 mask |= (~(type) 0) << (end % ts); \
123 cmask = (type) __v << (__start % ts); \
125 __ptr[this_unit] &= mask; \
126 __ptr[this_unit] |= cmask; \
129 if (__start % ts) { \
130 cshift = __start % ts; \
131 mask = ~((~(type) 0) << cshift); \
132 cmask = (type) __v << cshift; \
134 __ptr[this_unit] &= mask; \
135 __ptr[this_unit] |= cmask; \
136 __v = _bt_piecewise_rshift(__v, ts - cshift); \
137 __start += ts - cshift; \
140 for (; this_unit < end_unit - 1; this_unit++) { \
141 __ptr[this_unit] = (type) __v; \
142 __v = _bt_piecewise_rshift(__v, ts); \
146 mask = (~(type) 0) << (end % ts); \
147 cmask = (type) __v; \
149 __ptr[this_unit] &= mask; \
150 __ptr[this_unit] |= cmask; \
152 __ptr[this_unit] = (type) __v; \
155 #define _bt_bitfield_write_be(_ptr, type, _start, _length, _v) \
157 typeof(_v) __v = (_v); \
158 type *__ptr = (void *) (_ptr); \
159 unsigned long __start = (_start), __length = (_length); \
161 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
162 unsigned long start_unit, end_unit, this_unit; \
163 unsigned long end, cshift; /* cshift is "complement shift" */ \
168 end = __start + __length; \
169 start_unit = __start / ts; \
170 end_unit = (end + (ts - 1)) / ts; \
172 /* Trim v high bits */ \
173 if (__length < sizeof(__v) * CHAR_BIT) \
174 __v &= ~((~(typeof(__v)) 0) << __length); \
176 /* We can now append v with a simple "or", shift it piece-wise */ \
177 this_unit = end_unit - 1; \
178 if (start_unit == end_unit - 1) { \
179 mask = ~((~(type) 0) << ((ts - (end % ts)) % ts)); \
181 mask |= (~((type) 0)) << (ts - (__start % ts)); \
182 cmask = (type) __v << ((ts - (end % ts)) % ts); \
184 __ptr[this_unit] &= mask; \
185 __ptr[this_unit] |= cmask; \
190 mask = ~((~(type) 0) << (ts - cshift)); \
191 cmask = (type) __v << (ts - cshift); \
193 __ptr[this_unit] &= mask; \
194 __ptr[this_unit] |= cmask; \
195 __v = _bt_piecewise_rshift(__v, cshift); \
199 for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \
200 __ptr[this_unit] = (type) __v; \
201 __v = _bt_piecewise_rshift(__v, ts); \
204 if (__start % ts) { \
205 mask = (~(type) 0) << (ts - (__start % ts)); \
206 cmask = (type) __v; \
208 __ptr[this_unit] &= mask; \
209 __ptr[this_unit] |= cmask; \
211 __ptr[this_unit] = (type) __v; \
215 * bt_bitfield_write - write integer to a bitfield in native endianness
216 * bt_bitfield_write_le - write integer to a bitfield in little endian
217 * bt_bitfield_write_be - write integer to a bitfield in big endian
220 #if (BYTE_ORDER == LITTLE_ENDIAN)
222 #define bt_bitfield_write(ptr, type, _start, _length, _v) \
223 _bt_bitfield_write_le(ptr, type, _start, _length, _v)
225 #define bt_bitfield_write_le(ptr, type, _start, _length, _v) \
226 _bt_bitfield_write_le(ptr, type, _start, _length, _v)
228 #define bt_bitfield_write_be(ptr, type, _start, _length, _v) \
229 _bt_bitfield_write_be(ptr, unsigned char, _start, _length, _v)
231 #elif (BYTE_ORDER == BIG_ENDIAN)
233 #define bt_bitfield_write(ptr, type, _start, _length, _v) \
234 _bt_bitfield_write_be(ptr, type, _start, _length, _v)
236 #define bt_bitfield_write_le(ptr, type, _start, _length, _v) \
237 _bt_bitfield_write_le(ptr, unsigned char, _start, _length, _v)
239 #define bt_bitfield_write_be(ptr, type, _start, _length, _v) \
240 _bt_bitfield_write_be(ptr, type, _start, _length, _v)
242 #else /* (BYTE_ORDER == PDP_ENDIAN) */
244 #error "Byte order not supported"
248 #define _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
250 typeof(*(_vptr)) *__vptr = (_vptr); \
251 typeof(*__vptr) __v; \
252 type *__ptr = (void *) (_ptr); \
253 unsigned long __start = (_start), __length = (_length); \
255 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
256 unsigned long start_unit, end_unit, this_unit; \
257 unsigned long end, cshift; /* cshift is "complement shift" */ \
259 { _bt_check_max_64bit(type); } \
260 { _bt_check_max_64bit(typeof(*_vptr)); } \
261 { _bt_check_max_64bit(typeof(*_ptr)); } \
268 end = __start + __length; \
269 start_unit = __start / ts; \
270 end_unit = (end + (ts - 1)) / ts; \
272 this_unit = end_unit - 1; \
273 if (_bt_is_signed_type(typeof(__v)) \
274 && (__ptr[this_unit] & ((type) 1 << ((end % ts ? : ts) - 1)))) \
275 __v = ~(typeof(__v)) 0; \
278 if (start_unit == end_unit - 1) { \
279 cmask = __ptr[this_unit]; \
280 cmask >>= (__start % ts); \
281 if ((end - __start) % ts) { \
282 mask = ~((~(type) 0) << (end - __start)); \
285 __v = _bt_piecewise_lshift(__v, end - __start); \
286 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
292 mask = ~((~(type) 0) << cshift); \
293 cmask = __ptr[this_unit]; \
295 __v = _bt_piecewise_lshift(__v, cshift); \
296 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
300 for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \
301 __v = _bt_piecewise_lshift(__v, ts); \
302 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
305 if (__start % ts) { \
306 mask = ~((~(type) 0) << (ts - (__start % ts))); \
307 cmask = __ptr[this_unit]; \
308 cmask >>= (__start % ts); \
310 __v = _bt_piecewise_lshift(__v, ts - (__start % ts)); \
311 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
313 __v = _bt_piecewise_lshift(__v, ts); \
314 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
319 #define _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
321 typeof(*(_vptr)) *__vptr = (_vptr); \
322 typeof(*__vptr) __v; \
323 type *__ptr = (void *) (_ptr); \
324 unsigned long __start = (_start), __length = (_length); \
326 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
327 unsigned long start_unit, end_unit, this_unit; \
328 unsigned long end, cshift; /* cshift is "complement shift" */ \
330 { _bt_check_max_64bit(type); } \
331 { _bt_check_max_64bit(typeof(*_vptr)); } \
332 { _bt_check_max_64bit(typeof(*_ptr)); } \
339 end = __start + __length; \
340 start_unit = __start / ts; \
341 end_unit = (end + (ts - 1)) / ts; \
343 this_unit = start_unit; \
344 if (_bt_is_signed_type(typeof(__v)) \
345 && (__ptr[this_unit] & ((type) 1 << (ts - (__start % ts) - 1)))) \
346 __v = ~(typeof(__v)) 0; \
349 if (start_unit == end_unit - 1) { \
350 cmask = __ptr[this_unit]; \
351 cmask >>= (ts - (end % ts)) % ts; \
352 if ((end - __start) % ts) { \
353 mask = ~((~(type) 0) << (end - __start)); \
356 __v = _bt_piecewise_lshift(__v, end - __start); \
357 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
361 if (__start % ts) { \
362 cshift = __start % ts; \
363 mask = ~((~(type) 0) << (ts - cshift)); \
364 cmask = __ptr[this_unit]; \
366 __v = _bt_piecewise_lshift(__v, ts - cshift); \
367 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
368 __start += ts - cshift; \
371 for (; this_unit < end_unit - 1; this_unit++) { \
372 __v = _bt_piecewise_lshift(__v, ts); \
373 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
377 mask = ~((~(type) 0) << (end % ts)); \
378 cmask = __ptr[this_unit]; \
379 cmask >>= ts - (end % ts); \
381 __v = _bt_piecewise_lshift(__v, end % ts); \
382 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
384 __v = _bt_piecewise_lshift(__v, ts); \
385 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
391 * bt_bitfield_read - read integer from a bitfield in native endianness
392 * bt_bitfield_read_le - read integer from a bitfield in little endian
393 * bt_bitfield_read_be - read integer from a bitfield in big endian
396 #if (BYTE_ORDER == LITTLE_ENDIAN)
398 #define bt_bitfield_read(_ptr, type, _start, _length, _vptr) \
399 _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr)
401 #define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
402 _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr)
404 #define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
405 _bt_bitfield_read_be(_ptr, unsigned char, _start, _length, _vptr)
407 #elif (BYTE_ORDER == BIG_ENDIAN)
409 #define bt_bitfield_read(_ptr, type, _start, _length, _vptr) \
410 _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr)
412 #define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
413 _bt_bitfield_read_le(_ptr, unsigned char, _start, _length, _vptr)
415 #define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
416 _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr)
418 #else /* (BYTE_ORDER == PDP_ENDIAN) */
420 #error "Byte order not supported"
424 #endif /* _BABELTRACE_BITFIELD_H */
This page took 0.037356 seconds and 4 git commands to generate.