Skip to content

feat(primitivies): impl a handful of traits for fixed types #7565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

saiintbrisson
Copy link
Contributor

This change implements a handful of traits for fixed types:

  • The sum helper for iterators,
  • Rem/CheckedRem,
  • Signed/Unsigned and Num,
    • matching the bounds for the blanket impl for MultiplyArg to be used with PerThing,
  • and a companion function to_u32 to match to_float.

I'd also love to impl forward reference implementations for binops traits (Add<&Self> for Self, Add<Self> for &Self, etc), but this breaks the existing .into() calls as it turns the conversion ambiguous. Not sure if that's a price you are willing to pay, but would be very much appreciated.

@saiintbrisson saiintbrisson force-pushed the feat/primitivies/impl-sum-for-fixed-types branch 3 times, most recently from 9ea4117 to 85d920e Compare February 13, 2025 18:46
@saiintbrisson saiintbrisson force-pushed the feat/primitivies/impl-sum-for-fixed-types branch from 85d920e to 3944b6c Compare February 13, 2025 18:57
@saiintbrisson saiintbrisson marked this pull request as ready for review February 13, 2025 19:58
@saiintbrisson
Copy link
Contributor Author

saiintbrisson commented Mar 31, 2025

Hi, @bkchr, sorry for the ping. Can I get your attention here? Sum, in particular, is a very relevant operation, commonly needed in the project I'm in.

@@ -175,7 +175,7 @@ pub trait FixedPointNumber:
d: D,
) -> Option<Self> {
if d == D::zero() {
return None
return None;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably your IDE doing this; but you can improve review speed by omitting unrelated changes.

@@ -513,6 +513,11 @@ macro_rules! implement_fixed {
self.0 as f64 / <Self as FixedPointNumber>::DIV as f64
}

/// Convert into a `u32` value.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment on the rounding mode please? Seems to be Rounding::Down.

@@ -926,6 +969,18 @@ macro_rules! implement_fixed {
}
}

impl ::core::iter::Sum for $name {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(Self::zero(), |a, b| a + b)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can overflow?

assert_eq!(fixed.0, (u32::MAX as $inner_type) * $name::DIV);

let val = fixed.to_u32();
assert_eq!(val, u32::MAX);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some more cases here please? Maybe also a random generated value in a loop.

We have some fuzz tests in sp-arithmetic-fuzzer, probably overkill to add it there, just saying.

@@ -2204,6 +2318,50 @@ macro_rules! implement_fixed {
};
}

macro_rules! impl_signed_trait {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs?

return None
}
let (result, remainder) = Double128::product_of(a, b).div(c);
let Some((result, remainder)) = multiply_by_rational(a, b, c) else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need audit cc @Gioyik

@bkchr
Copy link
Member

bkchr commented Apr 17, 2025

@saiintbrisson can you answer the questions?

@Gioyik
Copy link

Gioyik commented May 8, 2025

I have opened #8472 which introduces multiple checked functions and new fuzzing tests.

To audit, there is no conflict between the introduced functions, so either of both could merge first. As I mentioned in the other PR, the whole crate is audited now, so I can start doing diff sec review of introduced changes to the crate once mine is merged and there are not many PR that I can't keep up with. cc/ @ggwpez

@saiintbrisson
Copy link
Contributor Author

Hi, I've been away for a while. I'll see what I can do in the coming weeks. @Gioyik let's have yours merged first! And then I'll adapt changes here. Sorry for the absence.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants