Blame view

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