Skip to content

Commit 9df60c6

Browse files
committed
Error handling cleanup in solvers/floatbv
1 parent a6108ec commit 9df60c6

File tree

4 files changed

+110
-104
lines changed

4 files changed

+110
-104
lines changed

src/solvers/floatbv/float_approximation.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ Author: Daniel Kroening, [email protected]
88

99
#include "float_approximation.h"
1010

11-
#include <cassert>
12-
1311
float_approximationt::~float_approximationt()
1412
{
1513
}

src/solvers/floatbv/float_bv.cpp

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ Author: Daniel Kroening, [email protected]
88

99
#include "float_bv.h"
1010

11-
#include <cassert>
1211
#include <algorithm>
1312

1413
#include <util/std_expr.h>
@@ -364,7 +363,9 @@ exprt float_bvt::conversion(
364363
unsignedbv_typet(dest_spec.f+1));
365364

366365
// the exponent gets sign-extended
367-
assert(unpacked_src.exponent.type().id()==ID_signedbv);
366+
INVARIANT(
367+
unpacked_src.exponent.type().id() == ID_signedbv,
368+
"the exponent needs to have a signed type");
368369
result.exponent=
369370
typecast_exprt(unpacked_src.exponent, signedbv_typet(dest_spec.e));
370371

@@ -406,15 +407,14 @@ exprt float_bvt::subtract_exponents(
406407
// extend both by one bit
407408
std::size_t old_width1=to_signedbv_type(src1.exponent.type()).get_width();
408409
std::size_t old_width2=to_signedbv_type(src2.exponent.type()).get_width();
409-
assert(old_width1==old_width2);
410+
PRECONDITION(old_width1 == old_width2);
410411

411412
const typecast_exprt extended_exponent1(
412413
src1.exponent, signedbv_typet(old_width1 + 1));
414+
413415
const typecast_exprt extended_exponent2(
414416
src2.exponent, signedbv_typet(old_width2 + 1));
415417

416-
assert(extended_exponent1.type()==extended_exponent2.type());
417-
418418
// compute shift distance (here is the subtraction)
419419
return minus_exprt(extended_exponent1, extended_exponent2);
420420
}
@@ -738,7 +738,9 @@ exprt float_bvt::relation(
738738
else if(rel==relt::GE)
739739
return relation(src2, relt::LE, src1, spec); // swapped
740740

741-
assert(rel==relt::EQ || rel==relt::LT || rel==relt::LE);
741+
INVARIANT(
742+
rel == relt::EQ || rel == relt::LT || rel == relt::LE,
743+
"relation should be equality, less-than, or less-or-equal");
742744

743745
// special cases: -0 and 0 are equal
744746
const exprt is_zero1 = is_zero(src1);
@@ -799,7 +801,7 @@ exprt float_bvt::relation(
799801
return and_exprt(or_bv, not_exprt(nan));
800802
}
801803
else
802-
assert(false);
804+
UNREACHABLE;
803805
}
804806
else if(rel==relt::EQ)
805807
{
@@ -810,9 +812,9 @@ exprt float_bvt::relation(
810812
not_exprt(nan));
811813
}
812814
else
813-
assert(0);
815+
UNREACHABLE;
814816

815-
// not reached
817+
UNREACHABLE;
816818
return false_exprt();
817819
}
818820

