Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 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 | #ifndef __ARM_COMPUTE_TEST_TENSOR_H__ |
| 25 | #define __ARM_COMPUTE_TEST_TENSOR_H__ |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | |
| 30 | namespace arm_compute |
| 31 | { |
| 32 | namespace test |
| 33 | { |
| 34 | namespace validation |
| 35 | { |
| 36 | template <typename T> |
| 37 | class Tensor |
| 38 | { |
| 39 | public: |
| 40 | Tensor() |
| 41 | : _shape(), _dt(DataType::UNKNOWN), _fixed_point_position(0), _ptr(nullptr), _ptr_const(nullptr) {}; |
| 42 | |
| 43 | Tensor(TensorShape shape, DataType dt, int fixed_point_position, T *ptr) |
| 44 | : _shape(shape), _dt(dt), _fixed_point_position(fixed_point_position), _ptr(ptr), _ptr_const(nullptr) {}; |
| 45 | |
| 46 | Tensor(TensorShape shape, DataType dt, int fixed_point_position, const T *ptr) |
| 47 | : _shape(shape), _dt(dt), _fixed_point_position(fixed_point_position), _ptr(nullptr), _ptr_const(ptr) {}; |
| 48 | |
| 49 | Tensor(const Tensor &tensor) = delete; |
| 50 | Tensor &operator=(const Tensor &) = delete; |
| 51 | Tensor(Tensor &&) = default; |
| 52 | Tensor &operator=(Tensor &&) = default; |
| 53 | |
| 54 | ~Tensor() = default; |
| 55 | |
| 56 | T &operator[](size_t offset) |
| 57 | { |
Moritz Pflanzer | e3bc5c9 | 2017-08-02 11:38:42 +0100 | [diff] [blame] | 58 | ARM_COMPUTE_ERROR_ON(_ptr == nullptr); |
| 59 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 60 | return _ptr[offset]; |
| 61 | } |
| 62 | |
| 63 | const T &operator[](size_t offset) const |
| 64 | { |
Moritz Pflanzer | e3bc5c9 | 2017-08-02 11:38:42 +0100 | [diff] [blame] | 65 | const T *ptr = (_ptr_const != nullptr) ? _ptr_const : _ptr; |
| 66 | |
| 67 | ARM_COMPUTE_ERROR_ON(ptr == nullptr); |
| 68 | |
Giorgio Arena | fc2817d | 2017-06-27 17:26:37 +0100 | [diff] [blame] | 69 | return ptr[offset]; // NOLINT |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | int num_elements() const |
| 73 | { |
| 74 | return std::accumulate(_shape.cbegin(), _shape.cend(), 1, std::multiplies<int>()); |
| 75 | } |
| 76 | |
| 77 | TensorShape shape() const |
| 78 | { |
| 79 | return _shape; |
| 80 | } |
| 81 | |
| 82 | DataType data_type() const |
| 83 | { |
| 84 | return _dt; |
| 85 | } |
| 86 | |
| 87 | int fixed_point_position() const |
| 88 | { |
| 89 | return _fixed_point_position; |
| 90 | } |
| 91 | |
| 92 | const T *data() const |
| 93 | { |
Moritz Pflanzer | e3bc5c9 | 2017-08-02 11:38:42 +0100 | [diff] [blame] | 94 | return (_ptr_const != nullptr) ? _ptr_const : _ptr; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 95 | } |
Moritz Pflanzer | e3bc5c9 | 2017-08-02 11:38:42 +0100 | [diff] [blame] | 96 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 97 | T *data() |
| 98 | { |
| 99 | return _ptr; |
| 100 | } |
| 101 | |
Moritz Pflanzer | e3bc5c9 | 2017-08-02 11:38:42 +0100 | [diff] [blame] | 102 | const T *data_const() const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | { |
| 104 | return _ptr_const; |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | TensorShape _shape; |
| 109 | DataType _dt; |
| 110 | int _fixed_point_position; |
| 111 | T *_ptr; |
| 112 | const T *_ptr_const; |
| 113 | }; |
| 114 | } // namespace validation |
| 115 | } // test |
| 116 | } // arm_compute |
| 117 | |
| 118 | #endif /* __ARM_COMPUTE_TEST_TENSOR_H__ */ |