From a0ea3f2c608a8935890264e6fb92218e49bee299 Mon Sep 17 00:00:00 2001 From: Trevor Spiteri Date: Tue, 22 Sep 2020 19:09:41 +0200 Subject: [PATCH] loosen Rhs constraint on MulAssign --- README.md | 10 +++++++--- RELEASES.md | 5 +++++ src/arith.rs | 17 +++++++++++++---- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 8bdaf6e..5bb75e2 100644 --- a/README.md +++ b/README.md @@ -80,8 +80,11 @@ The conversions supported cover the following cases. ### Version 1.3.0 news (unreleased) + * The [`MulAssign`] implementation on fixed-point numbers now + accepts an rhs fixed-point number with a different number of + fractional bits from `self`. * The following methods were added to all fixed-point types, to the - [`Fixed`][tf-1-3] trait, and to the [`Wrapping`][w-1-3] wrapper: + [`Fixed`][tf-ma-1-3] trait, and to the [`Wrapping`][w-ma-1-3] wrapper: * [`mul_add`][f-ma-1-3], [`checked_mul_add`][f-cma-1-3], [`saturating_mul_add`][f-sma-1-3], [`wrapping_mul_add`][f-wma-1-3], @@ -90,14 +93,15 @@ The conversions supported cover the following cases. providing arithmetic methods that panic on overflow even when debug assertions are disabled. +[`MulAssign`]: https://doc.rust-lang.org/nightly/core/ops/trait.MulAssign.html [f-cma-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.checked_mul_add [f-ma-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.mul_add [f-oma-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.overflowing_mul_add [f-sma-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.saturating_mul_add [f-wma-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.FixedI32.html#method.wrapping_mul_add [feat-un-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/#experimental-optional-features -[tf-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/traits/trait.Fixed.html -[w-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.Wrapping.html +[tf-ma-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/traits/trait.Fixed.html#tymethod.mul_add +[w-ma-1-3]: https://tspiteri.gitlab.io/fixed/dev/fixed/struct.Wrapping.html#method.mul_add ### Version 1.2.0 news (2020-09-02) diff --git a/RELEASES.md b/RELEASES.md index 23610f7..2e89a39 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -8,6 +8,9 @@ as-is, without any warranty. --> Version 1.3.0 (unreleased) ========================== + * The [`MulAssign`] implementation on fixed-point numbers now + accepts an rhs fixed-point number with a different number of + fractional bits from `self`. * The following methods were added to all fixed-point types, to the [`Fixed`][tf-1-3] trait, and to the [`Wrapping`][w-1-3] wrapper: * [`mul_add`][f-ma-1-3], [`checked_mul_add`][f-cma-1-3], @@ -407,3 +410,5 @@ Version 0.1.0 (2018-08-10) for the number of fractional bits. * Many methods and trait implementations available for primitive integers are now also supported by the fixed-point numbers. + +[`MulAssign`]: https://doc.rust-lang.org/nightly/core/ops/trait.MulAssign.html diff --git a/src/arith.rs b/src/arith.rs index 2d4caaa..1c21e23 100644 --- a/src/arith.rs +++ b/src/arith.rs @@ -238,14 +238,23 @@ macro_rules! fixed_arith { refs! { impl Mul for $Fixed($LeEqU) { mul } } - impl MulAssign<$Fixed> for $Fixed { + impl MulAssign<$Fixed> for $Fixed { #[inline] - fn mul_assign(&mut self, rhs: $Fixed) { - *self = (*self).mul(rhs) + fn mul_assign(&mut self, rhs: $Fixed) { + let (ans, overflow) = self.to_bits().mul_overflow(rhs.to_bits(), RhsFrac::U32); + debug_assert!(!overflow, "overflow"); + *self = Self::from_bits(ans); } } - refs_assign! { impl MulAssign for $Fixed($LeEqU) { mul_assign } } + impl MulAssign<&'_ $Fixed> for $Fixed { + #[inline] + fn mul_assign(&mut self, rhs: &$Fixed) { + let (ans, overflow) = self.to_bits().mul_overflow(rhs.to_bits(), RhsFrac::U32); + debug_assert!(!overflow, "overflow"); + *self = Self::from_bits(ans); + } + } impl Div<$Fixed> for $Fixed { type Output = $Fixed;