Debian Codegraph @

debian/rust-vendor/bitvec/doc/field.md

raw
1 # Bit-Field Memory Slots
2
3 This module implements a load/store protocol for [`BitSlice`] regions that
4 enables them to act as if they were a storage slot for integers. Implementations
5 of the [`BitField`] trait provide behavior similar to C and C++ language
6 bit-fields. While any `BitSlice<T, O>` instantiation is able to provide this
7 behavior, the lack of specialization in the language means that it is instead
8 only implemented for `BitSlice<T, Lsb0>` and `BitSlice<T, Msb0>` in order to
9 gain a performance advantage.
10
11 ## Batched Behavior
12
13 Bit-field behavior can be simulated using `BitSlice`’s existing APIs; however,
14 the inherent methods are all required to operate on each bit individually in
15 sequence. In addition to the semantic load/store behavior this module describes,
16 it also implements it in a way that takes advantage of the contiguity properties
17 of the `Lsb0` and `Msb0` orderings in order to maximize how many bits are
18 transferred in each cycle of the overall operation.
19
20 This is most efficient when using `BitSlice<usize, O>` as the storage bit-slice,
21 or using `.load::<usize>()` or `.store::<usize>()` as the transfer type.
22
23 ## Bit-Slice Storage and Integer Value Relationships
24
25 `BitField` permits any type of integer, *including signed integers*, to be
26 stored into or loaded out of a `BitSlice<T, _>` with any storage type `T`. While
27 the examples in this module will largely use `u8`, just to keep the text
28 concise, `BitField` is tested, and will work correctly, for any combination of
29 types.
30
31 `BitField` implementations use the processor’s own concept of integer registers
32 to operate. As such, the byte-wise memory access patters for types wider than
33 `u8` depends on your processor’s byte endianness, as well as which `BitField`
34 method, and which [`BitOrder`] type parameter, you are using.
35
36 `BitField` only operates within processor registers; traffic of `T` elements
37 between the memory bank and the processor register is controlled entirely by the
38 processor.
39
40 If you do not want to introduce the processor’s byte endianness as a variable
41 that affects the in-memory representation of stored integers, use
42 `BitSlice<u8, _>` as the bit-field storage type. In particular,
43 `BitSlice<u8, Msb0>` will fill memory in a way that intuitively matches what
44 most debuggers show when inspecting memory.
45
46 On the other hand, if you do not care about memory representation and just need
47 fast storage of less than an entire integer, `BitSlice<Lsb0, usize>` is likely
48 your best bet. As always, the choice of type parameters is a trade-off with
49 different advantages for each combination, which is why `bitvec` refuses to make
50 the choice for you.
51
52 ### Signed Behavior
53
54 The length of the `BitSlice` that stores a value is considered to be the width
55 of that value when it is loaded back out. As such, storing an `i16` into a
56 bit-slice of length `12` means that the stored value has type `i12`.
57
58 When calling `.load::<i16>()` on a 12-bit slice, the load will detect the sign
59 bit of the `i12` value and sign-extend it to `i16`. This means that storing
60 `2048i16` into a 12-bit slice and then loading it back out into an `i16` will
61 produce `-2048i16` (negative), not `2048i16` (positive), because `1 << 11` is
62 the sign bit.
63
64 `BitField` **does not** record the true sign bit of an integer being stored, and
65 will not attempt to set the sign bit of the narrowed value in storage. Storing
66 `-127i8` (`0b1000_0001`) into a 7-bit slice will load `1i8`.
67
68 ## Register Bit Order Preservation
69
70 The implementations in this module assume that the bits within a *value* being
71 transferred into or out of a bit-slice should not be re-ordered. While the
72 implementations will segment a value in order to make it fit into bit-slice
73 storage, and will order those *segments* in memory according to their type
74 parameter and specific trait method called, each segment will remain
75 individually unmodified.
76
77 If we consider the value `0b100_1011`, segmented at the underscore, then the
78 segments `0b100` and `0b1011` will be present somewhere in the bit-slice that
79 stores them. They may be shifted within an element or re-ordered across
80 elements, but each segment will not be changed.
81
82 ## Endianness
83
84 `bitvec` uses the `BitOrder` trait to describe the order of bits within a single
85 memory element. This ordering is independent of, and does not consider, the
86 ordering of memory elements in a sequence; `bitvec` is always “little-endian” in
87 this regard: lower indices are in lower memory addresses, higher indices are in
88 higher memory addresses.
89
90 However, `BitField` is *explicitly* aware of multiple storage elements in
91 sequence. It is by design able to allow combinations such as
92 `<BitSlice<u8, Lsb0> as BitField>::store_be::<u32>`. Even where the storage and
93 value types are the same, or the value is narrower, the bit-slice may be spread
94 across multiple elements and must segment the value across them.
95
96 The `_be` and `_le` orderings on `BitField` method names refer to the numeric
97 significance of *bit-slice storage elements*.
98
99 In `_be` methods, lower-address storage elements will hold more-significant
100 segments of the value, and higher-address storage will hold less-significant.
101
102 In `_le` methods, lower-address storage elements will hold *less*-significant
103 segments of the value, and higher-address storage will hold *more*-significant.
104
105 Consider again the value `0b100_1011`, segmented at the underscore. When used
106 with `.store_be()`, it will be placed into memory as `[0b…100…, 0b…1011…]`; when
107 used with `.store_le()`, it will be placed into memory as `[0b…1011…, 0b…100…]`.
108
109 ## Bit-Ordering Behaviors
110
111 The `_be` and `_le` suffices select the ordering of storage elements in memory.
112 The other critical aspect of the `BitField` memory behavior is selecting
113 *which bits* in a storage element are used when a bit-slice has partial
114 elements.
115
116 When `BitSlice<_, Lsb0>` produces a [`Domain::Region`], its `head` is in the
117 most-significant bits of its element and its `tail` is in the least-significant
118 bits. When `BitSlice<_, Msb0>` produces a `Region`, its `head` is in the
119 *least*-significant bits, and its `tail` is in the *most*-significant bits.
120
121 You can therefore use these combinations of `BitOrder` type parameter and
122 `BitField` method suffix to select exactly the memory behavior you want for a
123 storage region.
124
125 Each implementation of `BitField` has documentation showing exactly what its
126 memory layout looks like, with code examples and visual inspections of memory.
127 This documentation is likely collapsed by default when viewing the trait docs;
128 be sure to use the `[+]` button to expand it!
129
130 [`BitField`]: self::BitField
131 [`BitOrder`]: crate::order::BitOrder
132 [`BitSlice`]: crate::slice::BitSlice
133 [`Domain::Region`]: crate::domain::Domain::Region