On Tue, Feb 18, 2025 at 02:00:47PM +0100, Andreas Hindborg wrote:
> Add the trait `ParseInt` for parsing string representations of integers
> where the string representations are optionally prefixed by a radix
> specifier. Implement the trait for the primitive integer types.
> 
> Signed-off-by: Andreas Hindborg <a.hindb...@kernel.org>
> ---
>  rust/kernel/str.rs | 118 
> +++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 118 insertions(+)
> 
> diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
> index db272d2198fcc..8b0d814b47f52 100644
> --- a/rust/kernel/str.rs
> +++ b/rust/kernel/str.rs
> @@ -945,3 +945,121 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> 
> fmt::Result {

[...]

> +    pub trait ParseInt: FromStrRadix + TryFrom<i128> {
> +        /// Parse a string according to the description in [`Self`].
> +        fn from_str(src: &BStr) -> Result<Self> {
> +            match src.deref() {
> +                [b'-', rest @ ..] => {
> +                    let (radix, digits) = strip_radix(rest.as_ref());
> +                    // 2's complement values range from -2^(b-1) to 
> 2^(b-1)-1.
> +                    // So if we want to parse negative numbers as positive 
> and
> +                    // later multiply by -1, we have to parse into a larger
> +                    // integer. We choose i128 as sufficiently large.
> +                    let val = i128::from_str_radix(

The usage of i128 causes here following link errors on arm64 with
"rustc 1.84.1 (e71f9a9a9 2025-01-27) (Fedora 1.84.1-1.fc41)"

| ld: rust/kernel.o: in function `<i128>::from_str_radix':
| /usr/lib/rustlib/src/rust/library/core/src/num/mod.rs:1563:(.text+0x3bc): 
undefined reference to `__muloti4'
| ld: /usr/lib/rustlib/src/rust/library/core/src/num/mod.rs:1563:(.text+0x440): 
undefined reference to `__muloti4'
| ld: rust/kernel.o: in function `<i128>::overflowing_mul':
| 
/usr/lib/rustlib/src/rust/library/core/src/num/int_macros.rs:2517:(.text+0x4b4):
 undefined reference to `__muloti4'
| ld: 
/usr/lib/rustlib/src/rust/library/core/src/num/int_macros.rs:2517:(.text+0x534):
 undefined reference to `__muloti4'

The errors go away after exchanging i128 with i64 (while breaking the
parsing for large values).

ciao Janne

Reply via email to