Skip to content

Commit

Permalink
[#2] Implement type wrappers for bool and floating point types
Browse files Browse the repository at this point in the history
  • Loading branch information
Mi-La committed Sep 9, 2024
1 parent 3666ca6 commit a57fe76
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 110 deletions.
4 changes: 2 additions & 2 deletions runtime/src/zserio/BitSizeOfCalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace zserio
{

static const std::array<uint64_t, 2> VARIN16_MAX_VALUES = {
static const std::array<uint64_t, 2> VARINT16_MAX_VALUES = {
(UINT64_C(1) << (6)) - 1,
(UINT64_C(1) << (6 + 8)) - 1,
};
Expand Down Expand Up @@ -116,7 +116,7 @@ static uint64_t convertToAbsValue(T value)

size_t bitSizeOfVarInt16(int16_t value)
{
return bitSizeOfVarIntImpl(convertToAbsValue(value), VARIN16_MAX_VALUES, "varint16");
return bitSizeOfVarIntImpl(convertToAbsValue(value), VARINT16_MAX_VALUES, "varint16");
}

size_t bitSizeOfVarInt32(int32_t value)
Expand Down
53 changes: 49 additions & 4 deletions runtime/src/zserio/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,19 @@ class TypeWrapper
value_type m_value;
};

class BoolWrapper : public TypeWrapper<bool>
{
public:
using TypeWrapper<bool>::TypeWrapper;
};

template <typename VALUE_TYPE, BitSize BIT_SIZE = 8 * sizeof(VALUE_TYPE)>
class IntWrapper : public TypeWrapper<VALUE_TYPE>
{
private:
static_assert((BIT_SIZE + 7) / 8 <= sizeof(VALUE_TYPE), "BitSize doesn't fit to the VALUE_TYPE!");

public:
using value_type = VALUE_TYPE;

using TypeWrapper<VALUE_TYPE>::TypeWrapper;
};

Expand Down Expand Up @@ -81,13 +85,31 @@ class VarIntWrapper : public TypeWrapper<VALUE_TYPE>
using TypeWrapper<VALUE_TYPE>::TypeWrapper;
};

enum class FloatType : uint8_t
{
FLOAT16,
FLOAT32,
FLOAT64
};

template <typename VALUE_TYPE, FloatType FLOAT_TYPE>
class FloatWrapper : public TypeWrapper<VALUE_TYPE>
{
public:
using TypeWrapper<VALUE_TYPE>::TypeWrapper;
};

template <typename TYPE_WRAPPER>
struct needs_range_check
{};

template <typename TYPE_WRAPPER>
inline constexpr bool needs_range_check_v = needs_range_check<TYPE_WRAPPER>::value;

template <>
struct needs_range_check<BoolWrapper> : std::false_type
{};

template <typename VALUE_TYPE, BitSize BIT_SIZE>
struct needs_range_check<IntWrapper<VALUE_TYPE, BIT_SIZE>>
{
Expand All @@ -105,11 +127,14 @@ struct needs_range_check<VarIntWrapper<VALUE_TYPE, VAR_TYPE>>
static constexpr bool value = VAR_TYPE != VarIntType::VAR;
};

template <typename VALUE_TYPE, FloatType FLOAT_TYPE>
struct needs_range_check<FloatWrapper<VALUE_TYPE, FLOAT_TYPE>> : std::false_type
{};

template <typename TYPE_WRAPPER>
struct RangeChecker
{
static constexpr void check(typename TYPE_WRAPPER::value_type value) noexcept(
!detail::needs_range_check_v<TYPE_WRAPPER>)
static constexpr void check(typename TYPE_WRAPPER::value_type value)
{
if constexpr (std::is_signed_v<typename TYPE_WRAPPER::value_type>)
{
Expand Down Expand Up @@ -166,6 +191,20 @@ struct RangeChecker<DynIntWrapper<VALUE_TYPE>>

} // namespace detail

template <>
struct Limits<detail::BoolWrapper>
{
static constexpr bool min() noexcept
{
return false;
}

static constexpr bool max() noexcept
{
return true;
}
};

template <typename VALUE_TYPE, BitSize BIT_SIZE>
struct Limits<detail::IntWrapper<VALUE_TYPE, BIT_SIZE>>
{
Expand Down Expand Up @@ -360,6 +399,8 @@ constexpr typename TYPE_WRAPPER::value_type checked_cast(TYPE_WRAPPER wrapper, B
return wrapper;
}

using Bool = detail::BoolWrapper;

using Int8 = detail::IntWrapper<int8_t>;
using Int16 = detail::IntWrapper<int16_t>;
using Int32 = detail::IntWrapper<int32_t>;
Expand Down Expand Up @@ -514,6 +555,10 @@ using VarUInt = detail::VarIntWrapper<uint64_t, detail::VarIntType::VAR>;

using VarSize = detail::VarIntWrapper<uint32_t, detail::VarIntType::VARSIZE>;

using Float16 = detail::FloatWrapper<float, detail::FloatType::FLOAT16>;
using Float32 = detail::FloatWrapper<float, detail::FloatType::FLOAT32>;
using Float64 = detail::FloatWrapper<double, detail::FloatType::FLOAT64>;

} // namespace zserio

#endif // ifndef ZSERIO_TYPES_H_INC
Loading

0 comments on commit a57fe76

Please sign in to comment.