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());
199  return (std::size_t) ull;
200 }
201 
202 unsigned integer2unsigned(const mp_integer &n)
203 {
204  PRECONDITION(n>=0 && n<=std::numeric_limits<unsigned>::max());
206  return (unsigned)ull;
207 }
208 
213 {
214  PRECONDITION(a.is_ulong() && b.is_ulong());
215  ullong_t result=a.to_ulong()|b.to_ulong();
216  return result;
217 }
218 
223 {
224  PRECONDITION(a.is_ulong() && b.is_ulong());
225  ullong_t result=a.to_ulong()&b.to_ulong();
226  return result;
227 }
228 
233 {
234  PRECONDITION(a.is_ulong() && b.is_ulong());
235  ullong_t result=a.to_ulong()^b.to_ulong();
236  return result;
237 }
238 
243 {
244  PRECONDITION(a.is_ulong());
245  ullong_t result=~a.to_ulong();
246  return result;
247 }
248 
253  const mp_integer &a,
254  const mp_integer &b,
255  std::size_t true_size)
256 {
257  PRECONDITION(a.is_long() && b.is_ulong());
258  ullong_t shift=b.to_ulong();
259  if(shift>true_size && a!=mp_integer(0))
260  throw "shift value out of range";
261 
262  llong_t result=a.to_long()<<shift;
263  llong_t mask=
264  true_size<(sizeof(llong_t)*8) ?
265  (1L<<true_size)-1 :
266  -1;
267  return result&mask;
268 }
269 
274  const mp_integer &a,
275  const mp_integer &b,
276  std::size_t true_size)
277 {
278  PRECONDITION(a.is_long() && b.is_ulong());
279  llong_t number=a.to_long();
280  ullong_t shift=b.to_ulong();
281  if(shift>true_size)
282  throw "shift value out of range";
283 
284  llong_t sign=(1<<(true_size-1))&number;
285  llong_t pad=(sign==0) ? 0 : ~((1<<(true_size-shift))-1);
286  llong_t result=(number >> shift)|pad;
287  return result;
288 }
289 
294  const mp_integer &a,
295  const mp_integer &b,
296  std::size_t true_size)
297 {
298  PRECONDITION(a.is_long() && b.is_ulong());
299  ullong_t shift=b.to_ulong();
300  if(shift>true_size && a!=mp_integer(0))
301  throw "shift value out of range";
302  llong_t result=a.to_long()<<shift;
303  if(true_size<(sizeof(llong_t)*8))
304  {
305  llong_t sign=(1L<<(true_size-1))&result;
306  llong_t mask=(1L<<true_size)-1;
307  // Sign-fill out-of-range bits:
308  if(sign==0)
309  result&=mask;
310  else
311  result|=~mask;
312  }
313  return result;
314 }
315 
320  const mp_integer &a,
321  const mp_integer &b,
322  std::size_t true_size)
323 {
324  PRECONDITION(a.is_long() && b.is_ulong());
325  ullong_t shift=b.to_ulong();
326  if(shift>true_size)
327  throw "shift value out of range";
328 
329  ullong_t result=((ullong_t)a.to_long()) >> shift;
330  return result;
331 }
332 
337  const mp_integer &a,
338  const mp_integer &b,
339  std::size_t true_size)
340 {
341  PRECONDITION(a.is_ulong() && b.is_ulong());
342  ullong_t number=a.to_ulong();
343  ullong_t shift=b.to_ulong();
344  if(shift>true_size)
345  throw "shift value out of range";
346 
347  ullong_t revShift=true_size-shift;
348  ullong_t filter=1<<(true_size-1);
349  ullong_t result=(number >> shift)|((number<<revShift)&filter);
350  return result;
351 }
352 
357  const mp_integer &a,
358  const mp_integer &b,
359  std::size_t true_size)
360 {
361  PRECONDITION(a.is_ulong() && b.is_ulong());
362  ullong_t number=a.to_ulong();
363  ullong_t shift=b.to_ulong();
364  if(shift>true_size)
365  throw "shift value out of range";
366 
367  ullong_t revShift=true_size-shift;
368  ullong_t filter=1<<(true_size-1);
369  ullong_t result=((number<<shift)&filter)|((number&filter) >> revShift);
370  return result;
371 }
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:356
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:232
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:252
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:293
static struct_typet::componentst::iterator pad(struct_typet::componentst &components, struct_typet::componentst::iterator where, std::size_t pad_bits)
Definition: padding.cpp:152
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:319
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:222
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:273
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:242
mp_integer operator>>(const mp_integer &a, const mp_integer &b)
Definition: mp_arith.cpp:25
#define PRECONDITION(CONDITION)
Definition: invariant.h:230
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:336
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:212
unsigned integer2unsigned(const mp_integer &n)
Definition: mp_arith.cpp:202
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.