1 /* Implementation of global objects: 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_globals_inlines_hh
25 #define PPL_globals_inlines_hh 1
26 
27 #include "compiler.hh"
28 #include <limits>
29 #include <cassert>
30 #include <istream>
31 #include <ostream>
32 #include <cctype>
33 #include <stdexcept>
34 
35 namespace Parma_Polyhedra_Library {
36 
37 inline dimension_type
not_a_dimension()38 not_a_dimension() {
39   return std::numeric_limits<dimension_type>::max();
40 }
41 
42 inline int32_t
hash_code_from_dimension(dimension_type dim)43 hash_code_from_dimension(dimension_type dim) {
44   const dimension_type divisor = 1U << (32 - 1);
45   dim = dim % divisor;
46   return static_cast<int32_t>(dim);
47 }
48 
49 inline const Weightwatch_Traits::Threshold&
get()50 Weightwatch_Traits::get() {
51   return weight;
52 }
53 
54 inline bool
less_than(const Threshold & a,const Threshold & b)55 Weightwatch_Traits::less_than(const Threshold& a, const Threshold& b) {
56   return b - a < (1ULL << (sizeof_to_bits(sizeof(Threshold)) - 1));
57 }
58 
59 inline Weightwatch_Traits::Delta
compute_delta(unsigned long unscaled,unsigned scale)60 Weightwatch_Traits::compute_delta(unsigned long unscaled, unsigned scale) {
61   if ((std::numeric_limits<Delta>::max() >> scale) < unscaled) {
62     throw std::invalid_argument("PPL::Weightwatch_Traits::"
63                                 "compute_delta(u, s):\n"
64                                 "values of u and s cause wrap around.");
65   }
66   return static_cast<Delta>(unscaled) << scale;
67 }
68 
69 inline void
from_delta(Threshold & threshold,const Delta & delta)70 Weightwatch_Traits::from_delta(Threshold& threshold, const Delta& delta) {
71   threshold = weight + delta;
72 }
73 
74 inline void
maybe_abandon()75 maybe_abandon() {
76 #ifndef NDEBUG
77   if (In_Assert::asserting()) {
78     return;
79   }
80 #endif
81   if (Weightwatch_Traits::check_function != 0) {
82     Weightwatch_Traits::check_function();
83   }
84   if (const Throwable* const p = abandon_expensive_computations) {
85     p->throw_me();
86   }
87 }
88 
89 inline dimension_type
compute_capacity(const dimension_type requested_size,const dimension_type maximum_size)90 compute_capacity(const dimension_type requested_size,
91                  const dimension_type maximum_size) {
92   assert(requested_size <= maximum_size);
93   // Speculation factor 2.
94   return (requested_size < maximum_size/2)
95     ? (2*(requested_size + 1))
96     : maximum_size;
97   // Speculation factor 1.5.
98   // return (maximum_size - requested_size > requested_size/2)
99   //   ? requested_size + requested_size/2 + 1
100   //   : maximum_size;
101 }
102 
103 template <typename T>
104 inline typename
105 Enable_If<Is_Native<T>::value, memory_size_type>::type
external_memory_in_bytes(const T &)106 external_memory_in_bytes(const T&) {
107   return 0;
108 }
109 
110 template <typename T>
111 inline typename
112 Enable_If<Is_Native<T>::value, memory_size_type>::type
total_memory_in_bytes(const T &)113 total_memory_in_bytes(const T&) {
114   return sizeof(T);
115 }
116 
117 inline memory_size_type
external_memory_in_bytes(const mpz_class & x)118 external_memory_in_bytes(const mpz_class& x) {
119   return static_cast<memory_size_type>(x.get_mpz_t()[0]._mp_alloc)
120     * PPL_SIZEOF_MP_LIMB_T;
121 }
122 
123 inline memory_size_type
total_memory_in_bytes(const mpz_class & x)124 total_memory_in_bytes(const mpz_class& x) {
125   return sizeof(x) + external_memory_in_bytes(x);
126 }
127 
128 inline memory_size_type
external_memory_in_bytes(const mpq_class & x)129 external_memory_in_bytes(const mpq_class& x) {
130   return external_memory_in_bytes(x.get_num())
131     + external_memory_in_bytes(x.get_den());
132 }
133 
134 inline memory_size_type
total_memory_in_bytes(const mpq_class & x)135 total_memory_in_bytes(const mpq_class& x) {
136   return sizeof(x) + external_memory_in_bytes(x);
137 }
138 
139 inline void
ascii_dump(std::ostream & s,Representation r)140 ascii_dump(std::ostream& s, Representation r) {
141   if (r == DENSE) {
142     s << "DENSE";
143   }
144   else {
145     s << "SPARSE";
146   }
147 }
148 
149 inline bool
ascii_load(std::istream & is,Representation & r)150 ascii_load(std::istream& is, Representation& r) {
151   std::string s;
152   if (!(is >> s)) {
153     return false;
154   }
155 
156   if (s == "DENSE")  {
157     r = DENSE;
158     return true;
159   }
160   if (s == "SPARSE")  {
161     r = SPARSE;
162     return true;
163   }
164   return false;
165 }
166 
167 inline bool
is_space(char c)168 is_space(char c) {
169   return isspace(c) != 0;
170 }
171 
172 template <typename RA_Container>
173 inline typename RA_Container::iterator
nth_iter(RA_Container & cont,dimension_type n)174 nth_iter(RA_Container& cont, dimension_type n) {
175   typedef typename RA_Container::difference_type diff_t;
176   return cont.begin() + static_cast<diff_t>(n);
177 }
178 
179 template <typename RA_Container>
180 inline typename RA_Container::const_iterator
nth_iter(const RA_Container & cont,dimension_type n)181 nth_iter(const RA_Container& cont, dimension_type n) {
182   typedef typename RA_Container::difference_type diff_t;
183   return cont.begin() + static_cast<diff_t>(n);
184 }
185 
186 inline dimension_type
least_significant_one_mask(const dimension_type i)187 least_significant_one_mask(const dimension_type i) {
188   return i & (~i + 1U);
189 }
190 
191 } // namespace Parma_Polyhedra_Library
192 
193 #endif // !defined(PPL_globals_inlines_hh)
194