PPL  1.2
math_utilities_inlines.hh
Go to the documentation of this file.
1 /* Implementation of some math utility functions: inline functions.
2  Copyright (C) 2001-2010 Roberto Bagnara <bagnara@cs.unipr.it>
3  Copyright (C) 2010-2016 BUGSENG srl (http://bugseng.com)
4 
5 This file is part of the Parma Polyhedra Library (PPL).
6 
7 The PPL is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 The PPL is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
20 
21 For the most up-to-date information see the Parma Polyhedra Library
22 site: http://bugseng.com/products/ppl/ . */
23 
24 #ifndef PPL_math_utilities_inlines_hh
25 #define PPL_math_utilities_inlines_hh 1
26 
27 #include "Coefficient_defs.hh"
28 #include "assertions.hh"
29 #include <limits>
30 
31 namespace Parma_Polyhedra_Library {
32 
33 inline void
34 normalize2(Coefficient_traits::const_reference x,
35  Coefficient_traits::const_reference y,
36  Coefficient& n_x, Coefficient& n_y) {
38  gcd_assign(gcd, x, y);
39  exact_div_assign(n_x, x, gcd);
40  exact_div_assign(n_y, y, gcd);
41 }
42 
43 template <typename T>
44 inline T
45 low_bits_mask(const unsigned n) {
46  PPL_ASSERT(n < unsigned(std::numeric_limits<T>::digits));
47  return ~((~static_cast<T>(0)) << n);
48 }
49 
50 template <typename T>
51 inline typename Enable_If<Is_Native_Or_Checked<T>::value, void>::type
52 numer_denom(const T& from,
53  Coefficient& numer, Coefficient& denom) {
54  PPL_ASSERT(!is_not_a_number(from)
55  && !is_minus_infinity(from)
56  && !is_plus_infinity(from));
57  PPL_DIRTY_TEMP(mpq_class, q);
58  assign_r(q, from, ROUND_NOT_NEEDED);
59  numer = q.get_num();
60  denom = q.get_den();
61 }
62 
63 template <typename T>
64 inline typename Enable_If<Is_Native_Or_Checked<T>::value, void>::type
66  Coefficient_traits::const_reference x,
67  Coefficient_traits::const_reference y) {
68  PPL_DIRTY_TEMP(mpq_class, q_x);
69  PPL_DIRTY_TEMP(mpq_class, q_y);
70  // Note: this code assumes that a Coefficient is always convertible
71  // to an mpq_class without loss of precision.
72  assign_r(q_x, x, ROUND_NOT_NEEDED);
73  assign_r(q_y, y, ROUND_NOT_NEEDED);
74  div_assign_r(q_x, q_x, q_y, ROUND_NOT_NEEDED);
75  assign_r(to, q_x, ROUND_UP);
76 }
77 
78 template <typename N>
79 inline void
80 min_assign(N& x, const N& y) {
81  if (x > y) {
82  x = y;
83  }
84 }
85 
86 template <typename N>
87 inline void
88 max_assign(N& x, const N& y) {
89  if (x < y) {
90  x = y;
91  }
92 }
93 
94 template <typename T>
95 inline typename Enable_If<Is_Native_Or_Checked<T>::value, bool>::type
96 is_even(const T& x) {
97  T mod;
98  return umod_2exp_assign_r(mod, x, 1, ROUND_DIRECT | ROUND_STRICT_RELATION) == V_EQ
99  && mod == 0;
100 }
101 
102 template <typename T>
103 inline typename Enable_If<Is_Native_Or_Checked<T>::value, bool>::type
104 is_additive_inverse(const T& x, const T& y) {
105  T negated_x;
106  return neg_assign_r(negated_x, x, ROUND_DIRECT | ROUND_STRICT_RELATION) == V_EQ
107  && negated_x == y;
108 }
109 
110 inline bool
111 is_canonical(const mpq_class& x) {
112  if (x.get_den() <= 0) {
113  return false;
114  }
115  PPL_DIRTY_TEMP(mpq_class, temp);
116  temp = x;
117  temp.canonicalize();
118  return temp.get_num() == x.get_num();
119 }
120 
121 } // namespace Parma_Polyhedra_Library
122 
123 #endif // !defined(PPL_math_utilities_inlines_hh)
Enable_If< Is_Native_Or_Checked< To >::value &&Is_Special< From >::value, Result >::type assign_r(To &to, const From &, Rounding_Dir dir)
Enable_If< Is_Native_Or_Checked< T >::value, bool >::type is_minus_infinity(const T &x)
The computed result is exact.
Definition: Result_defs.hh:81
Enable_If< Is_Native_Or_Checked< T >::value, void >::type numer_denom(const T &from, Coefficient &numer, Coefficient &denom)
Extract the numerator and denominator components of from.
#define PPL_DIRTY_TEMP_COEFFICIENT(id)
Declare a local variable named id, of type Coefficient, and containing an unknown initial value...
void max_assign(N &x, const N &y)
Assigns to x the maximum between x and y.
void exact_div_assign(Checked_Number< T, Policy > &x, const Checked_Number< T, Policy > &y, const Checked_Number< T, Policy > &z)
T low_bits_mask(unsigned n)
Returns a mask for the lowest n bits,.
Enable_If< Is_Native_Or_Checked< T >::value, void >::type div_round_up(T &to, Coefficient_traits::const_reference x, Coefficient_traits::const_reference y)
Divides x by y into to, rounding the result towards plus infinity.
bool is_canonical(const mpq_class &x)
Returns true if and only if x is in canonical form.
Enable_If< Is_Native_Or_Checked< T >::value, bool >::type is_even(const T &x)
Returns true if and only if x is an even number.
Enable_If< Is_Native_Or_Checked< T >::value, bool >::type is_not_a_number(const T &x)
Enable_If< Is_Native_Or_Checked< T >::value, bool >::type is_plus_infinity(const T &x)
Coefficient value
Definition: PIP_Tree.cc:618
PPL_COEFFICIENT_TYPE Coefficient
An alias for easily naming the type of PPL coefficients.
Enable_If< Is_Native_Or_Checked< T >::value, bool >::type is_additive_inverse(const T &x, const T &y)
Returns true if and only if .
#define PPL_DIRTY_TEMP(T, id)
The entire library is confined to this namespace.
Definition: version.hh:61
void gcd_assign(GMP_Integer &x, const GMP_Integer &y, const GMP_Integer &z)
void normalize2(Coefficient_traits::const_reference x, Coefficient_traits::const_reference y, Coefficient &n_x, Coefficient &n_y)
If is the GCD of x and y, the values of x and y divided by are assigned to n_x and n_y...
void min_assign(N &x, const N &y)
Assigns to x the minimum between x and y.