isl_aff_floor: add special case for constant arguments
authorSven Verdoolaege <skimo@kotnet.org>
Mon, 9 Apr 2012 18:26:14 +0000 (20:26 +0200)
committerSven Verdoolaege <skimo@kotnet.org>
Fri, 11 May 2012 08:27:17 +0000 (10:27 +0200)
Requested-by: Tobias Grosser <tobias@grosser.es>
Signed-off-by: Sven Verdoolaege <skimo@kotnet.org>
isl_aff.c
isl_test.c

index 3970108..b316a31 100644 (file)
--- a/isl_aff.c
+++ b/isl_aff.c
@@ -624,6 +624,7 @@ __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
 
 /* Given f, return floor(f).
  * If f is an integer expression, then just return f.
+ * If f is a constant, then return the constant floor(f).
  * Otherwise, if f = g/m, write g = q m + r,
  * create a new div d = [r/m] and return the expression q + d.
  * The coefficients in r are taken to lie between -m/2 and m/2.
@@ -646,6 +647,15 @@ __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
                return NULL;
 
        aff->v = isl_vec_cow(aff->v);
+       if (!aff->v)
+               return isl_aff_free(aff);
+
+       if (isl_aff_is_cst(aff)) {
+               isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
+               isl_int_set_si(aff->v->el[0], 1);
+               return aff;
+       }
+
        div = isl_vec_copy(aff->v);
        div = isl_vec_cow(div);
        if (!div)
index 253bc10..fb76444 100644 (file)
@@ -2489,17 +2489,45 @@ int test_injective(isl_ctx *ctx)
        return 0;
 }
 
+static int aff_plain_is_equal(__isl_keep isl_aff *aff, const char *str)
+{
+       isl_aff *aff2;
+       int equal;
+
+       if (!aff)
+               return -1;
+
+       aff2 = isl_aff_read_from_str(isl_aff_get_ctx(aff), str);
+       equal = isl_aff_plain_is_equal(aff, aff2);
+       isl_aff_free(aff2);
+
+       return equal;
+}
+
+static int aff_check_plain_equal(__isl_keep isl_aff *aff, const char *str)
+{
+       int equal;
+
+       equal = aff_plain_is_equal(aff, str);
+       if (equal < 0)
+               return -1;
+       if (!equal)
+               isl_die(isl_aff_get_ctx(aff), isl_error_unknown,
+                       "result not as expected", return -1);
+       return 0;
+}
+
 int test_aff(isl_ctx *ctx)
 {
        const char *str;
        isl_set *set;
-       isl_space *dim;
+       isl_space *space;
        isl_local_space *ls;
        isl_aff *aff;
-       int zero;
+       int zero, equal;
 
-       dim = isl_space_set_alloc(ctx, 0, 1);
-       ls = isl_local_space_from_space(dim);
+       space = isl_space_set_alloc(ctx, 0, 1);
+       ls = isl_local_space_from_space(space);
        aff = isl_aff_zero_on_domain(ls);
 
        aff = isl_aff_add_coefficient_si(aff, isl_dim_in, 0, 1);
@@ -2523,6 +2551,14 @@ int test_aff(isl_ctx *ctx)
        if (!zero)
                isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
 
+       aff = isl_aff_read_from_str(ctx, "{ [-1] }");
+       aff = isl_aff_scale_down_ui(aff, 64);
+       aff = isl_aff_floor(aff);
+       equal = aff_check_plain_equal(aff, "{ [-1] }");
+       isl_aff_free(aff);
+       if (equal < 0)
+               return -1;
+
        return 0;
 }