add mul_add to Wrapping and Unwrapped

This commit is contained in:
Trevor Spiteri 2020-09-22 17:20:13 +02:00
parent 88f67dc43d
commit 0aa8af089f
4 changed files with 58 additions and 7 deletions

View File

@ -80,8 +80,8 @@ The conversions supported cover the following cases.
### Version 1.3.0 news (unreleased)
* The following methods were added to all fixed-point types and to
the `Fixed` trait:
* 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],
[`saturating_mul_add`][f-sma-1-3],
[`wrapping_mul_add`][f-wma-1-3],
@ -96,6 +96,8 @@ The conversions supported cover the following cases.
[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
### Version 1.2.0 news (2020-09-02)

View File

@ -8,8 +8,8 @@ as-is, without any warranty. -->
Version 1.3.0 (unreleased)
==========================
* The following methods were added to all fixed-point types and to
the `Fixed` trait:
* 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],
[`saturating_mul_add`][f-sma-1-3],
[`wrapping_mul_add`][f-wma-1-3],
@ -24,6 +24,8 @@ Version 1.3.0 (unreleased)
[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
Version 1.2.0 (2020-09-02)
==========================

View File

@ -619,6 +619,35 @@ impl<F: Fixed> Unwrapped<F> {
Unwrapped(self.0.rotate_right(n))
}
/// Multiply and add. Returns `self` × `mul` + `add`.
///
/// # Panics
///
/// Panics if the result does not fit.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Unwrapped};
/// let half = Unwrapped(I16F16::from_num(0.5));
/// let three = Unwrapped(I16F16::from_num(3));
/// let four = Unwrapped(I16F16::from_num(4));
/// assert_eq!(three.mul_add(half, four), Unwrapped(I16F16::from_num(5.5)));
/// ```
///
/// The following panics because of overflow.
///
/// ```should_panic
/// use fixed::{types::I16F16, Unwrapped};
/// let one = Unwrapped(I16F16::from_num(1));
/// let max = Unwrapped(I16F16::MAX);
/// let _overflow = max.mul_add(one, one);
/// ```
#[inline]
pub fn mul_add(self, mul: Unwrapped<F>, add: Unwrapped<F>) -> Unwrapped<F> {
Unwrapped(self.0.unwrapped_mul_add(mul.0, add.0))
}
/// Euclidean division.
///
/// # Panics
@ -639,7 +668,7 @@ impl<F: Fixed> Unwrapped<F> {
/// ```should_panic
/// use fixed::{types::I16F16, Unwrapped};
/// let quarter = Unwrapped(I16F16::from_num(0.25));
/// let _overflow = Unwrapped::MAX.div_euclid(quarter);
/// let _overflow = Unwrapped(I16F16::MAX).div_euclid(quarter);
/// ```
#[inline]
pub fn div_euclid(self, divisor: Unwrapped<F>) -> Unwrapped<F> {

View File

@ -570,6 +570,24 @@ impl<F: Fixed> Wrapping<F> {
Wrapping(self.0.rotate_right(n))
}
/// Multiply and add. Returns `self` × `mul` + `add`.
///
/// # Examples
///
/// ```rust
/// use fixed::{types::I16F16, Wrapping};
/// let half = Wrapping(I16F16::from_num(0.5));
/// let three = Wrapping(I16F16::from_num(3));
/// let four = Wrapping(I16F16::from_num(4));
/// let max = Wrapping(I16F16::MAX);
/// assert_eq!(three.mul_add(half, four), Wrapping(I16F16::from_num(5.5)));
/// assert_eq!(max.mul_add(three, max), Wrapping(I16F16::from_bits(!0 << 2)));
/// ```
#[inline]
pub fn mul_add(self, mul: Wrapping<F>, add: Wrapping<F>) -> Wrapping<F> {
Wrapping(self.0.wrapping_mul_add(mul.0, add.0))
}
/// Euclidean division.
///
/// # Panics
@ -584,8 +602,8 @@ impl<F: Fixed> Wrapping<F> {
/// let den = Wrapping(I16F16::from_num(2));
/// assert_eq!(num.div_euclid(den), Wrapping(I16F16::from_num(3)));
/// let quarter = Wrapping(I16F16::from_num(0.25));
/// let check = (Wrapping::MAX * 4i32).round_to_zero();
/// assert_eq!(Wrapping::MAX.div_euclid(quarter), check);
/// let check = (Wrapping(I16F16::MAX) * 4i32).round_to_zero();
/// assert_eq!(Wrapping(I16F16::MAX).div_euclid(quarter), check);
/// ```
#[inline]
pub fn div_euclid(self, divisor: Wrapping<F>) -> Wrapping<F> {