CBMC
float_utils.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 
10 #ifndef CPROVER_SOLVERS_FLOATBV_FLOAT_UTILS_H
11 #define CPROVER_SOLVERS_FLOATBV_FLOAT_UTILS_H
12 
13 #include <util/ieee_float.h>
14 
16 
18 {
19 public:
21  {
22  public:
23  // these are mutually exclusive, obviously
28 
34  {
35  }
36 
38  {
40  const_literal(false);
41 
42  switch(mode)
43  {
46  break;
47 
50  break;
51 
54  break;
55 
58  break;
59 
63  }
64  }
65  };
66 
68 
69  explicit float_utilst(propt &_prop):
70  prop(_prop),
71  bv_utils(_prop)
72  {
73  }
74 
75  float_utilst(propt &_prop, const floatbv_typet &type):
76  spec(ieee_float_spect(type)),
77  prop(_prop),
78  bv_utils(_prop)
79  {
80  }
81 
82  void set_rounding_mode(const bvt &);
83 
84  virtual ~float_utilst()
85  {
86  }
87 
89 
91 
92  static inline literalt sign_bit(const bvt &src)
93  {
94  // this is the top bit
95  return src[src.size()-1];
96  }
97 
98  // extraction
99  bvt get_exponent(const bvt &); // still biased
100  bvt get_fraction(const bvt &); // without hidden bit
101  literalt is_normal(const bvt &);
102  literalt is_zero(const bvt &); // this returns true on both 0 and -0
103  literalt is_infinity(const bvt &);
104  literalt is_plus_inf(const bvt &);
105  literalt is_minus_inf(const bvt &);
106  literalt is_NaN(const bvt &);
107 
108  // add/sub
109  virtual bvt add_sub(const bvt &src1, const bvt &src2, bool subtract);
110 
111  bvt add(const bvt &src1, const bvt &src2)
112  {
113  return add_sub(src1, src2, false);
114  }
115 
116  bvt sub(const bvt &src1, const bvt &src2)
117  {
118  return add_sub(src1, src2, true);
119  }
120 
121  // mul/div/rem
122  virtual bvt mul(const bvt &src1, const bvt &src2);
123  virtual bvt div(const bvt &src1, const bvt &src2);
124  virtual bvt rem(const bvt &src1, const bvt &src2);
125 
126  bvt abs(const bvt &);
127  bvt negate(const bvt &);
128 
129  // conversion
130  bvt from_unsigned_integer(const bvt &);
131  bvt from_signed_integer(const bvt &);
132  bvt to_integer(const bvt &src, std::size_t int_width, bool is_signed);
133  bvt to_signed_integer(const bvt &src, std::size_t int_width);
134  bvt to_unsigned_integer(const bvt &src, std::size_t int_width);
135  bvt conversion(const bvt &src, const ieee_float_spect &dest_spec);
136 
137  // relations
138  enum class relt { LT, LE, EQ, GT, GE };
139  literalt relation(const bvt &src1, relt rel, const bvt &src2);
140 
141  // constants
142  ieee_floatt get(const bvt &) const;
143 
144  // helpers
145  literalt exponent_all_ones(const bvt &);
148 
149  // debugging hooks
150  bvt debug1(const bvt &op0, const bvt &op1);
151  bvt debug2(const bvt &op0, const bvt &op1);
152 
153 protected:
156 
157  // unpacked
158  virtual void normalization_shift(bvt &fraction, bvt &exponent);
159  void denormalization_shift(bvt &fraction, bvt &exponent);
160 
161  bvt add_bias(const bvt &exponent);
162  bvt sub_bias(const bvt &exponent);
163 
164  bvt limit_distance(const bvt &dist, mp_integer limit);
165 
167  {
170 
172  sign(const_literal(false)),
173  infinity(const_literal(false)),
174  zero(const_literal(false)),
175  NaN(const_literal(false))
176  {
177  }
178  };
179 
180  // this has a biased exponent
181  // and an _implicit_ hidden bit
183  {
184  };
185 
186  // the hidden bit is explicit,
187  // and the exponent is not biased
189  {
190  };
191 
193 
194  // this takes unpacked format, and returns packed
195  virtual bvt rounder(const unbiased_floatt &);
196  bvt pack(const biased_floatt &);
197  unbiased_floatt unpack(const bvt &);
198 
199  void round_fraction(unbiased_floatt &result);
200  void round_exponent(unbiased_floatt &result);
201 
202  // rounding decision for fraction
204  const std::size_t dest_bits,
205  const literalt sign,
206  const bvt &fraction);
207 
208  // helpers for adder
209 
210  // computes src1.exponent-src2.exponent with extension
212  const unbiased_floatt &src1,
213  const unbiased_floatt &src2);
214 
215  // computes the "sticky-bit"
217  const bvt &op,
218  const bvt &dist,
219  literalt &sticky);
220 };
221 
222 #endif // CPROVER_SOLVERS_FLOATBV_FLOAT_UTILS_H
UNREACHABLE
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:503
ieee_floatt
Definition: ieee_float.h:116
float_utilst::abs
bvt abs(const bvt &)
Definition: float_utils.cpp:566
float_utilst::bias
biased_floatt bias(const unbiased_floatt &)
takes an unbiased float, and applies the bias
Definition: float_utils.cpp:1158
float_utilst::rounding_mode_bits
rounding_mode_bitst rounding_mode_bits
Definition: float_utils.h:67
mp_integer
BigInt mp_integer
Definition: smt_terms.h:17
float_utilst::fraction_rounding_decision
literalt fraction_rounding_decision(const std::size_t dest_bits, const literalt sign, const bvt &fraction)
rounding decision for fraction using sticky bit
Definition: float_utils.cpp:938
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::unpacked_floatt::exponent
bvt exponent
Definition: float_utils.h:169
float_utilst::rounding_mode_bitst::round_to_even
literalt round_to_even
Definition: float_utils.h:24
float_utilst
Definition: float_utils.h:17
bvt
std::vector< literalt > bvt
Definition: literal.h:201
float_utilst::sticky_right_shift
bvt sticky_right_shift(const bvt &op, const bvt &dist, literalt &sticky)
Definition: float_utils.cpp:1280
float_utilst::~float_utilst
virtual ~float_utilst()
Definition: float_utils.h:84
is_signed
bool is_signed(const typet &t)
Convenience function – is the type signed?
Definition: util.cpp:45
float_utilst::sub
bvt sub(const bvt &src1, const bvt &src2)
Definition: float_utils.h:116
floatbv_typet
Fixed-width bit-vector with IEEE floating-point interpretation.
Definition: bitvector_types.h:321
float_utilst::debug1
bvt debug1(const bvt &op0, const bvt &op1)
Definition: float_utils.cpp:1315
float_utilst::unpacked_floatt::unpacked_floatt
unpacked_floatt()
Definition: float_utils.h:171
float_utilst::unpacked_floatt::fraction
bvt fraction
Definition: float_utils.h:169
bv_utils.h
float_utilst::add
bvt add(const bvt &src1, const bvt &src2)
Definition: float_utils.h:111
float_utilst::float_utilst
float_utilst(propt &_prop, const floatbv_typet &type)
Definition: float_utils.h:75
float_utilst::rounder
virtual bvt rounder(const unbiased_floatt &)
Definition: float_utils.cpp:899
float_utilst::sub_bias
bvt sub_bias(const bvt &exponent)
Definition: float_utils.cpp:1197
float_utilst::unpack
unbiased_floatt unpack(const bvt &)
Definition: float_utils.cpp:1206
float_utilst::unpacked_floatt
Definition: float_utils.h:166
float_utilst::normalization_shift
virtual void normalization_shift(bvt &fraction, bvt &exponent)
normalize fraction/exponent pair returns 'zero' if fraction is zero
Definition: float_utils.cpp:743
float_utilst::relt::GE
@ GE
float_utilst::get
ieee_floatt get(const bvt &) const
Definition: float_utils.cpp:1266
float_utilst::rounding_mode_bitst::round_to_zero
literalt round_to_zero
Definition: float_utils.h:25
float_utilst::set_rounding_mode
void set_rounding_mode(const bvt &)
Definition: float_utils.cpp:15
float_utilst::add_bias
bvt add_bias(const bvt &exponent)
Definition: float_utils.cpp:1188
float_utilst::prop
propt & prop
Definition: float_utils.h:154
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
float_utilst::relt::GT
@ GT
ieee_float_spect
Definition: ieee_float.h:22
float_utilst::is_infinity
literalt is_infinity(const bvt &)
Definition: float_utils.cpp:673
float_utilst::is_minus_inf
literalt is_minus_inf(const bvt &)
Definition: float_utils.cpp:692
bv_utilst
Definition: bv_utils.h:23
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
float_utilst::denormalization_shift
void denormalization_shift(bvt &fraction, bvt &exponent)
make sure exponent is not too small; the exponent is unbiased
Definition: float_utils.cpp:831
float_utilst::is_zero
literalt is_zero(const bvt &)
Definition: float_utils.cpp:655
float_utilst::get_exponent
bvt get_exponent(const bvt &)
Gets the unbiased exponent in a floating-point bit-vector.
Definition: float_utils.cpp:681
float_utilst::is_normal
literalt is_normal(const bvt &)
Definition: float_utils.cpp:219
float_utilst::sign_bit
static literalt sign_bit(const bvt &src)
Definition: float_utils.h:92
float_utilst::exponent_all_ones
literalt exponent_all_ones(const bvt &)
Definition: float_utils.cpp:707
float_utilst::unpacked_floatt::sign
literalt sign
Definition: float_utils.h:168
float_utilst::pack
bvt pack(const biased_floatt &)
Definition: float_utils.cpp:1235
float_utilst::div
virtual bvt div(const bvt &src1, const bvt &src2)
Definition: float_utils.cpp:460
float_utilst::unpacked_floatt::infinity
literalt infinity
Definition: float_utils.h:168
float_utilst::build_constant
bvt build_constant(const ieee_floatt &)
Definition: float_utils.cpp:139
const_literal
literalt const_literal(bool value)
Definition: literal.h:188
float_utilst::debug2
bvt debug2(const bvt &op0, const bvt &op1)
Definition: float_utils.cpp:1322
float_utilst::subtract_exponents
bvt subtract_exponents(const unbiased_floatt &src1, const unbiased_floatt &src2)
Subtracts the exponents.
Definition: float_utils.cpp:227
float_utilst::is_NaN
literalt is_NaN(const bvt &)
Definition: float_utils.cpp:701
ieee_floatt::rounding_modet
rounding_modet
Definition: ieee_float.h:123
float_utilst::unpacked_floatt::zero
literalt zero
Definition: float_utils.h:168
float_utilst::unpacked_floatt::NaN
literalt NaN
Definition: float_utils.h:168
float_utilst::bv_utils
bv_utilst bv_utils
Definition: float_utils.h:155
float_utilst::to_integer
bvt to_integer(const bvt &src, std::size_t int_width, bool is_signed)
Definition: float_utils.cpp:81
float_utilst::rounding_mode_bitst::rounding_mode_bitst
rounding_mode_bitst()
Definition: float_utils.h:29
float_utilst::is_plus_inf
literalt is_plus_inf(const bvt &)
Definition: float_utils.cpp:664
ieee_floatt::UNKNOWN
@ UNKNOWN
Definition: ieee_float.h:127
float_utilst::relation
literalt relation(const bvt &src1, relt rel, const bvt &src2)
Definition: float_utils.cpp:574
propt
TO_BE_DOCUMENTED.
Definition: prop.h:22
float_utilst::biased_floatt
Definition: float_utils.h:182
float_utilst::get_fraction
bvt get_fraction(const bvt &)
Gets the fraction without hidden bit in a floating-point bit-vector src.
Definition: float_utils.cpp:687
ieee_floatt::ROUND_TO_PLUS_INF
@ ROUND_TO_PLUS_INF
Definition: ieee_float.h:126
float_utilst::relt
relt
Definition: float_utils.h:138
ieee_floatt::ROUND_TO_EVEN
@ ROUND_TO_EVEN
Definition: ieee_float.h:125
float_utilst::negate
bvt negate(const bvt &)
Definition: float_utils.cpp:557
float_utilst::exponent_all_zeros
literalt exponent_all_zeros(const bvt &)
Definition: float_utils.cpp:720
float_utilst::relt::LT
@ LT
ieee_float.h
float_utilst::unbiased_floatt
Definition: float_utils.h:188
literalt
Definition: literal.h:25
float_utilst::rounding_mode_bitst::set
void set(const ieee_floatt::rounding_modet mode)
Definition: float_utils.h:37
ieee_floatt::NONDETERMINISTIC
@ NONDETERMINISTIC
Definition: ieee_float.h:127
float_utilst::round_fraction
void round_fraction(unbiased_floatt &result)
Definition: float_utils.cpp:996
float_utilst::float_utilst
float_utilst(propt &_prop)
Definition: float_utils.h:69
float_utilst::fraction_all_zeros
literalt fraction_all_zeros(const bvt &)
Definition: float_utils.cpp:733
float_utilst::rounding_mode_bitst::round_to_plus_inf
literalt round_to_plus_inf
Definition: float_utils.h:26
float_utilst::limit_distance
bvt limit_distance(const bvt &dist, mp_integer limit)
Limits the shift distance.
Definition: float_utils.cpp:385
ieee_floatt::ROUND_TO_MINUS_INF
@ ROUND_TO_MINUS_INF
Definition: ieee_float.h:125
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
ieee_floatt::ROUND_TO_ZERO
@ ROUND_TO_ZERO
Definition: ieee_float.h:126
float_utilst::relt::LE
@ LE
float_utilst::rounding_mode_bitst
Definition: float_utils.h:20
float_utilst::round_exponent
void round_exponent(unbiased_floatt &result)
Definition: float_utils.cpp:1095
float_utilst::relt::EQ
@ EQ
float_utilst::rounding_mode_bitst::round_to_minus_inf
literalt round_to_minus_inf
Definition: float_utils.h:27