Skip to content

Commit fda83bd

Browse files
Danny van der Rijnbjacob
authored andcommitted
Large-scale change: Mark static const class/struct members as constexpr. This change fixes declarations that have initial values but are technically not definitions by marking them constexpr (which counts as a definition). This enables, among other things, the modified constants to be passed into functions and function templates that accept arguments by reference. Without this change, such functions would cause linker errors.
1 parent 023c190 commit fda83bd

File tree

10 files changed

+41
-39
lines changed

10 files changed

+41
-39
lines changed

internal/allocator.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ class Allocator {
8686
}
8787

8888
// Alignment of allocated blocks.
89-
static const std::size_t kAlignment = kDefaultCacheLineSize;
89+
static constexpr std::size_t kAlignment = kDefaultCacheLineSize;
9090

9191
// This is all we need so far, and since the usage pattern is fixed,
9292
// there is no point in allowing more until we need to.
93-
static const std::size_t kMaxBlocks = 5;
93+
static constexpr std::size_t kMaxBlocks = 5;
9494

9595
void Commit() {
9696
assert(!committed_);

internal/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ Integer RoundUpToPowerOfTwo(Integer n) {
165165

166166
template <int N>
167167
struct IsPowerOfTwo {
168-
static const bool value = !(N & (N - 1));
168+
static constexpr bool value = !(N & (N - 1));
169169
};
170170

171171
template <typename T>

internal/dispatch_gemm_shape.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ struct TransposeImpl<MatrixMap<Scalar, Order>> {
7474
template <VectorShape Shape>
7575
struct TransposeImpl<OutputStageQuantizeDownInt32ToUint8ScalePC<Shape>> {
7676
typedef OutputStageQuantizeDownInt32ToUint8ScalePC<Shape> SrcType;
77-
static const VectorShape TransposedShape = TransposeVectorShape<Shape>::Value;
77+
static constexpr VectorShape TransposedShape =
78+
TransposeVectorShape<Shape>::Value;
7879
typedef OutputStageQuantizeDownInt32ToUint8ScalePC<TransposedShape> DstType;
7980
static DstType Run(const SrcType& src) {
8081
DstType dst;
@@ -88,7 +89,8 @@ struct TransposeImpl<OutputStageQuantizeDownInt32ToUint8ScalePC<Shape>> {
8889
template <VectorShape Shape>
8990
struct TransposeImpl<OutputStageScaleInt32ByFixedPointAndExponentPC<Shape>> {
9091
typedef OutputStageScaleInt32ByFixedPointAndExponentPC<Shape> SrcType;
91-
static const VectorShape TransposedShape = TransposeVectorShape<Shape>::Value;
92+
static constexpr VectorShape TransposedShape =
93+
TransposeVectorShape<Shape>::Value;
9294
typedef OutputStageScaleInt32ByFixedPointAndExponentPC<TransposedShape>
9395
DstType;
9496
static DstType Run(const SrcType& src) {

internal/kernel.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@ enum class CellOrder { DepthMajor, WidthMajor, Diagonal };
126126
// out in a cell. That is, a CellOrder together with actual dimensions.
127127
template <int tWidth, int tDepth, CellOrder tOrder = CellOrder::DepthMajor>
128128
struct CellFormat {
129-
static const int kWidth = tWidth;
130-
static const int kDepth = tDepth;
131-
static const CellOrder kOrder = tOrder;
129+
static constexpr int kWidth = tWidth;
130+
static constexpr int kDepth = tDepth;
131+
static constexpr CellOrder kOrder = tOrder;
132132

133-
static const int kSize = kWidth * kDepth;
133+
static constexpr int kSize = kWidth * kDepth;
134134
};
135135

136136
// KernelSideFormat describes how data is laid out in a kernel side
@@ -142,9 +142,9 @@ struct CellFormat {
142142
template <typename tCellFormat, int tCells>
143143
struct KernelSideFormat {
144144
typedef tCellFormat Cell;
145-
static const int kCells = tCells;
146-
static const int kWidth = kCells * Cell::kWidth;
147-
static const int kDepth = Cell::kDepth;
145+
static constexpr int kCells = tCells;
146+
static constexpr int kWidth = kCells * Cell::kWidth;
147+
static constexpr int kDepth = Cell::kDepth;
148148
typedef std::uint8_t Scalar; // The scalar type of the Format.
149149
typedef std::uint8_t InputScalar; // The scalar type of the original input.
150150
};
@@ -173,9 +173,9 @@ struct KernelFormat {
173173
typedef tRhs Rhs;
174174

175175
static_assert(Lhs::Cell::kDepth == Rhs::Cell::kDepth, "");
176-
static const int kDepth = Lhs::Cell::kDepth;
177-
static const int kRows = Lhs::Cell::kWidth * Lhs::kCells;
178-
static const int kCols = Rhs::Cell::kWidth * Rhs::kCells;
176+
static constexpr int kDepth = Lhs::Cell::kDepth;
177+
static constexpr int kRows = Lhs::Cell::kWidth * Lhs::kCells;
178+
static constexpr int kCols = Rhs::Cell::kWidth * Rhs::kCells;
179179
};
180180

181181
inline const char* CellOrderName(CellOrder o) {

internal/pack.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ template <typename tScalar, SideMapOrder tOrder>
143143
class SideMap {
144144
public:
145145
typedef tScalar Scalar;
146-
static const SideMapOrder kOrder = tOrder;
146+
static constexpr SideMapOrder kOrder = tOrder;
147147

148148
SideMap(Scalar* data, int width, int depth, int stride)
149149
: data_(data), width_(width), depth_(depth), stride_(stride) {}
@@ -214,13 +214,13 @@ class PackingRegisterBlockBase {
214214
typedef typename KernelSideFormat::Cell CellFormat;
215215
typedef typename KernelSideFormat::InputScalar KernelInputScalar;
216216
typedef typename KernelSideFormat::Scalar KernelScalar;
217-
static const int kCells = KernelSideFormat::kCells;
218-
static const int kCellWidth = CellFormat::kWidth;
219-
static const int kKernelWidth = CellFormat::kWidth * kCells;
220-
static const int kCellDepth = CellFormat::kDepth;
221-
static const int kCellSize = CellFormat::kSize;
222-
static const SideMapOrder kSrcOrder = SrcMapType::kOrder;
223-
static const int kZeroPointInputValue =
217+
static constexpr int kCells = KernelSideFormat::kCells;
218+
static constexpr int kCellWidth = CellFormat::kWidth;
219+
static constexpr int kKernelWidth = CellFormat::kWidth * kCells;
220+
static constexpr int kCellDepth = CellFormat::kDepth;
221+
static constexpr int kCellSize = CellFormat::kSize;
222+
static constexpr SideMapOrder kSrcOrder = SrcMapType::kOrder;
223+
static constexpr int kZeroPointInputValue =
224224
ZeroPointInputValue<KernelInputScalar, KernelScalar>::kValue;
225225

226226
PackingRegisterBlockBase() : complete_src_(nullptr, 0, 0, 0) {}
@@ -302,10 +302,10 @@ class PackSideBlockImpl {
302302
public:
303303
typedef typename PackedSideBlock::KernelSideFormat KernelSideFormat;
304304
typedef typename KernelSideFormat::Cell CellFormat;
305-
static const int kCells = KernelSideFormat::kCells;
306-
static const int kCellWidth = CellFormat::kWidth;
307-
static const int kKernelWidth = CellFormat::kWidth * kCells;
308-
static const int kCellDepth = CellFormat::kDepth;
305+
static constexpr int kCells = KernelSideFormat::kCells;
306+
static constexpr int kCellWidth = CellFormat::kWidth;
307+
static constexpr int kKernelWidth = CellFormat::kWidth * kCells;
308+
static constexpr int kCellDepth = CellFormat::kDepth;
309309

310310
typedef PackingRegisterBlock<SrcMapType, PackedSideBlock>
311311
PackingRegisterBlockType;

internal/pack_sse.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ class PackingRegisterBlock<
4141
public:
4242
typedef WidthMajorSideFormatNCells4x2<Cells> KernelSideFormat;
4343
typedef typename KernelSideFormat::Cell CellFormat;
44-
static const int kCells = KernelSideFormat::kCells;
45-
static const int kCellWidth = CellFormat::kWidth;
46-
static const int kKernelWidth = CellFormat::kWidth * kCells;
47-
static const int kCellDepth = CellFormat::kDepth;
48-
static const int kCellSize = CellFormat::kSize;
44+
static constexpr int kCells = KernelSideFormat::kCells;
45+
static constexpr int kCellWidth = CellFormat::kWidth;
46+
static constexpr int kKernelWidth = CellFormat::kWidth * kCells;
47+
static constexpr int kCellDepth = CellFormat::kDepth;
48+
static constexpr int kCellSize = CellFormat::kSize;
4949

5050
void Pack(PackedSideBlock<KernelSideFormat>* dst, int start_width) {
5151
std::uint8_t* dst_ptr = dst->current_data();

public/bit_depth.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ namespace gemmlowp {
2222
// The range of allowed values for an operand.
2323
template <int tMinValue, int tMaxValue>
2424
struct OperandRange {
25-
static const int kMinValue = tMinValue;
26-
static const int kMaxValue = tMaxValue;
25+
static constexpr int kMinValue = tMinValue;
26+
static constexpr int kMaxValue = tMaxValue;
2727
static_assert(kMinValue < kMaxValue, "");
2828
};
2929

public/map.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ template <typename tScalar, MapOrder tOrder>
3232
class MatrixMap {
3333
public:
3434
typedef tScalar Scalar;
35-
static const MapOrder kOrder = tOrder;
35+
static constexpr MapOrder kOrder = tOrder;
3636

3737
protected:
3838
Scalar* data_; // not owned.
@@ -84,7 +84,7 @@ template <typename tScalar, VectorShape tShape>
8484
class VectorMap {
8585
public:
8686
typedef tScalar Scalar;
87-
static const VectorShape kShape = tShape;
87+
static constexpr VectorShape kShape = tShape;
8888

8989
protected:
9090
Scalar* data_; // not owned.
@@ -113,7 +113,7 @@ template <typename tScalar, VectorShape tShape>
113113
class VectorDup {
114114
public:
115115
typedef tScalar Scalar;
116-
static const VectorShape kShape = tShape;
116+
static constexpr VectorShape kShape = tShape;
117117

118118
protected:
119119
Scalar data_;

test/test.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Matrix : public MatrixMap<tScalar, tOrder> {
4949
typedef MatrixMap<tScalar, tOrder> Map;
5050
typedef MatrixMap<const tScalar, tOrder> ConstMap;
5151
typedef typename Map::Scalar Scalar;
52-
static const MapOrder Order = tOrder;
52+
static constexpr MapOrder Order = tOrder;
5353
using Map::kOrder;
5454
using Map::rows_;
5555
using Map::cols_;

test/test_blocking_counter.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class Thread {
7878
return nullptr;
7979
}
8080

81-
static const size_t max_stack_size_ = 256 * 1024;
81+
static constexpr size_t max_stack_size_ = 256 * 1024;
8282
BlockingCounter* const blocking_counter_;
8383
const int number_of_times_to_decrement_;
8484
pthread_t thread_;

0 commit comments

Comments
 (0)