From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 692183EDAD8 for ; Wed, 21 Jan 2026 10:10:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768990232; cv=none; b=XdZHZ4mMjxvhB/XKbWTI07S/NPe8Tv51zb3CurKs4+hpfH4orZ54zDQq5FV1JjpfswzIi9gibps2bqINXa5OQt4bT+PSCZy2z9FCAGbkeB40LWTwuTz5UgMjzFUyuXDwkaGPuGohnYbeFtvDHGgLPn68/qUl23q2/ZHO68EStN8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768990232; c=relaxed/simple; bh=jOYqjIeiIw1YTA5eQEBufSqdLY2oCyAkCwH8FtItF6Q=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oHwzwq8+ciYG9OeQZCzmiVSupH6Jp9sxWkgS9A56wNcoKJaF50Vu8wEPVCyzE90P4p+hHGhwaY2HexVRjO3m3cfZpTmqPv53af95fhgJU31g2e6gE9EvNU4KtF+FB9OZJuG1h9GUY8qZ4n8yZj6AiqAezTD/4Hi+Do8TBdgUORg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=c7bLGcei; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="c7bLGcei" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-430fcb6b2ebso5101122f8f.2 for ; Wed, 21 Jan 2026 02:10:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1768990229; x=1769595029; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=vjjEQotnj2Cyv4eb296qwZWd2dvM6FaXMgU0Htmcu9Y=; b=c7bLGcei1ZZ+s9GOm+DslogoKKDIxFdyNKC9faeclzm2//ZEfXId/BRT6Am3SWW0V8 8hk9NJ1udPvC9LaHrl6zBKKyWz+dnRKLHRjNGjeUCC8tAtMgRB/R1+Er5OjfbdqBwanl YqqoeOzDRN31in7Fc87gKdn0TTfbsjHyOwA9Tixs5ieBuY7Pa3KRAHCLwf3jAO5xV+RK zAqZwEdrcYaLoVh/aiM3Ixg5CHW7gic7hatijvsNPwvAVasVfM7VbKH5btjDqCKW0WL7 +KyDmih8lZ409v1cd/h/CWpBsI6hVPSXmMTL7IS+C9oDOFMGGoWX5kWJqPoNRUWsBJnA z5Xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768990229; x=1769595029; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=vjjEQotnj2Cyv4eb296qwZWd2dvM6FaXMgU0Htmcu9Y=; b=v86k0BDWimhO4FAy53+dC1p+QnwsD2MxqcO3Mdd5Qi1QAjQ5CSIgI4nXVxWcrEZKkm GS48I8l1Ekp/tlrTibHzHy6xTuuEua6SRK80nV0sXLMhDXaOojkfx/ZkV6rE9hvkm2UJ xOj7f2bxLLK+773bf8vE/+ccIKUP7dTzJ9sA355otX/nDdzQ/knlPRDX5INN8Ds4hn7Z 0qzQuTTCb1N/TLFiU0ZlQfy0w4gHvxxF89PfQft55B7SSYDUg24xc/NtTxBhETLOGuSq 2XllTluYDpn5Jwk5+1Vtz0SZbtT9v8Fk3I7IBEqmtfhETmK343vCHRPAudd1DbUYJ13Y iDAw== X-Forwarded-Encrypted: i=1; AJvYcCVCvnA2dGgqCLbO+qQNoJFCbdm9g6JdGO84Pc2etl+leVfBG2oE4vYp4sc67AgZNRqncH8bXLWr97bBsaE=@vger.kernel.org X-Gm-Message-State: AOJu0Yza4e1MrtjP8HoB5pG9lFVirI4YuYQfsN3CyMNhvnLHXmo3gwsT HEWOYYx6aV9fGXWt3wmvtRcdrXWwMCiaBUfuQYgC5H/vpJgPJBYkO4eBWzUR/Blu6GERMbOsT9J 5rsnR4s6s9+C1cBCfHA== X-Received: from wmor14.prod.google.com ([2002:a05:600c:458e:b0:477:a678:a39a]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3d8b:b0:479:2a09:9262 with SMTP id 5b1f17b1804b1-4801e30b49emr196650895e9.9.1768990228838; Wed, 21 Jan 2026 02:10:28 -0800 (PST) Date: Wed, 21 Jan 2026 10:10:28 +0000 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260120-register-v1-0-723a1743b557@nvidia.com> <20260120-register-v1-1-723a1743b557@nvidia.com> Message-ID: Subject: Re: [PATCH 1/6] rust: num: add `shr` and `shl` methods to `Bounded` From: Alice Ryhl To: Yury Norov Cc: Alexandre Courbot , Miguel Ojeda , Boqun Feng , Gary Guo , "=?utf-8?B?QmrDtnJu?= Roy Baron" , Benno Lossin , Andreas Hindborg , Trevor Gross , Danilo Krummrich , Yury Norov , John Hubbard , Alistair Popple , Joel Fernandes , Timur Tabi , Edwin Peer , Eliot Courtney , Daniel Almeida , Dirk Behme , Steven Price , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" On Wed, Jan 21, 2026 at 03:15:49AM -0500, Yury Norov wrote: > On Tue, Jan 20, 2026 at 03:17:54PM +0900, Alexandre Courbot wrote: > > Shifting a `Bounded` left or right changes the number of bits required > > to represent the value. Add methods that perform the shift and return a > > `Bounded` with the appropriately adjusted bit width. > > > > These methods are particularly useful for bitfield extraction. > > > > Suggested-by: Alice Ryhl > > Signed-off-by: Alexandre Courbot > > --- > > rust/kernel/num/bounded.rs | 40 ++++++++++++++++++++++++++++++++++++++++ > > 1 file changed, 40 insertions(+) > > > > diff --git a/rust/kernel/num/bounded.rs b/rust/kernel/num/bounded.rs > > index 5ef8361cf5d5..6e3f4a7a5262 100644 > > --- a/rust/kernel/num/bounded.rs > > +++ b/rust/kernel/num/bounded.rs > > @@ -475,6 +475,46 @@ pub fn cast(self) -> Bounded > > // `N` bits, and with the same signedness. > > unsafe { Bounded::__new(value) } > > } > > + > > + /// Right-shifts `self` by `SHIFT` and returns the result as a `Bounded<_, { N - SHIFT }>`. > > + /// > > + /// # Examples > > + /// > > + /// ``` > > + /// use kernel::num::Bounded; > > + /// > > + /// let v = Bounded::::new::<0xff00>(); > > + /// let v_shifted: Bounded:: = v.shr::<8, _>(); > > This syntax is really confusing. Does it work for runtime shifts? > Is there any chance to make it just v.shr(8)? The :: syntax is how you pass a build-time constant value in Rust. The syntax 'v.shr(8)' implies that 8 is a runtime value, and not a build-time constant. I agree it's ugly, but that's how it is. For runtime shifts, we can make it 'v.shr(8)' or even 'v >> 8' using operator overloading, but see my further reply below: > > + } > > + > > + /// Left-shifts `self` by `SHIFT` and returns the result as a `Bounded<_, { N + SHIFT }>`. > > + /// > > + /// # Examples > > + /// > > + /// ``` > > + /// use kernel::num::Bounded; > > + /// > > + /// let v = Bounded::::new::<0xff>(); > > + /// let v_shifted: Bounded:: = v.shl::<8, _>(); > > + /// > > + /// assert_eq!(v_shifted.get(), 0xff00); > > + /// ``` > > + pub fn shl(self) -> Bounded { > > + const { assert!(RES == N + SHIFT) } > > + > > + // SAFETY: we shift the value left by `SHIFT`, augmenting the number of bits needed to > > + // represent the shifted value by as much, and just asserted that `RES == N + SHIFT`. > > + unsafe { Bounded::__new(self.0 << SHIFT) } > > + } > > So, it protects most significant bits when shifting left, but doesn't > protect least significant bits when shifting right. > > It also makes impossible to left-shift Bounded:: at all, or > shift Bounded:: for 2 or more bits. This doesn't look nice. > > At this layer, there's seemingly nothing wrong to loose bits during > regular shift, just like non-bounded integers do. (Lets consider them > naturally bounded.) At higher layers, people may add any extra checks > as desired. > > Even more, you mention you're going to use .shl and .shr for bitfield > extraction, which means you want to loose some bits intentionally. > > Let's design shifts like this. Plain .shl() and .shr() will operate on > bounded integers just like '<<' and '>>' operate on non-bounded ones, > i.e. they may loose bits and the result has the same bit capacity. (But > shifting over the capacity should be be forbidden as undef). > > If I want to adjust the capacity, I just do it explicitly: > > let x = Bounded::::new::<0x123>(); > let a = x.shl(4); // 12-bit 0x230 > let b = x.extend::<12+4>().shl(4) // 16-bit 0x1230 > let c = x.shr(4); // 12-bit 0x012 > let d = if x & 0xf { None } else { x.shr(4).try_shrink::<12-4>() } > > For b and d you can invent handy helpers, of course, and for a and c > you can add 'safe' versions that will check shifted-out parts for > emptiness at runtime, in case you need it. This 'shr' method came up from a discussion that Alexandre and I had at plumbers. The entire point of them is that when you shift right by a compile-time constant amount, you can change the bit capacity in the type, and doing exactly that lets us remove BUILD_BUG_ON calls that Nova has right now. (Right now Nova does a shift-right by a compile-time constant, then uses BUILD_BUG_ON to assert that the resulting *runtime value* has the desired target bit-capacity.) So if we want a shift right method that doesn't change the bit-capacity, we can do that. It can even use the ordinary '>>' operator. But the purpose of this patch is precisely to support shifts that: 1. Shift by a compile-time constant amount. 2. Change the bit-capacity accordingly. All that to say ... support for left-shifting Bounded:: and similar certainly makes sense as something we could support, but that's not what this patch is trying to achieve. Ultimately, the end-goal of this patch is to allow us to get rid of BUILD_BUG_ON() calls operating on runtime values, while still performing the same check that the bit-capacity is as expected at build-time - just using types instead of compiler optimizations to check it. Alice