Skip to content

Commit 521c6b9

Browse files
authored
Merge pull request #2945 from danpoe/refactor/error-handling-solvers-floatbv
Error handling cleanup in solvers/floatbv
2 parents ad5752c + 0dce179 commit 521c6b9

File tree

4 files changed

+108
-104
lines changed

4 files changed

+108
-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: 28 additions & 26 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
{
@@ -809,10 +811,8 @@ exprt float_bvt::relation(
809811
or_exprt(bitwise_equal, both_zero),
810812
not_exprt(nan));
811813
}
812-
else
813-
assert(0);
814814

815-
// not reached
815+
UNREACHABLE;
816816
return false_exprt();
817817
}
818818

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

875875
std::size_t depth = address_bits(fraction_bits - 1);
876876

@@ -882,7 +882,9 @@ void float_bvt::normalization_shift(
882882
for(int d=depth-1; d>=0; d--)
883883
{
884884
unsigned distance=(1<<d);
885-
assert(fraction_bits>distance);
885+
INVARIANT(
886+
fraction_bits > distance,
887+
"distance must be within the range of fraction bits");
886888

887889
// check if first 'distance'-many bits are zeros
888890
const extractbits_exprt prefix(
@@ -900,7 +902,7 @@ void float_bvt::normalization_shift(
900902
if_exprt(prefix_is_zero, shifted, fraction);
901903

902904
// add corresponding weight to exponent
903-
assert(d<(signed int)exponent_bits);
905+
INVARIANT(d < (signed int)exponent_bits, "");
904906

905907
exponent_delta=
906908
bitor_exprt(exponent_delta,
@@ -926,7 +928,7 @@ void float_bvt::denormalization_shift(
926928
// exponent for subnormal numbers.
927929

928930
std::size_t exponent_bits=to_signedbv_type(exponent.type()).get_width();
929-
assert(exponent_bits>=spec.e);
931+
PRECONDITION(exponent_bits >= spec.e);
930932

931933
#if 1
932934
// Need to sign extend to avoid overflow. Note that this is a
@@ -1041,7 +1043,7 @@ exprt float_bvt::fraction_rounding_decision(
10411043
std::size_t fraction_bits=
10421044
to_unsignedbv_type(fraction.type()).get_width();
10431045

1044-
assert(dest_bits<fraction_bits);
1046+
PRECONDITION(dest_bits < fraction_bits);
10451047

10461048
// we have too many fraction bits
10471049
std::size_t extra_bits=fraction_bits-dest_bits;
@@ -1061,7 +1063,8 @@ exprt float_bvt::fraction_rounding_decision(
10611063
}
10621064

10631065
// the rounding bit is the last extra bit
1064-
assert(extra_bits>=1);
1066+
INVARIANT(
1067+
extra_bits >= 1, "the extra bits contain at least the rounding bit");
10651068
const extractbit_exprt rounding_bit(fraction, extra_bits - 1);
10661069

10671070
// we get one bit of the fraction for some rounding decisions
@@ -1116,7 +1119,8 @@ void float_bvt::round_fraction(
11161119
else // fraction gets smaller -- rounding
11171120
{
11181121
std::size_t extra_bits=result_fraction_size-fraction_size;
1119-
assert(extra_bits>=1);
1122+
INVARIANT(
1123+
extra_bits >= 1, "the extra bits include at least the rounding bit");
11201124

11211125
// this computes the rounding decision
11221126
exprt increment=fraction_rounding_decision(
@@ -1203,13 +1207,10 @@ void float_bvt::round_exponent(
12031207
std::size_t result_exponent_size=
12041208
to_signedbv_type(result.exponent.type()).get_width();
12051209

1210+
PRECONDITION(result_exponent_size >= spec.e);
1211+
12061212
// 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
1213+
if(result_exponent_size == spec.e) // it stays
12131214
{
12141215
// do nothing
12151216
}
@@ -1278,7 +1279,8 @@ float_bvt::biased_floatt float_bvt::bias(
12781279
result.exponent=add_bias(src.exponent, spec);
12791280

12801281
// strip off the hidden bit
1281-
assert(to_unsignedbv_type(src.fraction.type()).get_width()==spec.f+1);
1282+
PRECONDITION(
1283+
to_unsignedbv_type(src.fraction.type()).get_width() == spec.f + 1);
12821284

12831285
const extractbit_exprt hidden_bit(src.fraction, spec.f);
12841286
const not_exprt denormal(hidden_bit);
@@ -1354,8 +1356,8 @@ exprt float_bvt::pack(
13541356
const biased_floatt &src,
13551357
const ieee_float_spect &spec)
13561358
{
1357-
assert(to_unsignedbv_type(src.fraction.type()).get_width()==spec.f);
1358-
assert(to_unsignedbv_type(src.exponent.type()).get_width()==spec.e);
1359+
PRECONDITION(to_unsignedbv_type(src.fraction.type()).get_width() == spec.f);
1360+
PRECONDITION(to_unsignedbv_type(src.exponent.type()).get_width() == spec.e);
13591361

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

0 commit comments

Comments
 (0)