CBMC
Loading...
Searching...
No Matches
bitvector_types.h
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Pre-defined bitvector types
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
11
12#ifndef CPROVER_UTIL_BITVECTOR_TYPES_H
13#define CPROVER_UTIL_BITVECTOR_TYPES_H
14
15#include "std_types.h"
16
19inline bool is_signed_or_unsigned_bitvector(const typet &type)
20{
21 return type.id() == ID_signedbv || type.id() == ID_unsignedbv;
22}
23
32inline const bitvector_typet &to_bitvector_type(const typet &type)
33{
35
36 return static_cast<const bitvector_typet &>(type);
37}
38
41{
43
44 return static_cast<bitvector_typet &>(type);
45}
46
49{
50public:
51 explicit bv_typet(std::size_t width) : bitvector_typet(ID_bv)
52 {
54 }
55
56 static void check(
57 const typet &type,
59 {
61 vm, !type.get(ID_width).empty(), "bitvector type must have width");
62 }
63
64 // helpers to create common constants
67};
68
72template <>
73inline bool can_cast_type<bv_typet>(const typet &type)
74{
75 return type.id() == ID_bv;
76}
77
86inline const bv_typet &to_bv_type(const typet &type)
87{
89 bv_typet::check(type);
90 return static_cast<const bv_typet &>(type);
91}
92
95{
97 bv_typet::check(type);
98 return static_cast<bv_typet &>(type);
99}
100
103{
104public:
105 integer_bitvector_typet(const irep_idt &id, std::size_t width)
107 {
108 }
109
114
116 mp_integer smallest() const;
118 mp_integer largest() const;
119
120 // If we ever need any of the following three methods in \ref fixedbv_typet or
121 // \ref floatbv_typet, we might want to move them to a new class
122 // numeric_bitvector_typet, which would be between integer_bitvector_typet and
123 // bitvector_typet in the hierarchy.
124
131};
132
136template <>
138{
139 return type.id() == ID_signedbv || type.id() == ID_unsignedbv;
140}
141
150inline const integer_bitvector_typet &
152{
154
155 return static_cast<const integer_bitvector_typet &>(type);
156}
157
165
168{
169public:
170 explicit unsignedbv_typet(std::size_t width)
172 {
173 }
174
179
180 static void check(
181 const typet &type,
183 {
184 bitvector_typet::check(type, vm);
185 }
186};
187
191template <>
193{
194 return type.id() == ID_unsignedbv;
195}
196
205inline const unsignedbv_typet &to_unsignedbv_type(const typet &type)
206{
209 return static_cast<const unsignedbv_typet &>(type);
210}
211
214{
217 return static_cast<unsignedbv_typet &>(type);
218}
219
222{
223public:
224 explicit signedbv_typet(std::size_t width)
226 {
227 }
228
233
234 static void check(
235 const typet &type,
237 {
239 vm, !type.get(ID_width).empty(), "signed bitvector type must have width");
240 }
241};
242
246template <>
247inline bool can_cast_type<signedbv_typet>(const typet &type)
248{
249 return type.id() == ID_signedbv;
250}
251
260inline const signedbv_typet &to_signedbv_type(const typet &type)
261{
264 return static_cast<const signedbv_typet &>(type);
265}
266
269{
272 return static_cast<signedbv_typet &>(type);
273}
274
280{
281public:
285
286 std::size_t get_fraction_bits() const
287 {
288 return get_width() - get_integer_bits();
289 }
290
291 std::size_t get_integer_bits() const;
292
293 void set_integer_bits(std::size_t b)
294 {
296 }
297
298 static void check(
299 const typet &type,
301 {
303 vm, !type.get(ID_width).empty(), "fixed bitvector type must have width");
304 }
305};
306
310template <>
311inline bool can_cast_type<fixedbv_typet>(const typet &type)
312{
313 return type.id() == ID_fixedbv;
314}
315
324inline const fixedbv_typet &to_fixedbv_type(const typet &type)
325{
328 return static_cast<const fixedbv_typet &>(type);
329}
330
333{
336 return static_cast<fixedbv_typet &>(type);
337}
338
341{
342public:
346
347 std::size_t get_e() const
348 {
349 // subtract one for sign bit
350 return get_width() - get_f() - 1;
351 }
352
353 std::size_t get_f() const;
354
355 void set_f(std::size_t b)
356 {
357 set_size_t(ID_f, b);
358 }
359
360 static void check(
361 const typet &type,
363 {
365 vm, !type.get(ID_width).empty(), "float bitvector type must have width");
366 }
367};
368
372template <>
373inline bool can_cast_type<floatbv_typet>(const typet &type)
374{
375 return type.id() == ID_floatbv;
376}
377
386inline const floatbv_typet &to_floatbv_type(const typet &type)
387{
390 return static_cast<const floatbv_typet &>(type);
391}
392
395{
398 return static_cast<floatbv_typet &>(type);
399}
400
401#endif // CPROVER_UTIL_BITVECTOR_TYPES_H
const bv_typet & to_bv_type(const typet &type)
Cast a typet to a bv_typet.
bool is_signed_or_unsigned_bitvector(const typet &type)
This method tests, if the given typet is a signed or unsigned bitvector.
bool can_cast_type< signedbv_typet >(const typet &type)
Check whether a reference to a typet is a signedbv_typet.
const fixedbv_typet & to_fixedbv_type(const typet &type)
Cast a typet to a fixedbv_typet.
bool can_cast_type< integer_bitvector_typet >(const typet &type)
Check whether a reference to a typet is an integer_bitvector_typet.
bool can_cast_type< bv_typet >(const typet &type)
Check whether a reference to a typet is a bv_typet.
const bitvector_typet & to_bitvector_type(const typet &type)
Cast a typet to a bitvector_typet.
bool can_cast_type< fixedbv_typet >(const typet &type)
Check whether a reference to a typet is a fixedbv_typet.
bool can_cast_type< unsignedbv_typet >(const typet &type)
Check whether a reference to a typet is a unsignedbv_typet.
bool can_cast_type< floatbv_typet >(const typet &type)
Check whether a reference to a typet is a floatbv_typet.
const integer_bitvector_typet & to_integer_bitvector_type(const typet &type)
Cast a typet to an integer_bitvector_typet.
const floatbv_typet & to_floatbv_type(const typet &type)
Cast a typet to a floatbv_typet.
const unsignedbv_typet & to_unsignedbv_type(const typet &type)
Cast a typet to an unsignedbv_typet.
const signedbv_typet & to_signedbv_type(const typet &type)
Cast a typet to a signedbv_typet.
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
Base class of fixed-width bit-vector types.
Definition std_types.h:909
void set_width(std::size_t width)
Definition std_types.h:932
std::size_t get_width() const
Definition std_types.h:925
std::size_t width() const
static void check(const typet &type, const validation_modet vm=validation_modet::INVARIANT)
Definition std_types.h:939
Fixed-width bit-vector without numerical interpretation.
constant_exprt all_ones_expr() const
bv_typet(std::size_t width)
static void check(const typet &type, const validation_modet vm=validation_modet::INVARIANT)
constant_exprt all_zeros_expr() const
A constant literal expression.
Definition std_expr.h:3117
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:38
Fixed-width bit-vector with signed fixed-point interpretation.
void set_integer_bits(std::size_t b)
std::size_t get_fraction_bits() const
std::size_t get_integer_bits() const
static void check(const typet &type, const validation_modet vm=validation_modet::INVARIANT)
Fixed-width bit-vector with IEEE floating-point interpretation.
void set_f(std::size_t b)
std::size_t get_f() const
std::size_t get_e() const
static void check(const typet &type, const validation_modet vm=validation_modet::INVARIANT)
Fixed-width bit-vector representing a signed or unsigned integer.
integer_bitvector_typet(const irep_idt &id, std::size_t width)
constant_exprt largest_expr() const
Return an expression representing the largest value of this type.
integer_bitvector_typet(const irep_idt &id, const mp_integer &width)
mp_integer largest() const
Return the largest value that can be represented using this type.
constant_exprt zero_expr() const
Return an expression representing the zero value of this type.
constant_exprt smallest_expr() const
Return an expression representing the smallest value of this type.
mp_integer smallest() const
Return the smallest value that can be represented using this type.
const irep_idt & get(const irep_idt &name) const
Definition irep.cpp:44
void set_size_t(const irep_idt &name, const std::size_t value)
Definition irep.cpp:82
const irep_idt & id() const
Definition irep.h:388
Fixed-width bit-vector with two's complement interpretation.
static void check(const typet &type, const validation_modet vm=validation_modet::INVARIANT)
signedbv_typet(const mp_integer &width)
signedbv_typet(std::size_t width)
The type of an expression, extends irept.
Definition type.h:29
Fixed-width bit-vector with unsigned binary interpretation.
unsignedbv_typet(const mp_integer &width)
unsignedbv_typet(std::size_t width)
static void check(const typet &type, const validation_modet vm=validation_modet::INVARIANT)
BigInt mp_integer
Definition smt_terms.h:17
#define PRECONDITION(CONDITION)
Definition invariant.h:463
Pre-defined types.
#define DATA_CHECK(vm, condition, message)
This macro takes a condition which denotes a well-formedness criterion on goto programs,...
Definition validate.h:22
validation_modet