Coverage Report

Created: 2019-07-24 05:18

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