Message ID | 20240130-rust-bit-v1-1-ed2ed6389a58@christina-quast.de |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-45225-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp1460958dyb; Tue, 30 Jan 2024 11:55:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IHucztIuyj4f00WZeIcBg2ZpwpV2HOY9RpO2JDNGaxP2F7BIgV2zhU3lUToLCwCFfH69n7i X-Received: by 2002:a05:6a00:1798:b0:6db:d5b5:e380 with SMTP id s24-20020a056a00179800b006dbd5b5e380mr7575403pfg.24.1706644511832; Tue, 30 Jan 2024 11:55:11 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706644511; cv=pass; d=google.com; s=arc-20160816; b=Q906iEnXUcbyKys2zGfRibMEBWtATXGQ1KoLtHznn4AwVKq/2Sw9deqpiGtH27/fYn lNQZFQHhGp1+YdAX++gJRiGp37HsgFfQNICOmcxIe7VjktIQUU5lpKezD6zS6xRDlASS yLo82F2KOZzXzVc7dBDZxwc+klh/MBmqir8PXWy2ub3eBh810sX4KAgSNXNxitsb44xy uyH/S+jQ4dyLopFOIDlwCbbkHyKaTKvV5q0s8C3Epq1A+j9XghIV+lXoFGavBhHbvBzm Ne0o8uNq/hyO2G7+m1BxVlkgGSBQQRZs3DH1eWa+97HbQzDx8BNFlLLto79Vzjl9+28T LgSA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:message-id:content-transfer-encoding:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:subject:date :from; bh=0ADVgV0W2qr4/2M8YAKPNuJ/PmdJI1oDioLd2H2CEhs=; fh=hUsCs6AAE1rR6UueQu8Ky7Vz1eqbksdHYaZcO+jXHjI=; b=F5LToZ9hU966tpcfvZcMjqXOZY5fgsRF3Ri9acAzDue8zb7cJIXGcj0qXUtqeQfGVF dBbjlRUcotZ0zKnkGvfor5YgT48N9mzUB2kMDMbsZQYyhC/9QIcxUzoEunG34c0MAQ0Z 5QYXuCDVpZBDuUpY7a2cRlDcJVBfH17FI4lS1BK2+B1Q6vpOEslMfgTq+oBCoK/lCCzv kQyd4njHPrR+PpDjdRxNo/ok09MNRrEMoykLE1pX45PgzyI+zfkfFSX2fQUTmWwTCZ7g 9RSAd3Hu7I7plGwo33vHN7s5/8wZSsNuHa+/gS7sSsHZ5fQ8JOJG3drptWvLscISTBM/ BQAw== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=christina-quast.de); spf=pass (google.com: domain of linux-kernel+bounces-45225-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-45225-ouuuleilei=gmail.com@vger.kernel.org" Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id du2-20020a056a002b4200b006dadc44566bsi7954567pfb.58.2024.01.30.11.55.11 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 11:55:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-45225-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=christina-quast.de); spf=pass (google.com: domain of linux-kernel+bounces-45225-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-45225-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 90E8D28816B for <ouuuleilei@gmail.com>; Tue, 30 Jan 2024 19:55:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 69DD371B45; Tue, 30 Jan 2024 19:54:51 +0000 (UTC) Received: from mail.someserver.de (mail.someserver.de [116.202.193.223]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CAB4869D36; Tue, 30 Jan 2024 19:54:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=116.202.193.223 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706644489; cv=none; b=SZrcNhMEbtnW3wQkC+WCFuHDDo7PkyoC0e2eRu7q0AUyliwd49mTqz4JQy/3nlVTIUhWjbWPCH7nFpyCB+5Z7SGTUAmCTHUt9kuGcNvlN2PftyC3DPcbjS/rHAvIF67YtVBLLEYdpcF3z/eFzVWDde9D0OGIM7MToTYcMCYW8bk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706644489; c=relaxed/simple; bh=xgl3MC3s1y+/xuGn3VPAMSHRrzUBbY9DHF1bmTq1orY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=C94QUUE5DzNl4R2tzPv6/+R5lzdJelKEGFKFod5QIj4m1kABJ0hz0TBcG1vtcISMwcNPBzvNRJb+07/DOehgj1jFZOBU5GtDjVG9e0URGHHWVUhON95e8OBbBLZ2zGq7uLslOps/AAFV7wgWuF9mQ+s4o6zYzn2+FDlv7CsTIxw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=christina-quast.de; spf=pass smtp.mailfrom=christina-quast.de; arc=none smtp.client-ip=116.202.193.223 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=christina-quast.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=christina-quast.de Received: from localhost (unknown [195.162.191.218]) by mail.someserver.de (Postfix) with ESMTPSA id AC348A2194; Tue, 30 Jan 2024 20:48:00 +0100 (CET) From: Christina Quast <contact@christina-quast.de> Date: Tue, 30 Jan 2024 20:47:51 +0100 Subject: [PATCH] rust: prelude: add bit function Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20240130-rust-bit-v1-1-ed2ed6389a58@christina-quast.de> X-B4-Tracking: v=1; b=H4sIAGZSuWUC/6tWKk4tykwtVrJSqFYqSi3LLM7MzwNyDHUUlJIzE vPSU3UzU4B8JSMDIxMDQ2MD3aLS4hLdpMwSXUvLlBSzFONEA1NjMyWg8oKi1LTMCrBR0bG1tQD yELu2WgAAAA== To: Miguel Ojeda <ojeda@kernel.org>, Alex Gaynor <alex.gaynor@gmail.com>, Wedson Almeida Filho <wedsonaf@gmail.com>, Boqun Feng <boqun.feng@gmail.com>, Gary Guo <gary@garyguo.net>, =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= <bjorn3_gh@protonmail.com>, Benno Lossin <benno.lossin@proton.me>, Andreas Hindborg <a.hindborg@samsung.com>, Alice Ryhl <aliceryhl@google.com> Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Christina Quast <contact@christina-quast.de> X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1706644076; l=1210; i=contact@christina-quast.de; s=20240130; h=from:subject:message-id; bh=xgl3MC3s1y+/xuGn3VPAMSHRrzUBbY9DHF1bmTq1orY=; b=IHKGeUwSlFiuDYNIGEj+pe7lXU8/JC4z+PlzeygWkPNGUN6EbHVPeknbOQfa0VAhLSpY85+Pq jeN5dZXFEzADtKZ73EE41EokaLOnYmLWM/7L6Fez0t3E5YP0i9LR9MK X-Developer-Key: i=contact@christina-quast.de; a=ed25519; pk=aoQfinjbnr265vCkIZdYteLDcmIqLBhY1m74WfFUU9E= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789546475580402759 X-GMAIL-MSGID: 1789546475580402759 |
Series |
rust: prelude: add bit function
|
|
Commit Message
Christina Quast
Jan. 30, 2024, 7:47 p.m. UTC
In order to create masks easily, the define BIT() is used in C code.
This commit adds the same functionality to the rust kernel.
To use it, include the following into your rust file:
use kernel::prelude::bit
Signed-off-by: Christina Quast <contact@christina-quast.de>
---
This patch is needed for further patches porting the rockchip phy
driver to rust.
---
rust/kernel/prelude.rs | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
---
base-commit: 6613476e225e090cc9aad49be7fa504e290dd33d
change-id: 20240130-rust-bit-99dd6d3a0536
Best regards,
Comments
On 1/30/24 13:47, Christina Quast wrote: > In order to create masks easily, the define BIT() is used in C code. > This commit adds the same functionality to the rust kernel. > > To use it, include the following into your rust file: > use kernel::prelude::bit > > Signed-off-by: Christina Quast <contact@christina-quast.de> Reviewed-by: Carlos Bilbao <carlos.bilbao@amd.com> > --- > This patch is needed for further patches porting the rockchip phy > driver to rust. > --- > rust/kernel/prelude.rs | 16 ++++++++++++++++ > 1 file changed, 16 insertions(+) > > diff --git a/rust/kernel/prelude.rs b/rust/kernel/prelude.rs > index ae21600970b3..16e483de2f27 100644 > --- a/rust/kernel/prelude.rs > +++ b/rust/kernel/prelude.rs > @@ -38,3 +38,19 @@ > pub use super::init::{InPlaceInit, Init, PinInit}; > > pub use super::current; > + > +/// Returns a `u32` number that has only the `n`th bit set. > +/// > +/// # Arguments > +/// > +/// * `n` - A `u32` that specifies the bit position (zero-based index) > +/// > +/// # Example > +/// > +/// ``` > +/// let b = bit(2); > +/// assert_eq!(b, 4); > +#[inline] > +pub const fn bit(n: u32) -> u32 { > + 1 << n > +} > > --- > base-commit: 6613476e225e090cc9aad49be7fa504e290dd33d > change-id: 20240130-rust-bit-99dd6d3a0536 > > Best regards,
On Tue, 30 Jan 2024 at 16:48, Christina Quast <contact@christina-quast.de> wrote: > > In order to create masks easily, the define BIT() is used in C code. > This commit adds the same functionality to the rust kernel. The `rust` branch has a generic `bit` function that allows the returned bit to be used with or converted to several numeric types. https://github.com/Rust-for-Linux/linux/blob/rust/rust/kernel/types.rs#L344
On Tue, Jan 30, 2024 at 05:04:15PM -0300, Wedson Almeida Filho wrote: > On Tue, 30 Jan 2024 at 16:48, Christina Quast > <contact@christina-quast.de> wrote: > > > > In order to create masks easily, the define BIT() is used in C code. > > This commit adds the same functionality to the rust kernel. > > The `rust` branch has a generic `bit` function that allows the > returned bit to be used with or converted to several numeric types. > > https://github.com/Rust-for-Linux/linux/blob/rust/rust/kernel/types.rs#L344 Plus, I think it's better to put the implementation at somewhere like: kernel::type::bitops Of course, it's OK to prelude it, but we shouldn't put implementation in prelude.rs Regards, Boqun
Hi Christina, Thanks for the patch! Please see below. On Tue, Jan 30, 2024 at 8:48 PM Christina Quast <contact@christina-quast.de> wrote: > > In order to create masks easily, the define BIT() is used in C code. > This commit adds the same functionality to the rust kernel. > > To use it, include the following into your rust file: > use kernel::prelude::bit This is the prelude, i.e. the point is that it does not need to be `use`d. Did you check the `rust` branch? We had something like this there. > diff --git a/rust/kernel/prelude.rs b/rust/kernel/prelude.rs > index ae21600970b3..16e483de2f27 100644 > --- a/rust/kernel/prelude.rs > +++ b/rust/kernel/prelude.rs Please note that the prelude is meant to re-export existing functionality elsewhere in the `kernel` crate, not to define items there. > +/// # Arguments > +/// > +/// * `n` - A `u32` that specifies the bit position (zero-based index) We don't use "Arguments"-like sections. Please see other functions' documentation to see how we usually do it. > +/// # Example Please use the plural. > +/// ``` > +/// let b = bit(2); > +/// assert_eq!(b, 4); The example is not closed. > +#[inline] > +pub const fn bit(n: u32) -> u32 { > + 1 << n > +} Should it be a generic? Cheers, Miguel
diff --git a/rust/kernel/prelude.rs b/rust/kernel/prelude.rs index ae21600970b3..16e483de2f27 100644 --- a/rust/kernel/prelude.rs +++ b/rust/kernel/prelude.rs @@ -38,3 +38,19 @@ pub use super::init::{InPlaceInit, Init, PinInit}; pub use super::current; + +/// Returns a `u32` number that has only the `n`th bit set. +/// +/// # Arguments +/// +/// * `n` - A `u32` that specifies the bit position (zero-based index) +/// +/// # Example +/// +/// ``` +/// let b = bit(2); +/// assert_eq!(b, 4); +#[inline] +pub const fn bit(n: u32) -> u32 { + 1 << n +}