1 #ifndef _CTF_BITFIELD_H
2 #define _CTF_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 on int is undefined */
28 #define _ctf_piecewise_rshift(v, shift) \
30 unsigned long sb = (shift) / (sizeof(v) * CHAR_BIT - 1); \
31 unsigned long final = (shift) % (sizeof(v) * CHAR_BIT - 1); \
35 _v >>= sizeof(v) * CHAR_BIT - 1; \
40 * ctf_bitfield_write - write integer to a bitfield in native endianness
42 * Save integer to the bitfield, which starts at the "start" bit, has "len"
44 * The inside of a bitfield is from high bits to low bits.
45 * Uses native endianness.
46 * For unsigned "v", pad MSB with 0 if bitfield is larger than v.
47 * For signed "v", sign-extend v if bitfield is larger than v.
49 * On little endian, bytes are placed from the less significant to the most
50 * significant. Also, consecutive bitfields are placed from lower bits to higher
53 * On big endian, bytes are places from most significant to less significant.
54 * Also, consecutive bitfields are placed from higher to lower bits.
57 #define _ctf_bitfield_write_le(ptr, _start, _length, _v) \
59 typeof(*(ptr)) mask, cmask; \
60 unsigned long start = (_start), length = (_length); \
61 typeof(_v) v = (_v); \
62 unsigned long start_unit, end_unit, this_unit; \
63 unsigned long end, cshift; /* cshift is "complement shift" */ \
64 unsigned long ts = sizeof(typeof(*(ptr))) * CHAR_BIT; /* type size */ \
69 end = start + length; \
70 start_unit = start / ts; \
71 end_unit = (end + (ts - 1)) / ts; \
73 /* Trim v high bits */ \
74 if (length < sizeof(v) * CHAR_BIT) \
75 v &= ~((~(typeof(v)) 0) << length); \
77 /* We can now append v with a simple "or", shift it piece-wise */ \
78 this_unit = start_unit; \
79 if (start_unit == end_unit - 1) { \
80 mask = ~((~(typeof(*(ptr))) 0) << (start % ts)); \
82 mask |= (~(typeof(*(ptr))) 0) << (end % ts); \
83 cmask = (typeof(*(ptr))) v << (start % ts); \
84 (ptr)[this_unit] &= mask; \
85 (ptr)[this_unit] |= cmask; \
89 cshift = start % ts; \
90 mask = ~((~(typeof(*(ptr))) 0) << cshift); \
91 cmask = (typeof(*(ptr))) v << cshift; \
92 (ptr)[this_unit] &= mask; \
93 (ptr)[this_unit] |= cmask; \
94 v = _ctf_piecewise_rshift(v, ts - cshift); \
95 start += ts - cshift; \
98 for (; this_unit < end_unit - 1; this_unit++) { \
99 (ptr)[this_unit] = (typeof(*(ptr))) v; \
100 v = _ctf_piecewise_rshift(v, ts); \
104 mask = (~(typeof(*(ptr))) 0) << (end % ts); \
105 cmask = (typeof(*(ptr))) v; \
106 (ptr)[this_unit] &= mask; \
107 (ptr)[this_unit] |= cmask; \
109 (ptr)[this_unit] = (typeof(*(ptr))) v; \
112 #define _ctf_bitfield_write_be(ptr, _start, _length, _v) \
114 typeof(_v) v = (_v); \
115 unsigned long start = _start, length = _length; \
116 unsigned long start_unit, end_unit, this_unit; \
117 unsigned long end, cshift; /* cshift is "complement shift" */ \
118 typeof(*(ptr)) mask, cmask; \
119 unsigned long ts = sizeof(typeof(*(ptr))) * CHAR_BIT; /* type size */ \
124 end = start + length; \
125 start_unit = start / ts; \
126 end_unit = (end + (ts - 1)) / ts; \
128 /* Trim v high bits */ \
129 if (length < sizeof(v) * CHAR_BIT) \
130 v &= ~((~(typeof(v)) 0) << length); \
132 /* We can now append v with a simple "or", shift it piece-wise */ \
133 this_unit = end_unit - 1; \
134 if (start_unit == end_unit - 1) { \
135 mask = ~((~(typeof(*(ptr))) 0) << ((ts - (end % ts)) % ts)); \
137 mask |= (~((typeof(*(ptr))) 0)) << (ts - (start % ts)); \
138 cmask = (typeof(*(ptr))) v << ((ts - (end % ts)) % ts); \
139 (ptr)[this_unit] &= mask; \
140 (ptr)[this_unit] |= cmask; \
145 mask = ~((~(typeof(*(ptr))) 0) << (ts - cshift)); \
146 cmask = (typeof(*(ptr))) v << (ts - cshift); \
147 (ptr)[this_unit] &= mask; \
148 (ptr)[this_unit] |= cmask; \
149 v = _ctf_piecewise_rshift(v, cshift); \
153 for (; (long)this_unit >= (long)start_unit + 1; this_unit--) { \
154 (ptr)[this_unit] = (typeof(*(ptr))) v; \
155 v = _ctf_piecewise_rshift(v, ts); \
159 mask = (~(typeof(*(ptr))) 0) << (ts - (start % ts)); \
160 cmask = (typeof(*(ptr))) v; \
161 (ptr)[this_unit] &= mask; \
162 (ptr)[this_unit] |= cmask; \
164 (ptr)[this_unit] = (typeof(*(ptr))) v; \
168 * ctf_bitfield_write - write integer to a bitfield in native endianness
169 * ctf_bitfield_write_le - write integer to a bitfield in little endian
170 * ctf_bitfield_write_be - write integer to a bitfield in big endian
173 #if (BYTE_ORDER == LITTLE_ENDIAN)
175 #define ctf_bitfield_write(ptr, _start, _length, _v) \
176 _ctf_bitfield_write_le(ptr, _start, _length, _v)
178 #define ctf_bitfield_write_le(ptr, _start, _length, _v) \
179 _ctf_bitfield_write_le(ptr, _start, _length, _v)
181 #define ctf_bitfield_write_be(ptr, _start, _length, _v) \
182 _ctf_bitfield_write_be((uint8_t *) (ptr), _start, _length, _v)
184 #elif (BYTE_ORDER == BIG_ENDIAN)
186 #define ctf_bitfield_write(ptr, _start, _length, _v) \
187 _ctf_bitfield_write_be(ptr, _start, _length, _v)
189 #define ctf_bitfield_write_le(ptr, _start, _length, _v) \
190 _ctf_bitfield_write_le((uint8_t *) (ptr), _start, _length, _v)
192 #define ctf_bitfield_write_be(ptr, _start, _length, _v) \
193 _ctf_bitfield_write_be(ptr, _start, _length, _v)
195 #else /* (BYTE_ORDER == PDP_ENDIAN) */
197 #error "Byte order not supported"
202 #define _ctf_bitfield_read_le(ptr, _start, _length, _vptr) \
204 typeof(_vptr) vptr = (_vptr); \
205 unsigned long start = _start, length = _length; \
206 unsigned long start_unit, end_unit, this_unit; \
207 unsigned long end, cshift; /* cshift is "complement shift" */ \
208 typeof(*(ptr)) mask, cmask; \
209 unsigned long ts = sizeof(typeof(*(ptr))) * CHAR_BIT; /* type size */ \
214 end = start + length; \
215 start_unit = start / ts; \
216 end_unit = (end + (ts - 1)) / ts; \
222 * Read a bitfield byte-wise. This function is arch-agnostic.
226 uint64_t _ctf_bitfield_read_64(const unsigned char *ptr
,
227 unsigned long start
, unsigned long len
,
228 int byte_order
, int signedness
)
230 long start_unit
, end_unit
, this_unit
;
231 unsigned long end
, cshift
; /* cshift is "complement shift" */
232 unsigned long ts
= sizeof(unsigned char) * CHAR_BIT
;
233 unsigned char mask
, cmask
;
239 start_unit
= start
/ ts
;
240 end_unit
= (end
+ (ts
- 1)) / ts
;
243 * We can now fill v piece-wise, from lower bits to upper bits.
244 * We read the bitfield in the opposite direction it was written.
246 switch (byte_order
) {
248 this_unit
= end_unit
- 1;
250 if (ptr
[this_unit
] & (1U << ((end
% ts
? : ts
) - 1)))
253 if (start_unit
== end_unit
- 1) {
254 mask
= (~(unsigned char) 0) << (end
% ts
? : ts
);
255 mask
|= ~((~(unsigned char) 0) << (start
% ts
));
256 cmask
= ptr
[this_unit
];
258 cmask
>>= (start
% ts
);
264 cshift
= (end
% ts
? : ts
);
265 mask
= (~(unsigned char) 0) << cshift
;
266 cmask
= ptr
[this_unit
];
269 v
|= (uint64_t) cmask
;
273 for (; this_unit
>= start_unit
+ 1; this_unit
--) {
275 v
|= (uint64_t) ptr
[this_unit
];
279 cmask
= ptr
[this_unit
] >> (start
% ts
);
280 v
<<= ts
- (start
% ts
);
281 v
|= (uint64_t) cmask
;
284 v
|= (uint64_t) ptr
[this_unit
];
288 this_unit
= start_unit
;
290 if (ptr
[this_unit
] & (1U << (ts
- (start
% ts
) - 1)))
293 if (start_unit
== end_unit
- 1) {
294 mask
= (~(unsigned char) 0) << (ts
- (start
% ts
));
295 mask
|= ~((~(unsigned char) 0) << ((ts
- (end
% ts
)) % ts
));
296 cmask
= ptr
[this_unit
];
298 cmask
>>= (ts
- (end
% ts
)) % ts
;
300 v
|= (uint64_t) cmask
;
304 mask
= (~(unsigned char) 0) << (ts
- (start
% ts
));
305 cmask
= ptr
[this_unit
];
307 v
<<= ts
- (start
% ts
);
308 v
|= (uint64_t) cmask
;
309 start
+= ts
- (start
% ts
);
312 for (; this_unit
< end_unit
- 1; this_unit
++) {
314 v
|= (uint64_t) ptr
[this_unit
];
318 cmask
= ptr
[this_unit
];
319 cmask
>>= (ts
- (end
% ts
)) % ts
;
321 v
|= (uint64_t) cmask
;
324 v
|= (uint64_t) ptr
[this_unit
];
328 assert(0); /* TODO: support PDP_ENDIAN */
334 uint64_t ctf_bitfield_unsigned_read(const uint8_t *ptr
,
335 unsigned long start
, unsigned long len
,
338 return (uint64_t) _ctf_bitfield_read_64(ptr
, start
, len
, byte_order
, 0);
342 int64_t ctf_bitfield_signed_read(const uint8_t *ptr
,
343 unsigned long start
, unsigned long len
,
346 return (int64_t) _ctf_bitfield_read_64(ptr
, start
, len
, byte_order
, 1);
349 #endif /* _CTF_BITFIELD_H */
This page took 0.035636 seconds and 4 git commands to generate.