source/isl/domain.c: fix typo in comment
[cloog.git] / source / matrix.c
blob85a0cb3504218e44e77f15eebf365e3229b1aa93
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 <stdlib.h>
35 #include <stdio.h>
36 #include "../include/cloog/cloog.h"
38 /**
39 * cloog_matrix_alloc:
40 * Allocate a CloogMatrix data structure with NbRows rows and NbColumns columns.
41 * All values are initialized to 0.
42 * This method returns a pointer to the data structure if successful or a NULL
43 * pointer otherwise.
45 CloogMatrix *cloog_matrix_alloc(unsigned NbRows, unsigned NbColumns)
47 CloogMatrix *matrix;
48 cloog_int_t **p, *q;
49 int i, j;
51 matrix = (CloogMatrix *)malloc(sizeof(CloogMatrix));
53 if (!matrix)
54 return NULL;
56 matrix->NbRows = NbRows;
57 matrix->NbColumns = NbColumns;
59 if (!NbRows || !NbColumns) {
60 matrix->p = NULL;
61 matrix->p_Init = NULL;
62 return matrix;
65 p = (cloog_int_t **)malloc(NbRows * sizeof(cloog_int_t *));
67 if (p == NULL) {
68 free (matrix);
69 return NULL;
72 q = (cloog_int_t *)malloc(NbRows * NbColumns * sizeof(cloog_int_t));
74 if (q == NULL) {
75 free (matrix);
76 free (p);
77 return NULL;
80 matrix->p = p;
81 matrix->p_Init = q;
83 for (i = 0; i < NbRows; i++) {
84 *p++ = q;
85 for (j = 0; j < NbColumns; j++) {
86 cloog_int_init(*(q+j));
87 cloog_int_set_si(*(q+j), 0);
89 q += NbColumns;
92 return matrix;
95 /**
96 * cloog_matrix_free:
97 * Free matrix.
99 void cloog_matrix_free(CloogMatrix * matrix)
101 int i;
102 cloog_int_t *p;
103 int size = matrix->NbRows * matrix->NbColumns;
105 p = matrix->p_Init;
107 for (i = 0; i < size; i++)
108 cloog_int_clear(*p++);
110 if (matrix) {
111 free(matrix->p_Init);
112 free(matrix->p);
113 free(matrix);
118 * cloog_matrix_print function:
119 * This function prints the content of a CloogMatrix structure (matrix) into a
120 * file (foo, possibly stdout).
122 void cloog_matrix_print(FILE* foo, CloogMatrix* m)
124 int i, j;
126 if (!m)
127 fprintf(foo, "(null)\n");
129 for (i = 0; i < m->NbRows; ++i) {
130 for (j = 0; j < m->NbColumns; ++j) {
131 cloog_int_print(foo, m->p[i][j]);
132 fprintf(foo, " ");
134 fprintf(foo, "\n");
136 fflush(foo);