Skip to content

Commit

Permalink
Use optimized mul/div algos
Browse files Browse the repository at this point in the history
  • Loading branch information
ckormanyos committed Dec 17, 2024
1 parent 363241e commit 97ddd70
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 163 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2021 Fahad Syed.
// Copyright 2021 - 2023 Christopher Kormanyos.
// Copyright 2021 - 2024 Christopher Kormanyos.
// Copyright 2021 Janek Kozicki.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define BOOST_MP_CPP_DF_QF_DETAIL_CCMATH_2023_01_07_HPP

#include <boost/multiprecision/cpp_df_qf/cpp_df_qf_detail_ccmath_fabs.hpp>
#include <boost/multiprecision/cpp_df_qf/cpp_df_qf_detail_ccmath_fma.hpp>
#include <boost/multiprecision/cpp_df_qf/cpp_df_qf_detail_ccmath_frexp.hpp>
#include <boost/multiprecision/cpp_df_qf/cpp_df_qf_detail_ccmath_isinf.hpp>
#include <boost/multiprecision/cpp_df_qf/cpp_df_qf_detail_ccmath_isnan.hpp>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2024.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef BOOST_MP_CPP_DF_QF_DETAIL_CCMATH_FMA_2024_12_17_HPP
#define BOOST_MP_CPP_DF_QF_DETAIL_CCMATH_FMA_2024_12_17_HPP

#include <boost/multiprecision/cpp_df_qf/cpp_df_qf_detail_ccmath_isnan.hpp>
#include <boost/multiprecision/cpp_df_qf/cpp_df_qf_detail_ccmath_isinf.hpp>

namespace boost { namespace multiprecision { namespace backends { namespace cpp_df_qf_detail { namespace ccmath {

namespace detail {

template <class T>
constexpr T fma_imp(T x, T y, T z) noexcept
{
#if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER) && !defined(__INTEL_LLVM_COMPILER)

BOOST_IF_CONSTEXPR(::std::is_same<T, float>::value)
{
return __builtin_fmaf(x, y, z);
}
else BOOST_IF_CONSTEXPR(::std::is_same<T, double>::value)
{
return __builtin_fma(x, y, z);
}
else BOOST_IF_CONSTEXPR(::std::is_same<T, long double>::value)
{
return __builtin_fmal(x, y, z);
}
#endif

// If we can't use compiler intrinsics hope that -fma flag optimizes this call to fma instruction
return (x * y) + z;
};

} // namespace detail

template <typename Real>
constexpr Real fma(Real x, Real y, Real z) noexcept
{
if (x == 0 && cpp_df_qf_detail::ccmath::isinf(y))
{
return std::numeric_limits<Real>::quiet_NaN();
}
else if (y == 0 && cpp_df_qf_detail::ccmath::isinf(x))
{
return std::numeric_limits<Real>::quiet_NaN();
}
else if (cpp_df_qf_detail::ccmath::isnan(x))
{
return std::numeric_limits<Real>::quiet_NaN();
}
else if (cpp_df_qf_detail::ccmath::isnan(y))
{
return std::numeric_limits<Real>::quiet_NaN();
}
else if (cpp_df_qf_detail::ccmath::isnan(z))
{
return std::numeric_limits<Real>::quiet_NaN();
}

return detail::fma_imp(x, y, z);
}

} } } } } // namespace boost::multiprecision::backends::cpp_df_qf_detail::ccmath

#endif // BOOST_MP_CPP_DF_QF_DETAIL_CCMATH_FMA_2024_12_17_HPP
Loading

0 comments on commit 97ddd70

Please sign in to comment.