Imported Upstream version 1.4.0
[platform/core/ml/nnfw.git] / runtime / contrib / pure_arm_compute / src / internal / Swizzle.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    Swizzle.h
19  * @ingroup COM_AI_RUNTIME
20  * @brief   This file defines ARMComputeAxis class and utility functions to support mapping
21  *          between arm compute axis and NNAPI axis
22  */
23 #ifndef __SWIZZLE_H__
24 #define __SWIZZLE_H__
25
26 /**
27  * @brief Class to represent arm compute axis
28  */
29 class ARMComputeAxis
30 {
31 public:
32   /**
33    * @brief Construct a new ARMComputeAxis object
34    */
35   ARMComputeAxis() = default;
36
37 public:
38   /**
39    * @brief Construct a new ARMComputeAxis object
40    * @param[in] value Raw axis number
41    */
42   explicit ARMComputeAxis(uint32_t value) : _value{value}
43   {
44     // DO NOTHING
45   }
46
47 public:
48   /**
49    * @brief   Get raw axis number
50    * @return  Raw axis number
51    */
52   uint32_t value(void) const { return _value; }
53
54 private:
55   uint32_t _value;
56 };
57
58 /**
59  * @brief     Convert T/F Lite / NNAPI axis (based on ...NHWC) to arm compute axis (WHCN...)
60  * @param[in] rank  Rank of shape
61  * @param[in] axis  Axis to map
62  * @return    ARMComputeAxis including arm compute axis info
63  */
64 inline ARMComputeAxis ToARMComputeAxis(uint32_t rank, uint32_t axis)
65 {
66   assert(rank > axis);
67   const ARMComputeAxis reversed{(rank - axis) - 1};
68
69   if (rank < 4)
70   {
71     return reversed;
72   }
73
74   // DEPTH
75   if (0 == reversed.value())
76   {
77     return ARMComputeAxis{2};
78   }
79   // WIDTH
80   if (1 == reversed.value())
81   {
82     return ARMComputeAxis{0};
83   }
84   // HEIGHT
85   if (2 == reversed.value())
86   {
87     return ARMComputeAxis{1};
88   }
89
90   // ELSE
91   return reversed;
92 }
93
94 #include <cassert>
95
96 /**
97  * @brief     Covert bitmask info from NNAPI axis to arm compute axis
98  * @param[in] in        Bitmask data
99  * @param[in] numOfBits Used bits (rank)
100  * @return    Coverted bitmask
101  */
102 template <typename T> inline T ReorderBits(T in, size_t numOfBits)
103 {
104   assert(numOfBits > 0);
105   T out = 0;
106   for (int32_t i = numOfBits - 1; i >= 0; --i)
107   {
108     const uint32_t toShift = numOfBits - ToARMComputeAxis(numOfBits, i).value() - 1;
109     out += ((in & 1) << toShift);
110     in >>= 1;
111   }
112   return out;
113 }
114
115 #endif // __SWIZZLE_H__