Viet-Hoa Do | cd1f03e | 2023-09-19 16:41:34 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2023 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 | |
| 25 | #ifndef CKW_SRC_TILEVIEW_H |
| 26 | #define CKW_SRC_TILEVIEW_H |
| 27 | |
| 28 | #include "ckw/Error.h" |
| 29 | #include "ckw/types/DataType.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 30 | |
Viet-Hoa Do | cd1f03e | 2023-09-19 16:41:34 +0100 | [diff] [blame] | 31 | #include "src/ITile.h" |
| 32 | |
| 33 | #include <cstdint> |
| 34 | |
| 35 | namespace ckw |
| 36 | { |
| 37 | |
| 38 | /** A rectangular active area of a tile. */ |
| 39 | class TileArea |
| 40 | { |
| 41 | public: |
| 42 | /** Create a new tile rectangular active area. |
| 43 | * |
| 44 | * The range of rows and columns is defined by pairs of start and end indices, inclusive lower and exclusive upper. |
| 45 | * In other word, any row and column indices satisfied the following conditions will be part of the active area: |
| 46 | * |
| 47 | * row_start <= row_index < row_end |
| 48 | * col_start <= col_index < col_end |
| 49 | * |
| 50 | * @param[in] row_start The start index of the row range. |
| 51 | * @param[in] row_end The end index of the row range. |
| 52 | * @param[in] col_start The start index of the column range. |
| 53 | * @param[in] col_end The end index of the column range. |
| 54 | */ |
| 55 | TileArea(int32_t row_start, int32_t row_end, int32_t col_start, int32_t col_end); |
| 56 | |
| 57 | /** Get the start row index. */ |
| 58 | int32_t row_start() const; |
| 59 | |
| 60 | /** Get the end row (exclusive) index. */ |
| 61 | int32_t row_end() const; |
| 62 | |
| 63 | /** Get the start column index. */ |
| 64 | int32_t col_start() const; |
| 65 | |
| 66 | /** Get the end column (exclusive) index. */ |
| 67 | int32_t col_end() const; |
| 68 | |
| 69 | private: |
| 70 | int32_t _row_start; |
| 71 | int32_t _row_end; |
| 72 | int32_t _col_start; |
| 73 | int32_t _col_end; |
| 74 | }; |
| 75 | |
| 76 | /** A rectangular view of a tile. */ |
| 77 | template <typename T> |
| 78 | class TileView |
| 79 | { |
| 80 | public: |
| 81 | /** Create a tile view that refers to the whole tile. |
| 82 | * |
| 83 | * @param[in] tile The tile object. |
| 84 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 85 | TileView(const T &tile) : _tile(&tile), _area(0, tile.info().height(), 0, tile.info().width()) |
Viet-Hoa Do | cd1f03e | 2023-09-19 16:41:34 +0100 | [diff] [blame] | 86 | { |
| 87 | } |
| 88 | |
| 89 | /** Create a new rectangular view of the given tile. |
| 90 | * |
| 91 | * @param[in] tile The tile object. |
| 92 | * @param[in] area The rectangular active area. |
| 93 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 94 | TileView(const T &tile, const TileArea &area) : _tile(&tile), _area(area) |
Viet-Hoa Do | cd1f03e | 2023-09-19 16:41:34 +0100 | [diff] [blame] | 95 | { |
| 96 | } |
| 97 | |
| 98 | /** Get the tile object. |
| 99 | * |
| 100 | * The caller must guarantee that the tile view refers to the whole tile. |
| 101 | */ |
| 102 | const T &full_tile() const |
| 103 | { |
| 104 | CKW_ASSERT(is_full_tile()); |
| 105 | |
| 106 | return *_tile; |
| 107 | } |
| 108 | |
| 109 | /** Get the data type of the tile. */ |
| 110 | DataType data_type() const |
| 111 | { |
| 112 | return _tile->info().data_type(); |
| 113 | } |
| 114 | |
| 115 | /** Get the start row index. */ |
| 116 | int32_t row_start() const |
| 117 | { |
| 118 | return _area.row_start(); |
| 119 | } |
| 120 | |
| 121 | /** Get the end row index. */ |
| 122 | int32_t row_end() const |
| 123 | { |
| 124 | return _area.row_end(); |
| 125 | } |
| 126 | |
| 127 | /** Get the start column index. */ |
| 128 | int32_t col_start() const |
| 129 | { |
| 130 | return _area.col_start(); |
| 131 | } |
| 132 | |
| 133 | /** Get the end column index. */ |
| 134 | int32_t col_end() const |
| 135 | { |
| 136 | return _area.col_end(); |
| 137 | } |
| 138 | |
| 139 | /** Get the height of the tile view. */ |
| 140 | int32_t height() const |
| 141 | { |
| 142 | return _area.row_end() - _area.row_start(); |
| 143 | } |
| 144 | |
| 145 | /** Get the width of the tile view. */ |
| 146 | int32_t width() const |
| 147 | { |
| 148 | return _area.col_end() - _area.col_start(); |
| 149 | } |
| 150 | |
| 151 | /** See @ref IVectorAccess::vector. */ |
| 152 | TileVariable vector(int32_t row) const |
| 153 | { |
| 154 | return _tile->vector(row_start() + row, col_start(), width()); |
| 155 | } |
| 156 | |
| 157 | /** See @ref IScalarAccess::scalar. */ |
| 158 | TileVariable scalar(int32_t row, int32_t col) const |
| 159 | { |
| 160 | return _tile->scalar(row_start() + row, col_start() + col); |
| 161 | } |
| 162 | |
| 163 | /** Get the name of the tile. */ |
| 164 | const std::string &name() const |
| 165 | { |
| 166 | return _tile->name(); |
| 167 | } |
| 168 | |
| 169 | /** Get whether the tile view is a scalar element. */ |
| 170 | bool is_scalar() const |
| 171 | { |
| 172 | return height() == 1 && width() == 1; |
| 173 | } |
| 174 | |
| 175 | /** Get whether the tile view refers to the whole tile. */ |
| 176 | bool is_full_tile() const |
| 177 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 178 | return row_start() == 0 && row_end() == _tile->info().height() && col_start() == 0 && |
| 179 | col_end() == _tile->info().width(); |
Viet-Hoa Do | cd1f03e | 2023-09-19 16:41:34 +0100 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | private: |
| 183 | const T *_tile; |
| 184 | TileArea _area; |
| 185 | }; |
| 186 | |
| 187 | } // namespace ckw |
| 188 | |
| 189 | #endif // CKW_SRC_TILEVIEW_H |