@@ -870,7 +872,7 @@ void float_bvt::normalization_shift(
870872
// bits minus one, in case the faction is one exactly.
871873
std::size_t fraction_bits=to_unsignedbv_type(fraction.type()).get_width();
872874
std::size_t exponent_bits=to_signedbv_type(exponent.type()).get_width();
873-
assert(fraction_bits!=0);
875+
PRECONDITION(fraction_bits != 0);
874876

875877
std::size_t depth = address_bits(fraction_bits - 1);
876878

@@ -882,7 +884,9 @@ void float_bvt::normalization_shift(
882884
for(int d=depth-1; d>=0; d--)
883885
{
884886
unsigned distance=(1<<d);
885-
assert(fraction_bits>distance);
887+
INVARIANT(
888+
fraction_bits > distance,
889+
"distance must be within the range of fraction bits");
886890

887891
// check if first 'distance'-many bits are zeros
888892
const extractbits_exprt prefix(
@@ -900,7 +904,7 @@ void float_bvt::normalization_shift(
900904
if_exprt(prefix_is_zero, shifted, fraction);
901905

902906
// add corresponding weight to exponent
903-
assert(d<(signed int)exponent_bits);
907+
INVARIANT(d < (signed int)exponent_bits, "");
904908

905909
exponent_delta=
906910
bitor_exprt(exponent_delta,
@@ -926,7 +930,7 @@ void float_bvt::denormalization_shift(
926930
// exponent for subnormal numbers.
927931

928932
std::size_t exponent_bits=to_signedbv_type(exponent.type()).get_width();
929-
assert(exponent_bits>=spec.e);
933+
PRECONDITION(exponent_bits >= spec.e);
930934

931935
#if 1
932936
// Need to sign extend to avoid overflow. Note that this is a
@@ -1041,7 +1045,7 @@ exprt float_bvt::fraction_rounding_decision(
10411045
std::size_t fraction_bits=
10421046
to_unsignedbv_type(fraction.type()).get_width();
10431047

1044-
assert(dest_bits<fraction_bits);
1048+
PRECONDITION(dest_bits < fraction_bits);
10451049

10461050
// we have too many fraction bits
10471051
std::size_t extra_bits=fraction_bits-dest_bits;
@@ -1061,7 +1065,8 @@ exprt float_bvt::fraction_rounding_decision(
10611065
}
10621066

10631067
// the rounding bit is the last extra bit
1064-
assert(extra_bits>=1);
1068+
INVARIANT(
1069+
extra_bits >= 1, "the extra bits contain at least the rounding bit");
10651070
const extractbit_exprt rounding_bit(fraction, extra_bits - 1);
10661071

10671072
// we get one bit of the fraction for some rounding decisions
@@ -1116,7 +1121,8 @@ void float_bvt::round_fraction(
11161121
else // fraction gets smaller -- rounding
11171122
{
11181123
std::size_t extra_bits=result_fraction_size-fraction_size;
1119-
assert(extra_bits>=1);
1124+
INVARIANT(
1125+
extra_bits >= 1, "the extra bits include at least the rounding bit");
11201126

11211127
// this computes the rounding decision
11221128
exprt increment=fraction_rounding_decision(
@@ -1203,13 +1209,10 @@ void float_bvt::round_exponent(
12031209
std::size_t result_exponent_size=
12041210
to_signedbv_type(result.exponent.type()).get_width();
12051211

1212+
PRECONDITION(result_exponent_size >= spec.e);
1213+
12061214
// do we need to enlarge the exponent?
1207-
if(result_exponent_size<spec.e)
1208-
{
1209-
// should have been done before
1210-
assert(false);
1211-
}
1212-
else if(result_exponent_size==spec.e) // it stays
1215+
if(result_exponent_size == spec.e) // it stays
12131216
{
12141217
// do nothing
12151218
}
@@ -1278,7 +1281,8 @@ float_bvt::biased_floatt float_bvt::bias(
12781281
result.exponent=add_bias(src.exponent, spec);
12791282

12801283
// strip off the hidden bit
1281-
assert(to_unsignedbv_type(src.fraction.type()).get_width()==spec.f+1);
1284+
PRECONDITION(
1285+
to_unsignedbv_type(src.fraction.type()).get_width() == spec.f + 1);
12821286

12831287
const extractbit_exprt hidden_bit(src.fraction, spec.f);
12841288
const not_exprt denormal(hidden_bit);
@@ -1354,8 +1358,8 @@ exprt float_bvt::pack(
13541358
const biased_floatt &src,
13551359
const ieee_float_spect &spec)
13561360
{
1357-
assert(to_unsignedbv_type(src.fraction.type()).get_width()==spec.f);
1358-
assert(to_unsignedbv_type(src.exponent.type()).get_width()==spec.e);
1361+
PRECONDITION(to_unsignedbv_type(src.fraction.type()).get_width() == spec.f);
1362+
PRECONDITION(to_unsignedbv_type(src.exponent.type()).get_width() == spec.e);
13591363

13601364
// do sign -- we make this 'false' for NaN
13611365
const if_exprt sign_bit(src.NaN, false_exprt(), src.sign);

0 commit comments

Comments
 (0)