cprover
boolbv_floatbv_op.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 "boolbv.h"
10 
11 #include <algorithm>
12 #include <iostream>
13 
14 #include <util/std_types.h>
15 
17 
19 {
20  const exprt &op0=expr.op(); // number to convert
21  const exprt &op1=expr.rounding_mode(); // rounding mode
22 
23  bvt bv0=convert_bv(op0);
24  bvt bv1=convert_bv(op1);
25 
26  const typet &src_type = expr.op0().type();
27  const typet &dest_type = expr.type();
28 
29  if(src_type==dest_type) // redundant type cast?
30  return bv0;
31 
32  if(src_type.id() == ID_c_bit_field)
33  {
34  // go through underlying type
36  typecast_exprt(op0, src_type.subtype()), op1, dest_type));
37  }
38 
39  float_utilst float_utils(prop);
40 
41  float_utils.set_rounding_mode(convert_bv(op1));
42 
43  if(src_type.id()==ID_floatbv &&
44  dest_type.id()==ID_floatbv)
45  {
46  float_utils.spec=ieee_float_spect(to_floatbv_type(src_type));
47  return
48  float_utils.conversion(
49  bv0,
50  ieee_float_spect(to_floatbv_type(dest_type)));
51  }
52  else if(src_type.id()==ID_signedbv &&
53  dest_type.id()==ID_floatbv)
54  {
55  float_utils.spec=ieee_float_spect(to_floatbv_type(dest_type));
56  return float_utils.from_signed_integer(bv0);
57  }
58  else if(src_type.id()==ID_unsignedbv &&
59  dest_type.id()==ID_floatbv)
60  {
61  float_utils.spec=ieee_float_spect(to_floatbv_type(dest_type));
62  return float_utils.from_unsigned_integer(bv0);
63  }
64  else if(src_type.id()==ID_floatbv &&
65  dest_type.id()==ID_signedbv)
66  {
67  std::size_t dest_width=to_signedbv_type(dest_type).get_width();
68  float_utils.spec=ieee_float_spect(to_floatbv_type(src_type));
69  return float_utils.to_signed_integer(bv0, dest_width);
70  }
71  else if(src_type.id()==ID_floatbv &&
72  dest_type.id()==ID_unsignedbv)
73  {
74  std::size_t dest_width=to_unsignedbv_type(dest_type).get_width();
75  float_utils.spec=ieee_float_spect(to_floatbv_type(src_type));
76  return float_utils.to_unsigned_integer(bv0, dest_width);
77  }
78  else
79  return conversion_failed(expr);
80 }
81 
83 {
84  const exprt &lhs = expr.lhs();
85  const exprt &rhs = expr.rhs();
86  const exprt &rounding_mode = expr.rounding_mode();
87 
88  bvt lhs_as_bv = convert_bv(lhs);
89  bvt rhs_as_bv = convert_bv(rhs);
90  bvt rounding_mode_as_bv = convert_bv(rounding_mode);
91 
93  lhs.type() == expr.type() && rhs.type() == expr.type(),
94  "both operands of a floating point operator must match the expression type",
96 
97  float_utilst float_utils(prop);
98 
99  float_utils.set_rounding_mode(rounding_mode_as_bv);
100 
101  if(expr.type().id() == ID_floatbv)
102  {
103  float_utils.spec=ieee_float_spect(to_floatbv_type(expr.type()));
104 
105  if(expr.id()==ID_floatbv_plus)
106  return float_utils.add_sub(lhs_as_bv, rhs_as_bv, false);
107  else if(expr.id()==ID_floatbv_minus)
108  return float_utils.add_sub(lhs_as_bv, rhs_as_bv, true);
109  else if(expr.id()==ID_floatbv_mult)
110  return float_utils.mul(lhs_as_bv, rhs_as_bv);
111  else if(expr.id()==ID_floatbv_div)
112  return float_utils.div(lhs_as_bv, rhs_as_bv);
113  else if(expr.id()==ID_floatbv_rem)
114  return float_utils.rem(lhs_as_bv, rhs_as_bv);
115  else
116  UNREACHABLE;
117  }
118  else if(expr.type().id() == ID_vector || expr.type().id() == ID_complex)
119  {
120  const typet &subtype = expr.type().subtype();
121 
122  if(subtype.id()==ID_floatbv)
123  {
124  float_utils.spec=ieee_float_spect(to_floatbv_type(subtype));
125 
126  std::size_t width = boolbv_width(expr.type());
127  std::size_t sub_width=boolbv_width(subtype);
128 
130  sub_width > 0 && width % sub_width == 0,
131  "width of a vector subtype must be positive and evenly divide the "
132  "width of the vector");
133 
134  std::size_t size=width/sub_width;
135  bvt result_bv;
136  result_bv.resize(width);
137 
138  for(std::size_t i=0; i<size; i++)
139  {
140  bvt lhs_sub_bv, rhs_sub_bv, sub_result_bv;
141 
142  lhs_sub_bv.assign(
143  lhs_as_bv.begin() + i * sub_width,
144  lhs_as_bv.begin() + (i + 1) * sub_width);
145  rhs_sub_bv.assign(
146  rhs_as_bv.begin() + i * sub_width,
147  rhs_as_bv.begin() + (i + 1) * sub_width);
148 
149  if(expr.id()==ID_floatbv_plus)
150  sub_result_bv = float_utils.add_sub(lhs_sub_bv, rhs_sub_bv, false);
151  else if(expr.id()==ID_floatbv_minus)
152  sub_result_bv = float_utils.add_sub(lhs_sub_bv, rhs_sub_bv, true);
153  else if(expr.id()==ID_floatbv_mult)
154  sub_result_bv = float_utils.mul(lhs_sub_bv, rhs_sub_bv);
155  else if(expr.id()==ID_floatbv_div)
156  sub_result_bv = float_utils.div(lhs_sub_bv, rhs_sub_bv);
157  else
158  UNREACHABLE;
159 
160  INVARIANT(
161  sub_result_bv.size() == sub_width,
162  "we constructed a new vector of the right size");
163  INVARIANT(
164  i * sub_width + sub_width - 1 < result_bv.size(),
165  "the sub-bitvector fits into the result bitvector");
166  std::copy(
167  sub_result_bv.begin(),
168  sub_result_bv.end(),
169  result_bv.begin() + i * sub_width);
170  }
171 
172  return result_bv;
173  }
174  else
175  return conversion_failed(expr);
176  }
177  else
178  return conversion_failed(expr);
179 }
UNREACHABLE
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:504
typet::subtype
const typet & subtype() const
Definition: type.h:47
float_utilst::to_signed_integer
bvt to_signed_integer(const bvt &src, std::size_t int_width)
Definition: float_utils.cpp:67
float_utilst::add_sub
virtual bvt add_sub(const bvt &src1, const bvt &src2, bool subtract)
Definition: float_utils.cpp:243
float_utilst
Definition: float_utils.h:18
floatbv_typecast_exprt::op
exprt & op()
Definition: std_expr.h:2078
typet
The type of an expression, extends irept.
Definition: type.h:29
float_utils.h
bvt
std::vector< literalt > bvt
Definition: literal.h:201
exprt
Base class for all expressions.
Definition: expr.h:53
float_utilst::set_rounding_mode
void set_rounding_mode(const bvt &)
Definition: float_utils.cpp:15
float_utilst::to_unsigned_integer
bvt to_unsigned_integer(const bvt &src, std::size_t int_width)
Definition: float_utils.cpp:74
float_utilst::from_signed_integer
bvt from_signed_integer(const bvt &)
Definition: float_utils.cpp:32
ieee_float_spect
Definition: ieee_float.h:26
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:81
float_utilst::conversion
bvt conversion(const bvt &src, const ieee_float_spect &dest_spec)
Definition: float_utils.cpp:152
float_utilst::rem
virtual bvt rem(const bvt &src1, const bvt &src2)
Definition: float_utils.cpp:539
boolbvt::boolbv_width
boolbv_widtht boolbv_width
Definition: boolbv.h:95
DATA_INVARIANT_WITH_DIAGNOSTICS
#define DATA_INVARIANT_WITH_DIAGNOSTICS(CONDITION, REASON,...)
Definition: invariant.h:512
to_unsignedbv_type
const unsignedbv_typet & to_unsignedbv_type(const typet &type)
Cast a typet to an unsignedbv_typet.
Definition: std_types.h:1248
boolbvt::conversion_failed
void conversion_failed(const exprt &expr, bvt &bv)
Definition: boolbv.h:113
DATA_INVARIANT
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition: invariant.h:511
floatbv_typecast_exprt
Semantic type conversion from/to floating-point formats.
Definition: std_expr.h:2067
float_utilst::div
virtual bvt div(const bvt &src1, const bvt &src2)
Definition: float_utils.cpp:460
boolbvt::convert_floatbv_typecast
virtual bvt convert_floatbv_typecast(const floatbv_typecast_exprt &expr)
Definition: boolbv_floatbv_op.cpp:18
std_types.h
Pre-defined types.
irept::id
const irep_idt & id() const
Definition: irep.h:418
boolbvt::convert_bv
virtual const bvt & convert_bv(const exprt &expr, const optionalt< std::size_t > expected_width=nullopt)
Convert expression to vector of literalts, using an internal cache to speed up conversion if availabl...
Definition: boolbv.cpp:119
bitvector_typet::get_width
std::size_t get_width() const
Definition: std_types.h:1041
irep_pretty_diagnosticst
Definition: irep.h:524
boolbvt::convert_floatbv_op
virtual bvt convert_floatbv_op(const ieee_float_op_exprt &)
Definition: boolbv_floatbv_op.cpp:82
ieee_float_op_exprt::rounding_mode
exprt & rounding_mode()
Definition: std_expr.h:3821
floatbv_typecast_exprt::rounding_mode
exprt & rounding_mode()
Definition: std_expr.h:2088
to_signedbv_type
const signedbv_typet & to_signedbv_type(const typet &type)
Cast a typet to a signedbv_typet.
Definition: std_types.h:1298
ieee_float_op_exprt::lhs
exprt & lhs()
Definition: std_expr.h:3801
to_floatbv_type
const floatbv_typet & to_floatbv_type(const typet &type)
Cast a typet to a floatbv_typet.
Definition: std_types.h:1424
ieee_float_op_exprt
IEEE floating-point operations These have two data operands (op0 and op1) and one rounding mode (op2)...
Definition: std_expr.h:3790
boolbv.h
typecast_exprt
Semantic type conversion.
Definition: std_expr.h:2013
ieee_float_op_exprt::rhs
exprt & rhs()
Definition: std_expr.h:3811
float_utilst::spec
ieee_float_spect spec
Definition: float_utils.h:88
float_utilst::from_unsigned_integer
bvt from_unsigned_integer(const bvt &)
Definition: float_utils.cpp:50
float_utilst::mul
virtual bvt mul(const bvt &src1, const bvt &src2)
Definition: float_utils.cpp:408
binary_exprt::op0
exprt & op0()
Definition: expr.h:102
validation_modet::INVARIANT
@ INVARIANT
prop_conv_solvert::prop
propt & prop
Definition: prop_conv_solver.h:131