Skip to content

Commit

Permalink
Large-scale change: Mark static const class/struct members as constex…
Browse files Browse the repository at this point in the history
…pr. 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.
  • Loading branch information
Danny van der Rijn authored and bjacob committed May 6, 2020
1 parent 023c190 commit fda83bd
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 39 deletions.
4 changes: 2 additions & 2 deletions internal/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ class Allocator {
}

// Alignment of allocated blocks.
static const std::size_t kAlignment = kDefaultCacheLineSize;
static constexpr std::size_t kAlignment = kDefaultCacheLineSize;

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

void Commit() {
assert(!committed_);
Expand Down
2 changes: 1 addition & 1 deletion internal/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ Integer RoundUpToPowerOfTwo(Integer n) {

template <int N>
struct IsPowerOfTwo {
static const bool value = !(N & (N - 1));
static constexpr bool value = !(N & (N - 1));
};

template <typename T>
Expand Down
6 changes: 4 additions & 2 deletions internal/dispatch_gemm_shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ struct TransposeImpl<MatrixMap<Scalar, Order>> {
template <VectorShape Shape>
struct TransposeImpl<OutputStageQuantizeDownInt32ToUint8ScalePC<Shape>> {
typedef OutputStageQuantizeDownInt32ToUint8ScalePC<Shape> SrcType;
static const VectorShape TransposedShape = TransposeVectorShape<Shape>::Value;
static constexpr VectorShape TransposedShape =
TransposeVectorShape<Shape>::Value;
typedef OutputStageQuantizeDownInt32ToUint8ScalePC<TransposedShape> DstType;
static DstType Run(const SrcType& src) {
DstType dst;
Expand All @@ -88,7 +89,8 @@ struct TransposeImpl<OutputStageQuantizeDownInt32ToUint8ScalePC<Shape>> {
template <VectorShape Shape>
struct TransposeImpl<OutputStageScaleInt32ByFixedPointAndExponentPC<Shape>> {
typedef OutputStageScaleInt32ByFixedPointAndExponentPC<Shape> SrcType;
static const VectorShape TransposedShape = TransposeVectorShape<Shape>::Value;
static constexpr VectorShape TransposedShape =
TransposeVectorShape<Shape>::Value;
typedef OutputStageScaleInt32ByFixedPointAndExponentPC<TransposedShape>
DstType;
static DstType Run(const SrcType& src) {
Expand Down
20 changes: 10 additions & 10 deletions internal/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ enum class CellOrder { DepthMajor, WidthMajor, Diagonal };
// out in a cell. That is, a CellOrder together with actual dimensions.
template <int tWidth, int tDepth, CellOrder tOrder = CellOrder::DepthMajor>
struct CellFormat {
static const int kWidth = tWidth;
static const int kDepth = tDepth;
static const CellOrder kOrder = tOrder;
static constexpr int kWidth = tWidth;
static constexpr int kDepth = tDepth;
static constexpr CellOrder kOrder = tOrder;

static const int kSize = kWidth * kDepth;
static constexpr int kSize = kWidth * kDepth;
};

// KernelSideFormat describes how data is laid out in a kernel side
Expand All @@ -142,9 +142,9 @@ struct CellFormat {
template <typename tCellFormat, int tCells>
struct KernelSideFormat {
typedef tCellFormat Cell;
static const int kCells = tCells;
static const int kWidth = kCells * Cell::kWidth;
static const int kDepth = Cell::kDepth;
static constexpr int kCells = tCells;
static constexpr int kWidth = kCells * Cell::kWidth;
static constexpr int kDepth = Cell::kDepth;
typedef std::uint8_t Scalar; // The scalar type of the Format.
typedef std::uint8_t InputScalar; // The scalar type of the original input.
};
Expand Down Expand Up @@ -173,9 +173,9 @@ struct KernelFormat {
typedef tRhs Rhs;

static_assert(Lhs::Cell::kDepth == Rhs::Cell::kDepth, "");
static const int kDepth = Lhs::Cell::kDepth;
static const int kRows = Lhs::Cell::kWidth * Lhs::kCells;
static const int kCols = Rhs::Cell::kWidth * Rhs::kCells;
static constexpr int kDepth = Lhs::Cell::kDepth;
static constexpr int kRows = Lhs::Cell::kWidth * Lhs::kCells;
static constexpr int kCols = Rhs::Cell::kWidth * Rhs::kCells;
};

inline const char* CellOrderName(CellOrder o) {
Expand Down
24 changes: 12 additions & 12 deletions internal/pack.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ template <typename tScalar, SideMapOrder tOrder>
class SideMap {
public:
typedef tScalar Scalar;
static const SideMapOrder kOrder = tOrder;
static constexpr SideMapOrder kOrder = tOrder;

SideMap(Scalar* data, int width, int depth, int stride)
: data_(data), width_(width), depth_(depth), stride_(stride) {}
Expand Down Expand Up @@ -214,13 +214,13 @@ class PackingRegisterBlockBase {
typedef typename KernelSideFormat::Cell CellFormat;
typedef typename KernelSideFormat::InputScalar KernelInputScalar;
typedef typename KernelSideFormat::Scalar KernelScalar;
static const int kCells = KernelSideFormat::kCells;
static const int kCellWidth = CellFormat::kWidth;
static const int kKernelWidth = CellFormat::kWidth * kCells;
static const int kCellDepth = CellFormat::kDepth;
static const int kCellSize = CellFormat::kSize;
static const SideMapOrder kSrcOrder = SrcMapType::kOrder;
static const int kZeroPointInputValue =
static constexpr int kCells = KernelSideFormat::kCells;
static constexpr int kCellWidth = CellFormat::kWidth;
static constexpr int kKernelWidth = CellFormat::kWidth * kCells;
static constexpr int kCellDepth = CellFormat::kDepth;
static constexpr int kCellSize = CellFormat::kSize;
static constexpr SideMapOrder kSrcOrder = SrcMapType::kOrder;
static constexpr int kZeroPointInputValue =
ZeroPointInputValue<KernelInputScalar, KernelScalar>::kValue;

PackingRegisterBlockBase() : complete_src_(nullptr, 0, 0, 0) {}
Expand Down Expand Up @@ -302,10 +302,10 @@ class PackSideBlockImpl {
public:
typedef typename PackedSideBlock::KernelSideFormat KernelSideFormat;
typedef typename KernelSideFormat::Cell CellFormat;
static const int kCells = KernelSideFormat::kCells;
static const int kCellWidth = CellFormat::kWidth;
static const int kKernelWidth = CellFormat::kWidth * kCells;
static const int kCellDepth = CellFormat::kDepth;
static constexpr int kCells = KernelSideFormat::kCells;
static constexpr int kCellWidth = CellFormat::kWidth;
static constexpr int kKernelWidth = CellFormat::kWidth * kCells;
static constexpr int kCellDepth = CellFormat::kDepth;

typedef PackingRegisterBlock<SrcMapType, PackedSideBlock>
PackingRegisterBlockType;
Expand Down
10 changes: 5 additions & 5 deletions internal/pack_sse.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ class PackingRegisterBlock<
public:
typedef WidthMajorSideFormatNCells4x2<Cells> KernelSideFormat;
typedef typename KernelSideFormat::Cell CellFormat;
static const int kCells = KernelSideFormat::kCells;
static const int kCellWidth = CellFormat::kWidth;
static const int kKernelWidth = CellFormat::kWidth * kCells;
static const int kCellDepth = CellFormat::kDepth;
static const int kCellSize = CellFormat::kSize;
static constexpr int kCells = KernelSideFormat::kCells;
static constexpr int kCellWidth = CellFormat::kWidth;
static constexpr int kKernelWidth = CellFormat::kWidth * kCells;
static constexpr int kCellDepth = CellFormat::kDepth;
static constexpr int kCellSize = CellFormat::kSize;

void Pack(PackedSideBlock<KernelSideFormat>* dst, int start_width) {
std::uint8_t* dst_ptr = dst->current_data();
Expand Down
4 changes: 2 additions & 2 deletions public/bit_depth.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ namespace gemmlowp {
// The range of allowed values for an operand.
template <int tMinValue, int tMaxValue>
struct OperandRange {
static const int kMinValue = tMinValue;
static const int kMaxValue = tMaxValue;
static constexpr int kMinValue = tMinValue;
static constexpr int kMaxValue = tMaxValue;
static_assert(kMinValue < kMaxValue, "");
};

Expand Down
6 changes: 3 additions & 3 deletions public/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ template <typename tScalar, MapOrder tOrder>
class MatrixMap {
public:
typedef tScalar Scalar;
static const MapOrder kOrder = tOrder;
static constexpr MapOrder kOrder = tOrder;

protected:
Scalar* data_; // not owned.
Expand Down Expand Up @@ -84,7 +84,7 @@ template <typename tScalar, VectorShape tShape>
class VectorMap {
public:
typedef tScalar Scalar;
static const VectorShape kShape = tShape;
static constexpr VectorShape kShape = tShape;

protected:
Scalar* data_; // not owned.
Expand Down Expand Up @@ -113,7 +113,7 @@ template <typename tScalar, VectorShape tShape>
class VectorDup {
public:
typedef tScalar Scalar;
static const VectorShape kShape = tShape;
static constexpr VectorShape kShape = tShape;

protected:
Scalar data_;
Expand Down
2 changes: 1 addition & 1 deletion test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Matrix : public MatrixMap<tScalar, tOrder> {
typedef MatrixMap<tScalar, tOrder> Map;
typedef MatrixMap<const tScalar, tOrder> ConstMap;
typedef typename Map::Scalar Scalar;
static const MapOrder Order = tOrder;
static constexpr MapOrder Order = tOrder;
using Map::kOrder;
using Map::rows_;
using Map::cols_;
Expand Down
2 changes: 1 addition & 1 deletion test/test_blocking_counter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Thread {
return nullptr;
}

static const size_t max_stack_size_ = 256 * 1024;
static constexpr size_t max_stack_size_ = 256 * 1024;
BlockingCounter* const blocking_counter_;
const int number_of_times_to_decrement_;
pthread_t thread_;
Expand Down

0 comments on commit fda83bd

Please sign in to comment.