[PPL-devel] [GIT] ppl/ppl(master): Fixed a bug in wrap_assign() for grids.

Patricia Hill p.m.hill at leeds.ac.uk
Wed May 13 10:25:27 CEST 2009


Module: ppl/ppl
Branch: master
Commit: c8cbb00f29ac4c0d48daf14a9a9d3b93e559e207
URL:    http://www.cs.unipr.it/git/gitweb.cgi?p=ppl/ppl.git;a=commit;h=c8cbb00f29ac4c0d48daf14a9a9d3b93e559e207

Author: Patricia Hill <p.m.hill at leeds.ac.uk>
Date:   Wed May 13 09:21:47 2009 +0100

Fixed a bug in wrap_assign() for grids.

---

 src/Grid_public.cc  |   22 +++++++++++--------
 tests/Grid/wrap1.cc |   56 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+), 9 deletions(-)

diff --git a/src/Grid_public.cc b/src/Grid_public.cc
index 844c7ec..16e6ba9 100644
--- a/src/Grid_public.cc
+++ b/src/Grid_public.cc
@@ -2700,6 +2700,7 @@ PPL::Grid::wrap_assign(const Variables_Set& vars,
     PPL_DIRTY_TEMP_COEFFICIENT(f_d);
     PPL_DIRTY_TEMP_COEFFICIENT(v_n);
     PPL_DIRTY_TEMP_COEFFICIENT(v_d);
+    PPL_DIRTY_TEMP_COEFFICIENT(f_d_wrap_frequency);
     for (Variables_Set::const_iterator i = vars.begin(),
            vars_end = vars.end(); i != vars.end(); ++i) {
       const Variable x = Variable(*i);
@@ -2730,9 +2731,16 @@ PPL::Grid::wrap_assign(const Variables_Set& vars,
       }
 
       // `x' is not a constant in `gr'.
-      if (2*f_n > f_d * wrap_frequency) {
-        // If the grid frequency for `x' in `vars' is more than half the
-        // `wrap_frequency', then `x' can only take a unique (ie constant)
+      assert(f_n != 0);
+      Coefficient& wrap_modulus = f_n;
+      f_d_wrap_frequency = f_d * wrap_frequency;
+      if (o == OVERFLOW_WRAPS && f_n != f_d * wrap_frequency)
+        // We know that `x' is not a constant, so, if overflow wraps,
+        // `x' may wrap to a value modulo the `wrap_frequency'.
+        add_grid_generator(parameter(wrap_frequency * x));
+      else if ((o == OVERFLOW_IMPOSSIBLE && 2*f_n >= f_d_wrap_frequency)
+               || (f_n == f_d_wrap_frequency)) {
+        // In these cases, `x' can only take a unique (ie constant)
         // value.
         if (s == UNSIGNED && v_n < 0) {
           // `v_n' is the value closest to 0 and may be negative.
@@ -2742,16 +2750,12 @@ PPL::Grid::wrap_assign(const Variables_Set& vars,
         }
         add_constraint(v_d * x == v_n);
       }
-      else if (o == OVERFLOW_WRAPS)
-        // We know that `x' is not a constant, so, if overflow wraps,
-        // `x' may wrap to a value modulo the `wrap_frequency'.
-        add_grid_generator(parameter(wrap_frequency * x));
       else
-        // If overflow is impossible but the grid frequency is no more than
+        // If overflow is impossible but the grid frequency is less than
         // half the wrap frequency, then there is more than one possible
         // value for `x' in the range of the bounded integer type,
         // so the grid is unchanged.
-        assert(o == OVERFLOW_IMPOSSIBLE && 2*f_n <= f_d * wrap_frequency);
+        assert(o == OVERFLOW_IMPOSSIBLE && 2*f_n < f_d_wrap_frequency);
     }
     return;
   }
diff --git a/tests/Grid/wrap1.cc b/tests/Grid/wrap1.cc
index 235ba02..cf45a97 100644
--- a/tests/Grid/wrap1.cc
+++ b/tests/Grid/wrap1.cc
@@ -352,6 +352,60 @@ test13() {
   return ok;
 }
 
+bool
+test14() {
+  Variable x(0);
+  Variable y(1);
+
+  Grid gr1(2);
+  gr1.add_congruence((x %= 245) / 255);
+  Grid gr2(gr1);
+
+  Variables_Set vars(x);
+
+  gr1.wrap_assign(vars, BITS_8, UNSIGNED, OVERFLOW_WRAPS);
+  gr2.wrap_assign(vars, BITS_8, SIGNED_2_COMPLEMENT, OVERFLOW_WRAPS);
+
+  Grid known_result1(2);
+  known_result1.add_congruence((x %= 0) / 1);
+  Grid known_result2(2);
+  known_result2.add_congruence((x %= 0) / 1);
+
+  bool ok = (gr1 == known_result1 && gr2 == known_result2);
+
+  print_congruences(gr1, "*** gr1.wrap_assign(...) ***");
+  print_congruences(gr2, "*** gr2.wrap_assign(...) ***");
+
+  return ok;
+}
+
+bool
+test15() {
+  Variable x(0);
+  Variable y(1);
+
+  Grid gr1(2);
+  gr1.add_congruence((x %= 245) / 256);
+  Grid gr2(gr1);
+
+  Variables_Set vars(x);
+
+  gr1.wrap_assign(vars, BITS_8, UNSIGNED, OVERFLOW_WRAPS);
+  gr2.wrap_assign(vars, BITS_8, SIGNED_2_COMPLEMENT, OVERFLOW_WRAPS);
+
+  Grid known_result1(2);
+  known_result1.add_congruence((x %= 245) / 0);
+  Grid known_result2(2);
+  known_result2.add_congruence((x %= -11) / 0);
+
+  bool ok = (gr1 == known_result1 && gr2 == known_result2);
+
+  print_congruences(gr1, "*** gr1.wrap_assign(...) ***");
+  print_congruences(gr2, "*** gr2.wrap_assign(...) ***");
+
+  return ok;
+}
+
 
 } // namespace
 
@@ -369,4 +423,6 @@ BEGIN_MAIN
   DO_TEST_F8(test11);
   DO_TEST_F8(test12);
   DO_TEST_F8(test13);
+  DO_TEST_F8(test14);
+  DO_TEST_F8(test15);
 END_MAIN




More information about the PPL-devel mailing list