CBMC
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
bv_arithmetic.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module:
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
9#include "bv_arithmetic.h"
10
11#include "arith_tools.h"
12#include "bitvector_types.h"
13#include "std_expr.h"
14
16{
17 if(is_signed)
18 return signedbv_typet(width);
19 return unsignedbv_typet(width);
20}
21
23{
24 return is_signed?power(2, width-1)-1:
25 power(2, width)-1;
26}
27
29{
30 return is_signed?-power(2, width-1):
31 0;
32}
33
34void bv_spect::from_type(const typet &type)
35{
36 if(type.id()==ID_unsignedbv)
37 is_signed=false;
38 else if(type.id()==ID_signedbv)
39 is_signed=true;
40 else
42
43 width = to_bitvector_type(type).get_width();
44}
45
46void bv_arithmetict::print(std::ostream &out) const
47{
48 out << to_ansi_c_string();
49}
50
51std::string bv_arithmetict::format(const format_spect &) const
52{
53 std::string result;
54
55 result=integer2string(value);
56
57 return result;
58}
59
61{
62 value=i;
63 adjust();
64}
65
67{
69 value%=p;
70
71 if(value>=p/2)
72 value-=p;
73}
74
76{
77 if(value>=0)
78 return value;
79 return value+power(2, spec.width);
80}
81
86
88{
89 PRECONDITION(other.spec == spec);
90
91 if(other.value==0)
92 value=0;
93 else
94 value/=other.value;
95
96 return *this;
97}
98
100{
101 PRECONDITION(other.spec == spec);
102
103 value*=other.value;
104 adjust();
105
106 return *this;
107}
108
110{
111 PRECONDITION(other.spec == spec);
112
113 value+=other.value;
114 adjust();
115
116 return *this;
117}
118
120{
121 PRECONDITION(other.spec == spec);
122
123 value-=other.value;
124 adjust();
125
126 return *this;
127}
128
130{
131 PRECONDITION(other.spec == spec);
132
133 value%=other.value;
134 adjust();
135
136 return *this;
137}
138
140{
141 return value<other.value;
142}
143
145{
146 return value<=other.value;
147}
148
150{
151 return value>other.value;
152}
153
155{
156 return value>=other.value;
157}
158
160{
161 return value==other.value;
162}
163
165{
166 return value==i;
167}
168
170{
171 return value!=other.value;
172}
173
179
mp_integer bvrep2integer(const irep_idt &src, std::size_t width, bool is_signed)
convert a bit-vector representation (possibly signed) to integer
irep_idt integer2bvrep(const mp_integer &src, std::size_t width)
convert an integer to bit-vector representation with given width This uses two's complement for negat...
mp_integer power(const mp_integer &base, const mp_integer &exponent)
A multi-precision implementation of the power operator.
Pre-defined bitvector types.
const bitvector_typet & to_bitvector_type(const typet &type)
Cast a typet to a bitvector_typet.
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
bool operator==(const bv_arithmetict &other)
bool operator>=(const bv_arithmetict &other)
mp_integer value
bool operator<(const bv_arithmetict &other)
void change_spec(const bv_spect &dest_spec)
constant_exprt to_expr() const
void from_integer(const mp_integer &i)
bv_arithmetict & operator*=(const bv_arithmetict &other)
bool operator<=(const bv_arithmetict &other)
void print(std::ostream &out) const
std::string format(const format_spect &format_spec) const
bv_arithmetict & operator+=(const bv_arithmetict &other)
bool operator!=(const bv_arithmetict &other)
void from_expr(const constant_exprt &expr)
bv_arithmetict & operator%=(const bv_arithmetict &other)
bv_arithmetict & operator-=(const bv_arithmetict &other)
std::string to_ansi_c_string() const
bv_arithmetict & operator/=(const bv_arithmetict &other)
bool operator>(const bv_arithmetict &other)
mp_integer pack() const
std::size_t width
bool is_signed
void from_type(const typet &type)
mp_integer min_value() const
typet to_type() const
mp_integer max_value() const
A constant literal expression.
Definition std_expr.h:3117
const irep_idt & get_value() const
Definition std_expr.h:3125
typet & type()
Return the type of the expression.
Definition expr.h:84
const irep_idt & id() const
Definition irep.h:388
Fixed-width bit-vector with two's complement interpretation.
The type of an expression, extends irept.
Definition type.h:29
Fixed-width bit-vector with unsigned binary interpretation.
const std::string integer2string(const mp_integer &n, unsigned base)
Definition mp_arith.cpp:103
#define UNREACHABLE
This should be used to mark dead code.
Definition invariant.h:525
#define PRECONDITION(CONDITION)
Definition invariant.h:463
API to expression classes.