cprover
mp_arith.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 #include "mp_arith.h"
10 
11 #include <cassert>
12 #include <cctype>
13 #include <cstdlib>
14 #include <limits>
15 #include <ostream>
16 #include <sstream>
17 #include <vector>
18 
19 #include "arith_tools.h"
20 #include "invariant.h"
21 
22 typedef BigInt::ullong_t ullong_t; // NOLINT(readability/identifiers)
23 typedef BigInt::llong_t llong_t; // NOLINT(readability/identifiers)
24 
26 {
27  mp_integer power=::power(2, b);
28 
29  if(a>=0)
30  return a/power;
31  else
32  {
33  // arithmetic shift right isn't division for negative numbers!
34  // http://en.wikipedia.org/wiki/Arithmetic_shift
35 
36  if((a%power)==0)
37  return a/power;
38  else
39  return a/power-1;
40  }
41 }
42 
44 {
45  return a*power(2, b);
46 }
47 
48 std::ostream &operator<<(std::ostream &out, const mp_integer &n)
49 {
50  out << integer2string(n);
51  return out;
52 }
53 
57 const mp_integer string2integer(const std::string &n, unsigned base)
58 {
59  for(std::size_t i=0; i<n.size(); i++)
60  if(!(isalnum(n[i]) || (n[i]=='-' && i==0)))
61  return 0;
62 
63  return mp_integer(n.c_str(), base);
64 }
65 
67 const std::string integer2binary(const mp_integer &n, std::size_t width)
68 {
69  mp_integer a(n);
70 
71  if(width==0)
72  return "";
73 
74  bool neg=a.is_negative();
75 
76  if(neg)
77  {
78  a.negate();
79  a=a-1;
80  }
81 
82  std::size_t len = a.digits(2) + 2;
83  std::vector<char> buffer(len);
84  char *s = a.as_string(buffer.data(), len, 2);
85 
86  std::string result(s);
87 
88  if(result.size()<width)
89  {
90  std::string fill;
91  fill.resize(width-result.size(), '0');
92  result=fill+result;
93  }
94  else if(result.size()>width)
95  result=result.substr(result.size()-width, width);
96 
97  if(neg)
98  {
99  for(std::size_t i=0; i<result.size(); i++)
100  result[i]=(result[i]=='0')?'1':'0';
101  }
102 
103  return result;
104 }
105 
106 const std::string integer2string(const mp_integer &n, unsigned base)
107 {
108  unsigned len = n.digits(base) + 2;
109  std::vector<char> buffer(len);
110  char *s = n.as_string(buffer.data(), len, base);
111 
112  std::string result(s);
113 
114  return result;
115 }
116 
120 const mp_integer binary2integer(const std::string &n, bool is_signed)
121 {
122  if(n.empty())
123  return 0;
124 
125  if(n.size()<=(sizeof(unsigned long)*8))
126  {
127  // this is a tuned implementation for short integers
128 
129  unsigned long mask=1;
130  mask=mask << (n.size()-1);
131  mp_integer top_bit=(n[0]=='1') ? mask : 0;
132  if(is_signed)
133  top_bit.negate();
134  mask>>=1;
135  unsigned long other_bits=0;
136 
137  for(std::string::const_iterator it=++n.begin();
138  it!=n.end();
139  ++it)
140  {
141  if(*it=='1')
142  other_bits+=mask;
143  else if(*it!='0')
144  return 0;
145 
146  mask>>=1;
147  }
148 
149  return top_bit+other_bits;
150  }
151 
152  #if 0
153 
154  mp_integer mask=1;
155  mask=mask << (n.size()-1);
156  mp_integer result=(n[0]=='1') ? mask : 0;
157  if(is_signed)
158  result.negate();
159  mask=mask>>1;
160 
161  for(std::string::const_iterator it=++n.begin();
162  it!=n.end();
163  ++it)
164  {
165  if(*it=='1')
166  result+=mask;
167 
168  mask=mask>>1;
169  }
170 
171  return result;
172 
173  #else
174  if(n.find_first_not_of("01")!=std::string::npos)
175  return 0;
176 
177  if(is_signed && n[0]=='1')
178  {
179  mp_integer result(n.c_str()+1, 2);
180  result-=mp_integer(1)<<(n.size()-1);
181  return result;
182  }
183  else
184  return BigInt(n.c_str(), 2);
185 
186  #endif
187 }
188 
190 {
191  PRECONDITION(n.is_ulong());
192  return n.to_ulong();
193 }
194 
195 std::size_t integer2size_t(const mp_integer &n)
196 {
197  PRECONDITION(n>=0 && n<=std::numeric_limits<std::size_t>::max());
198  PRECONDITION(n.is_ulong());
199  mp_integer::ullong_t ull = n.to_ulong();
200  return (std::size_t) ull;
201 }
202 
203 unsigned integer2unsigned(const mp_integer &n)
204 {
205  PRECONDITION(n>=0 && n<=std::numeric_limits<unsigned>::max());
206  PRECONDITION(n.is_ulong());
207  mp_integer::ullong_t ull = n.to_ulong();
208  return (unsigned)ull;
209 }
210 
215 {
216  PRECONDITION(a.is_ulong() && b.is_ulong());
217  ullong_t result=a.to_ulong()|b.to_ulong();
218  return result;
219 }
220 
225 {
226  PRECONDITION(a.is_ulong() && b.is_ulong());
227  ullong_t result=a.to_ulong()&b.to_ulong();
228  return result;
229 }
230 
235 {
236  PRECONDITION(a.is_ulong() && b.is_ulong());
237  ullong_t result=a.to_ulong()^b.to_ulong();
238  return result;
239 }
240 
245 {
246  PRECONDITION(a.is_ulong());
247  ullong_t result=~a.to_ulong();
248  return result;
249 }
250 
255  const mp_integer &a,
256  const mp_integer &b,
257  std::size_t true_size)
258 {
259  PRECONDITION(a.is_long() && b.is_ulong());
260  ullong_t shift=b.to_ulong();
261  if(shift>true_size && a!=mp_integer(0))
262  throw "shift value out of range";
263 
264  llong_t result=a.to_long()<<shift;
265  llong_t mask=
266  true_size<(sizeof(llong_t)*8) ?
267  (1LL << true_size) - 1 :
268  -1;
269  return result&mask;
270 }
271 
276  const mp_integer &a,
277  const mp_integer &b,
278  std::size_t true_size)
279 {
280  PRECONDITION(a.is_long() && b.is_ulong());
281  llong_t number=a.to_long();
282  ullong_t shift=b.to_ulong();
283  if(shift>true_size)
284  throw "shift value out of range";
285 
286  const llong_t sign = (1LL << (true_size - 1)) & number;
287  const llong_t pad = (sign == 0) ? 0 : ~((1LL << (true_size - shift)) - 1);
288  llong_t result=(number >> shift)|pad;
289  return result;
290 }
291 
296  const mp_integer &a,
297  const mp_integer &b,
298  std::size_t true_size)
299 {
300  PRECONDITION(a.is_long() && b.is_ulong());
301  ullong_t shift=b.to_ulong();
302  if(shift>true_size && a!=mp_integer(0))
303  throw "shift value out of range";
304  llong_t result=a.to_long()<<shift;
305  if(true_size<(sizeof(llong_t)*8))
306  {
307  const llong_t sign = (1LL << (true_size - 1)) & result;
308  const llong_t mask = (1LL << true_size) - 1;
309  // Sign-fill out-of-range bits:
310  if(sign==0)
311  result&=mask;
312  else
313  result|=~mask;
314  }
315  return result;
316 }
317 
322  const mp_integer &a,
323  const mp_integer &b,
324  std::size_t true_size)
325 {
326  PRECONDITION(a.is_long() && b.is_ulong());
327  ullong_t shift=b.to_ulong();
328  if(shift>true_size)
329  throw "shift value out of range";
330 
331  ullong_t result=((ullong_t)a.to_long()) >> shift;
332  return result;
333 }
334 
339  const mp_integer &a,
340  const mp_integer &b,
341  std::size_t true_size)
342 {
343  PRECONDITION(a.is_ulong() && b.is_ulong());
344  ullong_t number=a.to_ulong();
345  ullong_t shift=b.to_ulong();
346  if(shift>true_size)
347  throw "shift value out of range";
348 
349  ullong_t revShift=true_size-shift;
350  const ullong_t filter = 1ULL << (true_size - 1);
351  ullong_t result=(number >> shift)|((number<<revShift)&filter);
352  return result;
353 }
354 
359  const mp_integer &a,
360  const mp_integer &b,
361  std::size_t true_size)
362 {
363  PRECONDITION(a.is_ulong() && b.is_ulong());
364  ullong_t number=a.to_ulong();
365  ullong_t shift=b.to_ulong();
366  if(shift>true_size)
367  throw "shift value out of range";
368 
369  ullong_t revShift=true_size-shift;
370  const ullong_t filter = 1ULL << (true_size - 1);
371  ullong_t result=((number<<shift)&filter)|((number&filter) >> revShift);
372  return result;
373 }
bool is_signed(const typet &t)
Convenience function – is the type signed?
Definition: util.cpp:45
BigInt mp_integer
Definition: mp_arith.h:22
mp_integer rotate_left(const mp_integer &a, const mp_integer &b, std::size_t true_size)
rotate left (LSB=MSB) bitwise operations only make sense on native objects, hence the largest object ...
Definition: mp_arith.cpp:358
const mp_integer string2integer(const std::string &n, unsigned base)
Definition: mp_arith.cpp:57
const std::string integer2string(const mp_integer &n, unsigned base)
Definition: mp_arith.cpp:106
mp_integer::ullong_t integer2ulong(const mp_integer &n)
Definition: mp_arith.cpp:189
mp_integer bitwise_xor(const mp_integer &a, const mp_integer &b)
bitwise xor bitwise operations only make sense on native objects, hence the largest object size shoul...
Definition: mp_arith.cpp:234
const mp_integer binary2integer(const std::string &n, bool is_signed)
convert binary string representation to mp_integer
Definition: mp_arith.cpp:120
mp_integer arith_left_shift(const mp_integer &a, const mp_integer &b, std::size_t true_size)
arithmetic left shift bitwise operations only make sense on native objects, hence the largest object ...
Definition: mp_arith.cpp:254
mp_integer logic_left_shift(const mp_integer &a, const mp_integer &b, std::size_t true_size)
logic left shift bitwise operations only make sense on native objects, hence the largest object size ...
Definition: mp_arith.cpp:295
static struct_typet::componentst::iterator pad(struct_typet::componentst &components, struct_typet::componentst::iterator where, std::size_t pad_bits)
Definition: padding.cpp:156
mp_integer operator<<(const mp_integer &a, const mp_integer &b)
Definition: mp_arith.cpp:43
mp_integer logic_right_shift(const mp_integer &a, const mp_integer &b, std::size_t true_size)
logic right shift (loads 0 on MSB) bitwise operations only make sense on native objects, hence the largest object size should be the largest available c++ integer size (currently long long)
Definition: mp_arith.cpp:321
mp_integer bitwise_and(const mp_integer &a, const mp_integer &b)
bitwise and bitwise operations only make sense on native objects, hence the largest object size shoul...
Definition: mp_arith.cpp:224
mp_integer arith_right_shift(const mp_integer &a, const mp_integer &b, std::size_t true_size)
arithmetic right shift (loads sign on MSB) bitwise operations only make sense on native objects...
Definition: mp_arith.cpp:275
mp_integer bitwise_neg(const mp_integer &a)
bitwise negation bitwise operations only make sense on native objects, hence the largest object size ...
Definition: mp_arith.cpp:244
mp_integer operator>>(const mp_integer &a, const mp_integer &b)
Definition: mp_arith.cpp:25
#define PRECONDITION(CONDITION)
Definition: invariant.h:242
mp_integer rotate_right(const mp_integer &a, const mp_integer &b, std::size_t true_size)
rotates right (MSB=LSB) bitwise operations only make sense on native objects, hence the largest objec...
Definition: mp_arith.cpp:338
BigInt::llong_t llong_t
Definition: mp_arith.cpp:23
mp_integer bitwise_or(const mp_integer &a, const mp_integer &b)
bitwise or bitwise operations only make sense on native objects, hence the largest object size should...
Definition: mp_arith.cpp:214
unsigned integer2unsigned(const mp_integer &n)
Definition: mp_arith.cpp:203
mstreamt & result() const
Definition: message.h:312
BigInt::ullong_t ullong_t
Definition: mp_arith.cpp:22
const std::string integer2binary(const mp_integer &n, std::size_t width)
Definition: mp_arith.cpp:67
literalt neg(literalt a)
Definition: literal.h:192
std::size_t integer2size_t(const mp_integer &n)
Definition: mp_arith.cpp:195
mp_integer power(const mp_integer &base, const mp_integer &exponent)
A multi-precision implementation of the power operator.