Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:45:14

0001 //  (C) Copyright Matt Borland 2021.
0002 //  Use, modification and distribution are subject to the
0003 //  Boost Software License, Version 1.0. (See accompanying file
0004 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 #ifndef BOOST_MATH_CCMATH_DIV_HPP
0007 #define BOOST_MATH_CCMATH_DIV_HPP
0008 
0009 #include <cinttypes>
0010 #include <cstdint>
0011 #include <boost/math/ccmath/detail/config.hpp>
0012 
0013 #ifdef BOOST_MATH_NO_CCMATH
0014 #error "The header <boost/math/div.hpp> can only be used in C++17 and later."
0015 #endif
0016 
0017 namespace boost::math::ccmath {
0018 
0019 namespace detail {
0020 
0021 template <typename ReturnType, typename Z>
0022 inline constexpr ReturnType div_impl(const Z x, const Z y) noexcept
0023 {
0024     // std::div_t/ldiv_t/lldiv_t/imaxdiv_t can be defined as either { Z quot; Z rem; }; or { Z rem; Z quot; };
0025     // so don't use braced initialization to guarantee compatibility
0026     ReturnType ans {0, 0};
0027 
0028     ans.quot = x / y;
0029     ans.rem = x % y;
0030 
0031     return ans;
0032 }
0033 
0034 } // Namespace detail
0035 
0036 // Used for types other than built-ins (e.g. boost multiprecision)
0037 template <typename Z>
0038 struct div_t
0039 {
0040     Z quot;
0041     Z rem;
0042 };
0043 
0044 template <typename Z>
0045 inline constexpr auto div(Z x, Z y) noexcept
0046 {
0047     if constexpr (std::is_same_v<Z, int>)
0048     {
0049         return detail::div_impl<std::div_t>(x, y);
0050     }
0051     else if constexpr (std::is_same_v<Z, long>)
0052     {
0053         return detail::div_impl<std::ldiv_t>(x, y);
0054     }
0055     else if constexpr (std::is_same_v<Z, long long>)
0056     {
0057         return detail::div_impl<std::lldiv_t>(x, y);
0058     }
0059     else if constexpr (std::is_same_v<Z, std::intmax_t>)
0060     {
0061         return detail::div_impl<std::imaxdiv_t>(x, y);
0062     }
0063     else
0064     {
0065         return detail::div_impl<boost::math::ccmath::div_t<Z>>(x, y);
0066     }
0067 }
0068 
0069 inline constexpr std::ldiv_t ldiv(long x, long y) noexcept
0070 {
0071     return detail::div_impl<std::ldiv_t>(x, y);
0072 }
0073 
0074 inline constexpr std::lldiv_t lldiv(long long x, long long y) noexcept
0075 {
0076     return detail::div_impl<std::lldiv_t>(x, y);
0077 }
0078 
0079 inline constexpr std::imaxdiv_t imaxdiv(std::intmax_t x, std::intmax_t y) noexcept
0080 {
0081     return detail::div_impl<std::imaxdiv_t>(x, y);
0082 }
0083 
0084 } // Namespaces
0085 
0086 #endif // BOOST_MATH_CCMATH_DIV_HPP