CLooG 0.18.4
[cloog.git] / source / matrix.c
blob43c238024c596f5cea918dbed2eb9a2e6eac97d0
1 /**-------------------------------------------------------------------**
2 ** CLooG **
3 **-------------------------------------------------------------------**
4 ** cloogmatrix.c **
5 **-------------------------------------------------------------------**/
8 /******************************************************************************
9 * CLooG : the Chunky Loop Generator (experimental) *
10 ******************************************************************************
11 * *
12 * Copyright (C) 2001-2005 Cedric Bastoul *
13 * *
14 * This library is free software; you can redistribute it and/or *
15 * modify it under the terms of the GNU Lesser General Public *
16 * License as published by the Free Software Foundation; either *
17 * version 2.1 of the License, or (at your option) any later version. *
18 * *
19 * This library is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Lesser General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Lesser General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
27 * Boston, MA 02110-1301 USA *
28 * *
29 * CLooG, the Chunky Loop Generator *
30 * Written by Cedric Bastoul, Cedric.Bastoul@inria.fr *
31 * *
32 ******************************************************************************/
34 #include <ctype.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include "../include/cloog/cloog.h"
39 /**
40 * cloog_matrix_alloc:
41 * Allocate a CloogMatrix data structure with NbRows rows and NbColumns columns.
42 * All values are initialized to 0.
43 * This method returns a pointer to the data structure if successful or a NULL
44 * pointer otherwise.
46 CloogMatrix *cloog_matrix_alloc(unsigned NbRows, unsigned NbColumns)
48 CloogMatrix *matrix;
49 cloog_int_t **p, *q;
50 int i, j;
52 matrix = (CloogMatrix *)malloc(sizeof(CloogMatrix));
54 if (!matrix)
55 return NULL;
57 matrix->NbRows = NbRows;
58 matrix->NbColumns = NbColumns;
60 if (!NbRows || !NbColumns) {
61 matrix->p = NULL;
62 matrix->p_Init = NULL;
63 return matrix;
66 p = (cloog_int_t **)malloc(NbRows * sizeof(cloog_int_t *));
68 if (p == NULL) {
69 free (matrix);
70 return NULL;
73 q = (cloog_int_t *)malloc(NbRows * NbColumns * sizeof(cloog_int_t));
75 if (q == NULL) {
76 free (matrix);
77 free (p);
78 return NULL;
81 matrix->p = p;
82 matrix->p_Init = q;
84 for (i = 0; i < NbRows; i++) {
85 *p++ = q;
86 for (j = 0; j < NbColumns; j++) {
87 cloog_int_init(*(q+j));
88 cloog_int_set_si(*(q+j), 0);
90 q += NbColumns;
93 return matrix;
96 /**
97 * cloog_matrix_free:
98 * Free matrix.
100 void cloog_matrix_free(CloogMatrix * matrix)
102 int i;
103 cloog_int_t *p;
104 int size = matrix->NbRows * matrix->NbColumns;
106 p = matrix->p_Init;
108 for (i = 0; i < size; i++)
109 cloog_int_clear(*p++);
111 if (matrix) {
112 free(matrix->p_Init);
113 free(matrix->p);
114 free(matrix);
120 * Print the elements of CloogMatrix M to file, with each row prefixed
121 * by prefix and suffixed by suffix.
123 void cloog_matrix_print_structure(FILE *file, CloogMatrix *M,
124 const char *prefix, const char *suffix)
126 int i, j;
128 for (i = 0; i < M->NbRows; ++i) {
129 fprintf(file, "%s", prefix);
130 for (j = 0; j < M->NbColumns; ++j) {
131 cloog_int_print(file, M->p[i][j]);
132 fprintf(file, " ");
134 fprintf(file, "%s\n", suffix);
139 * cloog_matrix_print function:
140 * This function prints the content of a CloogMatrix structure (matrix) into a
141 * file (foo, possibly stdout).
143 void cloog_matrix_print(FILE* foo, CloogMatrix* m)
145 if (!m)
146 fprintf(foo, "(null)\n");
148 fprintf(foo, "%d %d\n", m->NbRows, m->NbColumns);
149 cloog_matrix_print_structure(foo, m, "", "");
150 fflush(foo);
154 static char *next_line(FILE *input, char *line, unsigned len)
156 char *p;
158 do {
159 if (!(p = fgets(line, len, input)))
160 return NULL;
161 while (isspace(*p) && *p != '\n')
162 ++p;
163 } while (*p == '#' || *p == '\n');
165 return p;
168 CloogMatrix *cloog_matrix_read(FILE *input)
170 unsigned n_row, n_col;
171 char line[1024];
173 if (!next_line(input, line, sizeof(line)))
174 cloog_die("Input error.\n");
175 if (sscanf(line, "%u %u", &n_row, &n_col) != 2)
176 cloog_die("Input error.\n");
178 return cloog_matrix_read_of_size(input, n_row, n_col);
182 * Read a matrix in PolyLib format from input.
184 CloogMatrix *cloog_matrix_read_of_size(FILE *input,
185 unsigned n_row, unsigned n_col)
187 CloogMatrix *M;
188 int i, j;
189 char line[1024];
190 char val[1024];
191 char *p;
193 M = cloog_matrix_alloc(n_row, n_col);
194 if (!M)
195 cloog_die("memory overflow.\n");
196 for (i = 0; i < n_row; ++i) {
197 int offset;
198 int n;
200 p = next_line(input, line, sizeof(line));
201 if (!p)
202 cloog_die("Input error.\n");
203 for (j = 0; j < n_col; ++j) {
204 n = sscanf(p, "%s%n", val, &offset);
205 if (!n)
206 cloog_die("Input error.\n");
207 cloog_int_read(M->p[i][j], val);
208 p += offset;
212 return M;