1 #ifndef _BABELTRACE_BITFIELD_H
2 #define _BABELTRACE_BITFIELD_H
5 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #include <stdint.h> /* C99 5.2.4.2 Numerical limits */
27 #include <babeltrace/compat/limits-internal.h> /* C99 5.2.4.2 Numerical limits */
28 #include <babeltrace/endian-internal.h> /* Non-standard BIG_ENDIAN, LITTLE_ENDIAN, BYTE_ORDER */
30 /* We can't shift a int from 32 bit, >> 32 and << 32 on int is undefined */
31 #define _bt_piecewise_rshift(_v, _shift) \
33 typeof(_v) ___v = (_v); \
34 typeof(_shift) ___shift = (_shift); \
35 unsigned long sb = (___shift) / (sizeof(___v) * CHAR_BIT - 1); \
36 unsigned long final = (___shift) % (sizeof(___v) * CHAR_BIT - 1); \
39 ___v >>= sizeof(___v) * CHAR_BIT - 1; \
43 #define _bt_piecewise_lshift(_v, _shift) \
45 typeof(_v) ___v = (_v); \
46 typeof(_shift) ___shift = (_shift); \
47 unsigned long sb = (___shift) / (sizeof(___v) * CHAR_BIT - 1); \
48 unsigned long final = (___shift) % (sizeof(___v) * CHAR_BIT - 1); \
51 ___v <<= sizeof(___v) * CHAR_BIT - 1; \
55 #define _bt_is_signed_type(type) ((type) -1 < (type) 0)
58 * NOTE: The cast to (uint64_t) below ensures that we're not casting a
59 * negative value, which is undefined in C. However, this limits the
60 * maximum type size of `type` and `v` to 64-bit. The
61 * _bt_check_max_64bit() is used to check that the users of this header
62 * do not use types with a size greater than 64-bit.
64 #define _bt_unsigned_cast(type, v) \
66 (sizeof(v) < sizeof(type)) ? \
67 ((type) (v)) & ((type) (~(~(uint64_t) 0 << (sizeof(v) * CHAR_BIT)))) : \
71 #define _bt_check_max_64bit(type) \
72 char _max_64bit_assertion[sizeof(type) <= sizeof(uint64_t) ? 1 : -1] __attribute__((unused))
75 * bt_bitfield_write - write integer to a bitfield in native endianness
77 * Save integer to the bitfield, which starts at the "start" bit, has "len"
79 * The inside of a bitfield is from high bits to low bits.
80 * Uses native endianness.
81 * For unsigned "v", pad MSB with 0 if bitfield is larger than v.
82 * For signed "v", sign-extend v if bitfield is larger than v.
84 * On little endian, bytes are placed from the less significant to the most
85 * significant. Also, consecutive bitfields are placed from lower bits to higher
88 * On big endian, bytes are places from most significant to less significant.
89 * Also, consecutive bitfields are placed from higher to lower bits.
92 #define _bt_bitfield_write_le(_ptr, type, _start, _length, _v) \
94 typeof(_v) __v = (_v); \
95 type *__ptr = (void *) (_ptr); \
96 unsigned long __start = (_start), __length = (_length); \
98 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
99 unsigned long start_unit, end_unit, this_unit; \
100 unsigned long end, cshift; /* cshift is "complement shift" */ \
105 end = __start + __length; \
106 start_unit = __start / ts; \
107 end_unit = (end + (ts - 1)) / ts; \
109 /* Trim v high bits */ \
110 if (__length < sizeof(__v) * CHAR_BIT) \
111 __v &= ~((~(typeof(__v)) 0) << __length); \
113 /* We can now append v with a simple "or", shift it piece-wise */ \
114 this_unit = start_unit; \
115 if (start_unit == end_unit - 1) { \
116 mask = ~((~(type) 0) << (__start % ts)); \
118 mask |= (~(type) 0) << (end % ts); \
119 cmask = (type) __v << (__start % ts); \
121 __ptr[this_unit] &= mask; \
122 __ptr[this_unit] |= cmask; \
125 if (__start % ts) { \
126 cshift = __start % ts; \
127 mask = ~((~(type) 0) << cshift); \
128 cmask = (type) __v << cshift; \
130 __ptr[this_unit] &= mask; \
131 __ptr[this_unit] |= cmask; \
132 __v = _bt_piecewise_rshift(__v, ts - cshift); \
133 __start += ts - cshift; \
136 for (; this_unit < end_unit - 1; this_unit++) { \
137 __ptr[this_unit] = (type) __v; \
138 __v = _bt_piecewise_rshift(__v, ts); \
142 mask = (~(type) 0) << (end % ts); \
143 cmask = (type) __v; \
145 __ptr[this_unit] &= mask; \
146 __ptr[this_unit] |= cmask; \
148 __ptr[this_unit] = (type) __v; \
151 #define _bt_bitfield_write_be(_ptr, type, _start, _length, _v) \
153 typeof(_v) __v = (_v); \
154 type *__ptr = (void *) (_ptr); \
155 unsigned long __start = (_start), __length = (_length); \
157 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
158 unsigned long start_unit, end_unit, this_unit; \
159 unsigned long end, cshift; /* cshift is "complement shift" */ \
164 end = __start + __length; \
165 start_unit = __start / ts; \
166 end_unit = (end + (ts - 1)) / ts; \
168 /* Trim v high bits */ \
169 if (__length < sizeof(__v) * CHAR_BIT) \
170 __v &= ~((~(typeof(__v)) 0) << __length); \
172 /* We can now append v with a simple "or", shift it piece-wise */ \
173 this_unit = end_unit - 1; \
174 if (start_unit == end_unit - 1) { \
175 mask = ~((~(type) 0) << ((ts - (end % ts)) % ts)); \
177 mask |= (~((type) 0)) << (ts - (__start % ts)); \
178 cmask = (type) __v << ((ts - (end % ts)) % ts); \
180 __ptr[this_unit] &= mask; \
181 __ptr[this_unit] |= cmask; \
186 mask = ~((~(type) 0) << (ts - cshift)); \
187 cmask = (type) __v << (ts - cshift); \
189 __ptr[this_unit] &= mask; \
190 __ptr[this_unit] |= cmask; \
191 __v = _bt_piecewise_rshift(__v, cshift); \
195 for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \
196 __ptr[this_unit] = (type) __v; \
197 __v = _bt_piecewise_rshift(__v, ts); \
200 if (__start % ts) { \
201 mask = (~(type) 0) << (ts - (__start % ts)); \
202 cmask = (type) __v; \
204 __ptr[this_unit] &= mask; \
205 __ptr[this_unit] |= cmask; \
207 __ptr[this_unit] = (type) __v; \
211 * bt_bitfield_write - write integer to a bitfield in native endianness
212 * bt_bitfield_write_le - write integer to a bitfield in little endian
213 * bt_bitfield_write_be - write integer to a bitfield in big endian
216 #if (BYTE_ORDER == LITTLE_ENDIAN)
218 #define bt_bitfield_write(ptr, type, _start, _length, _v) \
219 _bt_bitfield_write_le(ptr, type, _start, _length, _v)
221 #define bt_bitfield_write_le(ptr, type, _start, _length, _v) \
222 _bt_bitfield_write_le(ptr, type, _start, _length, _v)
224 #define bt_bitfield_write_be(ptr, type, _start, _length, _v) \
225 _bt_bitfield_write_be(ptr, unsigned char, _start, _length, _v)
227 #elif (BYTE_ORDER == BIG_ENDIAN)
229 #define bt_bitfield_write(ptr, type, _start, _length, _v) \
230 _bt_bitfield_write_be(ptr, type, _start, _length, _v)
232 #define bt_bitfield_write_le(ptr, type, _start, _length, _v) \
233 _bt_bitfield_write_le(ptr, unsigned char, _start, _length, _v)
235 #define bt_bitfield_write_be(ptr, type, _start, _length, _v) \
236 _bt_bitfield_write_be(ptr, type, _start, _length, _v)
238 #else /* (BYTE_ORDER == PDP_ENDIAN) */
240 #error "Byte order not supported"
244 #define _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
246 typeof(*(_vptr)) *__vptr = (_vptr); \
247 typeof(*__vptr) __v; \
248 type *__ptr = (void *) (_ptr); \
249 unsigned long __start = (_start), __length = (_length); \
251 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
252 unsigned long start_unit, end_unit, this_unit; \
253 unsigned long end, cshift; /* cshift is "complement shift" */ \
255 { _bt_check_max_64bit(type); } \
256 { _bt_check_max_64bit(typeof(*_vptr)); } \
257 { _bt_check_max_64bit(typeof(*_ptr)); } \
264 end = __start + __length; \
265 start_unit = __start / ts; \
266 end_unit = (end + (ts - 1)) / ts; \
268 this_unit = end_unit - 1; \
269 if (_bt_is_signed_type(typeof(__v)) \
270 && (__ptr[this_unit] & ((type) 1 << ((end % ts ? : ts) - 1)))) \
271 __v = ~(typeof(__v)) 0; \
274 if (start_unit == end_unit - 1) { \
275 cmask = __ptr[this_unit]; \
276 cmask >>= (__start % ts); \
277 if ((end - __start) % ts) { \
278 mask = ~((~(type) 0) << (end - __start)); \
281 __v = _bt_piecewise_lshift(__v, end - __start); \
282 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
288 mask = ~((~(type) 0) << cshift); \
289 cmask = __ptr[this_unit]; \
291 __v = _bt_piecewise_lshift(__v, cshift); \
292 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
296 for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \
297 __v = _bt_piecewise_lshift(__v, ts); \
298 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
301 if (__start % ts) { \
302 mask = ~((~(type) 0) << (ts - (__start % ts))); \
303 cmask = __ptr[this_unit]; \
304 cmask >>= (__start % ts); \
306 __v = _bt_piecewise_lshift(__v, ts - (__start % ts)); \
307 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
309 __v = _bt_piecewise_lshift(__v, ts); \
310 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
315 #define _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
317 typeof(*(_vptr)) *__vptr = (_vptr); \
318 typeof(*__vptr) __v; \
319 type *__ptr = (void *) (_ptr); \
320 unsigned long __start = (_start), __length = (_length); \
322 unsigned long ts = sizeof(type) * CHAR_BIT; /* type size */ \
323 unsigned long start_unit, end_unit, this_unit; \
324 unsigned long end, cshift; /* cshift is "complement shift" */ \
326 { _bt_check_max_64bit(type); } \
327 { _bt_check_max_64bit(typeof(*_vptr)); } \
328 { _bt_check_max_64bit(typeof(*_ptr)); } \
335 end = __start + __length; \
336 start_unit = __start / ts; \
337 end_unit = (end + (ts - 1)) / ts; \
339 this_unit = start_unit; \
340 if (_bt_is_signed_type(typeof(__v)) \
341 && (__ptr[this_unit] & ((type) 1 << (ts - (__start % ts) - 1)))) \
342 __v = ~(typeof(__v)) 0; \
345 if (start_unit == end_unit - 1) { \
346 cmask = __ptr[this_unit]; \
347 cmask >>= (ts - (end % ts)) % ts; \
348 if ((end - __start) % ts) { \
349 mask = ~((~(type) 0) << (end - __start)); \
352 __v = _bt_piecewise_lshift(__v, end - __start); \
353 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
357 if (__start % ts) { \
358 cshift = __start % ts; \
359 mask = ~((~(type) 0) << (ts - cshift)); \
360 cmask = __ptr[this_unit]; \
362 __v = _bt_piecewise_lshift(__v, ts - cshift); \
363 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
364 __start += ts - cshift; \
367 for (; this_unit < end_unit - 1; this_unit++) { \
368 __v = _bt_piecewise_lshift(__v, ts); \
369 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
373 mask = ~((~(type) 0) << (end % ts)); \
374 cmask = __ptr[this_unit]; \
375 cmask >>= ts - (end % ts); \
377 __v = _bt_piecewise_lshift(__v, end % ts); \
378 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
380 __v = _bt_piecewise_lshift(__v, ts); \
381 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
387 * bt_bitfield_read - read integer from a bitfield in native endianness
388 * bt_bitfield_read_le - read integer from a bitfield in little endian
389 * bt_bitfield_read_be - read integer from a bitfield in big endian
392 #if (BYTE_ORDER == LITTLE_ENDIAN)
394 #define bt_bitfield_read(_ptr, type, _start, _length, _vptr) \
395 _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr)
397 #define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
398 _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr)
400 #define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
401 _bt_bitfield_read_be(_ptr, unsigned char, _start, _length, _vptr)
403 #elif (BYTE_ORDER == BIG_ENDIAN)
405 #define bt_bitfield_read(_ptr, type, _start, _length, _vptr) \
406 _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr)
408 #define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
409 _bt_bitfield_read_le(_ptr, unsigned char, _start, _length, _vptr)
411 #define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
412 _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr)
414 #else /* (BYTE_ORDER == PDP_ENDIAN) */
416 #error "Byte order not supported"
420 #endif /* _BABELTRACE_BITFIELD_H */
This page took 0.037312 seconds and 4 git commands to generate.