summaryrefslogtreecommitdiff
path: root/src/third_party/boost-1.69.0/boost/multiprecision/detail/precision.hpp
blob: f5963abf697aafca97af7245f2781e783959cf48 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
///////////////////////////////////////////////////////////////////////////////
//  Copyright 2018 John Maddock. Distributed under 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_MP_PRECISION_HPP
#define BOOST_MP_PRECISION_HPP

#include <boost/multiprecision/traits/is_variable_precision.hpp>
#include <boost/multiprecision/detail/number_base.hpp>

namespace boost{ namespace multiprecision{  namespace detail{

   template <class B, boost::multiprecision::expression_template_option ET>
   inline unsigned current_precision_of_imp(const boost::multiprecision::number<B, ET>& n, const mpl::true_&)
   {
      return n.precision();
   }
   template <class B, boost::multiprecision::expression_template_option ET>
   inline BOOST_CONSTEXPR unsigned current_precision_of_imp(const boost::multiprecision::number<B, ET>&, const mpl::false_&)
   {
      return std::numeric_limits<boost::multiprecision::number<B, ET> >::digits10;
   }

   template <class Terminal>
   inline BOOST_CONSTEXPR unsigned current_precision_of(const Terminal&)
   {
      return std::numeric_limits<Terminal>::digits10;
   }

   template <class Terminal, std::size_t N>
   inline BOOST_CONSTEXPR unsigned current_precision_of(const Terminal(&)[N])
   { // For string literals:
      return 0;
   }

   template <class B, boost::multiprecision::expression_template_option ET>
   inline BOOST_CONSTEXPR unsigned current_precision_of(const boost::multiprecision::number<B, ET>& n)
   {
      return current_precision_of_imp(n, boost::multiprecision::detail::is_variable_precision<boost::multiprecision::number<B, ET> >());
   }

   template<class tag, class Arg1>
   inline BOOST_CONSTEXPR unsigned current_precision_of(const expression<tag, Arg1, void, void, void>& expr)
   {
      return current_precision_of(expr.left_ref());
   }

   template<class Arg1>
   inline BOOST_CONSTEXPR unsigned current_precision_of(const expression<terminal, Arg1, void, void, void>& expr)
   {
      return current_precision_of(expr.value());
   }

   template <class tag, class Arg1, class Arg2>
   inline BOOST_CONSTEXPR unsigned current_precision_of(const expression<tag, Arg1, Arg2, void, void>& expr)
   {
      return (std::max)(current_precision_of(expr.left_ref()), current_precision_of(expr.right_ref()));
   }

   template <class tag, class Arg1, class Arg2, class Arg3>
   inline BOOST_CONSTEXPR unsigned current_precision_of(const expression<tag, Arg1, Arg2, Arg3, void>& expr)
   {
      return (std::max)((std::max)(current_precision_of(expr.left_ref()), current_precision_of(expr.right_ref())), current_precision_of(expr.middle_ref()));
   }

   template <class R, bool = boost::multiprecision::detail::is_variable_precision<R>::value>
   struct scoped_default_precision
   {
      template <class T>
      scoped_default_precision(const T&) {}
      template <class T, class U>
      scoped_default_precision(const T&, const U&) {}
      template <class T, class U, class V>
      scoped_default_precision(const T&, const U&, const V&) {}
   };

   template <class R>
   struct scoped_default_precision<R, true>
   {
      template <class T>
      scoped_default_precision(const T& a) 
      {
         init(current_precision_of(a));
      }
      template <class T, class U>
      scoped_default_precision(const T& a, const U& b)
      {
         init((std::max)(current_precision_of(a), current_precision_of(b)));
      }
      template <class T, class U, class V>
      scoped_default_precision(const T& a, const U& b, const V& c)
      {
         init((std::max)((std::max)(current_precision_of(a), current_precision_of(b)), current_precision_of(c)));
      }
      ~scoped_default_precision()
      {
         R::default_precision(m_prec);
      }
   private:
      void init(unsigned p)
      {
         m_prec = R::default_precision();
         if (p)
            R::default_precision(p);
      }
      unsigned m_prec;
   };

   template <class T>
   inline void maybe_promote_precision(T*, const mpl::false_&){}

   template <class T>
   inline void maybe_promote_precision(T* obj, const mpl::true_&)
   {
      if (obj->precision() != T::default_precision())
      {
         obj->precision(T::default_precision());
      }
   }

   template <class T>
   inline void maybe_promote_precision(T* obj)
   {
      maybe_promote_precision(obj, boost::multiprecision::detail::is_variable_precision<T>());
   }

#ifndef BOOST_NO_CXX17_IF_CONSTEXPR
#  define BOOST_MP_CONSTEXPR_IF_VARIABLE_PRECISION(T) if constexpr (boost::multiprecision::detail::is_variable_precision<T>::value)
#else
#  define BOOST_MP_CONSTEXPR_IF_VARIABLE_PRECISION(T) if(boost::multiprecision::detail::is_variable_precision<T>::value)
#endif


}
}
}

#endif // BOOST_MP_IS_BACKEND_HPP