Imported Upstream version 1.4.0
[platform/core/ml/nnfw.git] / runtime / contrib / pure_arm_compute / src / internal / nnapi / feature / View.h
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @file    View.h
19  * @ingroup COM_AI_RUNTIME
20  * @brief   This file defines internal::nnapi::feature::View class
21  */
22 #ifndef __INTERNAL_NNAPI_FEATURE_VIEW_H__
23 #define __INTERNAL_NNAPI_FEATURE_VIEW_H__
24
25 #include "internal/nnapi/feature/Utils.h"
26
27 #include "misc/feature/Reader.h"
28
29 namespace internal
30 {
31 namespace nnapi
32 {
33 namespace feature
34 {
35
36 /**
37  * @brief   Class to access feature's element information using index
38  */
39 template <typename T> class View final : public nnfw::misc::feature::Reader<T>
40 {
41 public:
42   /**
43    * @brief     Construct a new View object
44    * @param[in] shape Shape of feature
45    * @param[in] ptr   Pointer to feature data
46    * @param[in] len   Size of feature (byte)
47    * @return
48    */
49   // NOTE The parameter len denotes the number of bytes.
50   View(const ::nnfw::misc::feature::Shape &shape, T *ptr, size_t len) : _shape{shape}, _ptr{ptr}
51   {
52     assert(shape.N * shape.C * shape.H * shape.W * sizeof(T) == len);
53   }
54
55 public:
56   /**
57    * @brief   Get shape of feature
58    * @return  Shape of feature
59    */
60   const nnfw::misc::feature::Shape &shape(void) const { return _shape; }
61
62 public:
63   /**
64    * @brief     Get value of element in 3D feature using channel, row, and column index
65    * @param[in] ch  Channel index
66    * @param[in] row Row index
67    * @param[in] col Column index
68    * @return    Value of element
69    */
70   T at(uint32_t ch, uint32_t row, uint32_t col) const override
71   {
72     uint32_t index = index_of(_shape, ch, row, col);
73
74     return _ptr[index];
75   }
76
77   /**
78    * @brief     Get value of element in 4D feature using batch, channel, row and column index
79    * @param[in] batch Batch index
80    * @param[in] ch    Channel index
81    * @param[in] row   Row index
82    * @param[in] col   Column index
83    * @return    Value of element
84    */
85   T at(uint32_t batch, uint32_t ch, uint32_t row, uint32_t col) const override
86   {
87     uint32_t index = index_of(_shape, batch, ch, row, col);
88
89     return _ptr[index];
90   }
91
92   /**
93    * @brief     Get reference of element in 3D feature using channel, row, and column index
94    * @param[in] ch  Channel index
95    * @param[in] row Row index
96    * @param[in] col Column index
97    * @return    Reference of element
98    */
99   T &at(uint32_t ch, uint32_t row, uint32_t col)
100   {
101     uint32_t index = index_of(_shape, ch, row, col);
102
103     return _ptr[index];
104   }
105
106   /**
107    * @brief     Get reference of element in 4D feature using batch, channel, row and column index
108    * @param[in] batch Batch index
109    * @param[in] ch    Channel index
110    * @param[in] row   Row index
111    * @param[in] col   Column index
112    * @return    Reference of element
113    */
114   T &at(uint32_t batch, uint32_t ch, uint32_t row, uint32_t col)
115   {
116     uint32_t index = index_of(_shape, batch, ch, row, col);
117
118     return _ptr[index];
119   }
120
121 private:
122   nnfw::misc::feature::Shape _shape;
123
124 private:
125   T *_ptr;
126 };
127
128 } // namespace feature
129 } // namespace nnapi
130 } // namespace internal
131
132 #endif // __INTERNAL_NNAPI_FEATURE_VIEW_H__