isl_set_lattice_width: rename "dim" variable to "space"
[barvinok.git] / isl_aff_polylib.c
blobfee9a90cbb32f4461b7c8a546abd68876fb9eb6a
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2014 Ecole Normale Superieure
5 * Use of this software is governed by the GNU GPLv2+ licenses
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
12 #include <isl/val.h>
13 #include <isl/val_gmp.h>
14 #include <isl/aff.h>
15 #include "isl_aff_polylib.h"
17 /* Fill in row "row" of "M" from "aff".
18 * The input is assumed not to involve any integer divisions.
20 * The order of the coefficients in the rows of "M" is
22 * input-coefficients parameter-coefficients constant-term denominator
24 static Matrix *add_row(Matrix *M, int row, __isl_take isl_aff *aff)
26 enum isl_dim_type types[] = { isl_dim_in, isl_dim_param };
27 int i, j, k;
28 isl_val *d, *v;
30 if (!M || !aff)
31 goto error;
33 if (isl_aff_dim(aff, isl_dim_div) != 0)
34 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
35 "cannot handle integer divisions", goto error);
37 d = isl_aff_get_denominator_val(aff);
38 aff = isl_aff_scale_val(aff, isl_val_copy(d));
40 k = 0;
41 for (i = 0; i < 2; ++i) {
42 int n = isl_aff_dim(aff, types[i]);
43 for (j = 0; j < n; ++j) {
44 v = isl_aff_get_coefficient_val(aff, types[i], j);
45 isl_val_get_num_gmp(v, M->p[row][k++]);
46 isl_val_free(v);
50 v = isl_aff_get_constant_val(aff);
51 isl_val_get_num_gmp(v, M->p[row][k++]);
52 isl_val_free(v);
54 isl_val_get_num_gmp(d, M->p[row][k++]);
55 isl_val_free(d);
57 isl_aff_free(aff);
59 return M;
60 error:
61 isl_aff_free(aff);
62 if (M)
63 Matrix_Free(M);
64 return NULL;
67 /* Convert the multi-affine expression "ma" to a PolyLib Matrix.
68 * The input is assumed not to involve any integer divisions.
70 * The order of the coefficients in the rows of the Matrix is
72 * input-coefficients parameter-coefficients constant-term denominator
74 Matrix *isl_multi_aff_to_polylib(__isl_keep isl_multi_aff *ma)
76 Matrix *M;
77 unsigned nparam, n_in, n_out;
78 int i;
80 if (!ma)
81 return NULL;
83 nparam = isl_multi_aff_dim(ma, isl_dim_param);
84 n_in = isl_multi_aff_dim(ma, isl_dim_in);
85 n_out = isl_multi_aff_dim(ma, isl_dim_out);
86 M = Matrix_Alloc(n_out, n_in + nparam + 1 + 1);
87 for (i = 0; i < n_out; ++i)
88 M = add_row(M, i, isl_multi_aff_get_aff(ma, i));
90 return M;