xref: /aosp_15_r20/external/ComputeLibrary/src/core/NEON/wrapper/traits.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2018-2021 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_WRAPPER_TRAITS_H
25 #define ARM_COMPUTE_WRAPPER_TRAITS_H
26 
27 #include <arm_neon.h>
28 
29 #if defined(ARM_COMPUTE_ENABLE_SVE)
30 #include <arm_sve.h>
31 #endif /* defined(ARM_COMPUTE_ENABLE_SVE) */
32 
33 namespace arm_compute
34 {
35 namespace wrapper
36 {
37 namespace traits
38 {
39 // *INDENT-OFF*
40 // clang-format off
41 
42 /** 64-bit vector tag */
43 struct vector_64_tag {};
44 /** 128-bit vector tag */
45 struct vector_128_tag {};
46 
47 /** Create the appropriate SIMD vector given its type and size in terms of elements */
48 template <typename T, int S> struct neon_vector;
49 
50 // Specializations
51 #ifndef DOXYGEN_SKIP_THIS
52 template <> struct neon_vector<uint8_t, 8>{ using scalar_type = uint8_t; using type = uint8x8_t; using tag_type = vector_64_tag; };
53 template <> struct neon_vector<int8_t, 8>{ using scalar_type = int8_t; using type = int8x8_t; using tag_type = vector_64_tag; };
54 template <> struct neon_vector<uint8_t, 16>{ using scalar_type = uint8_t; using type = uint8x16_t; using tag_type = vector_128_tag; };
55 template <> struct neon_vector<int8_t, 16>{ using scalar_type = int8_t; using type = int8x16_t; using tag_type = vector_128_tag; };
56 template <> struct neon_vector<uint16_t, 4>{ using scalar_type = uint16_t; using type = uint16x4_t; using tag_type = vector_64_tag; };
57 template <> struct neon_vector<int16_t, 4>{ using scalar_type = int16_t; using type = int16x4_t; using tag_type = vector_64_tag; };
58 template <> struct neon_vector<uint16_t, 8>{ using scalar_type = uint16_t; using type = uint16x8_t; using tag_type = vector_128_tag; };
59 template <> struct neon_vector<uint16_t, 16>{ using scalar_type = uint16_t; using type = uint16x8x2_t; };
60 template <> struct neon_vector<int16_t, 8>{ using scalar_type = int16_t; using type = int16x8_t; using tag_type = vector_128_tag; };
61 template <> struct neon_vector<int16_t, 16>{ using scalar_type = int16_t; using type = int16x8x2_t; };
62 template <> struct neon_vector<uint32_t, 2>{ using scalar_type = uint32_t; using type = uint32x2_t; using tag_type = vector_64_tag; };
63 template <> struct neon_vector<int32_t, 2>{ using scalar_type = int32_t; using type = int32x2_t; using tag_type = vector_64_tag; };
64 template <> struct neon_vector<uint32_t, 4>{ using scalar_type = uint32_t; using type = uint32x4_t; using tag_type = vector_128_tag; };
65 template <> struct neon_vector<int32_t, 4>{ using scalar_type = int32_t; using type = int32x4_t; using tag_type = vector_128_tag; };
66 template <> struct neon_vector<uint64_t, 1>{ using scalar_type = uint64_t;using type = uint64x1_t; using tag_type = vector_64_tag; };
67 template <> struct neon_vector<int64_t, 1>{ using scalar_type = int64_t; using type = int64x1_t; using tag_type = vector_64_tag; };
68 template <> struct neon_vector<uint64_t, 2>{ using scalar_type = uint64_t; using type = uint64x2_t; using tag_type = vector_128_tag; };
69 template <> struct neon_vector<int64_t, 2>{ using scalar_type = int64_t; using type = int64x2_t; using tag_type = vector_128_tag; };
70 template <> struct neon_vector<float_t, 2>{ using scalar_type = float_t; using type = float32x2_t; using tag_type = vector_64_tag; };
71 template <> struct neon_vector<float_t, 4>{ using scalar_type = float_t; using type = float32x4_t; using tag_type = vector_128_tag; };
72 
73 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
74 template <> struct neon_vector<float16_t, 4>{ using scalar_type = float16_t; using type = float16x4_t; using tag_type = vector_64_tag; };
75 template <> struct neon_vector<float16_t, 8>{ using scalar_type = float16_t; using type = float16x8_t; using tag_type = vector_128_tag; };
76 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
77 #endif /* DOXYGEN_SKIP_THIS */
78 
79 /**  Helper type template to get the type of a neon vector */
80 template <typename T, int S> using neon_vector_t = typename neon_vector<T, S>::type;
81 /**  Helper type template to get the tag type of a neon vector */
82 template <typename T, int S> using neon_vector_tag_t = typename neon_vector<T, S>::tag_type;
83 
84 /** Vector bit-width enum class */
85 enum class BitWidth
86 {
87     W64,  /**< 64-bit width */
88     W128, /**< 128-bit width */
89 };
90 
91 /** Create the appropriate SIMD vector given its type and size in terms of bits */
92 template <typename T, BitWidth BW> struct neon_bitvector;
93 // Specializations
94 #ifndef DOXYGEN_SKIP_THIS
95 template <> struct neon_bitvector<uint8_t, BitWidth::W64>{ using type = uint8x8_t; using tag_type = vector_64_tag; };
96 template <> struct neon_bitvector<int8_t, BitWidth::W64>{ using type = int8x8_t; using tag_type = vector_64_tag; };
97 template <> struct neon_bitvector<uint8_t, BitWidth::W128>{ using type = uint8x16_t; using tag_type = vector_128_tag; };
98 template <> struct neon_bitvector<int8_t, BitWidth::W128>{ using type = int8x16_t; using tag_type = vector_128_tag; };
99 template <> struct neon_bitvector<uint16_t, BitWidth::W64>{ using type = uint16x4_t; using tag_type = vector_64_tag; };
100 template <> struct neon_bitvector<int16_t, BitWidth::W64>{ using type = int16x4_t; using tag_type = vector_64_tag; };
101 template <> struct neon_bitvector<uint16_t, BitWidth::W128>{ using type = uint16x8_t; using tag_type = vector_128_tag; };
102 template <> struct neon_bitvector<int16_t, BitWidth::W128>{ using type = int16x8_t; using tag_type = vector_128_tag; };
103 template <> struct neon_bitvector<uint32_t, BitWidth::W64>{ using type = uint32x2_t; using tag_type = vector_64_tag; };
104 template <> struct neon_bitvector<int32_t, BitWidth::W64>{ using type = int32x2_t; using tag_type = vector_64_tag; };
105 template <> struct neon_bitvector<uint32_t, BitWidth::W128>{ using type = uint32x4_t; using tag_type = vector_128_tag; };
106 template <> struct neon_bitvector<int32_t, BitWidth::W128>{ using type = int32x4_t; using tag_type = vector_128_tag; };
107 template <> struct neon_bitvector<uint64_t, BitWidth::W64>{ using type = uint64x1_t; using tag_type = vector_64_tag; };
108 template <> struct neon_bitvector<int64_t, BitWidth::W64>{ using type = int64x1_t; using tag_type = vector_64_tag; };
109 template <> struct neon_bitvector<uint64_t, BitWidth::W128>{ using type = uint64x2_t; using tag_type = vector_128_tag; };
110 template <> struct neon_bitvector<int64_t, BitWidth::W128>{ using type = int64x2_t; using tag_type = vector_128_tag; };
111 template <> struct neon_bitvector<float_t, BitWidth::W64>{ using type = float32x2_t; using tag_type = vector_64_tag; };
112 template <> struct neon_bitvector<float_t, BitWidth::W128>{ using type = float32x4_t; using tag_type = vector_128_tag; };
113 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
114 template <> struct neon_bitvector<float16_t, BitWidth::W64>{ using type = float16x4_t; using tag_type = vector_64_tag; };
115 template <> struct neon_bitvector<float16_t, BitWidth::W128>{ using type = float16x8_t; using tag_type = vector_128_tag; };
116 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
117 
118 
119 #if defined(ARM_COMPUTE_ENABLE_SVE)
120 /** Create the appropriate SVE vector given its type */
121 template <typename T> struct sve_vector;
122 
123 template <> struct sve_vector<uint8_t>{ using scalar_type = uint8_t; using type = svuint8_t; };
124 template <> struct sve_vector<int8_t>{ using scalar_type = int8_t; using type = svint8_t; };
125 #endif /* defined(ARM_COMPUTE_ENABLE_SVE) */
126 
127 #endif /* DOXYGEN_SKIP_THIS */
128 
129 /**  Helper type template to get the type of a neon vector */
130 template <typename T, BitWidth BW> using neon_bitvector_t = typename neon_bitvector<T, BW>::type;
131 /**  Helper type template to get the tag type of a neon vector */
132 template <typename T, BitWidth BW> using neon_bitvector_tag_t = typename neon_bitvector<T, BW>::tag_type;
133 
134 /** Promote a type */
135 template <typename T> struct promote { };
136 template <> struct promote<uint8_t> { using type = uint16_t; };
137 template <> struct promote<int8_t> { using type = int16_t; };
138 template <> struct promote<uint16_t> { using type = uint32_t; };
139 template <> struct promote<int16_t> { using type = int32_t; };
140 template <> struct promote<uint32_t> { using type = uint64_t; };
141 template <> struct promote<int32_t> { using type = int64_t; };
142 template <> struct promote<float> { using type = float; };
143 template <> struct promote<half> { using type = half; };
144 
145 /** Get promoted type */
146 template <typename T>
147 using promote_t = typename promote<T>::type;
148 
149 // clang-format on
150 // *INDENT-ON*
151 } // namespace traits
152 } // namespace wrapper
153 } // namespace arm_compute
154 #endif /* ARM_COMPUTE_WRAPPER_TRAITS_H */
155