Skip to content
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

Add fma to math.h #526

Merged
merged 3 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/utility/include/mp-units/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,37 @@ template<auto R, typename Rep>
return {static_cast<Rep>(abs(q.numerical_value_ref_in(q.unit))), R};
}

/**
* @brief Computes the fma of 3 quantities
*
* @param a: Multiplicand
* @param x: Multiplicand
* @param b: Addend
* @return Quantity: The nearest floating point representable to ax+b
*/
template<auto R, auto S, auto T, typename Rep1, typename Rep2, typename Rep3>
[[nodiscard]] constexpr QuantityOf<common_quantity_spec(get_quantity_spec(R) * get_quantity_spec(S),
get_quantity_spec(T))> auto
fma(const quantity<R, Rep1>& a, const quantity<S, Rep2>& x, const quantity<T, Rep3>& b) noexcept
requires requires { common_quantity_spec(get_quantity_spec(R) * get_quantity_spec(S), get_quantity_spec(T)); } &&
(get_unit(R) * get_unit(S) == get_unit(T)) &&
(
requires {
fma(a.numerical_value_ref_in(a.unit), x.numerical_value_ref_in(x.unit),
b.numerical_value_ref_in(b.unit));
} ||
requires {
std::fma(a.numerical_value_ref_in(a.unit), x.numerical_value_ref_in(x.unit),
b.numerical_value_ref_in(b.unit));
})
{
using std::fma;
return quantity{
fma(a.numerical_value_ref_in(a.unit), x.numerical_value_ref_in(x.unit), b.numerical_value_ref_in(b.unit)),
common_reference(R * S, T)};
}


/**
* @brief Returns the epsilon of the quantity
*
Expand Down
11 changes: 11 additions & 0 deletions test/unit_test/runtime/math_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ TEST_CASE("'cbrt()' on quantity changes the value and the dimension accordingly"
REQUIRE(cbrt(8 * isq::volume[m3]) == 2 * isq::length[m]);
}

TEST_CASE("'fma()' on quantity changes the value and the dimension accordingly", "[math][fma]")
{
REQUIRE(fma(1.0 * isq::length[m], 2.0 * one, 2.0 * isq::length[m]) == 4.0 * isq::length[m]);
}

TEST_CASE("'fma()' returns a common reference.", "[math][fma]")
{
REQUIRE(fma(isq::speed(10.0 * m / s), isq::time(2.0 * s), isq::height(42.0 * m)) == isq::length(62.0 * m));
}


TEST_CASE("'pow<Num, Den>()' on quantity changes the value and the dimension accordingly", "[math][pow]")
{
REQUIRE(pow<1, 4>(16 * isq::area[m2]) == sqrt(4 * isq::length[m]));
Expand Down
4 changes: 4 additions & 0 deletions test/unit_test/static/math_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ template<typename T1, typename T2, typename... Ts>

#if __cpp_lib_constexpr_cmath || MP_UNITS_COMP_GCC

static_assert(compare(fma(2.0 * s, 3.0 * Hz, 1.0 * one), 7.0 * one));
static_assert(compare(fma(2.0 * one, 3.0 * m, 1.0 * m), 7.0 * m));
static_assert(compare(fma(2.0 * m, 3.0 * one, 1.0 * m), 7.0 * m));
static_assert(compare(fma(2 * m, 3.0f * m, 1.0 * m2), 7.0 * m2));
static_assert(compare(pow<0>(2 * m), 1 * one));
static_assert(compare(pow<1>(2 * m), 2 * m));
static_assert(compare(pow<2>(2 * m), 4 * pow<2>(m), 4 * m2));
Expand Down