e5d95e6f0d618ffab29a356c56a83a8dba549215
[babeltrace.git] / include / babeltrace / bitfield-internal.h
1 #ifndef _BABELTRACE_BITFIELD_H
2 #define _BABELTRACE_BITFIELD_H
3
4 /*
5 * BabelTrace
6 *
7 * Bitfields read/write functions.
8 *
9 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
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:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
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
27 * SOFTWARE.
28 */
29
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 */
33
34 /* We can't shift a int from 32 bit, >> 32 and << 32 on int is undefined */
35 #define _bt_piecewise_rshift(_v, _shift) \
36 ({ \
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); \
41 \
42 for (; sb; sb--) \
43 ___v >>= sizeof(___v) * CHAR_BIT - 1; \
44 ___v >>= final; \
45 })
46
47 #define _bt_piecewise_lshift(_v, _shift) \
48 ({ \
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); \
53 \
54 for (; sb; sb--) \
55 ___v <<= sizeof(___v) * CHAR_BIT - 1; \
56 ___v <<= final; \
57 })
58
59 #define _bt_is_signed_type(type) ((type) -1 < (type) 0)
60
61 /*
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.
67 */
68 #define _bt_unsigned_cast(type, v) \
69 ({ \
70 (sizeof(v) < sizeof(type)) ? \
71 ((type) (v)) & ((type) (~(~(uint64_t) 0 << (sizeof(v) * CHAR_BIT)))) : \
72 (type) (v); \
73 })
74
75 #define _bt_check_max_64bit(type) \
76 char _max_64bit_assertion[sizeof(type) <= sizeof(uint64_t) ? 1 : -1] __attribute__((unused))
77
78 /*
79 * bt_bitfield_write - write integer to a bitfield in native endianness
80 *
81 * Save integer to the bitfield, which starts at the "start" bit, has "len"
82 * bits.
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.
87 *
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
90 * bits.
91 *
92 * On big endian, bytes are places from most significant to less significant.
93 * Also, consecutive bitfields are placed from higher to lower bits.
94 */
95
96 #define _bt_bitfield_write_le(_ptr, type, _start, _length, _v) \
97 do { \
98 typeof(_v) __v = (_v); \
99 type *__ptr = (void *) (_ptr); \
100 unsigned long __start = (_start), __length = (_length); \
101 type mask, cmask; \
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" */ \
105 \
106 if (!__length) \
107 break; \
108 \
109 end = __start + __length; \
110 start_unit = __start / ts; \
111 end_unit = (end + (ts - 1)) / ts; \
112 \
113 /* Trim v high bits */ \
114 if (__length < sizeof(__v) * CHAR_BIT) \
115 __v &= ~((~(typeof(__v)) 0) << __length); \
116 \
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)); \
121 if (end % ts) \
122 mask |= (~(type) 0) << (end % ts); \
123 cmask = (type) __v << (__start % ts); \
124 cmask &= ~mask; \
125 __ptr[this_unit] &= mask; \
126 __ptr[this_unit] |= cmask; \
127 break; \
128 } \
129 if (__start % ts) { \
130 cshift = __start % ts; \
131 mask = ~((~(type) 0) << cshift); \
132 cmask = (type) __v << cshift; \
133 cmask &= ~mask; \
134 __ptr[this_unit] &= mask; \
135 __ptr[this_unit] |= cmask; \
136 __v = _bt_piecewise_rshift(__v, ts - cshift); \
137 __start += ts - cshift; \
138 this_unit++; \
139 } \
140 for (; this_unit < end_unit - 1; this_unit++) { \
141 __ptr[this_unit] = (type) __v; \
142 __v = _bt_piecewise_rshift(__v, ts); \
143 __start += ts; \
144 } \
145 if (end % ts) { \
146 mask = (~(type) 0) << (end % ts); \
147 cmask = (type) __v; \
148 cmask &= ~mask; \
149 __ptr[this_unit] &= mask; \
150 __ptr[this_unit] |= cmask; \
151 } else \
152 __ptr[this_unit] = (type) __v; \
153 } while (0)
154
155 #define _bt_bitfield_write_be(_ptr, type, _start, _length, _v) \
156 do { \
157 typeof(_v) __v = (_v); \
158 type *__ptr = (void *) (_ptr); \
159 unsigned long __start = (_start), __length = (_length); \
160 type mask, cmask; \
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" */ \
164 \
165 if (!__length) \
166 break; \
167 \
168 end = __start + __length; \
169 start_unit = __start / ts; \
170 end_unit = (end + (ts - 1)) / ts; \
171 \
172 /* Trim v high bits */ \
173 if (__length < sizeof(__v) * CHAR_BIT) \
174 __v &= ~((~(typeof(__v)) 0) << __length); \
175 \
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)); \
180 if (__start % ts) \
181 mask |= (~((type) 0)) << (ts - (__start % ts)); \
182 cmask = (type) __v << ((ts - (end % ts)) % ts); \
183 cmask &= ~mask; \
184 __ptr[this_unit] &= mask; \
185 __ptr[this_unit] |= cmask; \
186 break; \
187 } \
188 if (end % ts) { \
189 cshift = end % ts; \
190 mask = ~((~(type) 0) << (ts - cshift)); \
191 cmask = (type) __v << (ts - cshift); \
192 cmask &= ~mask; \
193 __ptr[this_unit] &= mask; \
194 __ptr[this_unit] |= cmask; \
195 __v = _bt_piecewise_rshift(__v, cshift); \
196 end -= cshift; \
197 this_unit--; \
198 } \
199 for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \
200 __ptr[this_unit] = (type) __v; \
201 __v = _bt_piecewise_rshift(__v, ts); \
202 end -= ts; \
203 } \
204 if (__start % ts) { \
205 mask = (~(type) 0) << (ts - (__start % ts)); \
206 cmask = (type) __v; \
207 cmask &= ~mask; \
208 __ptr[this_unit] &= mask; \
209 __ptr[this_unit] |= cmask; \
210 } else \
211 __ptr[this_unit] = (type) __v; \
212 } while (0)
213
214 /*
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
218 */
219
220 #if (BYTE_ORDER == LITTLE_ENDIAN)
221
222 #define bt_bitfield_write(ptr, type, _start, _length, _v) \
223 _bt_bitfield_write_le(ptr, type, _start, _length, _v)
224
225 #define bt_bitfield_write_le(ptr, type, _start, _length, _v) \
226 _bt_bitfield_write_le(ptr, type, _start, _length, _v)
227
228 #define bt_bitfield_write_be(ptr, type, _start, _length, _v) \
229 _bt_bitfield_write_be(ptr, unsigned char, _start, _length, _v)
230
231 #elif (BYTE_ORDER == BIG_ENDIAN)
232
233 #define bt_bitfield_write(ptr, type, _start, _length, _v) \
234 _bt_bitfield_write_be(ptr, type, _start, _length, _v)
235
236 #define bt_bitfield_write_le(ptr, type, _start, _length, _v) \
237 _bt_bitfield_write_le(ptr, unsigned char, _start, _length, _v)
238
239 #define bt_bitfield_write_be(ptr, type, _start, _length, _v) \
240 _bt_bitfield_write_be(ptr, type, _start, _length, _v)
241
242 #else /* (BYTE_ORDER == PDP_ENDIAN) */
243
244 #error "Byte order not supported"
245
246 #endif
247
248 #define _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
249 do { \
250 typeof(*(_vptr)) *__vptr = (_vptr); \
251 typeof(*__vptr) __v; \
252 type *__ptr = (void *) (_ptr); \
253 unsigned long __start = (_start), __length = (_length); \
254 type mask, cmask; \
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" */ \
258 \
259 { _bt_check_max_64bit(type); } \
260 { _bt_check_max_64bit(typeof(*_vptr)); } \
261 { _bt_check_max_64bit(typeof(*_ptr)); } \
262 \
263 if (!__length) { \
264 *__vptr = 0; \
265 break; \
266 } \
267 \
268 end = __start + __length; \
269 start_unit = __start / ts; \
270 end_unit = (end + (ts - 1)) / ts; \
271 \
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; \
276 else \
277 __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)); \
283 cmask &= mask; \
284 } \
285 __v = _bt_piecewise_lshift(__v, end - __start); \
286 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
287 *__vptr = __v; \
288 break; \
289 } \
290 if (end % ts) { \
291 cshift = end % ts; \
292 mask = ~((~(type) 0) << cshift); \
293 cmask = __ptr[this_unit]; \
294 cmask &= mask; \
295 __v = _bt_piecewise_lshift(__v, cshift); \
296 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
297 end -= cshift; \
298 this_unit--; \
299 } \
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]);\
303 end -= ts; \
304 } \
305 if (__start % ts) { \
306 mask = ~((~(type) 0) << (ts - (__start % ts))); \
307 cmask = __ptr[this_unit]; \
308 cmask >>= (__start % ts); \
309 cmask &= mask; \
310 __v = _bt_piecewise_lshift(__v, ts - (__start % ts)); \
311 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
312 } else { \
313 __v = _bt_piecewise_lshift(__v, ts); \
314 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
315 } \
316 *__vptr = __v; \
317 } while (0)
318
319 #define _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
320 do { \
321 typeof(*(_vptr)) *__vptr = (_vptr); \
322 typeof(*__vptr) __v; \
323 type *__ptr = (void *) (_ptr); \
324 unsigned long __start = (_start), __length = (_length); \
325 type mask, cmask; \
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" */ \
329 \
330 { _bt_check_max_64bit(type); } \
331 { _bt_check_max_64bit(typeof(*_vptr)); } \
332 { _bt_check_max_64bit(typeof(*_ptr)); } \
333 \
334 if (!__length) { \
335 *__vptr = 0; \
336 break; \
337 } \
338 \
339 end = __start + __length; \
340 start_unit = __start / ts; \
341 end_unit = (end + (ts - 1)) / ts; \
342 \
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; \
347 else \
348 __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)); \
354 cmask &= mask; \
355 } \
356 __v = _bt_piecewise_lshift(__v, end - __start); \
357 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
358 *__vptr = __v; \
359 break; \
360 } \
361 if (__start % ts) { \
362 cshift = __start % ts; \
363 mask = ~((~(type) 0) << (ts - cshift)); \
364 cmask = __ptr[this_unit]; \
365 cmask &= mask; \
366 __v = _bt_piecewise_lshift(__v, ts - cshift); \
367 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
368 __start += ts - cshift; \
369 this_unit++; \
370 } \
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]);\
374 __start += ts; \
375 } \
376 if (end % ts) { \
377 mask = ~((~(type) 0) << (end % ts)); \
378 cmask = __ptr[this_unit]; \
379 cmask >>= ts - (end % ts); \
380 cmask &= mask; \
381 __v = _bt_piecewise_lshift(__v, end % ts); \
382 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
383 } else { \
384 __v = _bt_piecewise_lshift(__v, ts); \
385 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
386 } \
387 *__vptr = __v; \
388 } while (0)
389
390 /*
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
394 */
395
396 #if (BYTE_ORDER == LITTLE_ENDIAN)
397
398 #define bt_bitfield_read(_ptr, type, _start, _length, _vptr) \
399 _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr)
400
401 #define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
402 _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr)
403
404 #define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
405 _bt_bitfield_read_be(_ptr, unsigned char, _start, _length, _vptr)
406
407 #elif (BYTE_ORDER == BIG_ENDIAN)
408
409 #define bt_bitfield_read(_ptr, type, _start, _length, _vptr) \
410 _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr)
411
412 #define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
413 _bt_bitfield_read_le(_ptr, unsigned char, _start, _length, _vptr)
414
415 #define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
416 _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr)
417
418 #else /* (BYTE_ORDER == PDP_ENDIAN) */
419
420 #error "Byte order not supported"
421
422 #endif
423
424 #endif /* _BABELTRACE_BITFIELD_H */
This page took 0.039263 seconds and 3 git commands to generate.