From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) (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 82D9C27A462 for ; Mon, 16 Feb 2026 08:55:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771232120; cv=none; b=Jy+yuBHU8mccAUqaXMrsm//SB1j3Kk6RzWQB0miqkm52CBwVxikmnHnBsSD531JQb5fEuVrs/tE6XGJE5k8lcI+SapdtK2tTT16QEUFhf7f2fcKQ+svLrTULsgkKRQ3i4Qd1pjYznonORP5nK96XbKMZjvRyEmxAeKbotXf1Cw4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771232120; c=relaxed/simple; bh=G0NPfAeu8frTb2xM/sLVi66ElRp5GdIAOsIFOBjaDAo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ezJJdO650SQWaQ8e+CJYFo7OmLdLYw4pAgMoEdT7lA22PWkmdJNy6l2vahK/osr7ghwmz/sDNPnWLU//hnvtLoiiUBOUEnm0dYmBY4e/IHyV5Jhqxn9YFi/a7HajKPszQkZfSEf53vKkB54SW/wM3LkM0yD3IM24ilb2A9n7lwM= 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=Je9P4tRo; arc=none smtp.client-ip=209.85.218.73 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="Je9P4tRo" Received: by mail-ej1-f73.google.com with SMTP id a640c23a62f3a-b8f848ebcbbso213360766b.0 for ; Mon, 16 Feb 2026 00:55:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771232118; x=1771836918; 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=iUSN/imqpTffN3xHSkVDCNUkts/L74YOZ3w3u+fOV08=; b=Je9P4tRoyi5xLmP138Fi9B8DI//tVj4s12Haup+QR1vmk0TUPUcKa6/jUa05HfU1wb mZrgJJAanAFBaXdmeOWPKxvUih71G9F85vtaYzuoNu6nwanW6lNMXYpLyu0D6mNlPQan xgbJ9+um/e6bmxZpzU5k1yxC0MsEUZV0JXggKwENRRa9alRvgwMi+i1Gz2FMKCwnDWOL IzM+PQkWkIBEW/K829TKb7YXv1zjkaK2ADaKj9/+5Hts31hKRg4IFc4gtUs+kc9IK2XB 5P3eujm1jc4uv3LoLKaiCBWpyke33G6Td/ilUUmaT6h2pe3R9swQrnsHVArCChdqb0yk G5CA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771232118; x=1771836918; 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=iUSN/imqpTffN3xHSkVDCNUkts/L74YOZ3w3u+fOV08=; b=Xj7VhqqVnO9Z4plOxGlPypPcn6ilIMkMVMtLn2KqVPTPgWTCWSdII9XiIkkN8s6qMp QA4oDZIB1CsTD8r7eXItE2EXYVpsBRn4nuXVe1C8q6WXiSq4q6BWs1wfmz2rPVhMZ+fC yRQIh3XzYX9J8cvKAAyjre38dPlb5QxyXhnpTJ709TsRuvRL2T1mkhP9AhJOPZYqyzpM MF25SYjYeFpuDpEOTEaT5dwHWXX+Ph69kfo4KmBgVPxwQyj+P1sCYXTQLwRvswwxB9+p vYJ+VulzBD9UHMKlfhAlyBU+xA9Dz4f080Oo6iEZfmiqhHT9RtGlBfNm9MeKwdPiaF4T g0aw== X-Forwarded-Encrypted: i=1; AJvYcCVhO09zGSF0dngcpJJnr7kGP7wApmtYl/VFj3qBf/8E1TgWww4ZKgjVhb+CWR5T2pa7qZBnZtn3kNqHd5g=@vger.kernel.org X-Gm-Message-State: AOJu0YwKCAR+jXC8Eu/BF3qNRwFP/D7FvkzKEAfPuLHHE88mzHJfekWn Zn1+tJgIdfoKiwCF/yepV1TdXJQZIUxcSSQbsEMHdk9wSC+LRyWO2ANJGmVr+NoeF2HUpE6lRgn ONLlgjN1/nhDzltA/1Q== X-Received: from ejcgx19.prod.google.com ([2002:a17:906:8a53:b0:b80:292:d4f7]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:906:208f:b0:b8f:deff:a019 with SMTP id a640c23a62f3a-b8fdeffc44amr129833766b.0.1771232117755; Mon, 16 Feb 2026 00:55:17 -0800 (PST) Date: Mon, 16 Feb 2026 08:55:16 +0000 In-Reply-To: <20260216-register-v6-2-eec9a4de9e9e@nvidia.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260216-register-v6-0-eec9a4de9e9e@nvidia.com> <20260216-register-v6-2-eec9a4de9e9e@nvidia.com> Message-ID: Subject: Re: [PATCH v6 2/9] rust: num: add `shr` and `shl` methods to `Bounded` From: Alice Ryhl To: Alexandre Courbot Cc: Danilo Krummrich , Daniel Almeida , Miguel Ojeda , Gary Guo , "=?utf-8?B?QmrDtnJu?= Roy Baron" , Benno Lossin , Andreas Hindborg , Trevor Gross , Boqun Feng , Yury Norov , John Hubbard , Alistair Popple , Joel Fernandes , Timur Tabi , Edwin Peer , Eliot Courtney , Dirk Behme , Steven Price , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" On Mon, Feb 16, 2026 at 05:04:38PM +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 > Reviewed-by: Alice Ryhl > Reviewed-by: Gary Guo > Reviewed-by: Daniel Almeida > Tested-by: Dirk Behme > Acked-by: Miguel Ojeda > Signed-off-by: Alexandre Courbot > --- > rust/kernel/num/bounded.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 42 insertions(+) > > diff --git a/rust/kernel/num/bounded.rs b/rust/kernel/num/bounded.rs > index fa81acbdc8c2..7014b5be66db 100644 > --- a/rust/kernel/num/bounded.rs > +++ b/rust/kernel/num/bounded.rs > @@ -473,6 +473,48 @@ 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<_, RES>`, where `RES >= > + /// N - SHIFT`. > + /// > + /// # Examples > + /// > + /// ``` > + /// use kernel::num::Bounded; > + /// > + /// let v = Bounded::::new::<0xff00>(); > + /// let v_shifted: Bounded:: = v.shr::<8, _>(); > + /// > + /// assert_eq!(v_shifted.get(), 0xff); > + /// ``` > + pub fn shr(self) -> Bounded { > + const { assert!(RES >= N - SHIFT) } Perhaps slightly better as: const { assert!(RES + SHIFT >= N) } Alice