Skip to content

Commit

Permalink
C library: do not report overflow in sqrt
Browse files Browse the repository at this point in the history
We guess upper and lower bounds and check them for infinity afterwards.
Those multiplications should not be flagged by auto-generated
assertions.
  • Loading branch information
tautschnig committed Feb 7, 2024
1 parent ffc9032 commit c62afc8
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/ansi-c/library/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -899,11 +899,14 @@ float sqrtf(float f)
// number of exponent and significand bits. Thus they are
// given implicitly...

#pragma CPROVER check push
#pragma CPROVER check disable "float-overflow"
float lowerSquare = lower * lower;
__CPROVER_assume(__CPROVER_isnormalf(lowerSquare));

float upper = nextUpf(lower);
float upperSquare = upper * upper; // Might be +Inf
#pragma CPROVER check pop

// Restrict these to bound f and thus compute the possible
// values for the square root. Note that the lower bound
Expand Down Expand Up @@ -986,11 +989,14 @@ double sqrt(double d)
__CPROVER_assume(lower > 0.0);
__CPROVER_assume(__CPROVER_isnormald(lower));

#pragma CPROVER check push
#pragma CPROVER check disable "float-overflow"
double lowerSquare = lower * lower;
__CPROVER_assume(__CPROVER_isnormald(lowerSquare));

double upper = nextUp(lower);
double upperSquare = upper * upper; // Might be +Inf
#pragma CPROVER check pop

__CPROVER_assume(lowerSquare <= d);
__CPROVER_assume(d < upperSquare);
Expand Down Expand Up @@ -1060,11 +1066,14 @@ long double sqrtl(long double d)
__CPROVER_assume(lower > 0.0l);
__CPROVER_assume(__CPROVER_isnormalld(lower));

#pragma CPROVER check push
#pragma CPROVER check disable "float-overflow"
long double lowerSquare = lower * lower;
__CPROVER_assume(__CPROVER_isnormalld(lowerSquare));

long double upper = nextUpl(lower);
long double upperSquare = upper * upper; // Might be +Inf
#pragma CPROVER check pop

__CPROVER_assume(lowerSquare <= d);
__CPROVER_assume(d < upperSquare);
Expand Down

0 comments on commit c62afc8

Please sign in to comment.