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 <limits.h> /* C99 5.2.4.2 Numerical limits */
33 #include <babeltrace/endian.h> /* Non-standard BIG_ENDIAN, LITTLE_ENDIAN, BYTE_ORDER */
35 /* We can't shift a int from 32 bit, >> 32 and << 32 on int is undefined */
36 #define _bt_piecewise_rshift(_v, _shift) \
38 typeof(_v) ___v = (_v); \
39 typeof(_shift) ___shift = (_shift); \
40 unsigned long sb = (___shift) / (sizeof(___v) * CHAR_BIT - 1); \
41 unsigned long final = (___shift) % (sizeof(___v) * CHAR_BIT - 1); \
44 ___v >>= sizeof(___v) * CHAR_BIT - 1; \
48 #define _bt_piecewise_lshift(_v, _shift) \
50 typeof(_v) ___v = (_v); \
51 typeof(_shift) ___shift = (_shift); \
52 unsigned long sb = (___shift) / (sizeof(___v) * CHAR_BIT - 1); \
53 unsigned long final = (___shift) % (sizeof(___v) * CHAR_BIT - 1); \
56 ___v <<= sizeof(___v) * CHAR_BIT - 1; \
60 #define _bt_is_signed_type(type) ((type) -1 < (type) 0)
62 #define _bt_unsigned_cast(type, v) \
64 (sizeof(v) < sizeof(type)) ? \
65 ((type) (v)) & (~(~(type) 0 << (sizeof(v) * CHAR_BIT))) : \
70 * bt_bitfield_write - write integer to a bitfield in native endianness
72 * Save integer to the bitfield, which starts at the "start" bit, has "len"
74 * The inside of a bitfield is from high bits to low bits.
75 * Uses native endianness.
76 * For unsigned "v", pad MSB with 0 if bitfield is larger than v.
77 * For signed "v", sign-extend v if bitfield is larger than v.
79 * On little endian, bytes are placed from the less significant to the most
80 * significant. Also, consecutive bitfields are placed from lower bits to higher
83 * On big endian, bytes are places from most significant to less significant.
84 * Also, consecutive bitfields are placed from higher to lower bits.
87 #define _bt_bitfield_write_le(_ptr, type, _start, _length, _v) \
89 typeof(_v) __v = (_v); \
90 type *__ptr = (void *) (_ptr); \
91 unsigned long __start = (_start), __length = (_length); \
93 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
94 unsigned long start_unit, end_unit, this_unit; \
95 unsigned long end, cshift; /* cshift is "complement shift" */ \
100 end = __start + __length; \
101 start_unit = __start / ts; \
102 end_unit = (end + (ts - 1)) / ts; \
104 /* Trim v high bits */ \
105 if (__length < sizeof(__v) * CHAR_BIT) \
106 __v &= ~((~(typeof(__v)) 0) << __length); \
108 /* We can now append v with a simple "or", shift it piece-wise */ \
109 this_unit = start_unit; \
110 if (start_unit == end_unit - 1) { \
111 mask = ~((~(type) 0) << (__start % ts)); \
113 mask |= (~(type) 0) << (end % ts); \
114 cmask = (type) __v << (__start % ts); \
116 __ptr[this_unit] &= mask; \
117 __ptr[this_unit] |= cmask; \
120 if (__start % ts) { \
121 cshift = __start % ts; \
122 mask = ~((~(type) 0) << cshift); \
123 cmask = (type) __v << cshift; \
125 __ptr[this_unit] &= mask; \
126 __ptr[this_unit] |= cmask; \
127 __v = _bt_piecewise_rshift(__v, ts - cshift); \
128 __start += ts - cshift; \
131 for (; this_unit < end_unit - 1; this_unit++) { \
132 __ptr[this_unit] = (type) __v; \
133 __v = _bt_piecewise_rshift(__v, ts); \
137 mask = (~(type) 0) << (end % ts); \
138 cmask = (type) __v; \
140 __ptr[this_unit] &= mask; \
141 __ptr[this_unit] |= cmask; \
143 __ptr[this_unit] = (type) __v; \
146 #define _bt_bitfield_write_be(_ptr, type, _start, _length, _v) \
148 typeof(_v) __v = (_v); \
149 type *__ptr = (void *) (_ptr); \
150 unsigned long __start = (_start), __length = (_length); \
152 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
153 unsigned long start_unit, end_unit, this_unit; \
154 unsigned long end, cshift; /* cshift is "complement shift" */ \
159 end = __start + __length; \
160 start_unit = __start / ts; \
161 end_unit = (end + (ts - 1)) / ts; \
163 /* Trim v high bits */ \
164 if (__length < sizeof(__v) * CHAR_BIT) \
165 __v &= ~((~(typeof(__v)) 0) << __length); \
167 /* We can now append v with a simple "or", shift it piece-wise */ \
168 this_unit = end_unit - 1; \
169 if (start_unit == end_unit - 1) { \
170 mask = ~((~(type) 0) << ((ts - (end % ts)) % ts)); \
172 mask |= (~((type) 0)) << (ts - (__start % ts)); \
173 cmask = (type) __v << ((ts - (end % ts)) % ts); \
175 __ptr[this_unit] &= mask; \
176 __ptr[this_unit] |= cmask; \
181 mask = ~((~(type) 0) << (ts - cshift)); \
182 cmask = (type) __v << (ts - cshift); \
184 __ptr[this_unit] &= mask; \
185 __ptr[this_unit] |= cmask; \
186 __v = _bt_piecewise_rshift(__v, cshift); \
190 for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \
191 __ptr[this_unit] = (type) __v; \
192 __v = _bt_piecewise_rshift(__v, ts); \
195 if (__start % ts) { \
196 mask = (~(type) 0) << (ts - (__start % ts)); \
197 cmask = (type) __v; \
199 __ptr[this_unit] &= mask; \
200 __ptr[this_unit] |= cmask; \
202 __ptr[this_unit] = (type) __v; \
206 * bt_bitfield_write - write integer to a bitfield in native endianness
207 * bt_bitfield_write_le - write integer to a bitfield in little endian
208 * bt_bitfield_write_be - write integer to a bitfield in big endian
211 #if (BYTE_ORDER == LITTLE_ENDIAN)
213 #define bt_bitfield_write(ptr, type, _start, _length, _v) \
214 _bt_bitfield_write_le(ptr, type, _start, _length, _v)
216 #define bt_bitfield_write_le(ptr, type, _start, _length, _v) \
217 _bt_bitfield_write_le(ptr, type, _start, _length, _v)
219 #define bt_bitfield_write_be(ptr, type, _start, _length, _v) \
220 _bt_bitfield_write_be(ptr, unsigned char, _start, _length, _v)
222 #elif (BYTE_ORDER == BIG_ENDIAN)
224 #define bt_bitfield_write(ptr, type, _start, _length, _v) \
225 _bt_bitfield_write_be(ptr, type, _start, _length, _v)
227 #define bt_bitfield_write_le(ptr, type, _start, _length, _v) \
228 _bt_bitfield_write_le(ptr, unsigned char, _start, _length, _v)
230 #define bt_bitfield_write_be(ptr, type, _start, _length, _v) \
231 _bt_bitfield_write_be(ptr, type, _start, _length, _v)
233 #else /* (BYTE_ORDER == PDP_ENDIAN) */
235 #error "Byte order not supported"
239 #define _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
241 typeof(*(_vptr)) *__vptr = (_vptr); \
242 typeof(*__vptr) __v; \
243 type *__ptr = (void *) (_ptr); \
244 unsigned long __start = (_start), __length = (_length); \
246 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
247 unsigned long start_unit, end_unit, this_unit; \
248 unsigned long end, cshift; /* cshift is "complement shift" */ \
255 end = __start + __length; \
256 start_unit = __start / ts; \
257 end_unit = (end + (ts - 1)) / ts; \
259 this_unit = end_unit - 1; \
260 if (_bt_is_signed_type(typeof(__v)) \
261 && (__ptr[this_unit] & ((type) 1 << ((end % ts ? : ts) - 1)))) \
262 __v = ~(typeof(__v)) 0; \
265 if (start_unit == end_unit - 1) { \
266 cmask = __ptr[this_unit]; \
267 cmask >>= (__start % ts); \
268 if ((end - __start) % ts) { \
269 mask = ~((~(type) 0) << (end - __start)); \
272 __v = _bt_piecewise_lshift(__v, end - __start); \
273 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
279 mask = ~((~(type) 0) << cshift); \
280 cmask = __ptr[this_unit]; \
282 __v = _bt_piecewise_lshift(__v, cshift); \
283 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
287 for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \
288 __v = _bt_piecewise_lshift(__v, ts); \
289 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
292 if (__start % ts) { \
293 mask = ~((~(type) 0) << (ts - (__start % ts))); \
294 cmask = __ptr[this_unit]; \
295 cmask >>= (__start % ts); \
297 __v = _bt_piecewise_lshift(__v, ts - (__start % ts)); \
298 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
300 __v = _bt_piecewise_lshift(__v, ts); \
301 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
306 #define _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
308 typeof(*(_vptr)) *__vptr = (_vptr); \
309 typeof(*__vptr) __v; \
310 type *__ptr = (void *) (_ptr); \
311 unsigned long __start = (_start), __length = (_length); \
313 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
314 unsigned long start_unit, end_unit, this_unit; \
315 unsigned long end, cshift; /* cshift is "complement shift" */ \
322 end = __start + __length; \
323 start_unit = __start / ts; \
324 end_unit = (end + (ts - 1)) / ts; \
326 this_unit = start_unit; \
327 if (_bt_is_signed_type(typeof(__v)) \
328 && (__ptr[this_unit] & ((type) 1 << (ts - (__start % ts) - 1)))) \
329 __v = ~(typeof(__v)) 0; \
332 if (start_unit == end_unit - 1) { \
333 cmask = __ptr[this_unit]; \
334 cmask >>= (ts - (end % ts)) % ts; \
335 if ((end - __start) % ts) { \
336 mask = ~((~(type) 0) << (end - __start)); \
339 __v = _bt_piecewise_lshift(__v, end - __start); \
340 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
344 if (__start % ts) { \
345 cshift = __start % ts; \
346 mask = ~((~(type) 0) << (ts - cshift)); \
347 cmask = __ptr[this_unit]; \
349 __v = _bt_piecewise_lshift(__v, ts - cshift); \
350 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
351 __start += ts - cshift; \
354 for (; this_unit < end_unit - 1; this_unit++) { \
355 __v = _bt_piecewise_lshift(__v, ts); \
356 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
360 mask = ~((~(type) 0) << (end % ts)); \
361 cmask = __ptr[this_unit]; \
362 cmask >>= ts - (end % ts); \
364 __v = _bt_piecewise_lshift(__v, end % ts); \
365 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
367 __v = _bt_piecewise_lshift(__v, ts); \
368 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
374 * bt_bitfield_read - read integer from a bitfield in native endianness
375 * bt_bitfield_read_le - read integer from a bitfield in little endian
376 * bt_bitfield_read_be - read integer from a bitfield in big endian
379 #if (BYTE_ORDER == LITTLE_ENDIAN)
381 #define bt_bitfield_read(_ptr, type, _start, _length, _vptr) \
382 _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr)
384 #define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
385 _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr)
387 #define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
388 _bt_bitfield_read_be(_ptr, unsigned char, _start, _length, _vptr)
390 #elif (BYTE_ORDER == BIG_ENDIAN)
392 #define bt_bitfield_read(_ptr, type, _start, _length, _vptr) \
393 _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr)
395 #define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
396 _bt_bitfield_read_le(_ptr, unsigned char, _start, _length, _vptr)
398 #define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
399 _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr)
401 #else /* (BYTE_ORDER == PDP_ENDIAN) */
403 #error "Byte order not supported"
407 #endif /* _BABELTRACE_BITFIELD_H */
This page took 0.03856 seconds and 4 git commands to generate.