/home/arjun/llvm-project/mlir/lib/Analysis/Presburger/Matrix.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- Matrix.cpp - MLIR Matrix Class -------------------------------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | |
9 | | #include "mlir/Analysis/Presburger/Matrix.h" |
10 | | |
11 | | namespace mlir { |
12 | | |
13 | | Matrix::Matrix(unsigned rows, unsigned columns) |
14 | 93 | : nRows(rows), nColumns(columns), data(nRows * nColumns) {} |
15 | | |
16 | 29 | Matrix Matrix::identity(unsigned dimension) { |
17 | 29 | Matrix matrix(dimension, dimension); |
18 | 101 | for (unsigned i = 0; i < dimension; ++i) |
19 | 72 | matrix(i, i) = 1; |
20 | 29 | return matrix; |
21 | 29 | } |
22 | | |
23 | 187k | int64_t &Matrix::at(unsigned row, unsigned column) { |
24 | 187k | assert(row < getNumRows() && "Row outside of range"); |
25 | 187k | assert(column < getNumColumns() && "Column outside of range"); |
26 | 187k | return data[row * nColumns + column]; |
27 | 187k | } |
28 | | |
29 | 12.6k | int64_t Matrix::at(unsigned row, unsigned column) const { |
30 | 12.6k | assert(row < getNumRows() && "Row outside of range"); |
31 | 12.6k | assert(column < getNumColumns() && "Column outside of range"); |
32 | 12.6k | return data[row * nColumns + column]; |
33 | 12.6k | } |
34 | | |
35 | 185k | int64_t &Matrix::operator()(unsigned row, unsigned column) { |
36 | 185k | return at(row, column); |
37 | 185k | } |
38 | | |
39 | 12.6k | int64_t Matrix::operator()(unsigned row, unsigned column) const { |
40 | 12.6k | return at(row, column); |
41 | 12.6k | } |
42 | | |
43 | 200k | unsigned Matrix::getNumRows() const { return nRows; } |
44 | | |
45 | 199k | unsigned Matrix::getNumColumns() const { return nColumns; } |
46 | | |
47 | 1.10k | void Matrix::resizeVertically(unsigned newNRows) { |
48 | 1.10k | nRows = newNRows; |
49 | 1.10k | data.resize(nRows * nColumns); |
50 | 1.10k | } |
51 | | |
52 | 68 | void Matrix::swapRows(unsigned row, unsigned otherRow) { |
53 | 68 | assert((row < getNumRows() && otherRow < getNumRows()) && |
54 | 68 | "Given row out of bounds"); |
55 | 68 | if (row == otherRow) |
56 | 1 | return; |
57 | 457 | for (unsigned col = 0; col < nColumns; col++) |
58 | 390 | std::swap(at(row, col), at(otherRow, col)); |
59 | 67 | } |
60 | | |
61 | 6 | void Matrix::swapColumns(unsigned column, unsigned otherColumn) { |
62 | 6 | assert((column < getNumColumns() && otherColumn < getNumColumns()) && |
63 | 6 | "Given column out of bounds"); |
64 | 6 | if (column == otherColumn) |
65 | 1 | return; |
66 | 30 | for (unsigned row = 0; row < nRows; row++) |
67 | 25 | std::swap(at(row, column), at(row, otherColumn)); |
68 | 5 | } |
69 | | |
70 | 303 | ArrayRef<int64_t> Matrix::getRow(unsigned row) const { |
71 | 303 | return {&data[row * nColumns], nColumns}; |
72 | 303 | } |
73 | | |
74 | 81 | void Matrix::addToRow(unsigned sourceRow, unsigned targetRow, int64_t scale) { |
75 | 81 | if (scale == 0) |
76 | 24 | return; |
77 | 243 | for (unsigned col = 0; col < nColumns; ++col) |
78 | 186 | at(targetRow, col) += scale * at(sourceRow, col); |
79 | 57 | return; |
80 | 57 | } |
81 | | |
82 | 0 | void Matrix::print(raw_ostream &os) const { |
83 | 0 | for (unsigned row = 0; row < nRows; ++row) { |
84 | 0 | for (unsigned column = 0; column < nColumns; ++column) |
85 | 0 | os << at(row, column) << ' '; |
86 | 0 | os << '\n'; |
87 | 0 | } |
88 | 0 | } |
89 | | |
90 | 0 | void Matrix::dump() const { print(llvm::errs()); } |
91 | | |
92 | | } // namespace mlir |