00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef PPL_math_utilities_inlines_hh
00024 #define PPL_math_utilities_inlines_hh 1
00025
00026 #include "Coefficient.defs.hh"
00027 #include <limits>
00028 #include <cassert>
00029
00030 namespace Parma_Polyhedra_Library {
00031
00032 inline void
00033 normalize2(Coefficient_traits::const_reference x,
00034 Coefficient_traits::const_reference y,
00035 Coefficient& nx, Coefficient& ny) {
00036 TEMP_INTEGER(gcd);
00037 gcd_assign(gcd, x, y);
00038 exact_div_assign(nx, x, gcd);
00039 exact_div_assign(ny, y, gcd);
00040 }
00041
00042 template <typename T>
00043 inline T
00044 low_bits_mask(const unsigned n) {
00045 assert(n < unsigned(std::numeric_limits<T>::digits));
00046 return n == 0 ? 0 : ~(~(T(0u)) << n);
00047 }
00048
00049 template <typename T, typename Policy>
00050 inline void
00051 numer_denom(const Checked_Number<T, Policy>& from,
00052 Coefficient& num, Coefficient& den) {
00053 assert(!is_not_a_number(from)
00054 && !is_minus_infinity(from)
00055 && !is_plus_infinity(from));
00056 DIRTY_TEMP0(mpq_class, q);
00057 assign_r(q, from, ROUND_NOT_NEEDED);
00058 num = q.get_num();
00059 den = q.get_den();
00060 }
00061
00062 template <typename T, typename Policy>
00063 inline void
00064 div_round_up(Checked_Number<T, Policy>& to,
00065 Coefficient_traits::const_reference x,
00066 Coefficient_traits::const_reference y) {
00067 DIRTY_TEMP0(mpq_class, qx);
00068 DIRTY_TEMP0(mpq_class, qy);
00069
00070
00071 assign_r(qx, x, ROUND_NOT_NEEDED);
00072 assign_r(qy, y, ROUND_NOT_NEEDED);
00073 div_assign_r(qx, qx, qy, ROUND_NOT_NEEDED);
00074 assign_r(to, qx, ROUND_UP);
00075 }
00076
00077 template <typename N>
00078 inline void
00079 min_assign(N& x, const N& y) {
00080 if (x > y)
00081 x = y;
00082 }
00083
00084 template <typename N>
00085 inline void
00086 max_assign(N& x, const N& y) {
00087 if (x < y)
00088 x = y;
00089 }
00090
00091 template <typename T, typename Policy>
00092 inline bool
00093 is_even(const Checked_Number<T, Policy>& x) {
00094 Checked_Number<T, Policy> half_x;
00095 return div2exp_assign_r(half_x, x, 1, ROUND_DIRECT) == V_EQ
00096 && is_integer(half_x);
00097 }
00098
00099 template <typename T, typename Policy>
00100 inline bool
00101 is_additive_inverse(const Checked_Number<T, Policy>& x,
00102 const Checked_Number<T, Policy>& y) {
00103 Checked_Number<T, Policy> negated_x;
00104 return neg_assign_r(negated_x, x, ROUND_DIRECT) == V_EQ
00105 && negated_x == y;
00106 }
00107
00108 inline bool
00109 is_canonical(const mpq_class& x) {
00110 if (x.get_den() <= 0)
00111 return false;
00112 DIRTY_TEMP0(mpq_class, temp);
00113 temp = x;
00114 temp.canonicalize();
00115 return temp.get_num() == x.get_num();
00116 }
00117
00118 }
00119
00120 #endif // !defined(PPL_math_utilities_inlines_hh)