// (C) Copyright Matt Borland 2022. // Use, modification and distribution are subject to 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_MATH_CCMATH_FMA_HPP #define BOOST_MATH_CCMATH_FMA_HPP #include #include #include #include #include #include namespace boost::math::ccmath { namespace detail { template constexpr T fma_imp(const T x, const T y, const T z) noexcept { #if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER) && !defined(__INTEL_LLVM_COMPILER) if constexpr (std::is_same_v) { return __builtin_fmaf(x, y, z); } else if constexpr (std::is_same_v) { return __builtin_fma(x, y, z); } else if constexpr (std::is_same_v) { 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 , bool> = true> constexpr Real fma(Real x, Real y, Real z) noexcept { if (BOOST_MATH_IS_CONSTANT_EVALUATED(x)) { if (x == 0 && boost::math::ccmath::isinf(y)) { return std::numeric_limits::quiet_NaN(); } else if (y == 0 && boost::math::ccmath::isinf(x)) { return std::numeric_limits::quiet_NaN(); } else if (boost::math::ccmath::isnan(x)) { return std::numeric_limits::quiet_NaN(); } else if (boost::math::ccmath::isnan(y)) { return std::numeric_limits::quiet_NaN(); } else if (boost::math::ccmath::isnan(z)) { return std::numeric_limits::quiet_NaN(); } return boost::math::ccmath::detail::fma_imp(x, y, z); } else { using std::fma; return fma(x, y, z); } } template constexpr auto fma(T1 x, T2 y, T3 z) noexcept { if (BOOST_MATH_IS_CONSTANT_EVALUATED(x)) { // If the type is an integer (e.g. epsilon == 0) then set the epsilon value to 1 so that type is at a minimum // cast to double constexpr auto T1p = std::numeric_limits::epsilon() > 0 ? std::numeric_limits::epsilon() : 1; constexpr auto T2p = std::numeric_limits::epsilon() > 0 ? std::numeric_limits::epsilon() : 1; constexpr auto T3p = std::numeric_limits::epsilon() > 0 ? std::numeric_limits::epsilon() : 1; using promoted_type = #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS std::conditional_t>>>>>; #else >>>; #endif return boost::math::ccmath::fma(promoted_type(x), promoted_type(y), promoted_type(z)); } else { using std::fma; return fma(x, y, z); } } constexpr float fmaf(float x, float y, float z) noexcept { return boost::math::ccmath::fma(x, y, z); } #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS constexpr long double fmal(long double x, long double y, long double z) noexcept { return boost::math::ccmath::fma(x, y, z); } #endif } // Namespace boost::math::ccmath #endif // BOOST_MATH_CCMATH_FMA_HPP