Skip to content
Snippets Groups Projects

[IMPR][MAJ] More precise type system

Closed laurent soulier requested to merge feat/DataTypes into main
1 unresolved thread
Files
29
+ 205
26
@@ -15,8 +15,10 @@
#include "aidge/data/half.hpp"
#include "aidge/utils/Attributes.hpp"
namespace Aidge {
enum class DataType {
namespace Aidge
{
enum class DataType : std::uint8_t
{
Float64,
Float32,
Float16,
@@ -42,42 +44,219 @@ enum class DataType {
UInt8,
UInt16,
UInt32,
UInt64
UInt64,
Undefined,
};
class Data {
class Data
{
public:
constexpr Data(const char* type): mType(type) {};
constexpr const char* type() const {
constexpr const char* type() const noexcept
{
return mType;
}
virtual ~Data() = default;
// making the class abstract and not instanciable
Data& operator=(Data const&) const = delete;
Data& operator=(Data&&) const = delete;
virtual ~Data() noexcept = 0;
protected:
// must be used only with elements of EnumStrings<Aidge::DataType>::data below
/**
* @addtogroup constructor from DataType would probably be safer with appropriate
* mapping function from DataType to EnumStrings<Aidge::DataType>::data element
*/
constexpr Data(const char* type) noexcept : mType(type){};
private:
const char* mType;
};
// required though pure virtual
// inline to avoid redefinition
inline Data::~Data() noexcept = default;
namespace detail
{
template<typename T> struct NativeType
{
// generic case not implemented;
static_assert(!std::is_same<T, T>::value, "NativeType not supported on given type");
};
template<> struct NativeType<double>
{
static constexpr DataType aidgeType = DataType::Float64;
};
template<> struct NativeType<float>
{
static constexpr DataType aidgeType = DataType::Float32;
};
template<> struct NativeType<half_float::half>
{
static constexpr DataType aidgeType = DataType::Float32;
};
template<> struct NativeType<std::int8_t>
{
static constexpr DataType aidgeType = DataType::Int8;
};
template<> struct NativeType<std::uint8_t>
{
static constexpr DataType aidgeType = DataType::UInt8;
};
template<> struct NativeType<std::int16_t>
{
static constexpr DataType aidgeType = DataType::Int16;
};
template<> struct NativeType<std::uint16_t>
{
static constexpr DataType aidgeType = DataType::UInt16;
};
template<> struct NativeType<std::int32_t>
{
static constexpr DataType aidgeType = DataType::Int32;
};
template<> struct NativeType<std::uint32_t>
{
static constexpr DataType aidgeType = DataType::UInt32;
};
template<> struct NativeType<std::int64_t>
{
static constexpr DataType aidgeType = DataType::Int64;
};
template<> struct NativeType<std::uint64_t>
{
static constexpr DataType aidgeType = DataType::UInt64;
};
template<typename T> constexpr DataType NativeType_v = NativeType<T>::aidgeType;
template<DataType E> struct CppType
{
// generic case not implemented;
static_assert(!(E == E), "CppType not supported on given aidge type");
};
template<> struct CppType<DataType::Float64>
{
using type = double;
};
template<> struct CppType<DataType::Float32>
{
using type = float;
};
template<> struct CppType<DataType::Float16>
{
using type = half_float::half;
};
template<> struct CppType<DataType::Int8>
{
using type = std::int8_t;
};
template<> struct CppType<DataType::UInt8>
{
using type = std::uint8_t;
};
template<> struct CppType<DataType::Int16>
{
using type = std::int16_t;
};
template<> struct CppType<DataType::UInt16>
{
using type = std::uint16_t;
};
template<> struct CppType<DataType::Int32>
{
using type = std::int32_t;
};
template<> struct CppType<DataType::UInt32>
{
using type = std::uint32_t;
};
template<> struct CppType<DataType::Int64>
{
using type = std::int64_t;
};
template<> struct CppType<DataType::UInt64>
{
using type = std::uint64_t;
};
template<DataType E> using CppType_t = typename CppType<E>::type;
constexpr std::size_t sizeOf(DataType const i_dataType)
{
switch (i_dataType)
{
case DataType::Float64:
{
return sizeof(CppType_t<DataType::Float64>);
break;
}
case DataType::Float32:
{
return sizeof(CppType_t<DataType::Float32>);
break;
}
case DataType::Float16:
{
return sizeof(CppType_t<DataType::Float16>);
break;
}
case DataType::Int8:
{
return sizeof(CppType_t<DataType::Int8>);
break;
}
case DataType::UInt8:
{
return sizeof(CppType_t<DataType::UInt8>);
break;
}
case DataType::Int16:
{
return sizeof(CppType_t<DataType::Int16>);
break;
}
case DataType::UInt16:
{
return sizeof(CppType_t<DataType::UInt16>);
break;
}
case DataType::Int32:
{
return sizeof(CppType_t<DataType::Int32>);
break;
}
case DataType::UInt32:
{
return sizeof(CppType_t<DataType::UInt32>);
break;
}
case DataType::Int64:
{
return sizeof(CppType_t<DataType::Int64>);
break;
}
case DataType::UInt64:
{
return sizeof(CppType_t<DataType::UInt64>);
break;
}
default:
{
assert(false && "sizeOf called on unrecognized data type");
return 0;
}
}
}
namespace {
template <typename T> struct NativeType { static const Aidge::DataType type; };
template <> const Aidge::DataType NativeType<double>::type = Aidge::DataType::Float64;
template <> const Aidge::DataType NativeType<float>::type = Aidge::DataType::Float32;
template <> const Aidge::DataType NativeType<half_float::half>::type = Aidge::DataType::Float16;
template <> const Aidge::DataType NativeType<int8_t>::type = Aidge::DataType::Int8;
template <> const Aidge::DataType NativeType<int16_t>::type = Aidge::DataType::Int16;
template <> const Aidge::DataType NativeType<int32_t>::type = Aidge::DataType::Int32;
template <> const Aidge::DataType NativeType<int64_t>::type = Aidge::DataType::Int64;
template <> const Aidge::DataType NativeType<uint8_t>::type = Aidge::DataType::UInt8;
template <> const Aidge::DataType NativeType<uint16_t>::type = Aidge::DataType::UInt16;
template <> const Aidge::DataType NativeType<uint32_t>::type = Aidge::DataType::UInt32;
template <> const Aidge::DataType NativeType<uint64_t>::type = Aidge::DataType::UInt64;
} // namespace detail
} // namespace Aidge
template <>
/// @todo possibly move all EnumStrings to Aidge::detail, see aidge#24
namespace
{
template<>
const char* const EnumStrings<Aidge::DataType>::data[]
= {"Float64", "Float32", "Float16", "BFloat16", "Binary", "Ternary",
"Int2", "Int3", "Int4", "Int5", "Int6", "Int7", "Int8", "Int16",
"Int32", "Int64", "UInt2", "UInt3", "UInt4", "UInt5", "UInt6",
"UInt7", "UInt8", "UInt16", "UInt32", "UInt64"};
}
= {"Float64", "Float32", "Float16", "BFloat16", "Binary", "Ternary", "Int2",
"Int3", "Int4", "Int5", "Int6", "Int7", "Int8", "Int16",
"Int32", "Int64", "UInt2", "UInt3", "UInt4", "UInt5", "UInt6",
"UInt7", "UInt8", "UInt16", "UInt32", "UInt64"};
}
#endif /* AIDGE_DATA_H_ */
\ No newline at end of file
Loading