| 1 |
# Bit-Slice Specialization |
| 2 |
|
| 3 |
This module provides specialized implementations for `BitSlice<T, Lsb0>` and |
| 4 |
`BitSlice<T, Msb0>`. These implementations are able to use knowledge of their |
| 5 |
bit-ordering behavior to be faster and operate in batches. |
| 6 |
|
| 7 |
Since true specialization is not available in the language yet, this uses the |
| 8 |
`any::TypeId` system to detect if a type parameter is identical to a known type |
| 9 |
and conditionally force a cast and branch. Since type identifiers are compiler |
| 10 |
intrinsics produced during compilation, during monomorphization each branch |
| 11 |
has its conditional replaced with a compile-time constant value. The `if true` |
| 12 |
branch is retained, the `if false` branches are discarded, and the |
| 13 |
monomorphization proceeds with the specialized function replacing the generic |
| 14 |
body. |
| 15 |
|
| 16 |
The `.coerce()` and `.coerce_mut()` methods detect whether a bit-slice with |
| 17 |
generic type parameters matches statically-known type parameters, and return an |
| 18 |
`Option` of a value-identical bit-slice reference with the statically-known type |
| 19 |
parameters which can then invoke a specialization method. |
| 20 |
|
| 21 |
Generic methods can be specialized whenever their implementation is dependent on |
| 22 |
the `O` type parameter and the map of positions the ordering produces is |
| 23 |
easily legible to processor instructions. Because language-level specialization |
| 24 |
is unavailable, dispatch is only done in `bitvec` and cannot be extended to |
| 25 |
third-party crates. |
| 26 |
|
| 27 |
The `lsb0` and `msb0` modules should have identical symbols present. For |
| 28 |
implementation, remember that `Lsb0` and `Msb0` orderings **are** correlated |
| 29 |
with little-endian and big-endian byte operations! |