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/bitvector_types.h>
15 #include <util/floatbv_expr.h>
16 
18 
20 {
21  const exprt &op0=expr.op(); // number to convert
22  const exprt &op1=expr.rounding_mode(); // rounding mode
23 
24  bvt bv0=convert_bv(op0);
25  bvt bv1=convert_bv(op1);
26 
27  const typet &src_type = expr.op0().type();
28  const typet &dest_type = expr.type();
29 
30  if(src_type==dest_type) // redundant type cast?
31  return bv0;
32 
33  if(src_type.id() == ID_c_bit_field)
34  {
35  // go through underlying type
37  typecast_exprt(op0, src_type.subtype()), op1, dest_type));
38  }
39 
40  float_utilst float_utils(prop);
41 
42  float_utils.set_rounding_mode(convert_bv(op1));
43 
44  if(src_type.id()==ID_floatbv &&
45  dest_type.id()==ID_floatbv)
46  {
47  float_utils.spec=ieee_float_spect(to_floatbv_type(src_type));
48  return
49  float_utils.conversion(
50  bv0,
51  ieee_float_spect(to_floatbv_type(dest_type)));
52  }
53  else if(src_type.id()==ID_signedbv &&
54  dest_type.id()==ID_floatbv)
55  {
56  float_utils.spec=ieee_float_spect(to_floatbv_type(dest_type));
57  return float_utils.from_signed_integer(bv0);
58  }
59  else if(src_type.id()==ID_unsignedbv &&
60  dest_type.id()==ID_floatbv)
61  {
62  float_utils.spec=ieee_float_spect(to_floatbv_type(dest_type));
63  return float_utils.from_unsigned_integer(bv0);
64  }
65  else if(src_type.id()==ID_floatbv &&
66  dest_type.id()==ID_signedbv)
67  {
68  std::size_t dest_width=to_signedbv_type(dest_type).get_width();
69  float_utils.spec=ieee_float_spect(to_floatbv_type(src_type));
70  return float_utils.to_signed_integer(bv0, dest_width);
71  }
72  else if(src_type.id()==ID_floatbv &&
73  dest_type.id()==ID_unsignedbv)
74  {
75  std::size_t dest_width=to_unsignedbv_type(dest_type).get_width();
76  float_utils.spec=ieee_float_spect(to_floatbv_type(src_type));
77  return float_utils.to_unsigned_integer(bv0, dest_width);
78  }
79  else
80  return conversion_failed(expr);
81 }
82 
84 {
85  const exprt &lhs = expr.lhs();
86  const exprt &rhs = expr.rhs();
87  const exprt &rounding_mode = expr.rounding_mode();
88 
89  bvt lhs_as_bv = convert_bv(lhs);
90  bvt rhs_as_bv = convert_bv(rhs);
91  bvt rounding_mode_as_bv = convert_bv(rounding_mode);
92 
94  lhs.type() == expr.type() && rhs.type() == expr.type(),
95  "both operands of a floating point operator must match the expression type",
97 
98  float_utilst float_utils(prop);
99 
100  float_utils.set_rounding_mode(rounding_mode_as_bv);
101 
102  if(expr.type().id() == ID_floatbv)
103  {
104  float_utils.spec=ieee_float_spect(to_floatbv_type(expr.type()));
105 
106  if(expr.id()==ID_floatbv_plus)
107  return float_utils.add_sub(lhs_as_bv, rhs_as_bv, false);
108  else if(expr.id()==ID_floatbv_minus)
109  return float_utils.add_sub(lhs_as_bv, rhs_as_bv, true);
110  else if(expr.id()==ID_floatbv_mult)
111  return float_utils.mul(lhs_as_bv, rhs_as_bv);
112  else if(expr.id()==ID_floatbv_div)
113  return float_utils.div(lhs_as_bv, rhs_as_bv);
114  else
115  UNREACHABLE;
116  }
117  else if(expr.type().id() == ID_vector || expr.type().id() == ID_complex)
118  {
119  const typet &subtype = expr.type().subtype();
120 
121  if(subtype.id()==ID_floatbv)
122  {
123  float_utils.spec=ieee_float_spect(to_floatbv_type(subtype));
124 
125  std::size_t width = boolbv_width(expr.type());
126  std::size_t sub_width=boolbv_width(subtype);
127 
129  sub_width > 0 && width % sub_width == 0,
130  "width of a vector subtype must be positive and evenly divide the "
131  "width of the vector");
132 
133  std::size_t size=width/sub_width;
134  bvt result_bv;
135  result_bv.resize(width);
136 
137  for(std::size_t i=0; i<size; i++)
138  {
139  bvt lhs_sub_bv, rhs_sub_bv, sub_result_bv;
140 
141  lhs_sub_bv.assign(
142  lhs_as_bv.begin() + i * sub_width,
143  lhs_as_bv.begin() + (i + 1) * sub_width);
144  rhs_sub_bv.assign(
145  rhs_as_bv.begin() + i * sub_width,
146  rhs_as_bv.begin() + (i + 1) * sub_width);
147 
148  if(expr.id()==ID_floatbv_plus)
149  sub_result_bv = float_utils.add_sub(lhs_sub_bv, rhs_sub_bv, false);
150  else if(expr.id()==ID_floatbv_minus)
151  sub_result_bv = float_utils.add_sub(lhs_sub_bv, rhs_sub_bv, true);
152  else if(expr.id()==ID_floatbv_mult)
153  sub_result_bv = float_utils.mul(lhs_sub_bv, rhs_sub_bv);
154  else if(expr.id()==ID_floatbv_div)
155  sub_result_bv = float_utils.div(lhs_sub_bv, rhs_sub_bv);
156  else
157  UNREACHABLE;
158 
159  INVARIANT(
160  sub_result_bv.size() == sub_width,
161  "we constructed a new vector of the right size");
162  INVARIANT(
163  i * sub_width + sub_width - 1 < result_bv.size(),
164  "the sub-bitvector fits into the result bitvector");
165  std::copy(
166  sub_result_bv.begin(),
167  sub_result_bv.end(),
168  result_bv.begin() + i * sub_width);
169  }
170 
171  return result_bv;
172  }
173  else
174  return conversion_failed(expr);
175  }
176  else
177  return conversion_failed(expr);
178 }
UNREACHABLE
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:503
to_unsignedbv_type
const unsignedbv_typet & to_unsignedbv_type(const typet &type)
Cast a typet to an unsignedbv_typet.
Definition: bitvector_types.h:191
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: floatbv_expr.h:30
typet
The type of an expression, extends irept.
Definition: type.h:28
float_utils.h
bvt
std::vector< literalt > bvt
Definition: literal.h:201
to_floatbv_type
const floatbv_typet & to_floatbv_type(const typet &type)
Cast a typet to a floatbv_typet.
Definition: bitvector_types.h:367
exprt
Base class for all expressions.
Definition: expr.h:54
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:23
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:82
float_utilst::conversion
bvt conversion(const bvt &src, const ieee_float_spect &dest_spec)
Definition: float_utils.cpp:152
DATA_INVARIANT_WITH_DIAGNOSTICS
#define DATA_INVARIANT_WITH_DIAGNOSTICS(CONDITION, REASON,...)
Definition: invariant.h:511
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:510
boolbvt::boolbv_width
virtual std::size_t boolbv_width(const typet &type) const
Definition: boolbv.h:97
floatbv_typecast_exprt
Semantic type conversion from/to floating-point formats.
Definition: floatbv_expr.h:19
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:19
bitvector_types.h
Pre-defined bitvector types.
irept::id
const irep_idt & id() const
Definition: irep.h:407
floatbv_expr.h
API to expression classes for floating-point arithmetic.
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:40
to_signedbv_type
const signedbv_typet & to_signedbv_type(const typet &type)
Cast a typet to a signedbv_typet.
Definition: bitvector_types.h:241
bitvector_typet::get_width
std::size_t get_width() const
Definition: std_types.h:843
irep_pretty_diagnosticst
Definition: irep.h:514
boolbvt::convert_floatbv_op
virtual bvt convert_floatbv_op(const ieee_float_op_exprt &)
Definition: boolbv_floatbv_op.cpp:83
ieee_float_op_exprt::rounding_mode
exprt & rounding_mode()
Definition: floatbv_expr.h:395
floatbv_typecast_exprt::rounding_mode
exprt & rounding_mode()
Definition: floatbv_expr.h:40
ieee_float_op_exprt::lhs
exprt & lhs()
Definition: floatbv_expr.h:375
ieee_float_op_exprt
IEEE floating-point operations These have two data operands (op0 and op1) and one rounding mode (op2)...
Definition: floatbv_expr.h:364
boolbvt::conversion_failed
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:84
boolbv.h
INVARIANT
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition: invariant.h:423
typecast_exprt
Semantic type conversion.
Definition: std_expr.h:1866
ieee_float_op_exprt::rhs
exprt & rhs()
Definition: floatbv_expr.h:385
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:99
prop_conv_solvert::prop
propt & prop
Definition: prop_conv_solver.h:126