PR c++/29043
[official-gcc/constexpr.git] / gcc / lambda-mat.c
blob50fdb69992515187c717ee16b69cb86eaa76fbda
1 /* Integer matrix math routines
2 Copyright (C) 2003, 2004, 2005, 2007, 2008 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dberlin@dberlin.org>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "ggc.h"
26 #include "tree.h"
27 #include "tree-flow.h"
28 #include "lambda.h"
30 /* Allocate a matrix of M rows x N cols. */
32 lambda_matrix
33 lambda_matrix_new (int m, int n, struct obstack * lambda_obstack)
35 lambda_matrix mat;
36 int i;
38 mat = (lambda_matrix) obstack_alloc (lambda_obstack,
39 sizeof (lambda_vector *) * m);
41 for (i = 0; i < m; i++)
42 mat[i] = lambda_vector_new (n);
44 return mat;
47 /* Copy the elements of M x N matrix MAT1 to MAT2. */
49 void
50 lambda_matrix_copy (lambda_matrix mat1, lambda_matrix mat2,
51 int m, int n)
53 int i;
55 for (i = 0; i < m; i++)
56 lambda_vector_copy (mat1[i], mat2[i], n);
59 /* Store the N x N identity matrix in MAT. */
61 void
62 lambda_matrix_id (lambda_matrix mat, int size)
64 int i, j;
66 for (i = 0; i < size; i++)
67 for (j = 0; j < size; j++)
68 mat[i][j] = (i == j) ? 1 : 0;
71 /* Return true if MAT is the identity matrix of SIZE */
73 bool
74 lambda_matrix_id_p (lambda_matrix mat, int size)
76 int i, j;
77 for (i = 0; i < size; i++)
78 for (j = 0; j < size; j++)
80 if (i == j)
82 if (mat[i][j] != 1)
83 return false;
85 else
87 if (mat[i][j] != 0)
88 return false;
91 return true;
94 /* Negate the elements of the M x N matrix MAT1 and store it in MAT2. */
96 void
97 lambda_matrix_negate (lambda_matrix mat1, lambda_matrix mat2, int m, int n)
99 int i;
101 for (i = 0; i < m; i++)
102 lambda_vector_negate (mat1[i], mat2[i], n);
105 /* Take the transpose of matrix MAT1 and store it in MAT2.
106 MAT1 is an M x N matrix, so MAT2 must be N x M. */
108 void
109 lambda_matrix_transpose (lambda_matrix mat1, lambda_matrix mat2, int m, int n)
111 int i, j;
113 for (i = 0; i < n; i++)
114 for (j = 0; j < m; j++)
115 mat2[i][j] = mat1[j][i];
119 /* Add two M x N matrices together: MAT3 = MAT1+MAT2. */
121 void
122 lambda_matrix_add (lambda_matrix mat1, lambda_matrix mat2,
123 lambda_matrix mat3, int m, int n)
125 int i;
127 for (i = 0; i < m; i++)
128 lambda_vector_add (mat1[i], mat2[i], mat3[i], n);
131 /* MAT3 = CONST1 * MAT1 + CONST2 * MAT2. All matrices are M x N. */
133 void
134 lambda_matrix_add_mc (lambda_matrix mat1, int const1,
135 lambda_matrix mat2, int const2,
136 lambda_matrix mat3, int m, int n)
138 int i;
140 for (i = 0; i < m; i++)
141 lambda_vector_add_mc (mat1[i], const1, mat2[i], const2, mat3[i], n);
144 /* Multiply two matrices: MAT3 = MAT1 * MAT2.
145 MAT1 is an M x R matrix, and MAT2 is R x N. The resulting MAT2
146 must therefore be M x N. */
148 void
149 lambda_matrix_mult (lambda_matrix mat1, lambda_matrix mat2,
150 lambda_matrix mat3, int m, int r, int n)
153 int i, j, k;
155 for (i = 0; i < m; i++)
157 for (j = 0; j < n; j++)
159 mat3[i][j] = 0;
160 for (k = 0; k < r; k++)
161 mat3[i][j] += mat1[i][k] * mat2[k][j];
166 /* Delete rows r1 to r2 (not including r2). */
168 void
169 lambda_matrix_delete_rows (lambda_matrix mat, int rows, int from, int to)
171 int i;
172 int dist;
173 dist = to - from;
175 for (i = to; i < rows; i++)
176 mat[i - dist] = mat[i];
178 for (i = rows - dist; i < rows; i++)
179 mat[i] = NULL;
182 /* Swap rows R1 and R2 in matrix MAT. */
184 void
185 lambda_matrix_row_exchange (lambda_matrix mat, int r1, int r2)
187 lambda_vector row;
189 row = mat[r1];
190 mat[r1] = mat[r2];
191 mat[r2] = row;
194 /* Add a multiple of row R1 of matrix MAT with N columns to row R2:
195 R2 = R2 + CONST1 * R1. */
197 void
198 lambda_matrix_row_add (lambda_matrix mat, int n, int r1, int r2, int const1)
200 int i;
202 if (const1 == 0)
203 return;
205 for (i = 0; i < n; i++)
206 mat[r2][i] += const1 * mat[r1][i];
209 /* Negate row R1 of matrix MAT which has N columns. */
211 void
212 lambda_matrix_row_negate (lambda_matrix mat, int n, int r1)
214 lambda_vector_negate (mat[r1], mat[r1], n);
217 /* Multiply row R1 of matrix MAT with N columns by CONST1. */
219 void
220 lambda_matrix_row_mc (lambda_matrix mat, int n, int r1, int const1)
222 int i;
224 for (i = 0; i < n; i++)
225 mat[r1][i] *= const1;
228 /* Exchange COL1 and COL2 in matrix MAT. M is the number of rows. */
230 void
231 lambda_matrix_col_exchange (lambda_matrix mat, int m, int col1, int col2)
233 int i;
234 int tmp;
235 for (i = 0; i < m; i++)
237 tmp = mat[i][col1];
238 mat[i][col1] = mat[i][col2];
239 mat[i][col2] = tmp;
243 /* Add a multiple of column C1 of matrix MAT with M rows to column C2:
244 C2 = C2 + CONST1 * C1. */
246 void
247 lambda_matrix_col_add (lambda_matrix mat, int m, int c1, int c2, int const1)
249 int i;
251 if (const1 == 0)
252 return;
254 for (i = 0; i < m; i++)
255 mat[i][c2] += const1 * mat[i][c1];
258 /* Negate column C1 of matrix MAT which has M rows. */
260 void
261 lambda_matrix_col_negate (lambda_matrix mat, int m, int c1)
263 int i;
265 for (i = 0; i < m; i++)
266 mat[i][c1] *= -1;
269 /* Multiply column C1 of matrix MAT with M rows by CONST1. */
271 void
272 lambda_matrix_col_mc (lambda_matrix mat, int m, int c1, int const1)
274 int i;
276 for (i = 0; i < m; i++)
277 mat[i][c1] *= const1;
280 /* Compute the inverse of the N x N matrix MAT and store it in INV.
282 We don't _really_ compute the inverse of MAT. Instead we compute
283 det(MAT)*inv(MAT), and we return det(MAT) to the caller as the function
284 result. This is necessary to preserve accuracy, because we are dealing
285 with integer matrices here.
287 The algorithm used here is a column based Gauss-Jordan elimination on MAT
288 and the identity matrix in parallel. The inverse is the result of applying
289 the same operations on the identity matrix that reduce MAT to the identity
290 matrix.
292 When MAT is a 2 x 2 matrix, we don't go through the whole process, because
293 it is easily inverted by inspection and it is a very common case. */
295 static int lambda_matrix_inverse_hard (lambda_matrix, lambda_matrix, int,
296 struct obstack *);
299 lambda_matrix_inverse (lambda_matrix mat, lambda_matrix inv, int n,
300 struct obstack * lambda_obstack)
302 if (n == 2)
304 int a, b, c, d, det;
305 a = mat[0][0];
306 b = mat[1][0];
307 c = mat[0][1];
308 d = mat[1][1];
309 inv[0][0] = d;
310 inv[0][1] = -c;
311 inv[1][0] = -b;
312 inv[1][1] = a;
313 det = (a * d - b * c);
314 if (det < 0)
316 det *= -1;
317 inv[0][0] *= -1;
318 inv[1][0] *= -1;
319 inv[0][1] *= -1;
320 inv[1][1] *= -1;
322 return det;
324 else
325 return lambda_matrix_inverse_hard (mat, inv, n, lambda_obstack);
328 /* If MAT is not a special case, invert it the hard way. */
330 static int
331 lambda_matrix_inverse_hard (lambda_matrix mat, lambda_matrix inv, int n,
332 struct obstack * lambda_obstack)
334 lambda_vector row;
335 lambda_matrix temp;
336 int i, j;
337 int determinant;
339 temp = lambda_matrix_new (n, n, lambda_obstack);
340 lambda_matrix_copy (mat, temp, n, n);
341 lambda_matrix_id (inv, n);
343 /* Reduce TEMP to a lower triangular form, applying the same operations on
344 INV which starts as the identity matrix. N is the number of rows and
345 columns. */
346 for (j = 0; j < n; j++)
348 row = temp[j];
350 /* Make every element in the current row positive. */
351 for (i = j; i < n; i++)
352 if (row[i] < 0)
354 lambda_matrix_col_negate (temp, n, i);
355 lambda_matrix_col_negate (inv, n, i);
358 /* Sweep the upper triangle. Stop when only the diagonal element in the
359 current row is nonzero. */
360 while (lambda_vector_first_nz (row, n, j + 1) < n)
362 int min_col = lambda_vector_min_nz (row, n, j);
363 lambda_matrix_col_exchange (temp, n, j, min_col);
364 lambda_matrix_col_exchange (inv, n, j, min_col);
366 for (i = j + 1; i < n; i++)
368 int factor;
370 factor = -1 * row[i];
371 if (row[j] != 1)
372 factor /= row[j];
374 lambda_matrix_col_add (temp, n, j, i, factor);
375 lambda_matrix_col_add (inv, n, j, i, factor);
380 /* Reduce TEMP from a lower triangular to the identity matrix. Also compute
381 the determinant, which now is simply the product of the elements on the
382 diagonal of TEMP. If one of these elements is 0, the matrix has 0 as an
383 eigenvalue so it is singular and hence not invertible. */
384 determinant = 1;
385 for (j = n - 1; j >= 0; j--)
387 int diagonal;
389 row = temp[j];
390 diagonal = row[j];
392 /* The matrix must not be singular. */
393 gcc_assert (diagonal);
395 determinant = determinant * diagonal;
397 /* If the diagonal is not 1, then multiply the each row by the
398 diagonal so that the middle number is now 1, rather than a
399 rational. */
400 if (diagonal != 1)
402 for (i = 0; i < j; i++)
403 lambda_matrix_col_mc (inv, n, i, diagonal);
404 for (i = j + 1; i < n; i++)
405 lambda_matrix_col_mc (inv, n, i, diagonal);
407 row[j] = diagonal = 1;
410 /* Sweep the lower triangle column wise. */
411 for (i = j - 1; i >= 0; i--)
413 if (row[i])
415 int factor = -row[i];
416 lambda_matrix_col_add (temp, n, j, i, factor);
417 lambda_matrix_col_add (inv, n, j, i, factor);
423 return determinant;
426 /* Decompose a N x N matrix MAT to a product of a lower triangular H
427 and a unimodular U matrix such that MAT = H.U. N is the size of
428 the rows of MAT. */
430 void
431 lambda_matrix_hermite (lambda_matrix mat, int n,
432 lambda_matrix H, lambda_matrix U)
434 lambda_vector row;
435 int i, j, factor, minimum_col;
437 lambda_matrix_copy (mat, H, n, n);
438 lambda_matrix_id (U, n);
440 for (j = 0; j < n; j++)
442 row = H[j];
444 /* Make every element of H[j][j..n] positive. */
445 for (i = j; i < n; i++)
447 if (row[i] < 0)
449 lambda_matrix_col_negate (H, n, i);
450 lambda_vector_negate (U[i], U[i], n);
454 /* Stop when only the diagonal element is nonzero. */
455 while (lambda_vector_first_nz (row, n, j + 1) < n)
457 minimum_col = lambda_vector_min_nz (row, n, j);
458 lambda_matrix_col_exchange (H, n, j, minimum_col);
459 lambda_matrix_row_exchange (U, j, minimum_col);
461 for (i = j + 1; i < n; i++)
463 factor = row[i] / row[j];
464 lambda_matrix_col_add (H, n, j, i, -1 * factor);
465 lambda_matrix_row_add (U, n, i, j, factor);
471 /* Given an M x N integer matrix A, this function determines an M x
472 M unimodular matrix U, and an M x N echelon matrix S such that
473 "U.A = S". This decomposition is also known as "right Hermite".
475 Ref: Algorithm 2.1 page 33 in "Loop Transformations for
476 Restructuring Compilers" Utpal Banerjee. */
478 void
479 lambda_matrix_right_hermite (lambda_matrix A, int m, int n,
480 lambda_matrix S, lambda_matrix U)
482 int i, j, i0 = 0;
484 lambda_matrix_copy (A, S, m, n);
485 lambda_matrix_id (U, m);
487 for (j = 0; j < n; j++)
489 if (lambda_vector_first_nz (S[j], m, i0) < m)
491 ++i0;
492 for (i = m - 1; i >= i0; i--)
494 while (S[i][j] != 0)
496 int sigma, factor, a, b;
498 a = S[i-1][j];
499 b = S[i][j];
500 sigma = (a * b < 0) ? -1: 1;
501 a = abs (a);
502 b = abs (b);
503 factor = sigma * (a / b);
505 lambda_matrix_row_add (S, n, i, i-1, -factor);
506 lambda_matrix_row_exchange (S, i, i-1);
508 lambda_matrix_row_add (U, m, i, i-1, -factor);
509 lambda_matrix_row_exchange (U, i, i-1);
516 /* Given an M x N integer matrix A, this function determines an M x M
517 unimodular matrix V, and an M x N echelon matrix S such that "A =
518 V.S". This decomposition is also known as "left Hermite".
520 Ref: Algorithm 2.2 page 36 in "Loop Transformations for
521 Restructuring Compilers" Utpal Banerjee. */
523 void
524 lambda_matrix_left_hermite (lambda_matrix A, int m, int n,
525 lambda_matrix S, lambda_matrix V)
527 int i, j, i0 = 0;
529 lambda_matrix_copy (A, S, m, n);
530 lambda_matrix_id (V, m);
532 for (j = 0; j < n; j++)
534 if (lambda_vector_first_nz (S[j], m, i0) < m)
536 ++i0;
537 for (i = m - 1; i >= i0; i--)
539 while (S[i][j] != 0)
541 int sigma, factor, a, b;
543 a = S[i-1][j];
544 b = S[i][j];
545 sigma = (a * b < 0) ? -1: 1;
546 a = abs (a);
547 b = abs (b);
548 factor = sigma * (a / b);
550 lambda_matrix_row_add (S, n, i, i-1, -factor);
551 lambda_matrix_row_exchange (S, i, i-1);
553 lambda_matrix_col_add (V, m, i-1, i, factor);
554 lambda_matrix_col_exchange (V, m, i, i-1);
561 /* When it exists, return the first nonzero row in MAT after row
562 STARTROW. Otherwise return rowsize. */
565 lambda_matrix_first_nz_vec (lambda_matrix mat, int rowsize, int colsize,
566 int startrow)
568 int j;
569 bool found = false;
571 for (j = startrow; (j < rowsize) && !found; j++)
573 if ((mat[j] != NULL)
574 && (lambda_vector_first_nz (mat[j], colsize, startrow) < colsize))
575 found = true;
578 if (found)
579 return j - 1;
580 return rowsize;
583 /* Multiply a vector VEC by a matrix MAT.
584 MAT is an M*N matrix, and VEC is a vector with length N. The result
585 is stored in DEST which must be a vector of length M. */
587 void
588 lambda_matrix_vector_mult (lambda_matrix matrix, int m, int n,
589 lambda_vector vec, lambda_vector dest)
591 int i, j;
593 lambda_vector_clear (dest, m);
594 for (i = 0; i < m; i++)
595 for (j = 0; j < n; j++)
596 dest[i] += matrix[i][j] * vec[j];
599 /* Print out an M x N matrix MAT to OUTFILE. */
601 void
602 print_lambda_matrix (FILE * outfile, lambda_matrix matrix, int m, int n)
604 int i;
606 for (i = 0; i < m; i++)
607 print_lambda_vector (outfile, matrix[i], n);
608 fprintf (outfile, "\n");