Re: [PATCH v2 04/15] gpu: nova-core: add the GIN vector and subtree newtypes
From: Alexandre Courbot
Date: Tue Sep 01 2026 - 09:50:15 EST
On Sat Aug 29, 2026 at 10:22 AM JST, John Hubbard wrote:
<...>
> +impl GinVector {
> + /// Returns the vector numbered `VECTOR`.
> + ///
> + /// Fails at build time if `VECTOR` lies outside the widest tree any supported part
> + /// implements.
> + pub(super) const fn new<const VECTOR: u32>() -> Self {
> + build_assert!(VECTOR < LeafCount::Sixteen.vector_count());
> +
> + // INVARIANT: `VECTOR` is within the widest supported tree.
> + Self(VECTOR)
> + }
> +
> + /// Returns the vector number.
> + pub(super) const fn into_raw(self) -> u32 {
> + self.0
> + }
> +
> + /// Returns the leaf that carries this vector.
> + pub(super) fn leaf_index(self) -> LeafIndex {
> + // By the type invariant the quotient is already below 16, so the mask changes nothing. It
> + // is what proves the bound to `from_expr`.
> + LeafIndex::from_expr(crate::num::u32_as_usize(self.0 / VECTORS_PER_LEAF) & LEAF_INDEX_MASK)
Noticed this while reviewing patch 7.
Another nice side effect of making `GinVector` a `Bounded<u32, 9>` is
that you can now get rid of this `from_expr` (which relies on the
optimizer-dependent `build_assert!` under the hood) by doing
`self.0.shr::<5, 4>()` (i.e. divide by 32, aka. VECTORS_PER_LEAF) and
now you have... A `Bounded<u32, 4>` that fits perfectly into a
`LeafIndex`!
This method then becomes:
pub(super) fn leaf_index(self) -> LeafIndex {
// CALC: `self.0 / VECTORS_PER_LEAF`.
self.0.shr::<{ VECTORS_PER_LEAF.ilog2() }, _>().cast()
}
`LEAF_INDEX_MASK` is now unused and can be removed.