CBMC
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
boolbv_floatbv_op.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module:
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
10#include <util/c_types.h>
11#include <util/floatbv_expr.h>
12
14
15#include "boolbv.h"
16
18{
19 const exprt &op0=expr.op(); // number to convert
20 const exprt &op1=expr.rounding_mode(); // rounding mode
21
22 bvt bv0=convert_bv(op0);
23 bvt bv1=convert_bv(op1);
24
25 const typet &src_type = expr.op0().type();
26 const typet &dest_type = expr.type();
27
28 if(src_type==dest_type) // redundant type cast?
29 return bv0;
30
31 if(src_type.id() == ID_c_bit_field)
32 {
33 // go through underlying type
35 typecast_exprt(op0, to_c_bit_field_type(src_type).underlying_type()),
36 op1,
37 dest_type));
38 }
39
41
42 float_utils.set_rounding_mode(convert_bv(op1));
43
44 if(src_type.id()==ID_floatbv &&
46 {
48 return
49 float_utils.conversion(
50 bv0,
52 }
53 else if(src_type.id()==ID_signedbv &&
55 {
57 return float_utils.from_signed_integer(bv0);
58 }
59 else if(src_type.id()==ID_unsignedbv &&
61 {
63 return float_utils.from_unsigned_integer(bv0);
64 }
65 else if(src_type.id()==ID_floatbv &&
67 {
68 std::size_t dest_width=to_signedbv_type(dest_type).get_width();
70 return float_utils.to_signed_integer(bv0, dest_width);
71 }
72 else if(src_type.id()==ID_floatbv &&
74 {
75 std::size_t dest_width=to_unsignedbv_type(dest_type).get_width();
77 return float_utils.to_unsigned_integer(bv0, dest_width);
78 }
79 else
80 return conversion_failed(expr);
81}
82
85{
86 if(expr.op().type().id() == ID_floatbv)
87 {
89
90 float_utils.set_rounding_mode(convert_bv(expr.rounding_mode()));
92
93 auto op_bv = convert_bv(expr.op());
94
95 return float_utils.round_to_integral(op_bv);
96 }
97 else
98 return conversion_failed(expr);
99}
100
102{
103 const exprt &lhs = expr.lhs();
104 const exprt &rhs = expr.rhs();
105 const exprt &rounding_mode = expr.rounding_mode();
106
107 bvt lhs_as_bv = convert_bv(lhs);
108 bvt rhs_as_bv = convert_bv(rhs);
109 bvt rounding_mode_as_bv = convert_bv(rounding_mode);
110
112 lhs.type() == expr.type() && rhs.type() == expr.type(),
113 "both operands of a floating point operator must match the expression type",
115
117
118 float_utils.set_rounding_mode(rounding_mode_as_bv);
119
120 if(expr.type().id() == ID_floatbv)
121 {
123
124 if(expr.id()==ID_floatbv_plus)
125 return float_utils.add_sub(lhs_as_bv, rhs_as_bv, false);
126 else if(expr.id()==ID_floatbv_minus)
127 return float_utils.add_sub(lhs_as_bv, rhs_as_bv, true);
128 else if(expr.id()==ID_floatbv_mult)
129 return float_utils.mul(lhs_as_bv, rhs_as_bv);
130 else if(expr.id()==ID_floatbv_div)
131 return float_utils.div(lhs_as_bv, rhs_as_bv);
132 else
134 }
135 else if(expr.type().id() == ID_complex)
136 {
137 const typet &subtype = to_type_with_subtype(expr.type()).subtype();
138
139 if(subtype.id()==ID_floatbv)
140 {
142
143 std::size_t width = boolbv_width(expr.type());
144 std::size_t sub_width=boolbv_width(subtype);
145
147 sub_width > 0 && width % sub_width == 0,
148 "width of a complex subtype must be positive and evenly divide the "
149 "width of the complex expression");
151 sub_width * 2 == width, "a complex type consists of exactly two parts");
152
153 bvt lhs_real{lhs_as_bv.begin(), lhs_as_bv.begin() + sub_width};
154 bvt rhs_real{rhs_as_bv.begin(), rhs_as_bv.begin() + sub_width};
155
156 bvt lhs_imag{lhs_as_bv.begin() + sub_width, lhs_as_bv.end()};
157 bvt rhs_imag{rhs_as_bv.begin() + sub_width, rhs_as_bv.end()};
158
160
161 if(expr.id() == ID_floatbv_plus || expr.id() == ID_floatbv_minus)
162 {
163 result_real = float_utils.add_sub(
165 result_imag = float_utils.add_sub(
167 }
168 else if(expr.id() == ID_floatbv_mult)
169 {
170 // Could be optimised to just three multiplications with more additions
171 // instead, but then we'd have to worry about the impact of possible
172 // overflows. So we use the naive approach for now:
173 result_real = float_utils.add_sub(
176 true);
177 result_imag = float_utils.add_sub(
180 false);
181 }
182 else if(expr.id() == ID_floatbv_div)
183 {
187 false);
191 true);
192
193 bvt denominator = float_utils.add_sub(
196 false);
197
198 result_real = float_utils.div(numerator_real, denominator);
199 result_imag = float_utils.div(numerator_imag, denominator);
200 }
201 else
203
204 bvt result_bv = std::move(result_real);
205 result_bv.reserve(width);
206 result_bv.insert(
207 result_bv.end(),
208 std::make_move_iterator(result_imag.begin()),
209 std::make_move_iterator(result_imag.end()));
210
211 return result_bv;
212 }
213 else
214 return conversion_failed(expr);
215 }
216 else
217 return conversion_failed(expr);
218}
Pre-defined bitvector types.
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.
const c_bit_field_typet & to_c_bit_field_type(const typet &type)
Cast a typet to a c_bit_field_typet.
Definition c_types.h:80
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
exprt & op0()
Definition expr.h:133
virtual const bvt & convert_bv(const exprt &expr, const std::optional< std::size_t > expected_width={})
Convert expression to vector of literalts, using an internal cache to speed up conversion if availabl...
Definition boolbv.cpp:39
virtual bvt convert_floatbv_op(const ieee_float_op_exprt &)
bvt conversion_failed(const exprt &expr)
Print that the expression of x has failed conversion, then return a vector of x's width.
Definition boolbv.cpp:94
virtual std::size_t boolbv_width(const typet &type) const
Definition boolbv.h:103
virtual bvt convert_floatbv_typecast(const floatbv_typecast_exprt &expr)
virtual bvt convert_floatbv_round_to_integral(const floatbv_round_to_integral_exprt &)
Base class for all expressions.
Definition expr.h:56
typet & type()
Return the type of the expression.
Definition expr.h:84
Round a floating-point number to an integral value considering the given rounding mode.
Semantic type conversion from/to floating-point formats.
IEEE floating-point operations These have two data operands (op0 and op1) and one rounding mode (op2)...
const irep_idt & id() const
Definition irep.h:388
Semantic type conversion.
Definition std_expr.h:2073
The type of an expression, extends irept.
Definition type.h:29
API to expression classes for floating-point arithmetic.
std::vector< literalt > bvt
Definition literal.h:201
#define UNREACHABLE
This should be used to mark dead code.
Definition invariant.h:525
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition invariant.h:534
#define DATA_INVARIANT_WITH_DIAGNOSTICS(CONDITION, REASON,...)
Definition invariant.h:535
const type_with_subtypet & to_type_with_subtype(const typet &type)
Definition type.h:208