Make static const class/struct members as constexpr

PiperOrigin-RevId: 307458656
Change-Id: I4705d18ec0f2be8e306de6484f8a9b09df0a2beb
This commit is contained in:
A. Unique TensorFlower 2020-04-20 12:55:52 -07:00 committed by TensorFlower Gardener
parent 6728f85d82
commit 73b1648c8e
15 changed files with 74 additions and 74 deletions

View File

@ -132,7 +132,7 @@ using VariantDevice = absl::variant<Device*, CustomDevice*>;
class EagerContext : public AbstractContextInterface, public core::RefCounted {
public:
static const uint64 kInvalidContextId = 0;
static constexpr uint64 kInvalidContextId = 0;
static uint64 NewContextId() {
uint64 context_id = random::New64();

View File

@ -87,7 +87,7 @@ class EagerServiceImpl {
Status CreateMasterContext(const tensorflow::uint64 context_id,
EagerContext* context);
static const uint64 kInvalidStreamId = 0;
static constexpr uint64 kInvalidStreamId = 0;
// Used by both Enqueue and StreamingEnqueue RPCs.
Status Enqueue(const EnqueueRequest* request, EnqueueResponse* response,

View File

@ -197,7 +197,7 @@ class RpcRecvTensorFreeList {
}
private:
static const int kMaxObjects = 1000;
static constexpr int kMaxObjects = 1000;
mutex mu_;
std::vector<RpcRecvTensorCall*> objects_ TF_GUARDED_BY(mu_);

View File

@ -603,7 +603,7 @@ class CacheDatasetOp::FileDataset : public DatasetBase {
Env* const env_;
const size_t num_tensors_;
const size_t tensor_index_padding_size_;
static const size_t kMaxItems = 10000000; // 10 million
static constexpr size_t kMaxItems = 10000000; // 10 million
const size_t item_index_padding_size_;
const string tensor_format_string_;
}; // FileDataset

View File

@ -56,7 +56,7 @@ class FuzzStringSplitV2 : public FuzzSession {
}
private:
static const size_t kMaxSepSize = 4;
static constexpr size_t kMaxSepSize = 4;
};
STANDARD_TF_FUZZ_FUNCTION(FuzzStringSplitV2);

View File

@ -392,11 +392,11 @@ struct bfloat16 {
uint16_t value;
// A value that represents "not a number".
static const uint16_t NAN_VALUE = 0x7FC0;
static constexpr uint16_t NAN_VALUE = 0x7FC0;
private:
// A value that represents "zero".
static const uint16_t ZERO_VALUE = 0;
static constexpr uint16_t ZERO_VALUE = 0;
B16_DEVICE_FUNC static bool float_isnan(const float& x) {
#ifdef __CUDA_ARCH__

View File

@ -57,7 +57,7 @@ class Arena {
#ifdef __i386__
static const int kDefaultAlignment = 4;
#else
static const int kDefaultAlignment = 8;
static constexpr int kDefaultAlignment = 8;
#endif
protected:

View File

@ -63,7 +63,7 @@ class Bitmap {
private:
typedef uint32 Word;
static const size_t kBits = 32;
static constexpr size_t kBits = 32;
// Return the number of words needed to store n bits.
static size_t NumWords(size_t n) { return (n + kBits - 1) / kBits; }

View File

@ -45,8 +45,8 @@ template <typename Key, typename Bucket, class Hash, class Eq>
class FlatRep {
public:
// kWidth is the number of entries stored in a bucket.
static const uint32 kBase = 3;
static const uint32 kWidth = (1 << kBase);
static constexpr uint32 kBase = 3;
static constexpr uint32 kWidth = (1 << kBase);
FlatRep(size_t N, const Hash& hf, const Eq& eq) : hash_(hf), equal_(eq) {
Init(N);

View File

@ -44,7 +44,7 @@ class CacheTest : public ::testing::Test {
current_->deleted_values_.push_back(DecodeValue(v));
}
static const int kCacheSize = 1000;
static constexpr int kCacheSize = 1000;
std::vector<int> deleted_keys_;
std::vector<int> deleted_values_;
Cache* cache_;

View File

@ -63,8 +63,8 @@ class RecordReader {
// uint32 masked crc of length
// byte data[length]
// uint32 masked crc of data
static const size_t kHeaderSize = sizeof(uint64) + sizeof(uint32);
static const size_t kFooterSize = sizeof(uint32);
static constexpr size_t kHeaderSize = sizeof(uint64) + sizeof(uint32);
static constexpr size_t kFooterSize = sizeof(uint32);
// Statistics (sizes are in units of bytes)
struct Stats {

View File

@ -55,8 +55,8 @@ class RecordWriter {
// uint32 masked crc of length
// byte data[length]
// uint32 masked crc of data
static const size_t kHeaderSize = sizeof(uint64) + sizeof(uint32);
static const size_t kFooterSize = sizeof(uint32);
static constexpr size_t kHeaderSize = sizeof(uint64) + sizeof(uint32);
static constexpr size_t kFooterSize = sizeof(uint32);
// Create a writer that will append data to "*dest".
// "*dest" must be initially empty.

View File

@ -49,7 +49,7 @@ namespace random {
template <typename T, int ElementCount>
class Array {
public:
static const int kElementCount = ElementCount;
static constexpr int kElementCount = ElementCount;
PHILOX_DEVICE_INLINE Array() {
for (int i = 0; i < ElementCount; ++i) {
data_[i] = T(0);
@ -105,9 +105,9 @@ class PhiloxRandom {
using ResultType = Array<uint32, 4>;
using ResultElementType = uint32;
// The number of elements that will be returned.
static const int kResultElementCount = 4;
static constexpr int kResultElementCount = 4;
// Cost of generation of a single element (in cycles).
static const int kElementCost = 10;
static constexpr int kElementCost = 10;
// The type for the 64-bit key stored in the form of two 32-bit uint
// that are used in the diffusion process.
using Key = Array<uint32, 2>;
@ -192,10 +192,10 @@ class PhiloxRandom {
private:
// We use the same constants as recommended by the original paper.
static const uint32 kPhiloxW32A = 0x9E3779B9;
static const uint32 kPhiloxW32B = 0xBB67AE85;
static const uint32 kPhiloxM4x32A = 0xD2511F53;
static const uint32 kPhiloxM4x32B = 0xCD9E8D57;
static constexpr uint32 kPhiloxW32A = 0x9E3779B9;
static constexpr uint32 kPhiloxW32B = 0xBB67AE85;
static constexpr uint32 kPhiloxM4x32A = 0xD2511F53;
static constexpr uint32 kPhiloxM4x32B = 0xCD9E8D57;
// Helper function to skip the next sample of 128-bits in the current stream.
PHILOX_DEVICE_INLINE void SkipOne() {

View File

@ -71,12 +71,12 @@ template <class Generator>
class UniformDistribution<Generator, Eigen::half> {
public:
// The number of elements that will be returned.
static const int kResultElementCount = Generator::kResultElementCount;
static constexpr int kResultElementCount = Generator::kResultElementCount;
// Cost of generation of a single element (in cycles).
static const int kElementCost = 3;
static constexpr int kElementCost = 3;
// Indicate that this distribution may take variable number of samples
// during the runtime.
static const bool kVariableSamplesPerOutput = false;
static constexpr bool kVariableSamplesPerOutput = false;
typedef Array<Eigen::half, kResultElementCount> ResultType;
typedef Eigen::half ResultElementType;
@ -95,12 +95,12 @@ template <class Generator>
class UniformDistribution<Generator, bfloat16> {
public:
// The number of elements that will be returned.
static const int kResultElementCount = Generator::kResultElementCount;
static constexpr int kResultElementCount = Generator::kResultElementCount;
// Cost of generation of a single element (in cycles).
static const int kElementCost = 3;
static constexpr int kElementCost = 3;
// Indicate that this distribution may take variable number of samples
// during the runtime.
static const bool kVariableSamplesPerOutput = false;
static constexpr bool kVariableSamplesPerOutput = false;
typedef Array<bfloat16, kResultElementCount> ResultType;
typedef bfloat16 ResultElementType;
@ -119,12 +119,12 @@ template <class Generator>
class UniformDistribution<Generator, float> {
public:
// The number of elements that will be returned.
static const int kResultElementCount = Generator::kResultElementCount;
static constexpr int kResultElementCount = Generator::kResultElementCount;
// Cost of generation of a single element (in cycles).
static const int kElementCost = 3;
static constexpr int kElementCost = 3;
// Indicate that this distribution may take variable number of samples
// during the runtime.
static const bool kVariableSamplesPerOutput = false;
static constexpr bool kVariableSamplesPerOutput = false;
typedef Array<float, kResultElementCount> ResultType;
typedef float ResultElementType;
@ -143,12 +143,12 @@ template <class Generator>
class UniformDistribution<Generator, double> {
public:
// The number of elements that will be returned.
static const int kResultElementCount = Generator::kResultElementCount / 2;
static constexpr int kResultElementCount = Generator::kResultElementCount / 2;
// Cost of generation of a single element (in cycles).
static const int kElementCost = 3;
static constexpr int kElementCost = 3;
// Indicate that this distribution may take variable number of samples
// during the runtime.
static const bool kVariableSamplesPerOutput = false;
static constexpr bool kVariableSamplesPerOutput = false;
typedef Array<double, kResultElementCount> ResultType;
typedef double ResultElementType;
@ -167,12 +167,12 @@ template <class Generator>
class UniformDistribution<Generator, int32> {
public:
// The number of elements that will be returned.
static const int kResultElementCount = Generator::kResultElementCount;
static constexpr int kResultElementCount = Generator::kResultElementCount;
// Cost of generation of a single element (in cycles).
static const int kElementCost = 3;
static constexpr int kElementCost = 3;
// Indicate that this distribution may take variable number of samples
// during the runtime.
static const bool kVariableSamplesPerOutput = false;
static constexpr bool kVariableSamplesPerOutput = false;
typedef Array<int32, kResultElementCount> ResultType;
typedef int32 ResultElementType;
@ -202,12 +202,12 @@ template <class Generator>
class UniformDistribution<Generator, int64> {
public:
// The number of elements that will be returned.
static const int kResultElementCount = Generator::kResultElementCount / 2;
static constexpr int kResultElementCount = Generator::kResultElementCount / 2;
// Cost of generation of a single element (in cycles).
static const int kElementCost = 3;
static constexpr int kElementCost = 3;
// Indicate that this distribution may take variable number of samples
// during the runtime.
static const bool kVariableSamplesPerOutput = false;
static constexpr bool kVariableSamplesPerOutput = false;
typedef Array<int64, kResultElementCount> ResultType;
typedef int64 ResultElementType;
@ -244,12 +244,12 @@ template <typename Generator, typename IntType>
class UniformFullIntDistribution32 {
public:
// The number of elements that will be returned.
static const int kResultElementCount = Generator::kResultElementCount;
static constexpr int kResultElementCount = Generator::kResultElementCount;
// Cost of generation of a single element (in cycles).
static const int kElementCost = 3;
static constexpr int kElementCost = 3;
// Indicate that this distribution may take variable number of samples
// during the runtime.
static const bool kVariableSamplesPerOutput = false;
static constexpr bool kVariableSamplesPerOutput = false;
typedef Array<IntType, kResultElementCount> ResultType;
typedef IntType ResultElementType;
@ -268,12 +268,12 @@ template <typename Generator, typename IntType>
class UniformFullIntDistribution64 {
public:
// The number of elements that will be returned.
static const int kResultElementCount = Generator::kResultElementCount / 2;
static constexpr int kResultElementCount = Generator::kResultElementCount / 2;
// Cost of generation of a single element (in cycles).
static const int kElementCost = 3;
static constexpr int kElementCost = 3;
// Indicate that this distribution may take variable number of samples
// during the runtime.
static const bool kVariableSamplesPerOutput = false;
static constexpr bool kVariableSamplesPerOutput = false;
typedef Array<IntType, kResultElementCount> ResultType;
typedef IntType ResultElementType;
@ -307,9 +307,9 @@ template <class Generator>
class SingleSampleAdapter {
public:
// The number of elements that will be returned.
static const int kResultElementCount = 1;
static constexpr int kResultElementCount = 1;
// The number of elements that will be returned by the underlying generator.
static const int kNativeElementCount = Generator::kResultElementCount;
static constexpr int kNativeElementCount = Generator::kResultElementCount;
typedef typename Generator::ResultElementType ResultType;
typedef typename Generator::ResultElementType ResultElementType;
@ -391,12 +391,12 @@ template <class Generator>
class NormalDistribution<Generator, Eigen::half> {
public:
// The number of elements that will be returned.
static const int kResultElementCount = Generator::kResultElementCount;
static constexpr int kResultElementCount = Generator::kResultElementCount;
// Cost of generation of a single element (in cycles).
static const int kElementCost = 70;
static constexpr int kElementCost = 70;
// Indicate that this distribution may take variable number of samples
// during the runtime.
static const bool kVariableSamplesPerOutput = false;
static constexpr bool kVariableSamplesPerOutput = false;
typedef Array<Eigen::half, kResultElementCount> ResultType;
typedef Eigen::half ResultElementType;
@ -418,12 +418,12 @@ template <class Generator>
class NormalDistribution<Generator, bfloat16> {
public:
// The number of elements that will be returned.
static const int kResultElementCount = Generator::kResultElementCount;
static constexpr int kResultElementCount = Generator::kResultElementCount;
// Cost of generation of a single element (in cycles).
static const int kElementCost = 70;
static constexpr int kElementCost = 70;
// Indicate that this distribution may take variable number of samples
// during the runtime.
static const bool kVariableSamplesPerOutput = false;
static constexpr bool kVariableSamplesPerOutput = false;
typedef Array<bfloat16, kResultElementCount> ResultType;
typedef bfloat16 ResultElementType;
@ -448,12 +448,12 @@ template <class Generator>
class NormalDistribution<Generator, float> {
public:
// The number of elements that will be returned.
static const int kResultElementCount = Generator::kResultElementCount;
static constexpr int kResultElementCount = Generator::kResultElementCount;
// Cost of generation of a single element (in cycles).
static const int kElementCost = 70;
static constexpr int kElementCost = 70;
// Indicate that this distribution may take variable number of samples
// during the runtime.
static const bool kVariableSamplesPerOutput = false;
static constexpr bool kVariableSamplesPerOutput = false;
typedef Array<float, kResultElementCount> ResultType;
typedef float ResultElementType;
@ -472,12 +472,12 @@ template <class Generator>
class NormalDistribution<Generator, double> {
public:
// The number of elements that will be returned.
static const int kResultElementCount = Generator::kResultElementCount / 2;
static constexpr int kResultElementCount = Generator::kResultElementCount / 2;
// Cost of generation of a single element (in cycles).
static const int kElementCost = 70;
static constexpr int kElementCost = 70;
// Indicate that this distribution may take variable number of samples
// during the runtime.
static const bool kVariableSamplesPerOutput = false;
static constexpr bool kVariableSamplesPerOutput = false;
typedef Array<double, kResultElementCount> ResultType;
typedef double ResultElementType;
@ -515,13 +515,13 @@ template <class SingleSampleGenerator>
class TruncatedNormalDistribution<SingleSampleGenerator, Eigen::half> {
public:
// The number of elements that will be returned.
static const int kResultElementCount =
static constexpr int kResultElementCount =
SingleSampleGenerator::kNativeElementCount;
// Cost of generation of a single element (in cycles).
static const int kElementCost = 90;
static constexpr int kElementCost = 90;
// Indicate that this distribution may take variable number of samples
// during the runtime.
static const bool kVariableSamplesPerOutput = true;
static constexpr bool kVariableSamplesPerOutput = true;
// The threshold where the normal distribution is truncated.
const float kTruncateValue = 2.0f;
@ -561,13 +561,13 @@ template <class SingleSampleGenerator>
class TruncatedNormalDistribution<SingleSampleGenerator, bfloat16> {
public:
// The number of elements that will be returned.
static const int kResultElementCount =
static constexpr int kResultElementCount =
SingleSampleGenerator::kNativeElementCount;
// Cost of generation of a single element (in cycles).
static const int kElementCost = 90;
static constexpr int kElementCost = 90;
// Indicate that this distribution may take variable number of samples
// during the runtime.
static const bool kVariableSamplesPerOutput = true;
static constexpr bool kVariableSamplesPerOutput = true;
// The threshold where the normal distribution is truncated.
const float kTruncateValue = 2.0f;
@ -608,13 +608,13 @@ template <class SingleSampleGenerator>
class TruncatedNormalDistribution<SingleSampleGenerator, float> {
public:
// The number of elements that will be returned.
static const int kResultElementCount =
static constexpr int kResultElementCount =
SingleSampleGenerator::kNativeElementCount;
// Cost of generation of a single element (in cycles).
static const int kElementCost = 90;
static constexpr int kElementCost = 90;
// Indicate that this distribution may take variable number of samples
// during the runtime.
static const bool kVariableSamplesPerOutput = true;
static constexpr bool kVariableSamplesPerOutput = true;
// The threshold where the normal distribution is truncated.
const float kTruncateValue = 2.0f;
@ -655,15 +655,15 @@ template <class SingleSampleGenerator>
class TruncatedNormalDistribution<SingleSampleGenerator, double> {
public:
// The number of elements that will be returned.
static const int kResultElementCount =
static constexpr int kResultElementCount =
(SingleSampleGenerator::kNativeElementCount > 1)
? SingleSampleGenerator::kNativeElementCount / 2
: 1;
// Cost of generation of a single element (in cycles).
static const int kElementCost = 90;
static constexpr int kElementCost = 90;
// Indicate that this distribution may take variable number of samples
// during the runtime.
static const bool kVariableSamplesPerOutput = true;
static constexpr bool kVariableSamplesPerOutput = true;
typedef Array<double, kResultElementCount> ResultType;
typedef double ResultElementType;
const double kTruncateValue = 2.0;

View File

@ -307,7 +307,7 @@ class MockGenerator {
explicit MockGenerator(uint64 seed) : counter_(seed) {}
using ResultType = std::vector<uint32>;
using ResultElementType = uint32;
static const int kResultElementCount = 1;
static constexpr int kResultElementCount = 1;
ResultType operator()() {
ResultType result;
result.push_back(counter_++);