blob: f5a282df8ad15f18e7a2ba6dfadbb3f4cd08b5f9 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 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#include "arm_compute/core/SubTensorInfo.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/Validate.h"
29
30using namespace arm_compute;
31
32SubTensorInfo::SubTensorInfo()
33 : _parent(nullptr), _tensor_shape(), _coords(), _valid_region{ Coordinates(), _tensor_shape }
34{
35}
36
37SubTensorInfo::SubTensorInfo(ITensorInfo *parent, const TensorShape &tensor_shape, const Coordinates &coords)
38 : _parent(parent), _tensor_shape(tensor_shape), _coords(coords), _valid_region{ Coordinates(), _tensor_shape }
39{
40 ARM_COMPUTE_ERROR_ON(parent == nullptr);
41 ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR(parent->tensor_shape(), coords, tensor_shape);
42
43 // Initialize valid region
44 Coordinates coordinates;
45 coordinates.set_num_dimensions(_tensor_shape.num_dimensions());
46 _valid_region = ValidRegion{ coordinates, _tensor_shape };
47}
48
49void SubTensorInfo::set_tensor_shape(TensorShape shape)
50{
51 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
52 ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR(_parent->tensor_shape(), _coords, shape);
53 _tensor_shape = shape;
54}
55
56bool SubTensorInfo::extend_padding(const PaddingSize &padding)
57{
58 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
59 ARM_COMPUTE_ERROR_ON(!_parent->is_resizable());
60
61 // Extend parent padding if required
62 return _parent->extend_padding(padding);
63}
64
65size_t SubTensorInfo::offset_element_in_bytes(const Coordinates &pos) const
66{
67 ARM_COMPUTE_ERROR_ON_COORDINATES_DIMENSIONS_GTE(pos, _tensor_shape.num_dimensions());
68
69 size_t offset = offset_first_element_in_bytes();
70 const Strides &strides = strides_in_bytes();
71
72 for(size_t i = 0; i < _tensor_shape.num_dimensions(); ++i)
73 {
74 offset += pos[i] * strides[i];
75 }
76
77 return offset;
78}