blob: 20824dfc8b44b19d7781926a0fc3ee56a5c993ce [file] [log] [blame]
Georgios Pinitas1d480652019-01-23 11:24:50 +00001/*
2 * Copyright (c) 2019 Arm Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#pragma once
25
26#include <algorithm>
27#include <initializer_list>
28
29namespace arm_gemm {
30
31template<unsigned int D>
32class NDRange {
33private:
34 unsigned int m_sizes[D];
35 unsigned int m_totalsizes[D];
36
37 class NDRangeIterator {
38 private:
39 const NDRange &m_parent;
40 unsigned int m_pos = 0;
41 unsigned int m_end = 0;
42
43 public:
44 NDRangeIterator(const NDRange &p, unsigned int s, unsigned int e) : m_parent(p), m_pos(s), m_end(e) { }
45
46 bool done() const {
47 return (m_pos >= m_end);
48 }
49
50 unsigned int dim(unsigned int d) const {
51 unsigned int r = m_pos;
52
53 if (d < (D - 1)) {
54 r %= m_parent.m_totalsizes[d];
55 }
56
57 if (d > 0) {
58 r /= m_parent.m_totalsizes[d-1];
59 }
60
61 return r;
62 }
63
64 bool next_dim0() {
65 m_pos++;
66
67 return !done();
68 }
69
70 bool next_dim1() {
71 m_pos += m_parent.m_sizes[0] - dim(0);
72
73 return !done();
74 }
75
76 unsigned int dim0_max() const {
77 unsigned int offset = std::min(m_end - m_pos, m_parent.m_sizes[0] - dim(0));
78
79 return dim(0) + offset;
80 }
81 };
82
83public:
84 template <typename... T>
85 NDRange(T... ts) : m_sizes{ts...} {
86 unsigned int t=1;
87
88 for (unsigned int i=0; i<D; i++) {
89 t *= m_sizes[i];
90
91 m_totalsizes[i] = t;
92 }
93 }
94
95 NDRangeIterator iterator(unsigned int start, unsigned int end) const {
96 return NDRangeIterator(*this, start, end);
97 }
98
99 unsigned int total_size() const {
100 return m_totalsizes[D - 1];
101 }
102
103 unsigned int get_size(unsigned int v) const {
104 return m_sizes[v];
105 }
106};
107
108} // namespace arm_gemm