"src/targets/vscode:/vscode.git/clone" did not exist on "231d60a22b42ad53fa6d215f982c7e8d14e998c7"
shape.hpp 3.24 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
#ifndef GUARD_RTGLIB_SHAPE_HPP
#define GUARD_RTGLIB_SHAPE_HPP

#include <vector>
#include <cassert>
Paul's avatar
Paul committed
6
#include <ostream>
Paul's avatar
Paul committed
7

Paul's avatar
Paul committed
8
9
10
11
namespace rtg {

struct shape
{
Paul's avatar
Paul committed
12
13

// Add new types here
Paul's avatar
Paul committed
14
// clang-format off
Paul's avatar
Paul committed
15
16
#define RTG_SHAPE_VISIT_TYPES(m) \
    m(float_type, float) \
Paul's avatar
Paul committed
17
18
19
20
21
22
23
24
    m(double_type, double) \
    m(uint8_type, uint8_t) \
    m(int8_type, int8_t) \
    m(uint16_type, uint16_t) \
    m(int16_type, int16_t) \
    m(int32_type, int32_t) \
    m(int64_type, int64_t) \
    m(uint32_type, uint32_t) \
Paul's avatar
Paul committed
25
    m(uint64_type, uint64_t)
Paul's avatar
Paul committed
26
// clang-format on
Paul's avatar
Paul committed
27

Paul's avatar
Paul committed
28
#define RTG_SHAPE_ENUM_TYPES(x, t) x,
Paul's avatar
Paul committed
29
30
    enum type_t
    {
Paul's avatar
Paul committed
31
        RTG_SHAPE_VISIT_TYPES(RTG_SHAPE_ENUM_TYPES)
Paul's avatar
Paul committed
32
    };
Paul's avatar
Paul committed
33
34
#undef RTG_SHAPE_ENUM_TYPES

Paul's avatar
Paul committed
35
    template <class T, class = void>
Paul's avatar
Paul committed
36
    struct get_type;
Paul's avatar
Paul committed
37
38
#define RTG_SHAPE_GET_TYPE(x, t)                              \
    template <class T>                                        \
Paul's avatar
Paul committed
39
    struct get_type<t, T> : std::integral_constant<type_t, x> \
Paul's avatar
Paul committed
40
41
    {                                                         \
    };
Paul's avatar
Paul committed
42
43
    RTG_SHAPE_VISIT_TYPES(RTG_SHAPE_GET_TYPE)
#undef RTG_SHAPE_GET_TYPE
Paul's avatar
Paul committed
44
45
46
47
48
49
50

    shape();
    shape(type_t t);
    shape(type_t t, std::vector<std::size_t> l);
    shape(type_t t, std::vector<std::size_t> l, std::vector<std::size_t> s);

    type_t type() const;
Paul's avatar
Paul committed
51
52
    const std::vector<std::size_t>& lens() const;
    const std::vector<std::size_t>& strides() const;
Paul's avatar
Paul committed
53
54
55
    std::size_t elements() const;
    std::size_t bytes() const;

Paul's avatar
Paul committed
56
57
58
    std::size_t index(std::initializer_list<std::size_t> l) const;
    std::size_t index(const std::vector<std::size_t>& l) const;

Paul's avatar
Paul committed
59
60
61
62
63
    // Map element index to space index
    std::size_t index(std::size_t i) const;

    bool packed() const;

Paul's avatar
Paul committed
64
65
    friend bool operator==(const shape& x, const shape& y);
    friend bool operator!=(const shape& x, const shape& y);
Paul's avatar
Paul committed
66
    friend std::ostream& operator<<(std::ostream& os, const shape& x);
Paul's avatar
Paul committed
67

Paul's avatar
Paul committed
68
    template <class T>
Paul's avatar
Paul committed
69
70
71
72
    struct as
    {
        using type = T;

Paul's avatar
Paul committed
73
        template <class U>
Paul's avatar
Paul committed
74
75
76
77
78
        T operator()(U u) const
        {
            return T(u);
        }

Paul's avatar
Paul committed
79
        template <class U>
Paul's avatar
Paul committed
80
81
82
83
84
        T* operator()(U* u) const
        {
            return static_cast<T*>(u);
        }

Paul's avatar
Paul committed
85
        template <class U>
Paul's avatar
Paul committed
86
87
88
89
90
        const T* operator()(const U* u) const
        {
            return static_cast<T*>(u);
        }

Paul's avatar
Paul committed
91
        T operator()() const { return {}; }
Paul's avatar
Paul committed
92

Paul's avatar
Paul committed
93
        std::size_t size(std::size_t n = 1) const { return sizeof(T) * n; }
Paul's avatar
Paul committed
94

Paul's avatar
Paul committed
95
96
        template <class U>
        T* from(U* buffer, std::size_t n = 0) const
Paul's avatar
Paul committed
97
        {
Paul's avatar
Paul committed
98
            return reinterpret_cast<T*>(buffer) + n;
Paul's avatar
Paul committed
99
        }
Paul's avatar
Paul committed
100

Paul's avatar
Paul committed
101
102
        template <class U>
        const T* from(const U* buffer, std::size_t n = 0) const
Paul's avatar
Paul committed
103
        {
Paul's avatar
Paul committed
104
            return reinterpret_cast<const T*>(buffer) + n;
Paul's avatar
Paul committed
105
        }
Paul's avatar
Paul committed
106
107
    };

Paul's avatar
Paul committed
108
    template <class Visitor>
Paul's avatar
Paul committed
109
110
    void visit_type(Visitor v) const
    {
Paul's avatar
Paul committed
111
        switch(this->type_)
Paul's avatar
Paul committed
112
        {
Paul's avatar
Paul committed
113
#define RTG_SHAPE_VISITOR_CASE(x, t) \
Paul's avatar
Paul committed
114
    case x: v(as<t>()); return;
Paul's avatar
Paul committed
115
116
            RTG_SHAPE_VISIT_TYPES(RTG_SHAPE_VISITOR_CASE)
#undef RTG_SHAPE_VISITOR_CASE
Paul's avatar
Paul committed
117
118
119
        }
        assert(true);
    }
Paul's avatar
Paul committed
120
121

    private:
Paul's avatar
Paul committed
122
123
124
    type_t type_;
    std::vector<std::size_t> lens_;
    std::vector<std::size_t> strides_;
Paul's avatar
Paul committed
125
    bool packed_;
Paul's avatar
Paul committed
126
127
128

    void calculate_strides();
    std::size_t element_space() const;
Paul's avatar
Paul committed
129
    std::string type_string() const;
Paul's avatar
Paul committed
130
131
};

Paul's avatar
Paul committed
132
} // namespace rtg
Paul's avatar
Paul committed
133
134

#endif