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.
22 #include "../lttng-endian.h"
28 /* We can't shift a int from 32 bit, >> 32 and << 32 on int is undefined */
29 #define _bt_piecewise_rshift(_v, _shift) \
31 typeof(_v) ___v = (_v); \
32 typeof(_shift) ___shift = (_shift); \
33 unsigned long sb = (___shift) / (sizeof(___v) * CHAR_BIT - 1); \
34 unsigned long final = (___shift) % (sizeof(___v) * CHAR_BIT - 1); \
37 ___v >>= sizeof(___v) * CHAR_BIT - 1; \
41 #define _bt_piecewise_lshift(_v, _shift) \
43 typeof(_v) ___v = (_v); \
44 typeof(_shift) ___shift = (_shift); \
45 unsigned long sb = (___shift) / (sizeof(___v) * CHAR_BIT - 1); \
46 unsigned long final = (___shift) % (sizeof(___v) * CHAR_BIT - 1); \
49 ___v <<= sizeof(___v) * CHAR_BIT - 1; \
53 #define _bt_is_signed_type(type) (((type)(-1)) < 0)
55 #define _bt_unsigned_cast(type, v) \
57 (sizeof(v) < sizeof(type)) ? \
58 ((type) (v)) & (~(~(type) 0 << (sizeof(v) * CHAR_BIT))) : \
63 * bt_bitfield_write - write integer to a bitfield in native endianness
65 * Save integer to the bitfield, which starts at the "start" bit, has "len"
67 * The inside of a bitfield is from high bits to low bits.
68 * Uses native endianness.
69 * For unsigned "v", pad MSB with 0 if bitfield is larger than v.
70 * For signed "v", sign-extend v if bitfield is larger than v.
72 * On little endian, bytes are placed from the less significant to the most
73 * significant. Also, consecutive bitfields are placed from lower bits to higher
76 * On big endian, bytes are places from most significant to less significant.
77 * Also, consecutive bitfields are placed from higher to lower bits.
80 #define _bt_bitfield_write_le(_ptr, type, _start, _length, _v) \
82 typeof(_v) __v = (_v); \
83 type *__ptr = (void *) (_ptr); \
84 unsigned long __start = (_start), __length = (_length); \
86 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
87 unsigned long start_unit, end_unit, this_unit; \
88 unsigned long end, cshift; /* cshift is "complement shift" */ \
93 end = __start + __length; \
94 start_unit = __start / ts; \
95 end_unit = (end + (ts - 1)) / ts; \
97 /* Trim v high bits */ \
98 if (__length < sizeof(__v) * CHAR_BIT) \
99 __v &= ~((~(typeof(__v)) 0) << __length); \
101 /* We can now append v with a simple "or", shift it piece-wise */ \
102 this_unit = start_unit; \
103 if (start_unit == end_unit - 1) { \
104 mask = ~((~(type) 0) << (__start % ts)); \
106 mask |= (~(type) 0) << (end % ts); \
107 cmask = (type) __v << (__start % ts); \
109 __ptr[this_unit] &= mask; \
110 __ptr[this_unit] |= cmask; \
113 if (__start % ts) { \
114 cshift = __start % ts; \
115 mask = ~((~(type) 0) << cshift); \
116 cmask = (type) __v << cshift; \
118 __ptr[this_unit] &= mask; \
119 __ptr[this_unit] |= cmask; \
120 __v = _bt_piecewise_rshift(__v, ts - cshift); \
121 __start += ts - cshift; \
124 for (; this_unit < end_unit - 1; this_unit++) { \
125 __ptr[this_unit] = (type) __v; \
126 __v = _bt_piecewise_rshift(__v, ts); \
130 mask = (~(type) 0) << (end % ts); \
131 cmask = (type) __v; \
133 __ptr[this_unit] &= mask; \
134 __ptr[this_unit] |= cmask; \
136 __ptr[this_unit] = (type) __v; \
139 #define _bt_bitfield_write_be(_ptr, type, _start, _length, _v) \
141 typeof(_v) __v = (_v); \
142 type *__ptr = (void *) (_ptr); \
143 unsigned long __start = (_start), __length = (_length); \
145 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
146 unsigned long start_unit, end_unit, this_unit; \
147 unsigned long end, cshift; /* cshift is "complement shift" */ \
152 end = __start + __length; \
153 start_unit = __start / ts; \
154 end_unit = (end + (ts - 1)) / ts; \
156 /* Trim v high bits */ \
157 if (__length < sizeof(__v) * CHAR_BIT) \
158 __v &= ~((~(typeof(__v)) 0) << __length); \
160 /* We can now append v with a simple "or", shift it piece-wise */ \
161 this_unit = end_unit - 1; \
162 if (start_unit == end_unit - 1) { \
163 mask = ~((~(type) 0) << ((ts - (end % ts)) % ts)); \
165 mask |= (~((type) 0)) << (ts - (__start % ts)); \
166 cmask = (type) __v << ((ts - (end % ts)) % ts); \
168 __ptr[this_unit] &= mask; \
169 __ptr[this_unit] |= cmask; \
174 mask = ~((~(type) 0) << (ts - cshift)); \
175 cmask = (type) __v << (ts - cshift); \
177 __ptr[this_unit] &= mask; \
178 __ptr[this_unit] |= cmask; \
179 __v = _bt_piecewise_rshift(__v, cshift); \
183 for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \
184 __ptr[this_unit] = (type) __v; \
185 __v = _bt_piecewise_rshift(__v, ts); \
188 if (__start % ts) { \
189 mask = (~(type) 0) << (ts - (__start % ts)); \
190 cmask = (type) __v; \
192 __ptr[this_unit] &= mask; \
193 __ptr[this_unit] |= cmask; \
195 __ptr[this_unit] = (type) __v; \
199 * bt_bitfield_write - write integer to a bitfield in native endianness
200 * bt_bitfield_write_le - write integer to a bitfield in little endian
201 * bt_bitfield_write_be - write integer to a bitfield in big endian
204 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
206 #define bt_bitfield_write(ptr, type, _start, _length, _v) \
207 _bt_bitfield_write_le(ptr, type, _start, _length, _v)
209 #define bt_bitfield_write_le(ptr, type, _start, _length, _v) \
210 _bt_bitfield_write_le(ptr, type, _start, _length, _v)
212 #define bt_bitfield_write_be(ptr, type, _start, _length, _v) \
213 _bt_bitfield_write_be(ptr, unsigned char, _start, _length, _v)
215 #elif (__BYTE_ORDER == __BIG_ENDIAN)
217 #define bt_bitfield_write(ptr, type, _start, _length, _v) \
218 _bt_bitfield_write_be(ptr, type, _start, _length, _v)
220 #define bt_bitfield_write_le(ptr, type, _start, _length, _v) \
221 _bt_bitfield_write_le(ptr, unsigned char, _start, _length, _v)
223 #define bt_bitfield_write_be(ptr, type, _start, _length, _v) \
224 _bt_bitfield_write_be(ptr, type, _start, _length, _v)
226 #else /* (BYTE_ORDER == PDP_ENDIAN) */
228 #error "Byte order not supported"
232 #define _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
234 typeof(*(_vptr)) *__vptr = (_vptr); \
235 typeof(*__vptr) __v; \
236 type *__ptr = (void *) (_ptr); \
237 unsigned long __start = (_start), __length = (_length); \
239 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
240 unsigned long start_unit, end_unit, this_unit; \
241 unsigned long end, cshift; /* cshift is "complement shift" */ \
248 end = __start + __length; \
249 start_unit = __start / ts; \
250 end_unit = (end + (ts - 1)) / ts; \
252 this_unit = end_unit - 1; \
253 if (_bt_is_signed_type(typeof(__v)) \
254 && (__ptr[this_unit] & ((type) 1 << ((end % ts ? : ts) - 1)))) \
255 __v = ~(typeof(__v)) 0; \
258 if (start_unit == end_unit - 1) { \
259 cmask = __ptr[this_unit]; \
260 cmask >>= (__start % ts); \
261 if ((end - __start) % ts) { \
262 mask = ~((~(type) 0) << (end - __start)); \
265 __v = _bt_piecewise_lshift(__v, end - __start); \
266 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
272 mask = ~((~(type) 0) << cshift); \
273 cmask = __ptr[this_unit]; \
275 __v = _bt_piecewise_lshift(__v, cshift); \
276 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
280 for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \
281 __v = _bt_piecewise_lshift(__v, ts); \
282 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
285 if (__start % ts) { \
286 mask = ~((~(type) 0) << (ts - (__start % ts))); \
287 cmask = __ptr[this_unit]; \
288 cmask >>= (__start % ts); \
290 __v = _bt_piecewise_lshift(__v, ts - (__start % ts)); \
291 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
293 __v = _bt_piecewise_lshift(__v, ts); \
294 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
299 #define _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
301 typeof(*(_vptr)) *__vptr = (_vptr); \
302 typeof(*__vptr) __v; \
303 type *__ptr = (void *) (_ptr); \
304 unsigned long __start = (_start), __length = (_length); \
306 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
307 unsigned long start_unit, end_unit, this_unit; \
308 unsigned long end, cshift; /* cshift is "complement shift" */ \
315 end = __start + __length; \
316 start_unit = __start / ts; \
317 end_unit = (end + (ts - 1)) / ts; \
319 this_unit = start_unit; \
320 if (_bt_is_signed_type(typeof(__v)) \
321 && (__ptr[this_unit] & ((type) 1 << (ts - (__start % ts) - 1)))) \
322 __v = ~(typeof(__v)) 0; \
325 if (start_unit == end_unit - 1) { \
326 cmask = __ptr[this_unit]; \
327 cmask >>= (ts - (end % ts)) % ts; \
328 if ((end - __start) % ts) { \
329 mask = ~((~(type) 0) << (end - __start)); \
332 __v = _bt_piecewise_lshift(__v, end - __start); \
333 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
337 if (__start % ts) { \
338 cshift = __start % ts; \
339 mask = ~((~(type) 0) << (ts - cshift)); \
340 cmask = __ptr[this_unit]; \
342 __v = _bt_piecewise_lshift(__v, ts - cshift); \
343 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
344 __start += ts - cshift; \
347 for (; this_unit < end_unit - 1; this_unit++) { \
348 __v = _bt_piecewise_lshift(__v, ts); \
349 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
353 mask = ~((~(type) 0) << (end % ts)); \
354 cmask = __ptr[this_unit]; \
355 cmask >>= ts - (end % ts); \
357 __v = _bt_piecewise_lshift(__v, end % ts); \
358 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
360 __v = _bt_piecewise_lshift(__v, ts); \
361 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
367 * bt_bitfield_read - read integer from a bitfield in native endianness
368 * bt_bitfield_read_le - read integer from a bitfield in little endian
369 * bt_bitfield_read_be - read integer from a bitfield in big endian
372 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
374 #define bt_bitfield_read(_ptr, type, _start, _length, _vptr) \
375 _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr)
377 #define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
378 _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr)
380 #define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
381 _bt_bitfield_read_be(_ptr, unsigned char, _start, _length, _vptr)
383 #elif (__BYTE_ORDER == __BIG_ENDIAN)
385 #define bt_bitfield_read(_ptr, type, _start, _length, _vptr) \
386 _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr)
388 #define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
389 _bt_bitfield_read_le(_ptr, unsigned char, _start, _length, _vptr)
391 #define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
392 _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr)
394 #else /* (__BYTE_ORDER == __PDP_ENDIAN) */
396 #error "Byte order not supported"
400 #endif /* _BABELTRACE_BITFIELD_H */
This page took 0.03657 seconds and 4 git commands to generate.