Skip to content

Commit 0fcc504

Browse files
author
Daniel Kroening
committed
floatbv can now do floatbv_typecast from c_bit_field
1 parent 57b4a33 commit 0fcc504

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

src/solvers/floatbv/float_bv.cpp

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,21 @@ exprt float_bvt::convert(const exprt &expr) const
2525
return not_exprt(is_equal(expr.op0(), expr.op1(), get_spec(expr.op0())));
2626
else if(expr.id()==ID_floatbv_typecast)
2727
{
28-
const typet &src_type=expr.op0().type();
29-
const typet &dest_type=expr.type();
28+
const auto &floatbv_typecast_expr = to_floatbv_typecast_expr(expr);
29+
30+
const exprt &src = floatbv_typecast_expr.op();
31+
const exprt &rm = floatbv_typecast_expr.rounding_mode();
32+
const typet &src_type = src.type();
33+
const typet &dest_type = floatbv_typecast_expr.type();
34+
35+
if(src_type.id() == ID_c_bit_field)
36+
{
37+
// go through underlying type
38+
return convert(floatbv_typecast_exprt(
39+
typecast_exprt(src, to_c_bit_field_type(src_type).subtype()),
40+
rm,
41+
dest_type));
42+
}
3043

3144
if(dest_type.id()==ID_signedbv &&
3245
src_type.id()==ID_floatbv) // float -> signed
@@ -60,10 +73,30 @@ exprt float_bvt::convert(const exprt &expr) const
6073
else
6174
return nil_exprt();
6275
}
63-
else if(expr.id()==ID_typecast &&
64-
expr.type().id()==ID_bool &&
65-
expr.op0().type().id()==ID_floatbv) // float -> bool
66-
return not_exprt(is_zero(expr.op0()));
76+
else if(expr.id() == ID_typecast)
77+
{
78+
const typet &src_type = expr.op0().type();
79+
const typet &dest_type = expr.type();
80+
81+
if(
82+
dest_type.id() == ID_bool && src_type.id() == ID_floatbv) // float -> bool
83+
{
84+
return not_exprt(is_zero(expr.op0()));
85+
}
86+
else if(
87+
src_type.id() == ID_c_bool &&
88+
dest_type.id() == ID_floatbv) // c_bool -> float
89+
{
90+
return from_c_bool(expr.op0(), get_spec(expr));
91+
}
92+
else if(
93+
src_type.id() == ID_bool && dest_type.id() == ID_floatbv) // bool -> float
94+
{
95+
return from_bool(expr.op0(), get_spec(expr));
96+
}
97+
else
98+
return nil_exprt();
99+
}
67100
else if(expr.id()==ID_floatbv_plus)
68101
return add_sub(false, expr.op0(), expr.op1(), expr.op2(), get_spec(expr));
69102
else if(expr.id()==ID_floatbv_minus)
@@ -254,6 +287,24 @@ exprt float_bvt::from_unsigned_integer(
254287
return rounder(result, rm, spec);
255288
}
256289

290+
exprt float_bvt::from_bool(const exprt &src, const ieee_float_spect &spec) const
291+
{
292+
ieee_floatt zero(spec);
293+
zero.make_zero();
294+
const auto zero_expr = zero.to_expr();
295+
ieee_floatt one(spec);
296+
one.build(1, 0);
297+
const auto one_expr = one.to_expr();
298+
return if_exprt(src, one_expr, zero_expr);
299+
}
300+
301+
exprt float_bvt::from_c_bool(const exprt &src, const ieee_float_spect &spec)
302+
const
303+
{
304+
// we go via bool
305+
return from_bool(typecast_exprt(src, bool_typet()), spec);
306+
}
307+
257308
exprt float_bvt::to_signed_integer(
258309
const exprt &src,
259310
std::size_t dest_width,

src/solvers/floatbv/float_bv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class float_bvt
5555
const exprt &,
5656
const exprt &rm,
5757
const ieee_float_spect &) const;
58+
exprt from_c_bool(const exprt &, const ieee_float_spect &) const;
59+
exprt from_bool(const exprt &, const ieee_float_spect &) const;
5860
exprt from_signed_integer(
5961
const exprt &,
6062
const exprt &rm,

0 commit comments

Comments
 (0)