ONE - On-device Neural Engine
Loading...
Searching...
No Matches
IndexEnumerator.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
18
19#include <cassert>
20
22
23inline uint32_t axis_of(const Shape &shape, uint32_t cursor)
24{
25 const uint32_t rank = shape.rank();
26 assert(cursor < rank);
27 return rank - cursor - 1;
28}
29
30namespace nncc
31{
32namespace core
33{
34namespace ADT
35{
36namespace tensor
37{
38
39IndexEnumerator::IndexEnumerator(const Shape &shape) : _shape{shape}, _cursor(0)
40{
41 const uint32_t rank = _shape.rank();
42
43 // Initialize _index
44 _index.resize(rank);
45 for (uint32_t axis = 0; axis < rank; ++axis)
46 {
47 _index.at(axis) = 0;
48 }
49
50 // Initialize _cursor
51 for (_cursor = 0; _cursor < rank; ++_cursor)
52 {
53 const auto axis = axis_of(_shape, _cursor);
54
55 if (_index.at(axis) < _shape.dim(axis))
56 {
57 break;
58 }
59 }
60}
61
63{
64 const uint32_t rank = _shape.rank();
65
66 // Find axis to be updated
67 while (_cursor < rank)
68 {
69 const auto axis = axis_of(_shape, _cursor);
70
71 if ((_index.at(axis)) + 1 < _shape.dim(axis))
72 {
73 break;
74 }
75
76 ++_cursor;
77 }
78
79 if (_cursor == rank)
80 {
81 return;
82 }
83
84 // Update index
85 _index.at(axis_of(_shape, _cursor)) += 1;
86
87 for (uint32_t pos = 0; pos < _cursor; ++pos)
88 {
89 const auto axis = axis_of(_shape, pos);
90 _index.at(axis) = 0;
91 }
92
93 // Reset cursor
94 _cursor = 0;
95}
96
97} // namespace tensor
98} // namespace ADT
99} // namespace core
100} // namespace nncc
Index & resize(uint32_t size)
Definition Index.cpp:37
uint32_t & at(uint32_t axis)
Definition Index.cpp:49
uint32_t & dim(uint32_t axis)
Definition Shape.cpp:42
uint32_t rank(void) const
Definition Shape.cpp:35
uint32_t axis_of(const Shape &shape, uint32_t cursor)