fma.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // (C) Copyright Matt Borland 2022.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_CCMATH_FMA_HPP
  6. #define BOOST_MATH_CCMATH_FMA_HPP
  7. #include <boost/math/ccmath/detail/config.hpp>
  8. #ifdef BOOST_MATH_NO_CCMATH
  9. #error "The header <boost/math/fma.hpp> can only be used in C++17 and later."
  10. #endif
  11. #include <boost/math/ccmath/isinf.hpp>
  12. #include <boost/math/ccmath/isnan.hpp>
  13. namespace boost::math::ccmath {
  14. namespace detail {
  15. template <typename T>
  16. constexpr T fma_imp(const T x, const T y, const T z) noexcept
  17. {
  18. #if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER) && !defined(__INTEL_LLVM_COMPILER)
  19. if constexpr (std::is_same_v<T, float>)
  20. {
  21. return __builtin_fmaf(x, y, z);
  22. }
  23. else if constexpr (std::is_same_v<T, double>)
  24. {
  25. return __builtin_fma(x, y, z);
  26. }
  27. else if constexpr (std::is_same_v<T, long double>)
  28. {
  29. return __builtin_fmal(x, y, z);
  30. }
  31. #endif
  32. // If we can't use compiler intrinsics hope that -fma flag optimizes this call to fma instruction
  33. return (x * y) + z;
  34. }
  35. } // Namespace detail
  36. template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
  37. constexpr Real fma(Real x, Real y, Real z) noexcept
  38. {
  39. if (BOOST_MATH_IS_CONSTANT_EVALUATED(x))
  40. {
  41. if (x == 0 && boost::math::ccmath::isinf(y))
  42. {
  43. return std::numeric_limits<Real>::quiet_NaN();
  44. }
  45. else if (y == 0 && boost::math::ccmath::isinf(x))
  46. {
  47. return std::numeric_limits<Real>::quiet_NaN();
  48. }
  49. else if (boost::math::ccmath::isnan(x))
  50. {
  51. return std::numeric_limits<Real>::quiet_NaN();
  52. }
  53. else if (boost::math::ccmath::isnan(y))
  54. {
  55. return std::numeric_limits<Real>::quiet_NaN();
  56. }
  57. else if (boost::math::ccmath::isnan(z))
  58. {
  59. return std::numeric_limits<Real>::quiet_NaN();
  60. }
  61. return boost::math::ccmath::detail::fma_imp(x, y, z);
  62. }
  63. else
  64. {
  65. using std::fma;
  66. return fma(x, y, z);
  67. }
  68. }
  69. template <typename T1, typename T2, typename T3>
  70. constexpr auto fma(T1 x, T2 y, T3 z) noexcept
  71. {
  72. if (BOOST_MATH_IS_CONSTANT_EVALUATED(x))
  73. {
  74. // If the type is an integer (e.g. epsilon == 0) then set the epsilon value to 1 so that type is at a minimum
  75. // cast to double
  76. constexpr auto T1p = std::numeric_limits<T1>::epsilon() > 0 ? std::numeric_limits<T1>::epsilon() : 1;
  77. constexpr auto T2p = std::numeric_limits<T2>::epsilon() > 0 ? std::numeric_limits<T2>::epsilon() : 1;
  78. constexpr auto T3p = std::numeric_limits<T3>::epsilon() > 0 ? std::numeric_limits<T3>::epsilon() : 1;
  79. using promoted_type =
  80. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  81. std::conditional_t<T1p <= LDBL_EPSILON && T1p <= T2p, T1,
  82. std::conditional_t<T2p <= LDBL_EPSILON && T2p <= T1p, T2,
  83. std::conditional_t<T3p <= LDBL_EPSILON && T3p <= T2p, T3,
  84. #endif
  85. std::conditional_t<T1p <= DBL_EPSILON && T1p <= T2p, T1,
  86. std::conditional_t<T2p <= DBL_EPSILON && T2p <= T1p, T2,
  87. std::conditional_t<T3p <= DBL_EPSILON && T3p <= T2p, T3, double
  88. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  89. >>>>>>;
  90. #else
  91. >>>;
  92. #endif
  93. return boost::math::ccmath::fma(promoted_type(x), promoted_type(y), promoted_type(z));
  94. }
  95. else
  96. {
  97. using std::fma;
  98. return fma(x, y, z);
  99. }
  100. }
  101. constexpr float fmaf(float x, float y, float z) noexcept
  102. {
  103. return boost::math::ccmath::fma(x, y, z);
  104. }
  105. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  106. constexpr long double fmal(long double x, long double y, long double z) noexcept
  107. {
  108. return boost::math::ccmath::fma(x, y, z);
  109. }
  110. #endif
  111. } // Namespace boost::math::ccmath
  112. #endif // BOOST_MATH_CCMATH_FMA_HPP