Poplar and PopLibs
TypeTraits.hpp
1// Copyright (c) 2017 Graphcore Ltd. All rights reserved.
2
3#ifndef poplar_TypeTraits_hpp
4#define poplar_TypeTraits_hpp
5
6#include <cstdint>
7#include <type_traits>
8#include <vector>
9
10namespace poplar {
11
12// A half type internal to Poplar.
13class IeeeHalf;
14
15// A quarter type internal to Poplar.
16class Quarter;
17
22struct TypeTraits {
23
24 std::size_t size;
25 std::size_t align;
26 bool isIntegral;
27 bool isFloat;
28 bool isSigned;
29
30 template <typename T> static TypeTraits make();
31
35 template <typename T> static constexpr bool isSimpleType();
36
37 template <typename T> static constexpr bool requiresMetadata();
38
39 bool isSimpleType() const { return isIntegral || isFloat; }
40};
41
42template <typename T> TypeTraits TypeTraits::make() {
43 TypeTraits t;
44 t.size = sizeof(T);
45 t.align = std::alignment_of<T>::value;
46 t.isIntegral = std::is_integral<T>::value;
47 t.isFloat = std::is_floating_point<T>::value;
48 t.isSigned = std::is_signed<T>::value;
49 return t;
50}
51
52template <> inline TypeTraits TypeTraits::make<IeeeHalf>() {
53 TypeTraits t;
54 t.size = sizeof(uint16_t);
55 t.align = std::alignment_of<uint16_t>::value;
56 t.isIntegral = false;
57 t.isFloat = true;
58 t.isSigned = true;
59 return t;
60}
61
62template <> inline TypeTraits TypeTraits::make<Quarter>() {
63 TypeTraits t;
64 t.size = sizeof(uint8_t);
65 t.align = std::alignment_of<uint8_t>::value;
66 t.isIntegral = false;
67 t.isFloat = true;
68 t.isSigned = true;
69 return t;
70}
71
72template <typename T> constexpr bool TypeTraits::isSimpleType() {
73 return std::is_integral<T>::value || std::is_floating_point<T>::value;
74}
75
76template <> constexpr bool TypeTraits::isSimpleType<IeeeHalf>() { return true; }
77
78template <> constexpr bool TypeTraits::isSimpleType<Quarter>() { return true; }
79
80template <typename T> constexpr bool TypeTraits::requiresMetadata() {
81 return false;
82}
83
84template <> constexpr bool TypeTraits::requiresMetadata<Quarter>() {
85 return true;
86}
87
88} // end namespace poplar
89
90#endif // poplar_TypeTraits_hpp
Poplar classes and functions.
Definition: ArrayRef.hpp:14
A structure to provide information about arithmetic (integer and floating point) types.
Definition: TypeTraits.hpp:22
static constexpr bool isSimpleType()
Test if argument is a basic numeric type.
Definition: TypeTraits.hpp:72