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 <stdint.h> /* C99 5.2.4.2 Numerical limits */
23 #include <limits.h> /* C99 5.2.4.2 Numerical limits */
24 #include <endian.h> /* Non-standard BIG_ENDIAN, LITTLE_ENDIAN, BYTE_ORDER */
27 /* We can't shift a int from 32 bit, >> 32 and << 32 on int is undefined */
28 #define _bt_piecewise_rshift(_v, _shift) \
30 typeof(_v) ___v = (_v); \
31 typeof(_shift) ___shift = (_shift); \
32 unsigned long sb = (___shift) / (sizeof(___v) * CHAR_BIT - 1); \
33 unsigned long final = (___shift) % (sizeof(___v) * CHAR_BIT - 1); \
36 ___v >>= sizeof(___v) * CHAR_BIT - 1; \
40 #define _bt_piecewise_lshift(_v, _shift) \
42 typeof(_v) ___v = (_v); \
43 typeof(_shift) ___shift = (_shift); \
44 unsigned long sb = (___shift) / (sizeof(___v) * CHAR_BIT - 1); \
45 unsigned long final = (___shift) % (sizeof(___v) * CHAR_BIT - 1); \
48 ___v <<= sizeof(___v) * CHAR_BIT - 1; \
52 #define _bt_is_signed_type(type) (((type)(-1)) < 0)
54 #define _bt_unsigned_cast(type, v) \
56 (sizeof(v) < sizeof(type)) ? \
57 ((type) (v)) & (~(~(type) 0 << (sizeof(v) * CHAR_BIT))) : \
62 * bt_bitfield_write - write integer to a bitfield in native endianness
64 * Save integer to the bitfield, which starts at the "start" bit, has "len"
66 * The inside of a bitfield is from high bits to low bits.
67 * Uses native endianness.
68 * For unsigned "v", pad MSB with 0 if bitfield is larger than v.
69 * For signed "v", sign-extend v if bitfield is larger than v.
71 * On little endian, bytes are placed from the less significant to the most
72 * significant. Also, consecutive bitfields are placed from lower bits to higher
75 * On big endian, bytes are places from most significant to less significant.
76 * Also, consecutive bitfields are placed from higher to lower bits.
79 #define _bt_bitfield_write_le(_ptr, type, _start, _length, _v) \
81 typeof(_v) __v = (_v); \
82 type *__ptr = (void *) (_ptr); \
83 unsigned long __start = (_start), __length = (_length); \
85 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
86 unsigned long start_unit, end_unit, this_unit; \
87 unsigned long end, cshift; /* cshift is "complement shift" */ \
92 end = __start + __length; \
93 start_unit = __start / ts; \
94 end_unit = (end + (ts - 1)) / ts; \
96 /* Trim v high bits */ \
97 if (__length < sizeof(__v) * CHAR_BIT) \
98 __v &= ~((~(typeof(__v)) 0) << __length); \
100 /* We can now append v with a simple "or", shift it piece-wise */ \
101 this_unit = start_unit; \
102 if (start_unit == end_unit - 1) { \
103 mask = ~((~(type) 0) << (__start % ts)); \
105 mask |= (~(type) 0) << (end % ts); \
106 cmask = (type) __v << (__start % ts); \
108 __ptr[this_unit] &= mask; \
109 __ptr[this_unit] |= cmask; \
112 if (__start % ts) { \
113 cshift = __start % ts; \
114 mask = ~((~(type) 0) << cshift); \
115 cmask = (type) __v << cshift; \
117 __ptr[this_unit] &= mask; \
118 __ptr[this_unit] |= cmask; \
119 __v = _bt_piecewise_rshift(__v, ts - cshift); \
120 __start += ts - cshift; \
123 for (; this_unit < end_unit - 1; this_unit++) { \
124 __ptr[this_unit] = (type) __v; \
125 __v = _bt_piecewise_rshift(__v, ts); \
129 mask = (~(type) 0) << (end % ts); \
130 cmask = (type) __v; \
132 __ptr[this_unit] &= mask; \
133 __ptr[this_unit] |= cmask; \
135 __ptr[this_unit] = (type) __v; \
138 #define _bt_bitfield_write_be(_ptr, type, _start, _length, _v) \
140 typeof(_v) __v = (_v); \
141 type *__ptr = (void *) (_ptr); \
142 unsigned long __start = (_start), __length = (_length); \
144 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
145 unsigned long start_unit, end_unit, this_unit; \
146 unsigned long end, cshift; /* cshift is "complement shift" */ \
151 end = __start + __length; \
152 start_unit = __start / ts; \
153 end_unit = (end + (ts - 1)) / ts; \
155 /* Trim v high bits */ \
156 if (__length < sizeof(__v) * CHAR_BIT) \
157 __v &= ~((~(typeof(__v)) 0) << __length); \
159 /* We can now append v with a simple "or", shift it piece-wise */ \
160 this_unit = end_unit - 1; \
161 if (start_unit == end_unit - 1) { \
162 mask = ~((~(type) 0) << ((ts - (end % ts)) % ts)); \
164 mask |= (~((type) 0)) << (ts - (__start % ts)); \
165 cmask = (type) __v << ((ts - (end % ts)) % ts); \
167 __ptr[this_unit] &= mask; \
168 __ptr[this_unit] |= cmask; \
173 mask = ~((~(type) 0) << (ts - cshift)); \
174 cmask = (type) __v << (ts - cshift); \
176 __ptr[this_unit] &= mask; \
177 __ptr[this_unit] |= cmask; \
178 __v = _bt_piecewise_rshift(__v, cshift); \
182 for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \
183 __ptr[this_unit] = (type) __v; \
184 __v = _bt_piecewise_rshift(__v, ts); \
187 if (__start % ts) { \
188 mask = (~(type) 0) << (ts - (__start % ts)); \
189 cmask = (type) __v; \
191 __ptr[this_unit] &= mask; \
192 __ptr[this_unit] |= cmask; \
194 __ptr[this_unit] = (type) __v; \
198 * bt_bitfield_write - write integer to a bitfield in native endianness
199 * bt_bitfield_write_le - write integer to a bitfield in little endian
200 * bt_bitfield_write_be - write integer to a bitfield in big endian
203 #if (BYTE_ORDER == LITTLE_ENDIAN)
205 #define bt_bitfield_write(ptr, type, _start, _length, _v) \
206 _bt_bitfield_write_le(ptr, type, _start, _length, _v)
208 #define bt_bitfield_write_le(ptr, type, _start, _length, _v) \
209 _bt_bitfield_write_le(ptr, type, _start, _length, _v)
211 #define bt_bitfield_write_be(ptr, type, _start, _length, _v) \
212 _bt_bitfield_write_be(ptr, unsigned char, _start, _length, _v)
214 #elif (BYTE_ORDER == BIG_ENDIAN)
216 #define bt_bitfield_write(ptr, type, _start, _length, _v) \
217 _bt_bitfield_write_be(ptr, type, _start, _length, _v)
219 #define bt_bitfield_write_le(ptr, type, _start, _length, _v) \
220 _bt_bitfield_write_le(ptr, unsigned char, _start, _length, _v)
222 #define bt_bitfield_write_be(ptr, type, _start, _length, _v) \
223 _bt_bitfield_write_be(ptr, type, _start, _length, _v)
225 #else /* (BYTE_ORDER == PDP_ENDIAN) */
227 #error "Byte order not supported"
231 #define _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
233 typeof(*(_vptr)) *__vptr = (_vptr); \
234 typeof(*__vptr) __v; \
235 type *__ptr = (void *) (_ptr); \
236 unsigned long __start = (_start), __length = (_length); \
238 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
239 unsigned long start_unit, end_unit, this_unit; \
240 unsigned long end, cshift; /* cshift is "complement shift" */ \
247 end = __start + __length; \
248 start_unit = __start / ts; \
249 end_unit = (end + (ts - 1)) / ts; \
251 this_unit = end_unit - 1; \
252 if (_bt_is_signed_type(typeof(__v)) \
253 && (__ptr[this_unit] & ((type) 1 << ((end % ts ? : ts) - 1)))) \
254 __v = ~(typeof(__v)) 0; \
257 if (start_unit == end_unit - 1) { \
258 cmask = __ptr[this_unit]; \
259 cmask >>= (__start % ts); \
260 if ((end - __start) % ts) { \
261 mask = ~((~(type) 0) << (end - __start)); \
264 __v = _bt_piecewise_lshift(__v, end - __start); \
265 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
271 mask = ~((~(type) 0) << cshift); \
272 cmask = __ptr[this_unit]; \
274 __v = _bt_piecewise_lshift(__v, cshift); \
275 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
279 for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \
280 __v = _bt_piecewise_lshift(__v, ts); \
281 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
284 if (__start % ts) { \
285 mask = ~((~(type) 0) << (ts - (__start % ts))); \
286 cmask = __ptr[this_unit]; \
287 cmask >>= (__start % ts); \
289 __v = _bt_piecewise_lshift(__v, ts - (__start % ts)); \
290 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
292 __v = _bt_piecewise_lshift(__v, ts); \
293 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
298 #define _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
300 typeof(*(_vptr)) *__vptr = (_vptr); \
301 typeof(*__vptr) __v; \
302 type *__ptr = (void *) (_ptr); \
303 unsigned long __start = (_start), __length = (_length); \
305 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
306 unsigned long start_unit, end_unit, this_unit; \
307 unsigned long end, cshift; /* cshift is "complement shift" */ \
314 end = __start + __length; \
315 start_unit = __start / ts; \
316 end_unit = (end + (ts - 1)) / ts; \
318 this_unit = start_unit; \
319 if (_bt_is_signed_type(typeof(__v)) \
320 && (__ptr[this_unit] & ((type) 1 << (ts - (__start % ts) - 1)))) \
321 __v = ~(typeof(__v)) 0; \
324 if (start_unit == end_unit - 1) { \
325 cmask = __ptr[this_unit]; \
326 cmask >>= (ts - (end % ts)) % ts; \
327 if ((end - __start) % ts) { \
328 mask = ~((~(type) 0) << (end - __start)); \
331 __v = _bt_piecewise_lshift(__v, end - __start); \
332 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
336 if (__start % ts) { \
337 cshift = __start % ts; \
338 mask = ~((~(type) 0) << (ts - cshift)); \
339 cmask = __ptr[this_unit]; \
341 __v = _bt_piecewise_lshift(__v, ts - cshift); \
342 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
343 __start += ts - cshift; \
346 for (; this_unit < end_unit - 1; this_unit++) { \
347 __v = _bt_piecewise_lshift(__v, ts); \
348 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
352 mask = ~((~(type) 0) << (end % ts)); \
353 cmask = __ptr[this_unit]; \
354 cmask >>= ts - (end % ts); \
356 __v = _bt_piecewise_lshift(__v, end % ts); \
357 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
359 __v = _bt_piecewise_lshift(__v, ts); \
360 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
366 * bt_bitfield_read - read integer from a bitfield in native endianness
367 * bt_bitfield_read_le - read integer from a bitfield in little endian
368 * bt_bitfield_read_be - read integer from a bitfield in big endian
371 #if (BYTE_ORDER == LITTLE_ENDIAN)
373 #define bt_bitfield_read(_ptr, type, _start, _length, _vptr) \
374 _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr)
376 #define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
377 _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr)
379 #define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
380 _bt_bitfield_read_be(_ptr, unsigned char, _start, _length, _vptr)
382 #elif (BYTE_ORDER == BIG_ENDIAN)
384 #define bt_bitfield_read(_ptr, type, _start, _length, _vptr) \
385 _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr)
387 #define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
388 _bt_bitfield_read_le(_ptr, unsigned char, _start, _length, _vptr)
390 #define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
391 _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr)
393 #else /* (BYTE_ORDER == PDP_ENDIAN) */
395 #error "Byte order not supported"
399 #endif /* _BABELTRACE_BITFIELD_H */
This page took 0.038007 seconds and 4 git commands to generate.