extract out shared isl_pw_*_un_op
authorSven Verdoolaege <sven@cerebras.net>
Mon, 6 May 2019 11:00:33 +0000 (6 13:00 +0200)
committerSven Verdoolaege <sven@cerebras.net>
Sun, 1 Aug 2021 12:22:03 +0000 (1 14:22 +0200)
This removes some code duplication.

Signed-off-by: Sven Verdoolaege <sven@cerebras.net>
Makefile.am
isl_aff.c
isl_polynomial.c
isl_pw_neg_templ.c
isl_pw_un_op_templ.c [copied from isl_pw_neg_templ.c with 51% similarity]

index 3e7d7b1..939dbea 100644 (file)
@@ -588,6 +588,7 @@ EXTRA_DIST = \
        isl_pw_pullback_templ.c \
        isl_pw_range_tuple_id_templ.c \
        isl_pw_sub_templ.c \
+       isl_pw_un_op_templ.c \
        isl_pw_union_opt.c \
        read_in_string_templ.c \
        set_to_map.c \
index 40ca78d..f1876ec 100644 (file)
--- a/isl_aff.c
+++ b/isl_aff.c
@@ -2806,6 +2806,7 @@ static __isl_give isl_aff *isl_aff_zero_in_space(__isl_take isl_space *space)
 #define DEFAULT_IS_ZERO 0
 
 #include <isl_pw_templ.c>
+#include <isl_pw_un_op_templ.c>
 #include <isl_pw_add_constant_val_templ.c>
 #include <isl_pw_bind_domain_templ.c>
 #include <isl_pw_eval.c>
@@ -3289,40 +3290,12 @@ __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
 
 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
 {
-       int i;
-
-       pwaff = isl_pw_aff_cow(pwaff);
-       if (!pwaff)
-               return NULL;
-       if (pwaff->n == 0)
-               return pwaff;
-
-       for (i = 0; i < pwaff->n; ++i) {
-               pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
-               if (!pwaff->p[i].aff)
-                       return isl_pw_aff_free(pwaff);
-       }
-
-       return pwaff;
+       return isl_pw_aff_un_op(pwaff, &isl_aff_floor);
 }
 
 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
 {
-       int i;
-
-       pwaff = isl_pw_aff_cow(pwaff);
-       if (!pwaff)
-               return NULL;
-       if (pwaff->n == 0)
-               return pwaff;
-
-       for (i = 0; i < pwaff->n; ++i) {
-               pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
-               if (!pwaff->p[i].aff)
-                       return isl_pw_aff_free(pwaff);
-       }
-
-       return pwaff;
+       return isl_pw_aff_un_op(pwaff, &isl_aff_ceil);
 }
 
 /* Assuming that "cond1" and "cond2" are disjoint,
@@ -4513,6 +4486,7 @@ __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
 #define DEFAULT_IS_ZERO 0
 
 #include <isl_pw_templ.c>
+#include <isl_pw_un_op_templ.c>
 #include <isl_pw_add_constant_multi_val_templ.c>
 #include <isl_pw_add_constant_val_templ.c>
 #include <isl_pw_bind_domain_templ.c>
index 0aac613..170cb9e 100644 (file)
@@ -3108,6 +3108,7 @@ static __isl_give isl_qpolynomial *isl_qpolynomial_zero_in_space(
 #define DEFAULT_IS_ZERO 1
 
 #include <isl_pw_templ.c>
+#include <isl_pw_un_op_templ.c>
 #include <isl_pw_eval.c>
 #include <isl_pw_insert_dims_templ.c>
 #include <isl_pw_lift_templ.c>
index 65970cf..d3da913 100644 (file)
 
 __isl_give PW *FN(PW,neg)(__isl_take PW *pw)
 {
-       int i;
-
-       if (!pw)
-               return NULL;
-
-       if (FN(PW,IS_ZERO)(pw))
-               return pw;
-
-       pw = FN(PW,cow)(pw);
-       if (!pw)
-               return NULL;
-
-       for (i = 0; i < pw->n; ++i) {
-               pw->p[i].FIELD = FN(EL,neg)(pw->p[i].FIELD);
-               if (!pw->p[i].FIELD)
-                       return FN(PW,free)(pw);
-       }
-
-       return pw;
+       return FN(PW,un_op)(pw, &FN(EL,neg));
 }
similarity index 51%
copy from isl_pw_neg_templ.c
copy to isl_pw_un_op_templ.c
index 65970cf..a3eded7 100644 (file)
 
 #include <isl_pw_macro.h>
 
-__isl_give PW *FN(PW,neg)(__isl_take PW *pw)
+/* Apply "fn" to each of the base expressions of "pw".
+ * The function is assumed to have no effect on the default value
+ * (i.e., zero for those objects with a default value).
+ */
+static __isl_give PW *FN(PW,un_op)(__isl_take PW *pw,
+       __isl_give EL *(*fn)(__isl_take EL *el))
 {
+       isl_bool is_zero;
        int i;
 
-       if (!pw)
-               return NULL;
-
-       if (FN(PW,IS_ZERO)(pw))
+       is_zero = FN(PW,IS_ZERO)(pw);
+       if (is_zero < 0)
+               return FN(PW,free)(pw);
+       if (is_zero)
                return pw;
 
        pw = FN(PW,cow)(pw);
@@ -25,7 +31,7 @@ __isl_give PW *FN(PW,neg)(__isl_take PW *pw)
                return NULL;
 
        for (i = 0; i < pw->n; ++i) {
-               pw->p[i].FIELD = FN(EL,neg)(pw->p[i].FIELD);
+               pw->p[i].FIELD = fn(pw->p[i].FIELD);
                if (!pw->p[i].FIELD)
                        return FN(PW,free)(pw);
        }