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 <algorithm>
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 
194  const mp_integer &a,
195  const mp_integer &b,
196  std::function<bool(bool, bool)> f)
197 {
198  const auto digits = std::max(a.digits(2), b.digits(2));
199 
200  mp_integer result = 0;
201  mp_integer tmp_a = a, tmp_b = b;
202 
203  for(std::size_t i = 0; i < digits; i++)
204  {
205  const bool bit_a = tmp_a.is_odd();
206  const bool bit_b = tmp_b.is_odd();
207  const bool bit_result = f(bit_a, bit_b);
208  if(bit_result)
209  result += power(2, i);
210  tmp_a /= 2;
211  tmp_b /= 2;
212  }
213 
214  return result;
215 }
216 
219 {
220  PRECONDITION(!a.is_negative() && !b.is_negative());
221 
222  // fast path for small numbers
223  if(a.is_ulong() && b.is_ulong())
224  return a.to_ulong() | b.to_ulong();
225 
226  return bitwise(a, b, [](bool a, bool b) { return a || b; });
227 }
228 
231 {
232  PRECONDITION(!a.is_negative() && !b.is_negative());
233 
234  // fast path for small numbers
235  if(a.is_ulong() && b.is_ulong())
236  return a.to_ulong() & b.to_ulong();
237 
238  return bitwise(a, b, [](bool a, bool b) { return a && b; });
239 }
240 
243 {
244  PRECONDITION(!a.is_negative() && !b.is_negative());
245 
246  // fast path for small numbers
247  if(a.is_ulong() && b.is_ulong())
248  return a.to_ulong() ^ b.to_ulong();
249 
250  return bitwise(a, b, [](bool a, bool b) { return a != b; });
251 }
252 
257  const mp_integer &a,
258  const mp_integer &b,
259  std::size_t true_size)
260 {
261  PRECONDITION(a.is_long() && b.is_ulong());
262  PRECONDITION(b <= true_size || a == 0);
263 
264  ullong_t shift=b.to_ulong();
265 
266  llong_t result=a.to_long()<<shift;
267  llong_t mask=
268  true_size<(sizeof(llong_t)*8) ?
269  (1LL << true_size) - 1 :
270  -1;
271  return result&mask;
272 }
273 
278  const mp_integer &a,
279  const mp_integer &b,
280  std::size_t true_size)
281 {
282  PRECONDITION(a.is_long() && b.is_ulong());
283  llong_t number=a.to_long();
284  ullong_t shift=b.to_ulong();
285  PRECONDITION(shift <= true_size);
286 
287  const llong_t sign = (1LL << (true_size - 1)) & number;
288  const llong_t pad = (sign == 0) ? 0 : ~((1LL << (true_size - shift)) - 1);
289  llong_t result=(number >> shift)|pad;
290  return result;
291 }
292 
297  const mp_integer &a,
298  const mp_integer &b,
299  std::size_t true_size)
300 {
301  PRECONDITION(a.is_long() && b.is_ulong());
302  PRECONDITION(b <= true_size || a == 0);
303 
304  ullong_t shift=b.to_ulong();
305  llong_t result=a.to_long()<<shift;
306  if(true_size<(sizeof(llong_t)*8))
307  {
308  const llong_t sign = (1LL << (true_size - 1)) & result;
309  const llong_t mask = (1LL << true_size) - 1;
310  // Sign-fill out-of-range bits:
311  if(sign==0)
312  result&=mask;
313  else
314  result|=~mask;
315  }
316  return result;
317 }
318 
323  const mp_integer &a,
324  const mp_integer &b,
325  std::size_t true_size)
326 {
327  PRECONDITION(a.is_long() && b.is_ulong());
328  PRECONDITION(b <= true_size);
329 
330  ullong_t shift = b.to_ulong();
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  PRECONDITION(b <= true_size);
345 
346  ullong_t number=a.to_ulong();
347  ullong_t shift=b.to_ulong();
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  PRECONDITION(b <= true_size);
365 
366  ullong_t number=a.to_ulong();
367  ullong_t shift=b.to_ulong();
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 }
rotate_right
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
arith_tools.h
mp_arith.h
mp_integer
BigInt mp_integer
Definition: mp_arith.h:19
is_signed
bool is_signed(const typet &t)
Convenience function – is the type signed?
Definition: util.cpp:45
string2integer
const mp_integer string2integer(const std::string &n, unsigned base)
Definition: mp_arith.cpp:57
bitwise_and
mp_integer bitwise_and(const mp_integer &a, const mp_integer &b)
bitwise 'and' of two nonnegative integers
Definition: mp_arith.cpp:230
operator>>
mp_integer operator>>(const mp_integer &a, const mp_integer &b)
Definition: mp_arith.cpp:25
bitwise
mp_integer bitwise(const mp_integer &a, const mp_integer &b, std::function< bool(bool, bool)> f)
bitwise binary operation over two integers, given as a functor
Definition: mp_arith.cpp:193
operator<<
mp_integer operator<<(const mp_integer &a, const mp_integer &b)
Definition: mp_arith.cpp:43
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:464
logic_right_shift
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,...
Definition: mp_arith.cpp:322
pad
static struct_typet::componentst::iterator pad(struct_typet::componentst &components, struct_typet::componentst::iterator where, std::size_t pad_bits)
Definition: padding.cpp:151
llong_t
BigInt::llong_t llong_t
Definition: mp_arith.cpp:23
arith_right_shift
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:277
arith_left_shift
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:256
bitwise_xor
mp_integer bitwise_xor(const mp_integer &a, const mp_integer &b)
bitwise 'xor' of two nonnegative integers
Definition: mp_arith.cpp:242
logic_left_shift
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:296
binary2integer
const mp_integer binary2integer(const std::string &n, bool is_signed)
convert binary string representation to mp_integer
Definition: mp_arith.cpp:120
invariant.h
ullong_t
BigInt::ullong_t ullong_t
Definition: mp_arith.cpp:22
power
mp_integer power(const mp_integer &base, const mp_integer &exponent)
A multi-precision implementation of the power operator.
Definition: arith_tools.cpp:194
integer2binary
const std::string integer2binary(const mp_integer &n, std::size_t width)
Definition: mp_arith.cpp:67
neg
literalt neg(literalt a)
Definition: literal.h:193
bitwise_or
mp_integer bitwise_or(const mp_integer &a, const mp_integer &b)
bitwise 'or' of two nonnegative integers
Definition: mp_arith.cpp:218
rotate_left
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
integer2string
const std::string integer2string(const mp_integer &n, unsigned base)
Definition: mp_arith.cpp:106