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
normalize2(Coefficient_traits::const_reference x,Coefficient_traits::const_reference y,Coefficient & n_x,Coefficient & n_y)34 normalize2(Coefficient_traits::const_reference x,
35            Coefficient_traits::const_reference y,
36            Coefficient& n_x, Coefficient& n_y) {
37   PPL_DIRTY_TEMP_COEFFICIENT(gcd);
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
low_bits_mask(const unsigned n)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
numer_denom(const T & from,Coefficient & numer,Coefficient & denom)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
div_round_up(T & to,Coefficient_traits::const_reference x,Coefficient_traits::const_reference y)65 div_round_up(T& to,
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
min_assign(N & x,const N & y)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
max_assign(N & x,const N & y)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
is_even(const T & x)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
is_additive_inverse(const T & x,const T & y)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
is_canonical(const mpq_class & x)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)
124