blob: 4aac0e29c0d7ca37c047f7be278f743465d6eb8b [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2019 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_TEST_RAW_TENSOR_H
25#define ARM_COMPUTE_TEST_RAW_TENSOR_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010027#include "tests/SimpleTensor.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028
29namespace arm_compute
30{
31namespace test
32{
Moritz Pflanzer82e70a12017-08-08 16:20:45 +010033/** Subclass of SimpleTensor using uint8_t as value type.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034 *
Moritz Pflanzer82e70a12017-08-08 16:20:45 +010035 * Access operations (except for operator[]) will be based on the data type to
36 * copy the right number of elements.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037 */
Moritz Pflanzer82e70a12017-08-08 16:20:45 +010038class RawTensor : public SimpleTensor<uint8_t>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039{
40public:
41 /** Create an uninitialised tensor of the given @p shape and @p format.
42 *
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010043 * @param[in] shape Shape of the new raw tensor.
44 * @param[in] format Format of the new raw tensor.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010045 */
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010046 RawTensor(TensorShape shape, Format format);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047
48 /** Create an uninitialised tensor of the given @p shape and @p data type.
49 *
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010050 * @param[in] shape Shape of the new raw tensor.
51 * @param[in] data_type Data type of the new raw tensor.
52 * @param[in] num_channels (Optional) Number of channels (default = 1).
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053 */
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010054 RawTensor(TensorShape shape, DataType data_type, int num_channels = 1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010056 /** Conversion constructor from SimpleTensor.
57 *
58 * The passed SimpleTensor will be destroyed after it has been converted to
59 * a RawTensor.
60 *
61 * @param[in,out] tensor SimpleTensor to be converted to a RawTensor.
62 */
63 template <typename T>
64 RawTensor(SimpleTensor<T> &&tensor)
65 {
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010066 _buffer = std::unique_ptr<uint8_t[]>(reinterpret_cast<uint8_t *>(tensor._buffer.release()));
67 _shape = std::move(tensor._shape);
68 _format = tensor._format;
69 _data_type = tensor._data_type;
70 _num_channels = tensor._num_channels;
71 _data_layout = tensor._data_layout;
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010072 }
73
74 /** Conversion operator to SimpleTensor.
75 *
76 * The current RawTensor must not be used after the conversion.
77 *
78 * @return SimpleTensor of the given type.
79 */
80 template <typename T>
81 operator SimpleTensor<T>()
82 {
83 SimpleTensor<T> cast;
Vidhya Sudhan Loganathan014333d2018-07-02 09:13:49 +010084 cast._buffer = std::unique_ptr<T[]>(reinterpret_cast<T *>(_buffer.release()));
85 cast._shape = std::move(_shape);
86 cast._format = _format;
87 cast._data_type = _data_type;
88 cast._num_channels = _num_channels;
89 cast._data_layout = _data_layout;
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010090
91 return cast;
92 }
93
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094 /** Create a deep copy of the given @p tensor.
95 *
96 * @param[in] tensor To be copied tensor.
97 */
98 RawTensor(const RawTensor &tensor);
99
Alex Gildayc357c472018-03-21 13:54:09 +0000100 /** Copy the given @p tensor.
101 *
102 * @param[in] tensor To be copied tensor.
103 *
104 * @return a copy of the given tensor.
105 */
106 RawTensor &operator=(RawTensor tensor);
107 /** Allow instances of this class to be move constructed */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108 RawTensor(RawTensor &&) = default;
Alex Gildayc357c472018-03-21 13:54:09 +0000109 /** Default destructor. */
110 ~RawTensor() = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112 /** Read only access to the specified element.
113 *
114 * @param[in] coord Coordinates of the desired element.
115 *
116 * @return A pointer to the desired element.
117 */
Moritz Pflanzer82e70a12017-08-08 16:20:45 +0100118 const void *operator()(const Coordinates &coord) const override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119
120 /** Access to the specified element.
121 *
122 * @param[in] coord Coordinates of the desired element.
123 *
124 * @return A pointer to the desired element.
125 */
Moritz Pflanzer82e70a12017-08-08 16:20:45 +0100126 void *operator()(const Coordinates &coord) override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127};
128} // namespace test
129} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000130#endif /* ARM_COMPUTE_TEST_RAW_TENSOR_H */