mirror of https://github.com/ecmwf/eccodes.git
ECC-1930: Cleanup
This commit is contained in:
parent
f0b40ca4d4
commit
5f5ae2056f
|
@ -11,15 +11,21 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <cstdint>
|
|
||||||
#include <climits> // CHAR_BIT
|
#include <climits> // CHAR_BIT
|
||||||
|
|
||||||
// NumericLimits is a class template that provides the minimum and maximum values for a given number of bits.
|
// NumericLimits is a class template that provides the minimum and maximum values for a given number of bits.
|
||||||
// The minimum and maximum values are calculated for both signed and unsigned integral types.
|
// The minimum and maximum values are calculated for both signed and unsigned integral types.
|
||||||
|
|
||||||
|
template <typename ValueType, bool = std::is_signed<ValueType>::value, bool = std::is_integral<ValueType>::value>
|
||||||
|
struct NumericLimits
|
||||||
|
{
|
||||||
|
static_assert(std::is_integral<ValueType>::value, "ValueType must be an integral type");
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
// Example:
|
// Example:
|
||||||
// For a 16-bit signed integer, the minimum and maximum values are:
|
// For a 16-bit signed integer, the minimum and maximum values are:
|
||||||
// nbits | min | max
|
// nBits | min | max
|
||||||
// ----- | --- | ---
|
// ----- | --- | ---
|
||||||
// 1 | -1 | 0
|
// 1 | -1 | 0
|
||||||
// 2 | -2 | 1
|
// 2 | -2 | 1
|
||||||
|
@ -29,47 +35,40 @@
|
||||||
// 15 | -16384 | 16383
|
// 15 | -16384 | 16383
|
||||||
// 16 | -32768 | 32767
|
// 16 | -32768 | 32767
|
||||||
|
|
||||||
template <typename ValueType, bool = std::is_signed<ValueType>::value, bool = std::is_integral<ValueType>::value>
|
template <typename ValueType>
|
||||||
struct NumericLimits
|
struct NumericLimits<ValueType, true, true>
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
static constexpr uint8_t MaxN = CHAR_BIT * sizeof(ValueType);
|
static constexpr std::size_t maxNBits = CHAR_BIT * sizeof(ValueType);
|
||||||
|
|
||||||
static constexpr std::array<ValueType, MaxN> max_ = []() {
|
static constexpr std::array<ValueType, maxNBits> max_ = []() {
|
||||||
std::array<ValueType, MaxN> max{};
|
std::array<ValueType, maxNBits> max{};
|
||||||
using UnsignedValueType = std::make_unsigned_t<ValueType>;
|
using UnsignedValueType = std::make_unsigned_t<ValueType>;
|
||||||
constexpr UnsignedValueType ones = ~UnsignedValueType{ 0 };
|
constexpr UnsignedValueType ones = ~UnsignedValueType{ 0 };
|
||||||
max[0] = 0;
|
max[0] = 0;
|
||||||
for (uint8_t i = 1; i < MaxN; ++i) {
|
for (std::size_t i = 1; i < maxNBits; ++i) {
|
||||||
max[i] = static_cast<ValueType>(ones >> (MaxN - i));
|
max[i] = static_cast<ValueType>(ones >> (maxNBits - i));
|
||||||
}
|
}
|
||||||
return max;
|
return max;
|
||||||
}();
|
}();
|
||||||
|
|
||||||
static constexpr std::array<ValueType, MaxN> min_ = []() {
|
static constexpr std::array<ValueType, maxNBits> min_ = []() {
|
||||||
std::array<ValueType, MaxN> min{};
|
std::array<ValueType, maxNBits> min{};
|
||||||
for (uint8_t i = 0; i < MaxN; ++i) {
|
for (std::size_t i = 0; i < maxNBits; ++i) {
|
||||||
min[i] = ~max_[i];
|
min[i] = ~max_[i];
|
||||||
}
|
}
|
||||||
return min;
|
return min;
|
||||||
}();
|
}();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static constexpr ValueType min(uint8_t nbits)
|
static constexpr ValueType min(std::size_t nBits) { return min_[nBits - 1]; }
|
||||||
{
|
static constexpr ValueType max(std::size_t nBits) { return max_[nBits - 1]; }
|
||||||
return min_[nbits - 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
static constexpr ValueType max(uint8_t nbits)
|
|
||||||
{
|
|
||||||
return max_[nbits - 1];
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Example:
|
// Example:
|
||||||
// For a 16-bit unsigned integer, the minimum and maximum values are:
|
// For a 16-bit unsigned integer, the minimum and maximum values are:
|
||||||
// nbits | min | max
|
// nBits | min | max
|
||||||
// ----- | --- | ---
|
// ----- | --- | ---
|
||||||
// 1 | 0 | 1
|
// 1 | 0 | 1
|
||||||
// 2 | 0 | 3
|
// 2 | 0 | 3
|
||||||
|
@ -82,29 +81,20 @@ public:
|
||||||
template <typename ValueType>
|
template <typename ValueType>
|
||||||
struct NumericLimits<ValueType, false, true>
|
struct NumericLimits<ValueType, false, true>
|
||||||
{
|
{
|
||||||
static_assert(std::is_integral<ValueType>::value, "ValueType must be an integral type");
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static constexpr uint8_t MaxN = CHAR_BIT * sizeof(ValueType);
|
static constexpr std::size_t maxNBits = CHAR_BIT * sizeof(ValueType);
|
||||||
|
|
||||||
static constexpr std::array<ValueType, MaxN> max_ = []() {
|
static constexpr std::array<ValueType, maxNBits> max_ = []() {
|
||||||
std::array<ValueType, MaxN> max{};
|
std::array<ValueType, maxNBits> max{};
|
||||||
constexpr ValueType ones = ~(static_cast<ValueType>(0));
|
constexpr ValueType ones = ~(static_cast<ValueType>(0));
|
||||||
max[0] = 1;
|
max[0] = 1;
|
||||||
for (uint8_t i = 1; i < MaxN; ++i) {
|
for (std::size_t i = 1; i < maxNBits; ++i) {
|
||||||
max[i] = ones >> (MaxN - i - 1);
|
max[i] = ones >> (maxNBits - i - 1);
|
||||||
}
|
}
|
||||||
return max;
|
return max;
|
||||||
}();
|
}();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static constexpr ValueType min(uint8_t nbits)
|
static constexpr ValueType min(std::size_t nBits) { return 0; }
|
||||||
{
|
static constexpr ValueType max(std::size_t nBits) { return max_[nBits - 1]; }
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static constexpr ValueType max(uint8_t nbits)
|
|
||||||
{
|
|
||||||
return max_[nbits - 1];
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue