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=ns.follow(expr.op0().type());
27  const typet &dest_type=ns.follow(expr.type());
28 
29  if(src_type==dest_type) // redundant type cast?
30  return bv0;
31 
32  float_utilst float_utils(prop);
33 
34  float_utils.set_rounding_mode(convert_bv(op1));
35 
36  if(src_type.id()==ID_floatbv &&
37  dest_type.id()==ID_floatbv)
38  {
39  float_utils.spec=ieee_float_spect(to_floatbv_type(src_type));
40  return
41  float_utils.conversion(
42  bv0,
43  ieee_float_spect(to_floatbv_type(dest_type)));
44  }
45  else if(src_type.id()==ID_signedbv &&
46  dest_type.id()==ID_floatbv)
47  {
48  float_utils.spec=ieee_float_spect(to_floatbv_type(dest_type));
49  return float_utils.from_signed_integer(bv0);
50  }
51  else if(src_type.id()==ID_unsignedbv &&
52  dest_type.id()==ID_floatbv)
53  {
54  float_utils.spec=ieee_float_spect(to_floatbv_type(dest_type));
55  return float_utils.from_unsigned_integer(bv0);
56  }
57  else if(src_type.id()==ID_floatbv &&
58  dest_type.id()==ID_signedbv)
59  {
60  std::size_t dest_width=to_signedbv_type(dest_type).get_width();
61  float_utils.spec=ieee_float_spect(to_floatbv_type(src_type));
62  return float_utils.to_signed_integer(bv0, dest_width);
63  }
64  else if(src_type.id()==ID_floatbv &&
65  dest_type.id()==ID_unsignedbv)
66  {
67  std::size_t dest_width=to_unsignedbv_type(dest_type).get_width();
68  float_utils.spec=ieee_float_spect(to_floatbv_type(src_type));
69  return float_utils.to_unsigned_integer(bv0, dest_width);
70  }
71  else
72  return conversion_failed(expr);
73 }
74 
76 {
77  const exprt::operandst &operands=expr.operands();
78 
79  if(operands.size()!=3)
80  throw "operator "+expr.id_string()+" takes three operands";
81 
82  const exprt &lhs = expr.op0();
83  const exprt &rhs = expr.op1();
84  const exprt &rounding_mode = expr.op2();
85 
86  bvt lhs_as_bv = convert_bv(lhs);
87  bvt rhs_as_bv = convert_bv(rhs);
88  bvt rounding_mode_as_bv = convert_bv(rounding_mode);
89 
90  const typet &resolved_type = ns.follow(expr.type());
92  lhs.type() == resolved_type && rhs.type() == resolved_type,
93  "both operands of a floating point operator must have the same type",
95 
96  float_utilst float_utils(prop);
97 
98  float_utils.set_rounding_mode(rounding_mode_as_bv);
99 
100  if(resolved_type.id() == ID_floatbv)
101  {
102  float_utils.spec=ieee_float_spect(to_floatbv_type(expr.type()));
103 
104  if(expr.id()==ID_floatbv_plus)
105  return float_utils.add_sub(lhs_as_bv, rhs_as_bv, false);
106  else if(expr.id()==ID_floatbv_minus)
107  return float_utils.add_sub(lhs_as_bv, rhs_as_bv, true);
108  else if(expr.id()==ID_floatbv_mult)
109  return float_utils.mul(lhs_as_bv, rhs_as_bv);
110  else if(expr.id()==ID_floatbv_div)
111  return float_utils.div(lhs_as_bv, rhs_as_bv);
112  else if(expr.id()==ID_floatbv_rem)
113  return float_utils.rem(lhs_as_bv, rhs_as_bv);
114  else
115  UNREACHABLE;
116  }
117  else if(resolved_type.id() == ID_vector || resolved_type.id() == ID_complex)
118  {
119  const typet &subtype = ns.follow(resolved_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(resolved_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 }
exprt::op2
exprt & op2()
Definition: expr.h:90
typet::subtype
const typet & subtype() const
Definition: type.h:38
float_utilst::to_signed_integer
bvt to_signed_integer(const bvt &src, std::size_t int_width)
Definition: float_utils.cpp:67
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:485
float_utilst
Definition: float_utils.h:17
floatbv_typecast_exprt::op
exprt & op()
Definition: std_expr.h:2351
typet
The type of an expression, extends irept.
Definition: type.h:27
float_utils.h
bvt
std::vector< literalt > bvt
Definition: literal.h:200
boolbvt::convert_floatbv_op
virtual bvt convert_floatbv_op(const exprt &expr)
Definition: boolbv_floatbv_op.cpp:75
exprt
Base class for all expressions.
Definition: expr.h:54
exprt::op0
exprt & op0()
Definition: expr.h:84
UNREACHABLE
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:478
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:25
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:68
float_utilst::conversion
bvt conversion(const bvt &src, const ieee_float_spect &dest_spec)
Definition: float_utils.cpp:152
boolbvt::boolbv_width
boolbv_widtht boolbv_width
Definition: boolbv.h:92
to_unsignedbv_type
const unsignedbv_typet & to_unsignedbv_type(const typet &type)
Cast a typet to an unsignedbv_typet.
Definition: std_types.h:1262
boolbvt::conversion_failed
void conversion_failed(const exprt &expr, bvt &bv)
Definition: boolbv.h:110
floatbv_typecast_exprt
Semantic type conversion from/to floating-point formats.
Definition: std_expr.h:2336
boolbvt::convert_floatbv_typecast
virtual bvt convert_floatbv_typecast(const floatbv_typecast_exprt &expr)
Definition: boolbv_floatbv_op.cpp:18
std_types.h
irept::id_string
const std::string & id_string() const
Definition: irep.h:262
exprt::op1
exprt & op1()
Definition: expr.h:87
decision_proceduret::ns
const namespacet & ns
Definition: decision_procedure.h:61
irept::id
const irep_idt & id() const
Definition: irep.h:259
exprt::operandst
std::vector< exprt > operandst
Definition: expr.h:57
boolbvt::convert_bv
virtual const bvt & convert_bv(const exprt &expr, const optionalt< std::size_t > expected_width=nullopt)
Definition: boolbv.cpp:112
bitvector_typet::get_width
std::size_t get_width() const
Definition: std_types.h:1117
irep_pretty_diagnosticst
Definition: irep.h:466
DATA_INVARIANT_WITH_DIAGNOSTICS
#define DATA_INVARIANT_WITH_DIAGNOSTICS(CONDITION, REASON,...)
Definition: invariant.h:486
floatbv_typecast_exprt::rounding_mode
exprt & rounding_mode()
Definition: std_expr.h:2361
namespace_baset::follow
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:62
to_signedbv_type
const signedbv_typet & to_signedbv_type(const typet &type)
Cast a typet to a signedbv_typet.
Definition: std_types.h:1316
to_floatbv_type
const floatbv_typet & to_floatbv_type(const typet &type)
Cast a typet to a floatbv_typet.
Definition: std_types.h:1442
boolbv.h
exprt::operands
operandst & operands()
Definition: expr.h:78
float_utilst::spec
ieee_float_spect spec
Definition: float_utils.h:87
float_utilst::from_unsigned_integer
bvt from_unsigned_integer(const bvt &)
Definition: float_utils.cpp:50
validation_modet::INVARIANT
prop_conv_solvert::prop
propt & prop
Definition: prop_conv.h:152