blob: 878283bd8e15fc9f751dcf46b0c05c51117d769b [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);
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010041 // Check if subtensor is valid if parent is configured
42 if(parent->tensor_shape().total_size() != 0)
43 {
44 ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR(parent->tensor_shape(), coords, tensor_shape);
45 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046
47 // Initialize valid region
48 Coordinates coordinates;
49 coordinates.set_num_dimensions(_tensor_shape.num_dimensions());
50 _valid_region = ValidRegion{ coordinates, _tensor_shape };
51}
52
53void SubTensorInfo::set_tensor_shape(TensorShape shape)
54{
55 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
Georgios Pinitase2c82fe2017-10-02 18:51:47 +010056 // Check if subtensor is valid if parent is configured
57 if(_parent->tensor_shape().total_size() != 0)
58 {
59 ARM_COMPUTE_ERROR_ON_INVALID_SUBTENSOR(_parent->tensor_shape(), _coords, shape);
60 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061 _tensor_shape = shape;
62}
63
64bool SubTensorInfo::extend_padding(const PaddingSize &padding)
65{
66 ARM_COMPUTE_ERROR_ON(_parent == nullptr);
67 ARM_COMPUTE_ERROR_ON(!_parent->is_resizable());
68
69 // Extend parent padding if required
70 return _parent->extend_padding(padding);
71}
72
73size_t SubTensorInfo::offset_element_in_bytes(const Coordinates &pos) const
74{
75 ARM_COMPUTE_ERROR_ON_COORDINATES_DIMENSIONS_GTE(pos, _tensor_shape.num_dimensions());
76
77 size_t offset = offset_first_element_in_bytes();
78 const Strides &strides = strides_in_bytes();
79
80 for(size_t i = 0; i < _tensor_shape.num_dimensions(); ++i)
81 {
82 offset += pos[i] * strides[i];
83 }
84
85 return offset;
86}