OpenASIP  2.0
BitMatrix.icc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file BitMatrix.icc
26  *
27  * Inline implementation of BitMatrix class.
28  *
29  * @author Pekka Jääskeläinen 2007 (pekka.jaaskelainen-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
33 #include "BaseType.hh"
34 
35 /**
36  * Count of rows in the bit matrix.
37  *
38  * @return Count of rows.
39  */
40 inline int
41 BitMatrix::rowCount() const {
42  return rows_;
43 }
44 
45 /**
46  * Count of columns in the bit matrix.
47  *
48  * @return Count of columns.
49  */
50 inline int
51 BitMatrix::columnCount() const {
52  return columns_;
53 }
54 
55 /**
56  * Performs an AND operation between the matrices for all elements and
57  * returns true in case any result is nonzero.
58  *
59  * This is used to check for conflicts between two bit matrices, that is,
60  * to look for element in which both matrices have value '1'.
61  *
62  * @note Both matrices must be of same size. This is not checked by the
63  * method.
64  *
65  * @param another The bit matrix to AND with.
66  */
67 inline bool
68 BitMatrix::conflictsWith(const BitMatrix& another) const {
69  for (int word = 0; word < rows_ * wordsPerRow_; ++word) {
70  RowWord conflicts =
71  matrix_[word] & another.matrix_[word];
72  if (conflicts != 0)
73  return true;
74  }
75  return false;
76 }
77 
78 /**
79  * ORs the given matrix with this one.
80  *
81  * The resulting matrix has '1' in an element in case either the merged
82  * matrix or the original had '1' there.
83  *
84  * @param another The resource table of the merged.
85  */
86 inline void
87 BitMatrix::orWith(const BitMatrix& another) {
88  for (int word = 0; word < rows_ * wordsPerRow_; ++word) {
89  matrix_[word] |= another.matrix_[word];
90  }
91 }
92 
93 /**
94  * Sets the bit at given element.
95  *
96  * @param column The column.
97  * @param row The row.
98  * @param value The bit.
99  */
100 inline void
101 BitMatrix::setBit(int column, int row, bool value) {
102 
103  const int wordPos = column / (sizeof(RowWord) * BYTE_BITWIDTH);
104  const int bitPos = column % (sizeof(RowWord) * BYTE_BITWIDTH);
105  const RowWord oldWord = matrix_[row * wordsPerRow_ + wordPos];
106  RowWord newWord = 0;
107  if (value) {
108  // set bit to 1 by ORing
109  RowWord mask = 1 << bitPos;
110  newWord = oldWord | mask;
111  } else {
112  // set the bit to 0 by ANDing with all ones except the bit to set
113  RowWord mask = 1 << bitPos;
114  mask = ~mask;
115  newWord = oldWord & mask;
116  }
117  matrix_[row * wordsPerRow_ + wordPos] = newWord;
118 }
119 
120 /**
121  * Checks if the given bit is set.
122  *
123  * @param column The column.
124  * @param row The row.
125  * @return Value of the bit.
126  */
127 inline bool
128 BitMatrix::bitAt(int column, int row) const {
129 
130  const int wordPos = column / (sizeof(RowWord) * BYTE_BITWIDTH);
131  const int bitPos = column % (sizeof(RowWord) * BYTE_BITWIDTH);
132  const RowWord word = matrix_[row * wordsPerRow_ + wordPos];
133  // reset all bits except that one by ANDing
134  RowWord mask = 1 << bitPos;
135  return (word & mask) != 0;
136 }
137 
138 /**
139  * Shifts the matrix left one step.
140  */
141 inline void
142 BitMatrix::shiftLeft() {
143 
144  const int shiftAmount = sizeof(RowWord)*BYTE_BITWIDTH - 1;
145 
146  for (int col = 0; col < wordsPerRow_; ++col) {
147  for (int row = 0; row < rows_; ++row) {
148  RowWord word = matrix_[row*wordsPerRow_ + col];
149  word >>= 1;
150  // transfer the upper bit from the next column
151  if (col + 1 < wordsPerRow_) {
152  unsigned upperBit =
153  matrix_[row*wordsPerRow_ + col + 1] << shiftAmount;
154  word |= upperBit;
155  }
156  matrix_[row*wordsPerRow_ + col] = word;
157  }
158  }
159 }