1 // (C) Copyright Matt Borland 2021.
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)
6 #ifndef BOOST_MATH_CCMATH_DIV_HPP
7 #define BOOST_MATH_CCMATH_DIV_HPP
13 #include <type_traits>
15 namespace boost::math::ccmath {
19 template <typename ReturnType, typename Z>
20 inline constexpr ReturnType div_impl(const Z x, const Z y) noexcept
22 // std::div_t/ldiv_t/lldiv_t/imaxdiv_t can be defined as either { Z quot; Z rem; }; or { Z rem; Z quot; };
23 // so don't use braced initialziation to guarantee compatibility
24 ReturnType ans {0, 0};
34 // Used for types other than built-ins (e.g. boost multiprecision)
43 inline constexpr auto div(Z x, Z y) noexcept
45 if constexpr (std::is_same_v<Z, int>)
47 return detail::div_impl<std::div_t>(x, y);
49 else if constexpr (std::is_same_v<Z, long>)
51 return detail::div_impl<std::ldiv_t>(x, y);
53 else if constexpr (std::is_same_v<Z, long long>)
55 return detail::div_impl<std::lldiv_t>(x, y);
57 else if constexpr (std::is_same_v<Z, std::intmax_t>)
59 return detail::div_impl<std::imaxdiv_t>(x, y);
63 return detail::div_impl<boost::math::ccmath::div_t<Z>>(x, y);
67 inline constexpr std::ldiv_t ldiv(long x, long y) noexcept
69 return detail::div_impl<std::ldiv_t>(x, y);
72 inline constexpr std::lldiv_t lldiv(long long x, long long y) noexcept
74 return detail::div_impl<std::lldiv_t>(x, y);
77 inline constexpr std::imaxdiv_t imaxdiv(std::intmax_t x, std::intmax_t y) noexcept
79 return detail::div_impl<std::imaxdiv_t>(x, y);
84 #endif // BOOST_MATH_CCMATH_DIV_HPP