2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @brief This file contains utility macro
22 #ifndef __ONERT_UTIL_UTILS_H__
23 #define __ONERT_UTIL_UTILS_H__
25 #include "ir/Coordinates.h"
28 #define UNUSED_RELEASE(a) (void)(a)
30 template <size_t from, size_t to, typename Enable = void> struct ForEachDimension
33 static void unroll(const onert::ir::Shape &shape, onert::ir::Coordinates &coords,
36 static_assert(from < to, "from must not be less than to");
37 assert(static_cast<int>(to) <= shape.rank());
38 const auto &d = shape.dim(from);
40 for (auto v = 0; v < d; v++)
43 ForEachDimension<from + 1, to>::unroll(shape, coords, lambda_function);
48 template <size_t from, size_t to>
49 struct ForEachDimension<from, to, typename std::enable_if<from == to>::type>
52 static void unroll(const onert::ir::Shape &shape, onert::ir::Coordinates &coords,
55 UNUSED_RELEASE(shape);
56 assert(static_cast<int>(to) <= shape.rank());
57 lambda_function(coords);
61 template <typename L> inline void ShapeLoop(const onert::ir::Shape &shape, L lambda_function)
63 assert(shape.rank() > 0);
64 for (auto i = 0; i < shape.rank(); ++i)
66 assert(shape.dim(i) > 0);
69 onert::ir::Coordinates coords;
74 ForEachDimension<0, 0>::unroll(shape, coords, lambda_function);
77 ForEachDimension<0, 1>::unroll(shape, coords, lambda_function);
80 ForEachDimension<0, 2>::unroll(shape, coords, lambda_function);
83 ForEachDimension<0, 3>::unroll(shape, coords, lambda_function);
86 ForEachDimension<0, 4>::unroll(shape, coords, lambda_function);
89 ForEachDimension<0, 5>::unroll(shape, coords, lambda_function);
92 ForEachDimension<0, 6>::unroll(shape, coords, lambda_function);
95 assert(false && "ShapeLoop, 1 <= Shape'rank <= 6");
99 #endif // __ONERT_UTIL_UTILS_H__