From: Zhao Liu <zhao1.liu@intel.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: qemu-devel@nongnu.org, qemu-rust@nongnu.org,
Junjie Mao <junjie.mao@hotmail.com>
Subject: Re: [PATCH 06/26] rust: add a bit operation module
Date: Tue, 10 Dec 2024 16:13:20 +0800 [thread overview]
Message-ID: <Z1f4IN3mfYB/jq8G@intel.com> (raw)
In-Reply-To: <20241209123717.99077-7-pbonzini@redhat.com>
On Mon, Dec 09, 2024 at 01:36:57PM +0100, Paolo Bonzini wrote:
> Date: Mon, 9 Dec 2024 13:36:57 +0100
> From: Paolo Bonzini <pbonzini@redhat.com>
> Subject: [PATCH 06/26] rust: add a bit operation module
> X-Mailer: git-send-email 2.47.1
>
> The bindgen supports `static inline` function binding since v0.64.0 as
> an experimental feature (`--wrap-static-fns`), and stabilizes it after
> v0.70.0.
>
> But the oldest version of bindgen supported by QEMU is v0.60.1, so
> there's no way to generate the binding for deposit64() which is `static
> inline` (in include/qemu/bitops.h).
>
> Instead, implement it by hand in Rust and make it available for all
> unsigned types through an IntegerExt trait. Since it only involves bit
> operations, the Rust version of the code is almost identical to the
> original C version, but it applies to more types than just u64.
>
> Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
> Co-authored-by: Paolo Bonzini <pbonzini@redhat.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
> rust/qemu-api/meson.build | 1 +
> rust/qemu-api/src/bitops.rs | 119 +++++++++++++++++++++++++++++++++++
> rust/qemu-api/src/lib.rs | 1 +
> rust/qemu-api/src/prelude.rs | 2 +
> 4 files changed, 123 insertions(+)
> create mode 100644 rust/qemu-api/src/bitops.rs
>
> diff --git a/rust/qemu-api/meson.build b/rust/qemu-api/meson.build
> index b927eb58c8e..adcee661150 100644
> --- a/rust/qemu-api/meson.build
> +++ b/rust/qemu-api/meson.build
> @@ -16,6 +16,7 @@ _qemu_api_rs = static_library(
> [
> 'src/lib.rs',
> 'src/bindings.rs',
> + 'src/bitops.rs',
> 'src/cell.rs',
> 'src/c_str.rs',
> 'src/definitions.rs',
> diff --git a/rust/qemu-api/src/bitops.rs b/rust/qemu-api/src/bitops.rs
> new file mode 100644
> index 00000000000..5acd6642d1a
> --- /dev/null
> +++ b/rust/qemu-api/src/bitops.rs
> @@ -0,0 +1,119 @@
> +// Copyright (C) 2024 Intel Corporation.
> +// Author(s): Zhao Liu <zhai1.liu@intel.com>
You deserve to be the author!
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +
> +//! This module provides bit operation extensions to integer types.
> +//! It is usually included via the `qemu_api` prelude.
> +
> +use std::ops::{
> + Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
> + Mul, MulAssign, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign,
> +};
> +
> +/// Trait for extensions to integer types
> +pub trait IntegerExt:
> + Add<Self, Output = Self> + AddAssign<Self> +
> + BitAnd<Self, Output = Self> + BitAndAssign<Self> +
> + BitOr<Self, Output = Self> + BitOrAssign<Self> +
> + BitXor<Self, Output = Self> + BitXorAssign<Self> +
> + Copy +
> + Div<Self, Output = Self> + DivAssign<Self> +
> + Eq +
> + Mul<Self, Output = Self> + MulAssign<Self> +
> + Not<Output = Self> + Ord + PartialOrd +
> + Rem<Self, Output = Self> + RemAssign<Self> +
> + Shl<Self, Output = Self> + ShlAssign<Self> +
> + Shl<u32, Output = Self> + ShlAssign<u32> + // add more as needed
Ah, I used to define shift bits as usize. I can change the bit shift
type back to u32 in HPET.
> + Shr<Self, Output = Self> + ShrAssign<Self> +
> + Shr<u32, Output = Self> + ShrAssign<u32> // add more as needed
> +{
> + const BITS: u32;
> + const MAX: Self;
> + const MIN: Self;
> + const ONE: Self;
> + const ZERO: Self;
> +
> + #[inline]
> + #[must_use]
> + fn bit(start: u32) -> Self
> + {
> + assert!(start <= Self::BITS);
> +
> + Self::ONE << start
> + }
I think with this helper, I can add activating_bit() and deactivating_bit()
bindings, as they are also commonly used operations.
I will rename them to activate_bit and deactivate_bit, if no one has any
objections.
> + #[inline]
> + #[must_use]
> + fn mask(start: u32, length: u32) -> Self
> + {
> + /* FIXME: Implement a more elegant check with error handling support? */
I think current design is elegant enough, and this FIXME is not needed.
> + assert!(length > 0 && length <= Self::BITS - start);
> +
> + (Self::MAX >> (Self::BITS - length)) << start
> + }
> +
> + #[inline]
> + #[must_use]
> + fn deposit<U: IntegerExt>(self, start: u32, length: u32,
> + fieldval: U) -> Self
> + where Self: From<U>
> + {
> + debug_assert!(length <= U::BITS);
assert? as you've already replaced debug_assert with assert in BqlCell.
> + let mask = Self::mask(start, length);
> + (self & !mask) | ((Self::from(fieldval) << start) & mask)
> + }
> +
Very useful for HPET! Thanks.
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
next prev parent reply other threads:[~2024-12-10 7:56 UTC|newest]
Thread overview: 81+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-09 12:36 [PATCH 00/26] rust: bundle of prerequisites for HPET implementation Paolo Bonzini
2024-12-09 12:36 ` [PATCH 01/26] bql: check that the BQL is not dropped within marked sections Paolo Bonzini
2024-12-09 12:36 ` [PATCH 02/26] rust: cell: add BQL-enforcing Cell variant Paolo Bonzini
2024-12-09 12:36 ` [PATCH 03/26] rust: cell: add BQL-enforcing RefCell variant Paolo Bonzini
2024-12-09 12:36 ` [PATCH 04/26] rust: define prelude Paolo Bonzini
2024-12-10 7:19 ` Zhao Liu
2024-12-09 12:36 ` [PATCH 05/26] rust: add bindings for interrupt sources Paolo Bonzini
2024-12-10 7:25 ` Zhao Liu
2024-12-09 12:36 ` [PATCH 06/26] rust: add a bit operation module Paolo Bonzini
2024-12-10 8:13 ` Zhao Liu [this message]
2024-12-10 9:30 ` Paolo Bonzini
2024-12-09 12:36 ` [PATCH 07/26] rust: qom: add default definitions for ObjectImpl Paolo Bonzini
2024-12-10 8:21 ` Zhao Liu
2024-12-09 12:36 ` [PATCH 08/26] rust: qom: rename Class trait to ClassInitImpl Paolo Bonzini
2024-12-10 8:24 ` Zhao Liu
2024-12-09 12:37 ` [PATCH 09/26] rust: qom: convert type_info! macro to an associated const Paolo Bonzini
2024-12-10 8:30 ` Zhao Liu
2024-12-09 12:37 ` [PATCH 10/26] rust: qom: move ClassInitImpl to the instance side Paolo Bonzini
2024-12-10 16:03 ` Zhao Liu
2024-12-09 12:37 ` [PATCH 11/26] rust: qdev: move device_class_init! body to generic function, ClassInitImpl implementation to macro Paolo Bonzini
2024-12-10 16:06 ` Zhao Liu
2024-12-09 12:37 ` [PATCH 12/26] rust: qdev: move bridge for realize and reset functions out of pl011 Paolo Bonzini
2024-12-10 16:07 ` Zhao Liu
2024-12-09 12:37 ` [PATCH 13/26] rust: qom: automatically use Drop trait to implement instance_finalize Paolo Bonzini
2024-12-10 16:16 ` Zhao Liu
2024-12-11 12:42 ` Paolo Bonzini
2024-12-11 15:37 ` Zhao Liu
2024-12-09 12:37 ` [PATCH 14/26] rust: qom: move bridge for TypeInfo functions out of pl011 Paolo Bonzini
2024-12-10 15:50 ` Zhao Liu
2024-12-10 17:38 ` Paolo Bonzini
2024-12-11 7:59 ` Zhao Liu
2024-12-11 9:11 ` Paolo Bonzini
2024-12-11 16:56 ` Zhao Liu
2024-12-12 9:24 ` Paolo Bonzini
2024-12-13 8:53 ` Zhao Liu
2024-12-10 16:02 ` Zhao Liu
2024-12-09 12:37 ` [PATCH 15/26] rust: qom: split ObjectType from ObjectImpl trait Paolo Bonzini
2024-12-11 8:41 ` Zhao Liu
2024-12-09 12:37 ` [PATCH 16/26] rust: qom: change the parent type to an associated type Paolo Bonzini
2024-12-11 8:47 ` Zhao Liu
2024-12-09 12:37 ` [PATCH 17/26] rust: qom: put class_init together from multiple ClassInitImpl<> Paolo Bonzini
2024-12-12 9:15 ` Zhao Liu
2024-12-09 12:37 ` [PATCH 18/26] rust: qom: add possibility of overriding unparent Paolo Bonzini
2024-12-12 9:40 ` Zhao Liu
2024-12-12 11:15 ` Paolo Bonzini
2024-12-09 12:37 ` [PATCH 19/26] rust: rename qemu-api modules to follow C code a bit more Paolo Bonzini
2024-12-12 9:52 ` Zhao Liu
2024-12-12 11:28 ` Paolo Bonzini
2024-12-13 9:19 ` Zhao Liu
2024-12-13 11:24 ` Paolo Bonzini
2024-12-09 12:37 ` [PATCH 20/26] rust: re-export C types from qemu-api submodules Paolo Bonzini
2024-12-12 9:55 ` Zhao Liu
2024-12-09 12:37 ` [PATCH 21/26] rust: tests: allow writing more than one test Paolo Bonzini
2024-12-12 10:04 ` Zhao Liu
2024-12-16 15:07 ` Paolo Bonzini
2024-12-09 12:37 ` [PATCH 22/26] rust: qom: add casting functionality Paolo Bonzini
2024-12-16 12:53 ` Zhao Liu
2024-12-16 15:17 ` Paolo Bonzini
2024-12-09 12:37 ` [PATCH 23/26] rust: qom: add initial subset of methods on Object Paolo Bonzini
2024-12-16 15:18 ` Zhao Liu
2024-12-09 12:37 ` [PATCH 24/26] rust: qom: move device_id to PL011 class side Paolo Bonzini
2024-12-17 3:58 ` Zhao Liu
2024-12-17 16:50 ` Paolo Bonzini
2024-12-18 6:57 ` Zhao Liu
2024-12-18 7:14 ` Paolo Bonzini
2024-12-18 10:26 ` Paolo Bonzini
2024-12-18 14:46 ` Zhao Liu
2024-12-18 16:01 ` Paolo Bonzini
2024-12-18 14:30 ` Zhao Liu
2024-12-18 14:47 ` Zhao Liu
2024-12-09 12:37 ` [PATCH 25/26] rust: qemu-api: add a module to wrap functions and zero-sized closures Paolo Bonzini
2024-12-17 15:03 ` Zhao Liu
2024-12-09 12:37 ` [PATCH 26/26] rust: callbacks: allow passing optional callbacks as () Paolo Bonzini
2024-12-17 16:13 ` Zhao Liu
2024-12-17 16:40 ` Paolo Bonzini
2024-12-18 7:09 ` Zhao Liu
2024-12-18 7:32 ` Paolo Bonzini
2024-12-18 15:09 ` Zhao Liu
2024-12-09 16:24 ` [PATCH 00/26] rust: bundle of prerequisites for HPET implementation Philippe Mathieu-Daudé
2024-12-09 17:23 ` Paolo Bonzini
2024-12-10 2:38 ` Zhao Liu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Z1f4IN3mfYB/jq8G@intel.com \
--to=zhao1.liu@intel.com \
--cc=junjie.mao@hotmail.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=qemu-rust@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.