c5d5eccdf0d191714d9c53421ad5a97fc6b2599c
[babeltrace.git] / include / babeltrace / bitfield-internal.h
1 #ifndef _BABELTRACE_BITFIELD_H
2 #define _BABELTRACE_BITFIELD_H
3
4 /*
5 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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
23 * SOFTWARE.
24 */
25
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 */
29
30 /* We can't shift a int from 32 bit, >> 32 and << 32 on int is undefined */
31 #define _bt_piecewise_rshift(_v, _shift) \
32 ({ \
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); \
37 \
38 for (; sb; sb--) \
39 ___v >>= sizeof(___v) * CHAR_BIT - 1; \
40 ___v >>= final; \
41 })
42
43 #define _bt_piecewise_lshift(_v, _shift) \
44 ({ \
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); \
49 \
50 for (; sb; sb--) \
51 ___v <<= sizeof(___v) * CHAR_BIT - 1; \
52 ___v <<= final; \
53 })
54
55 #define _bt_is_signed_type(type) ((type) -1 < (type) 0)
56
57 /*
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.
63 */
64 #define _bt_unsigned_cast(type, v) \
65 ({ \
66 (sizeof(v) < sizeof(type)) ? \
67 ((type) (v)) & ((type) (~(~(uint64_t) 0 << (sizeof(v) * CHAR_BIT)))) : \
68 (type) (v); \
69 })
70
71 #define _bt_check_max_64bit(type) \
72 char _max_64bit_assertion[sizeof(type) <= sizeof(uint64_t) ? 1 : -1] __attribute__((unused))
73
74 /*
75 * bt_bitfield_write - write integer to a bitfield in native endianness
76 *
77 * Save integer to the bitfield, which starts at the "start" bit, has "len"
78 * bits.
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.
83 *
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
86 * bits.
87 *
88 * On big endian, bytes are places from most significant to less significant.
89 * Also, consecutive bitfields are placed from higher to lower bits.
90 */
91
92 #define _bt_bitfield_write_le(_ptr, type, _start, _length, _v) \
93 do { \
94 typeof(_v) __v = (_v); \
95 type *__ptr = (void *) (_ptr); \
96 unsigned long __start = (_start), __length = (_length); \
97 type mask, cmask; \
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" */ \
101 \
102 if (!__length) \
103 break; \
104 \
105 end = __start + __length; \
106 start_unit = __start / ts; \
107 end_unit = (end + (ts - 1)) / ts; \
108 \
109 /* Trim v high bits */ \
110 if (__length < sizeof(__v) * CHAR_BIT) \
111 __v &= ~((~(typeof(__v)) 0) << __length); \
112 \
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)); \
117 if (end % ts) \
118 mask |= (~(type) 0) << (end % ts); \
119 cmask = (type) __v << (__start % ts); \
120 cmask &= ~mask; \
121 __ptr[this_unit] &= mask; \
122 __ptr[this_unit] |= cmask; \
123 break; \
124 } \
125 if (__start % ts) { \
126 cshift = __start % ts; \
127 mask = ~((~(type) 0) << cshift); \
128 cmask = (type) __v << cshift; \
129 cmask &= ~mask; \
130 __ptr[this_unit] &= mask; \
131 __ptr[this_unit] |= cmask; \
132 __v = _bt_piecewise_rshift(__v, ts - cshift); \
133 __start += ts - cshift; \
134 this_unit++; \
135 } \
136 for (; this_unit < end_unit - 1; this_unit++) { \
137 __ptr[this_unit] = (type) __v; \
138 __v = _bt_piecewise_rshift(__v, ts); \
139 __start += ts; \
140 } \
141 if (end % ts) { \
142 mask = (~(type) 0) << (end % ts); \
143 cmask = (type) __v; \
144 cmask &= ~mask; \
145 __ptr[this_unit] &= mask; \
146 __ptr[this_unit] |= cmask; \
147 } else \
148 __ptr[this_unit] = (type) __v; \
149 } while (0)
150
151 #define _bt_bitfield_write_be(_ptr, type, _start, _length, _v) \
152 do { \
153 typeof(_v) __v = (_v); \
154 type *__ptr = (void *) (_ptr); \
155 unsigned long __start = (_start), __length = (_length); \
156 type mask, cmask; \
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" */ \
160 \
161 if (!__length) \
162 break; \
163 \
164 end = __start + __length; \
165 start_unit = __start / ts; \
166 end_unit = (end + (ts - 1)) / ts; \
167 \
168 /* Trim v high bits */ \
169 if (__length < sizeof(__v) * CHAR_BIT) \
170 __v &= ~((~(typeof(__v)) 0) << __length); \
171 \
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)); \
176 if (__start % ts) \
177 mask |= (~((type) 0)) << (ts - (__start % ts)); \
178 cmask = (type) __v << ((ts - (end % ts)) % ts); \
179 cmask &= ~mask; \
180 __ptr[this_unit] &= mask; \
181 __ptr[this_unit] |= cmask; \
182 break; \
183 } \
184 if (end % ts) { \
185 cshift = end % ts; \
186 mask = ~((~(type) 0) << (ts - cshift)); \
187 cmask = (type) __v << (ts - cshift); \
188 cmask &= ~mask; \
189 __ptr[this_unit] &= mask; \
190 __ptr[this_unit] |= cmask; \
191 __v = _bt_piecewise_rshift(__v, cshift); \
192 end -= cshift; \
193 this_unit--; \
194 } \
195 for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \
196 __ptr[this_unit] = (type) __v; \
197 __v = _bt_piecewise_rshift(__v, ts); \
198 end -= ts; \
199 } \
200 if (__start % ts) { \
201 mask = (~(type) 0) << (ts - (__start % ts)); \
202 cmask = (type) __v; \
203 cmask &= ~mask; \
204 __ptr[this_unit] &= mask; \
205 __ptr[this_unit] |= cmask; \
206 } else \
207 __ptr[this_unit] = (type) __v; \
208 } while (0)
209
210 /*
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
214 */
215
216 #if (BYTE_ORDER == LITTLE_ENDIAN)
217
218 #define bt_bitfield_write(ptr, type, _start, _length, _v) \
219 _bt_bitfield_write_le(ptr, type, _start, _length, _v)
220
221 #define bt_bitfield_write_le(ptr, type, _start, _length, _v) \
222 _bt_bitfield_write_le(ptr, type, _start, _length, _v)
223
224 #define bt_bitfield_write_be(ptr, type, _start, _length, _v) \
225 _bt_bitfield_write_be(ptr, unsigned char, _start, _length, _v)
226
227 #elif (BYTE_ORDER == BIG_ENDIAN)
228
229 #define bt_bitfield_write(ptr, type, _start, _length, _v) \
230 _bt_bitfield_write_be(ptr, type, _start, _length, _v)
231
232 #define bt_bitfield_write_le(ptr, type, _start, _length, _v) \
233 _bt_bitfield_write_le(ptr, unsigned char, _start, _length, _v)
234
235 #define bt_bitfield_write_be(ptr, type, _start, _length, _v) \
236 _bt_bitfield_write_be(ptr, type, _start, _length, _v)
237
238 #else /* (BYTE_ORDER == PDP_ENDIAN) */
239
240 #error "Byte order not supported"
241
242 #endif
243
244 #define _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
245 do { \
246 typeof(*(_vptr)) *__vptr = (_vptr); \
247 typeof(*__vptr) __v; \
248 type *__ptr = (void *) (_ptr); \
249 unsigned long __start = (_start), __length = (_length); \
250 type mask, cmask; \
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" */ \
254 \
255 { _bt_check_max_64bit(type); } \
256 { _bt_check_max_64bit(typeof(*_vptr)); } \
257 { _bt_check_max_64bit(typeof(*_ptr)); } \
258 \
259 if (!__length) { \
260 *__vptr = 0; \
261 break; \
262 } \
263 \
264 end = __start + __length; \
265 start_unit = __start / ts; \
266 end_unit = (end + (ts - 1)) / ts; \
267 \
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; \
272 else \
273 __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)); \
279 cmask &= mask; \
280 } \
281 __v = _bt_piecewise_lshift(__v, end - __start); \
282 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
283 *__vptr = __v; \
284 break; \
285 } \
286 if (end % ts) { \
287 cshift = end % ts; \
288 mask = ~((~(type) 0) << cshift); \
289 cmask = __ptr[this_unit]; \
290 cmask &= mask; \
291 __v = _bt_piecewise_lshift(__v, cshift); \
292 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
293 end -= cshift; \
294 this_unit--; \
295 } \
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]);\
299 end -= ts; \
300 } \
301 if (__start % ts) { \
302 mask = ~((~(type) 0) << (ts - (__start % ts))); \
303 cmask = __ptr[this_unit]; \
304 cmask >>= (__start % ts); \
305 cmask &= mask; \
306 __v = _bt_piecewise_lshift(__v, ts - (__start % ts)); \
307 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
308 } else { \
309 __v = _bt_piecewise_lshift(__v, ts); \
310 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
311 } \
312 *__vptr = __v; \
313 } while (0)
314
315 #define _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
316 do { \
317 typeof(*(_vptr)) *__vptr = (_vptr); \
318 typeof(*__vptr) __v; \
319 type *__ptr = (void *) (_ptr); \
320 unsigned long __start = (_start), __length = (_length); \
321 type mask, cmask; \
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" */ \
325 \
326 { _bt_check_max_64bit(type); } \
327 { _bt_check_max_64bit(typeof(*_vptr)); } \
328 { _bt_check_max_64bit(typeof(*_ptr)); } \
329 \
330 if (!__length) { \
331 *__vptr = 0; \
332 break; \
333 } \
334 \
335 end = __start + __length; \
336 start_unit = __start / ts; \
337 end_unit = (end + (ts - 1)) / ts; \
338 \
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; \
343 else \
344 __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)); \
350 cmask &= mask; \
351 } \
352 __v = _bt_piecewise_lshift(__v, end - __start); \
353 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
354 *__vptr = __v; \
355 break; \
356 } \
357 if (__start % ts) { \
358 cshift = __start % ts; \
359 mask = ~((~(type) 0) << (ts - cshift)); \
360 cmask = __ptr[this_unit]; \
361 cmask &= mask; \
362 __v = _bt_piecewise_lshift(__v, ts - cshift); \
363 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
364 __start += ts - cshift; \
365 this_unit++; \
366 } \
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]);\
370 __start += ts; \
371 } \
372 if (end % ts) { \
373 mask = ~((~(type) 0) << (end % ts)); \
374 cmask = __ptr[this_unit]; \
375 cmask >>= ts - (end % ts); \
376 cmask &= mask; \
377 __v = _bt_piecewise_lshift(__v, end % ts); \
378 __v |= _bt_unsigned_cast(typeof(__v), cmask); \
379 } else { \
380 __v = _bt_piecewise_lshift(__v, ts); \
381 __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\
382 } \
383 *__vptr = __v; \
384 } while (0)
385
386 /*
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
390 */
391
392 #if (BYTE_ORDER == LITTLE_ENDIAN)
393
394 #define bt_bitfield_read(_ptr, type, _start, _length, _vptr) \
395 _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr)
396
397 #define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
398 _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr)
399
400 #define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
401 _bt_bitfield_read_be(_ptr, unsigned char, _start, _length, _vptr)
402
403 #elif (BYTE_ORDER == BIG_ENDIAN)
404
405 #define bt_bitfield_read(_ptr, type, _start, _length, _vptr) \
406 _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr)
407
408 #define bt_bitfield_read_le(_ptr, type, _start, _length, _vptr) \
409 _bt_bitfield_read_le(_ptr, unsigned char, _start, _length, _vptr)
410
411 #define bt_bitfield_read_be(_ptr, type, _start, _length, _vptr) \
412 _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr)
413
414 #else /* (BYTE_ORDER == PDP_ENDIAN) */
415
416 #error "Byte order not supported"
417
418 #endif
419
420 #endif /* _BABELTRACE_BITFIELD_H */
This page took 0.037929 seconds and 4 git commands to generate.