isl_seq_normalize: use pre-allocated temporary variable in isl_ctx
authorSven Verdoolaege <skimo@kotnet.org>
Sat, 15 Aug 2009 14:46:16 +0000 (16:46 +0200)
committerSven Verdoolaege <skimo@kotnet.org>
Fri, 28 Aug 2009 17:42:20 +0000 (19:42 +0200)
This requires that we pass along an isl_ctx, but it removes
the need for allocating a temporary variable every time
isl_seq_normalize is called.
In some cases, up to 20% of the execution time of isl_seq_normalize
was wasted on initializing and clearing this temporary variable.

include/isl_ctx.h.in
include/isl_seq.h
isl_convex_hull.c
isl_ctx.c
isl_seq.c
isl_tab.c
isl_vec.c

index 9168ff3..780ec36 100644 (file)
@@ -50,6 +50,8 @@ struct isl_ctx {
        isl_int                 one;
        isl_int                 negone;
 
+       isl_int                 normalize_gcd;
+
        int                     n_cached;
        struct isl_blk          cache[ISL_BLK_CACHE_SIZE];
        struct isl_hash_table   name_hash;
index d4ca039..145846e 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <sys/types.h>
 #include <isl_int.h>
+#include <isl_ctx.h>
 
 /* Some common operations on sequences of isl_int's */
 
@@ -22,7 +23,7 @@ void isl_seq_elim(isl_int *dst, isl_int *src, unsigned pos, unsigned len,
                  isl_int *m);
 void isl_seq_gcd(isl_int *p, unsigned len, isl_int *gcd);
 void isl_seq_lcm(isl_int *p, unsigned len, isl_int *lcm);
-void isl_seq_normalize(isl_int *p, unsigned len);
+void isl_seq_normalize(struct isl_ctx *ctx, isl_int *p, unsigned len);
 void isl_seq_inner_product(isl_int *p1, isl_int *p2, unsigned len,
                           isl_int *prod);
 int isl_seq_first_non_zero(isl_int *p, unsigned len);
index 6461b81..0bab5fa 100644 (file)
@@ -1207,9 +1207,9 @@ static struct isl_vec *valid_direction(
                                bset1->ctx->one, dir->block.data,
                                sample->block.data[n++], bset1->ineq[i], 1 + d);
        isl_vec_free(sample);
+       isl_seq_normalize(bset1->ctx, dir->block.data + 1, dir->size - 1);
        isl_basic_set_free(bset1);
        isl_basic_set_free(bset2);
-       isl_seq_normalize(dir->block.data + 1, dir->size - 1);
        return dir;
 error:
        isl_vec_free(sample);
index bee266b..a9e29f7 100644 (file)
--- a/isl_ctx.c
+++ b/isl_ctx.c
@@ -27,6 +27,8 @@ struct isl_ctx *isl_ctx_alloc()
        isl_int_init(ctx->negone);
        isl_int_set_si(ctx->negone, -1);
 
+       isl_int_init(ctx->normalize_gcd);
+
        ctx->n_cached = 0;
 
 #ifdef ISL_POLYLIB
@@ -66,6 +68,7 @@ void isl_ctx_free(struct isl_ctx *ctx)
        isl_blk_clear_cache(ctx);
        isl_int_clear(ctx->one);
        isl_int_clear(ctx->negone);
+       isl_int_clear(ctx->normalize_gcd);
        free(ctx->stats);
        free(ctx);
 }
index 30e7f54..7c9459a 100644 (file)
--- a/isl_seq.c
+++ b/isl_seq.c
@@ -202,17 +202,14 @@ void isl_seq_gcd(isl_int *p, unsigned len, isl_int *gcd)
        }
 }
 
-void isl_seq_normalize(isl_int *p, unsigned len)
+void isl_seq_normalize(struct isl_ctx *ctx, isl_int *p, unsigned len)
 {
-       isl_int gcd;
-
        if (len == 0)
                return;
-       isl_int_init(gcd);
-       isl_seq_gcd(p, len, &gcd);
-       if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd))
-               isl_seq_scale_down(p, p, gcd, len);
-       isl_int_clear(gcd);
+       isl_seq_gcd(p, len, &ctx->normalize_gcd);
+       if (!isl_int_is_zero(ctx->normalize_gcd) &&
+           !isl_int_is_one(ctx->normalize_gcd))
+               isl_seq_scale_down(p, p, ctx->normalize_gcd, len);
 }
 
 void isl_seq_lcm(isl_int *p, unsigned len, isl_int *lcm)
index e7dabc8..3adb11d 100644 (file)
--- a/isl_tab.c
+++ b/isl_tab.c
@@ -1,6 +1,7 @@
 #include "isl_mat.h"
 #include "isl_map_private.h"
 #include "isl_tab.h"
+#include "isl_seq.h"
 
 /*
  * The implementation of tableaus in this file was inspired by Section 8
@@ -689,7 +690,7 @@ void isl_tab_pivot(struct isl_tab *tab, int row, int col)
                        isl_int_neg(mat->row[row][1 + j], mat->row[row][1 + j]);
                }
        if (!isl_int_is_one(mat->row[row][0]))
-               isl_seq_normalize(mat->row[row], off + tab->n_col);
+               isl_seq_normalize(mat->ctx, mat->row[row], off + tab->n_col);
        for (i = 0; i < tab->n_row; ++i) {
                if (i == row)
                        continue;
@@ -707,7 +708,7 @@ void isl_tab_pivot(struct isl_tab *tab, int row, int col)
                isl_int_mul(mat->row[i][off + col],
                            mat->row[i][off + col], mat->row[row][off + col]);
                if (!isl_int_is_one(mat->row[i][0]))
-                       isl_seq_normalize(mat->row[i], off + tab->n_col);
+                       isl_seq_normalize(mat->ctx, mat->row[i], off + tab->n_col);
        }
        t = tab->row_var[row];
        tab->row_var[row] = tab->col_var[col];
@@ -1197,7 +1198,7 @@ int isl_tab_add_row(struct isl_tab *tab, isl_int *line)
                if (tab->M && i >= tab->n_param && i < tab->n_var - tab->n_div)
                        isl_int_submul(row[2], line[1 + i], row[0]);
        }
-       isl_seq_normalize(row, off + tab->n_col);
+       isl_seq_normalize(tab->mat->ctx, row, off + tab->n_col);
        isl_int_clear(a);
        isl_int_clear(b);
 
index 8bfa391..5a71e7a 100644 (file)
--- a/isl_vec.c
+++ b/isl_vec.c
@@ -116,6 +116,6 @@ struct isl_vec *isl_vec_normalize(struct isl_vec *vec)
 {
        if (!vec)
                return NULL;
-       isl_seq_normalize(vec->el, vec->size);
+       isl_seq_normalize(vec->ctx, vec->el, vec->size);
        return vec;
 }