Coverage Report

Created: 2018-07-18 22:01

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/CodeGen/PBQP/Math.h
Line
Count
Source (jump to first uncovered line)
1
//===- Math.h - PBQP Vector and Matrix classes ------------------*- C++ -*-===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
10
#ifndef LLVM_CODEGEN_PBQP_MATH_H
11
#define LLVM_CODEGEN_PBQP_MATH_H
12
13
#include "llvm/ADT/Hashing.h"
14
#include "llvm/ADT/STLExtras.h"
15
#include <algorithm>
16
#include <cassert>
17
#include <functional>
18
#include <memory>
19
20
namespace llvm {
21
namespace PBQP {
22
23
using PBQPNum = float;
24
25
/// PBQP Vector class.
26
class Vector {
27
  friend hash_code hash_value(const Vector &);
28
29
public:
30
  /// Construct a PBQP vector of the given size.
31
  explicit Vector(unsigned Length)
32
561
    : Length(Length), Data(llvm::make_unique<PBQPNum []>(Length)) {}
33
34
  /// Construct a PBQP vector with initializer.
35
  Vector(unsigned Length, PBQPNum InitVal)
36
153
    : Length(Length), Data(llvm::make_unique<PBQPNum []>(Length)) {
37
153
    std::fill(Data.get(), Data.get() + Length, InitVal);
38
153
  }
39
40
  /// Copy construct a PBQP vector.
41
  Vector(const Vector &V)
42
360
    : Length(V.Length), Data(llvm::make_unique<PBQPNum []>(Length)) {
43
360
    std::copy(V.Data.get(), V.Data.get() + Length, Data.get());
44
360
  }
45
46
  /// Move construct a PBQP vector.
47
  Vector(Vector &&V)
48
1.28k
    : Length(V.Length), Data(std::move(V.Data)) {
49
1.28k
    V.Length = 0;
50
1.28k
  }
51
52
  /// Comparison operator.
53
414
  bool operator==(const Vector &V) const {
54
414
    assert(Length != 0 && Data && "Invalid vector");
55
414
    if (Length != V.Length)
56
19
      return false;
57
395
    return std::equal(Data.get(), Data.get() + Length, V.Data.get());
58
395
  }
59
60
  /// Return the length of the vector
61
16.0k
  unsigned getLength() const {
62
16.0k
    assert(Length != 0 && Data && "Invalid vector");
63
16.0k
    return Length;
64
16.0k
  }
65
66
  /// Element access.
67
16.4k
  PBQPNum& operator[](unsigned Index) {
68
16.4k
    assert(Length != 0 && Data && "Invalid vector");
69
16.4k
    assert(Index < Length && "Vector element access out of bounds.");
70
16.4k
    return Data[Index];
71
16.4k
  }
72
73
  /// Const element access.
74
1.16M
  const PBQPNum& operator[](unsigned Index) const {
75
1.16M
    assert(Length != 0 && Data && "Invalid vector");
76
1.16M
    assert(Index < Length && "Vector element access out of bounds.");
77
1.16M
    return Data[Index];
78
1.16M
  }
79
80
  /// Add another vector to this one.
81
561
  Vector& operator+=(const Vector &V) {
82
561
    assert(Length != 0 && Data && "Invalid vector");
83
561
    assert(Length == V.Length && "Vector length mismatch.");
84
561
    std::transform(Data.get(), Data.get() + Length, V.Data.get(), Data.get(),
85
561
                   std::plus<PBQPNum>());
86
561
    return *this;
87
561
  }
88
89
  /// Returns the index of the minimum value in this vector
90
153
  unsigned minIndex() const {
91
153
    assert(Length != 0 && Data && "Invalid vector");
92
153
    return std::min_element(Data.get(), Data.get() + Length) - Data.get();
93
153
  }
94
95
private:
96
  unsigned Length;
97
  std::unique_ptr<PBQPNum []> Data;
98
};
99
100
/// Return a hash_value for the given vector.
101
605
inline hash_code hash_value(const Vector &V) {
102
605
  unsigned *VBegin = reinterpret_cast<unsigned*>(V.Data.get());
103
605
  unsigned *VEnd = reinterpret_cast<unsigned*>(V.Data.get() + V.Length);
104
605
  return hash_combine(V.Length, hash_combine_range(VBegin, VEnd));
105
605
}
106
107
/// Output a textual representation of the given vector on the given
108
///        output stream.
109
template <typename OStream>
110
0
OStream& operator<<(OStream &OS, const Vector &V) {
111
0
  assert((V.getLength() != 0) && "Zero-length vector badness.");
112
0
113
0
  OS << "[ " << V[0];
114
0
  for (unsigned i = 1; i < V.getLength(); ++i)
115
0
    OS << ", " << V[i];
116
0
  OS << " ]";
117
0
118
0
  return OS;
119
0
}
120
121
/// PBQP Matrix class
122
class Matrix {
123
private:
124
  friend hash_code hash_value(const Matrix &);
125
126
public:
127
  /// Construct a PBQP Matrix with the given dimensions.
128
  Matrix(unsigned Rows, unsigned Cols) :
129
65
    Rows(Rows), Cols(Cols), Data(llvm::make_unique<PBQPNum []>(Rows * Cols)) {
130
65
  }
131
132
  /// Construct a PBQP Matrix with the given dimensions and initial
133
  /// value.
134
  Matrix(unsigned Rows, unsigned Cols, PBQPNum InitVal)
135
    : Rows(Rows), Cols(Cols),
136
74
      Data(llvm::make_unique<PBQPNum []>(Rows * Cols)) {
137
74
    std::fill(Data.get(), Data.get() + (Rows * Cols), InitVal);
138
74
  }
139
140
  /// Copy construct a PBQP matrix.
141
  Matrix(const Matrix &M)
142
    : Rows(M.Rows), Cols(M.Cols),
143
65
      Data(llvm::make_unique<PBQPNum []>(Rows * Cols)) {
144
65
    std::copy(M.Data.get(), M.Data.get() + (Rows * Cols), Data.get());
145
65
  }
146
147
  /// Move construct a PBQP matrix.
148
  Matrix(Matrix &&M)
149
459
    : Rows(M.Rows), Cols(M.Cols), Data(std::move(M.Data)) {
150
459
    M.Rows = M.Cols = 0;
151
459
  }
152
153
  /// Comparison operator.
154
159
  bool operator==(const Matrix &M) const {
155
159
    assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
156
159
    if (Rows != M.Rows || 
Cols != M.Cols127
)
157
32
      return false;
158
127
    return std::equal(Data.get(), Data.get() + (Rows * Cols), M.Data.get());
159
127
  }
160
161
  /// Return the number of rows in this matrix.
162
2.23k
  unsigned getRows() const {
163
2.23k
    assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
164
2.23k
    return Rows;
165
2.23k
  }
166
167
  /// Return the number of cols in this matrix.
168
62.1k
  unsigned getCols() const {
169
62.1k
    assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
170
62.1k
    return Cols;
171
62.1k
  }
172
173
  /// Matrix element access.
174
147k
  PBQPNum* operator[](unsigned R) {
175
147k
    assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
176
147k
    assert(R < Rows && "Row out of bounds.");
177
147k
    return Data.get() + (R * Cols);
178
147k
  }
179
180
  /// Matrix element access.
181
2.41M
  const PBQPNum* operator[](unsigned R) const {
182
2.41M
    assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
183
2.41M
    assert(R < Rows && "Row out of bounds.");
184
2.41M
    return Data.get() + (R * Cols);
185
2.41M
  }
186
187
  /// Returns the given row as a vector.
188
394
  Vector getRowAsVector(unsigned R) const {
189
394
    assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
190
394
    Vector V(Cols);
191
9.86k
    for (unsigned C = 0; C < Cols; 
++C9.46k
)
192
9.46k
      V[C] = (*this)[R][C];
193
394
    return V;
194
394
  }
195
196
  /// Returns the given column as a vector.
197
167
  Vector getColAsVector(unsigned C) const {
198
167
    assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
199
167
    Vector V(Rows);
200
5.25k
    for (unsigned R = 0; R < Rows; 
++R5.08k
)
201
5.08k
      V[R] = (*this)[R][C];
202
167
    return V;
203
167
  }
204
205
  /// Matrix transpose.
206
30
  Matrix transpose() const {
207
30
    assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
208
30
    Matrix M(Cols, Rows);
209
977
    for (unsigned r = 0; r < Rows; 
++r947
)
210
31.3k
      
for (unsigned c = 0; 947
c < Cols;
++c30.3k
)
211
30.3k
        M[c][r] = (*this)[r][c];
212
30
    return M;
213
30
  }
214
215
  /// Add the given matrix to this one.
216
35
  Matrix& operator+=(const Matrix &M) {
217
35
    assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
218
35
    assert(Rows == M.Rows && Cols == M.Cols &&
219
35
           "Matrix dimensions mismatch.");
220
35
    std::transform(Data.get(), Data.get() + (Rows * Cols), M.Data.get(),
221
35
                   Data.get(), std::plus<PBQPNum>());
222
35
    return *this;
223
35
  }
224
225
35
  Matrix operator+(const Matrix &M) {
226
35
    assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
227
35
    Matrix Tmp(*this);
228
35
    Tmp += M;
229
35
    return Tmp;
230
35
  }
231
232
private:
233
  unsigned Rows, Cols;
234
  std::unique_ptr<PBQPNum []> Data;
235
};
236
237
/// Return a hash_code for the given matrix.
238
206
inline hash_code hash_value(const Matrix &M) {
239
206
  unsigned *MBegin = reinterpret_cast<unsigned*>(M.Data.get());
240
206
  unsigned *MEnd =
241
206
    reinterpret_cast<unsigned*>(M.Data.get() + (M.Rows * M.Cols));
242
206
  return hash_combine(M.Rows, M.Cols, hash_combine_range(MBegin, MEnd));
243
206
}
244
245
/// Output a textual representation of the given matrix on the given
246
///        output stream.
247
template <typename OStream>
248
OStream& operator<<(OStream &OS, const Matrix &M) {
249
  assert((M.getRows() != 0) && "Zero-row matrix badness.");
250
  for (unsigned i = 0; i < M.getRows(); ++i)
251
    OS << M.getRowAsVector(i) << "\n";
252
  return OS;
253
}
254
255
template <typename Metadata>
256
class MDVector : public Vector {
257
public:
258
  MDVector(const Vector &v) : Vector(v), md(*this) {}
259
  MDVector(Vector &&v) : Vector(std::move(v)), md(*this) { }
260
261
  const Metadata& getMetadata() const { return md; }
262
263
private:
264
  Metadata md;
265
};
266
267
template <typename Metadata>
268
inline hash_code hash_value(const MDVector<Metadata> &V) {
269
  return hash_value(static_cast<const Vector&>(V));
270
}
271
272
template <typename Metadata>
273
class MDMatrix : public Matrix {
274
public:
275
  MDMatrix(const Matrix &m) : Matrix(m), md(*this) {}
276
70
  MDMatrix(Matrix &&m) : Matrix(std::move(m)), md(*this) { }
277
278
1.75k
  const Metadata& getMetadata() const { return md; }
279
280
private:
281
  Metadata md;
282
};
283
284
template <typename Metadata>
285
86
inline hash_code hash_value(const MDMatrix<Metadata> &M) {
286
86
  return hash_value(static_cast<const Matrix&>(M));
287
86
}
288
289
} // end namespace PBQP
290
} // end namespace llvm
291
292
#endif // LLVM_CODEGEN_PBQP_MATH_H