Debian Codegraph @

third-party/boost-math/include/boost/math/ccmath/div.hpp

raw
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)
5
6 #ifndef BOOST_MATH_CCMATH_DIV_HPP
7 #define BOOST_MATH_CCMATH_DIV_HPP
8
9 #include <cinttypes>
10 #include <cstdint>
11 #include <boost/math/ccmath/detail/config.hpp>
12
13 #ifdef BOOST_MATH_NO_CCMATH
14 #error "The header <boost/math/div.hpp> can only be used in C++17 and later."
15 #endif
16
17 namespace boost::math::ccmath {
18
19 namespace detail {
20
21 template <typename ReturnType, typename Z>
22 inline constexpr ReturnType div_impl(const Z x, const Z y) noexcept
23 {
24 // std::div_t/ldiv_t/lldiv_t/imaxdiv_t can be defined as either { Z quot; Z rem; }; or { Z rem; Z quot; };
25 // so don't use braced initialization to guarantee compatibility
26 ReturnType ans {0, 0};
27
28 ans.quot = x / y;
29 ans.rem = x % y;
30
31 return ans;
32 }
33
34 } // Namespace detail
35
36 // Used for types other than built-ins (e.g. boost multiprecision)
37 template <typename Z>
38 struct div_t
39 {
40 Z quot;
41 Z rem;
42 };
43
44 template <typename Z>
45 inline constexpr auto div(Z x, Z y) noexcept
46 {
47 if constexpr (std::is_same_v<Z, int>)
48 {
49 return detail::div_impl<std::div_t>(x, y);
50 }
51 else if constexpr (std::is_same_v<Z, long>)
52 {
53 return detail::div_impl<std::ldiv_t>(x, y);
54 }
55 else if constexpr (std::is_same_v<Z, long long>)
56 {
57 return detail::div_impl<std::lldiv_t>(x, y);
58 }
59 else if constexpr (std::is_same_v<Z, std::intmax_t>)
60 {
61 return detail::div_impl<std::imaxdiv_t>(x, y);
62 }
63 else
64 {
65 return detail::div_impl<boost::math::ccmath::div_t<Z>>(x, y);
66 }
67 }
68
69 inline constexpr std::ldiv_t ldiv(long x, long y) noexcept
70 {
71 return detail::div_impl<std::ldiv_t>(x, y);
72 }
73
74 inline constexpr std::lldiv_t lldiv(long long x, long long y) noexcept
75 {
76 return detail::div_impl<std::lldiv_t>(x, y);
77 }
78
79 inline constexpr std::imaxdiv_t imaxdiv(std::intmax_t x, std::intmax_t y) noexcept
80 {
81 return detail::div_impl<std::imaxdiv_t>(x, y);
82 }
83
84 } // Namespaces
85
86 #endif // BOOST_MATH_CCMATH_DIV_HPP