blob: 4c558bfae9bff4de4b6e7cf5b4bfbdc774ff1aac [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"
Georgios Pinitas283c1792017-11-10 18:14:06 +000029#include "support/ToolchainSupport.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030
31using namespace arm_compute;
32
33SubTensorInfo::SubTensorInfo()
34 : _parent(nullptr), _tensor_shape(), _coords(), _valid_region{ Coordinates(), _tensor_shape }
35{
36}
37
Georgios Pinitas283c1792017-11-10 18:14:06 +000038SubTensorInfo::SubTensorInfo(ITensorInfo *parent, TensorShape tensor_shape, Coordinates coords)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039 : _parent(parent), _tensor_shape(tensor_shape), _coords(coords), _valid_region{ Coordinates(), _tensor_shape }
40{
41 ARM_COMPUTE_ERROR_ON(parent == nullptr);
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010042 // Check if subtensor is valid if parent is configured
43 if(parent->tensor_shape().total_size() != 0)
44 {
45 ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR(parent->tensor_shape(), coords, tensor_shape);
46 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047
48 // Initialize valid region
49 Coordinates coordinates;
50 coordinates.set_num_dimensions(_tensor_shape.num_dimensions());
51 _valid_region = ValidRegion{ coordinates, _tensor_shape };
52}
53
Georgios Pinitas283c1792017-11-10 18:14:06 +000054std::unique_ptr<ITensorInfo> SubTensorInfo::clone() const
55{
56 return support::cpp14::make_unique<SubTensorInfo>(*this);
57}
58
59ITensorInfo &SubTensorInfo::set_tensor_shape(TensorShape shape)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010060{
61 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010062 // Check if subtensor is valid if parent is configured
63 if(_parent->tensor_shape().total_size() != 0)
64 {
65 ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR(_parent->tensor_shape(), _coords, shape);
66 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067 _tensor_shape = shape;
Georgios Pinitas283c1792017-11-10 18:14:06 +000068 return *this;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069}
70
71bool SubTensorInfo::extend_padding(const PaddingSize &padding)
72{
73 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
74 ARM_COMPUTE_ERROR_ON(!_parent->is_resizable());
75
76 // Extend parent padding if required
77 return _parent->extend_padding(padding);
78}
79
80size_t SubTensorInfo::offset_element_in_bytes(const Coordinates &pos) const
81{
82 ARM_COMPUTE_ERROR_ON_COORDINATES_DIMENSIONS_GTE(pos, _tensor_shape.num_dimensions());
83
84 size_t offset = offset_first_element_in_bytes();
85 const Strides &strides = strides_in_bytes();
86
87 for(size_t i = 0; i < _tensor_shape.num_dimensions(); ++i)
88 {
89 offset += pos[i] * strides[i];
90 }
91
92 return offset;
93}