Georgios Pinitas | 82833b8 | 2018-01-30 12:14:24 +0000 | [diff] [blame] | 1 | /* |
Gunes Bayir | c071328 | 2023-09-14 15:14:48 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, 2023 Arm Limited. |
Georgios Pinitas | 82833b8 | 2018-01-30 12:14:24 +0000 | [diff] [blame] | 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 | */ |
Gunes Bayir | c071328 | 2023-09-14 15:14:48 +0100 | [diff] [blame] | 24 | #ifndef ACL_SRC_CORE_NEON_WRAPPER_TRAITS_H |
| 25 | #define ACL_SRC_CORE_NEON_WRAPPER_TRAITS_H |
| 26 | |
| 27 | #include "arm_compute/core/CoreTypes.h" |
| 28 | |
| 29 | #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) |
| 30 | #include "src/cpu/CpuTypes.h" // required for float16_t |
| 31 | #endif // defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) |
Georgios Pinitas | 82833b8 | 2018-01-30 12:14:24 +0000 | [diff] [blame] | 32 | |
| 33 | #include <arm_neon.h> |
| 34 | |
Viet-Hoa Do | bcf9552 | 2023-11-01 11:27:32 +0000 | [diff] [blame^] | 35 | #if defined(ARM_COMPUTE_ENABLE_SVE) && defined(__ARM_FEATURE_SVE) |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 36 | #include <arm_sve.h> |
Viet-Hoa Do | bcf9552 | 2023-11-01 11:27:32 +0000 | [diff] [blame^] | 37 | #endif /* defined(ARM_COMPUTE_ENABLE_SVE) && defined(__ARM_FEATURE_SVE) */ |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 38 | |
Gunes Bayir | c071328 | 2023-09-14 15:14:48 +0100 | [diff] [blame] | 39 | #include <cmath> |
| 40 | #include <cstdint> |
| 41 | |
Georgios Pinitas | 82833b8 | 2018-01-30 12:14:24 +0000 | [diff] [blame] | 42 | namespace arm_compute |
| 43 | { |
| 44 | namespace wrapper |
| 45 | { |
| 46 | namespace traits |
| 47 | { |
| 48 | // *INDENT-OFF* |
| 49 | // clang-format off |
| 50 | |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 51 | /** 64-bit vector tag */ |
| 52 | struct vector_64_tag {}; |
| 53 | /** 128-bit vector tag */ |
| 54 | struct vector_128_tag {}; |
| 55 | |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 56 | /** Create the appropriate SIMD vector given its type and size in terms of elements */ |
Georgios Pinitas | 82833b8 | 2018-01-30 12:14:24 +0000 | [diff] [blame] | 57 | template <typename T, int S> struct neon_vector; |
Manuel Bottini | b4bb827 | 2019-12-18 18:01:27 +0000 | [diff] [blame] | 58 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 59 | // Specializations |
| 60 | #ifndef DOXYGEN_SKIP_THIS |
giuros01 | d513436 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 61 | template <> struct neon_vector<uint8_t, 8>{ using scalar_type = uint8_t; using type = uint8x8_t; using tag_type = vector_64_tag; }; |
| 62 | template <> struct neon_vector<int8_t, 8>{ using scalar_type = int8_t; using type = int8x8_t; using tag_type = vector_64_tag; }; |
| 63 | template <> struct neon_vector<uint8_t, 16>{ using scalar_type = uint8_t; using type = uint8x16_t; using tag_type = vector_128_tag; }; |
| 64 | template <> struct neon_vector<int8_t, 16>{ using scalar_type = int8_t; using type = int8x16_t; using tag_type = vector_128_tag; }; |
| 65 | template <> struct neon_vector<uint16_t, 4>{ using scalar_type = uint16_t; using type = uint16x4_t; using tag_type = vector_64_tag; }; |
| 66 | template <> struct neon_vector<int16_t, 4>{ using scalar_type = int16_t; using type = int16x4_t; using tag_type = vector_64_tag; }; |
| 67 | template <> struct neon_vector<uint16_t, 8>{ using scalar_type = uint16_t; using type = uint16x8_t; using tag_type = vector_128_tag; }; |
Manuel Bottini | b4bb827 | 2019-12-18 18:01:27 +0000 | [diff] [blame] | 68 | template <> struct neon_vector<uint16_t, 16>{ using scalar_type = uint16_t; using type = uint16x8x2_t; }; |
giuros01 | d513436 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 69 | template <> struct neon_vector<int16_t, 8>{ using scalar_type = int16_t; using type = int16x8_t; using tag_type = vector_128_tag; }; |
Manuel Bottini | b4bb827 | 2019-12-18 18:01:27 +0000 | [diff] [blame] | 70 | template <> struct neon_vector<int16_t, 16>{ using scalar_type = int16_t; using type = int16x8x2_t; }; |
giuros01 | d513436 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 71 | template <> struct neon_vector<uint32_t, 2>{ using scalar_type = uint32_t; using type = uint32x2_t; using tag_type = vector_64_tag; }; |
| 72 | template <> struct neon_vector<int32_t, 2>{ using scalar_type = int32_t; using type = int32x2_t; using tag_type = vector_64_tag; }; |
| 73 | template <> struct neon_vector<uint32_t, 4>{ using scalar_type = uint32_t; using type = uint32x4_t; using tag_type = vector_128_tag; }; |
| 74 | template <> struct neon_vector<int32_t, 4>{ using scalar_type = int32_t; using type = int32x4_t; using tag_type = vector_128_tag; }; |
| 75 | template <> struct neon_vector<uint64_t, 1>{ using scalar_type = uint64_t;using type = uint64x1_t; using tag_type = vector_64_tag; }; |
| 76 | template <> struct neon_vector<int64_t, 1>{ using scalar_type = int64_t; using type = int64x1_t; using tag_type = vector_64_tag; }; |
| 77 | template <> struct neon_vector<uint64_t, 2>{ using scalar_type = uint64_t; using type = uint64x2_t; using tag_type = vector_128_tag; }; |
| 78 | template <> struct neon_vector<int64_t, 2>{ using scalar_type = int64_t; using type = int64x2_t; using tag_type = vector_128_tag; }; |
| 79 | template <> struct neon_vector<float_t, 2>{ using scalar_type = float_t; using type = float32x2_t; using tag_type = vector_64_tag; }; |
| 80 | template <> struct neon_vector<float_t, 4>{ using scalar_type = float_t; using type = float32x4_t; using tag_type = vector_128_tag; }; |
Manuel Bottini | b4bb827 | 2019-12-18 18:01:27 +0000 | [diff] [blame] | 81 | |
Georgios Pinitas | aaba4c6 | 2018-08-22 16:20:21 +0100 | [diff] [blame] | 82 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
giuros01 | d513436 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 83 | template <> struct neon_vector<float16_t, 4>{ using scalar_type = float16_t; using type = float16x4_t; using tag_type = vector_64_tag; }; |
| 84 | template <> struct neon_vector<float16_t, 8>{ using scalar_type = float16_t; using type = float16x8_t; using tag_type = vector_128_tag; }; |
Georgios Pinitas | aaba4c6 | 2018-08-22 16:20:21 +0100 | [diff] [blame] | 85 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 86 | #endif /* DOXYGEN_SKIP_THIS */ |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 87 | |
| 88 | /** Helper type template to get the type of a neon vector */ |
Georgios Pinitas | 82833b8 | 2018-01-30 12:14:24 +0000 | [diff] [blame] | 89 | template <typename T, int S> using neon_vector_t = typename neon_vector<T, S>::type; |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 90 | /** Helper type template to get the tag type of a neon vector */ |
| 91 | template <typename T, int S> using neon_vector_tag_t = typename neon_vector<T, S>::tag_type; |
Georgios Pinitas | 52ebf42 | 2018-12-17 18:21:02 +0000 | [diff] [blame] | 92 | |
| 93 | /** Vector bit-width enum class */ |
| 94 | enum class BitWidth |
| 95 | { |
| 96 | W64, /**< 64-bit width */ |
| 97 | W128, /**< 128-bit width */ |
| 98 | }; |
| 99 | |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 100 | /** Create the appropriate SIMD vector given its type and size in terms of bits */ |
Georgios Pinitas | 52ebf42 | 2018-12-17 18:21:02 +0000 | [diff] [blame] | 101 | template <typename T, BitWidth BW> struct neon_bitvector; |
| 102 | // Specializations |
| 103 | #ifndef DOXYGEN_SKIP_THIS |
| 104 | template <> struct neon_bitvector<uint8_t, BitWidth::W64>{ using type = uint8x8_t; using tag_type = vector_64_tag; }; |
| 105 | template <> struct neon_bitvector<int8_t, BitWidth::W64>{ using type = int8x8_t; using tag_type = vector_64_tag; }; |
| 106 | template <> struct neon_bitvector<uint8_t, BitWidth::W128>{ using type = uint8x16_t; using tag_type = vector_128_tag; }; |
| 107 | template <> struct neon_bitvector<int8_t, BitWidth::W128>{ using type = int8x16_t; using tag_type = vector_128_tag; }; |
| 108 | template <> struct neon_bitvector<uint16_t, BitWidth::W64>{ using type = uint16x4_t; using tag_type = vector_64_tag; }; |
| 109 | template <> struct neon_bitvector<int16_t, BitWidth::W64>{ using type = int16x4_t; using tag_type = vector_64_tag; }; |
| 110 | template <> struct neon_bitvector<uint16_t, BitWidth::W128>{ using type = uint16x8_t; using tag_type = vector_128_tag; }; |
| 111 | template <> struct neon_bitvector<int16_t, BitWidth::W128>{ using type = int16x8_t; using tag_type = vector_128_tag; }; |
| 112 | template <> struct neon_bitvector<uint32_t, BitWidth::W64>{ using type = uint32x2_t; using tag_type = vector_64_tag; }; |
| 113 | template <> struct neon_bitvector<int32_t, BitWidth::W64>{ using type = int32x2_t; using tag_type = vector_64_tag; }; |
| 114 | template <> struct neon_bitvector<uint32_t, BitWidth::W128>{ using type = uint32x4_t; using tag_type = vector_128_tag; }; |
| 115 | template <> struct neon_bitvector<int32_t, BitWidth::W128>{ using type = int32x4_t; using tag_type = vector_128_tag; }; |
| 116 | template <> struct neon_bitvector<uint64_t, BitWidth::W64>{ using type = uint64x1_t; using tag_type = vector_64_tag; }; |
| 117 | template <> struct neon_bitvector<int64_t, BitWidth::W64>{ using type = int64x1_t; using tag_type = vector_64_tag; }; |
| 118 | template <> struct neon_bitvector<uint64_t, BitWidth::W128>{ using type = uint64x2_t; using tag_type = vector_128_tag; }; |
| 119 | template <> struct neon_bitvector<int64_t, BitWidth::W128>{ using type = int64x2_t; using tag_type = vector_128_tag; }; |
| 120 | template <> struct neon_bitvector<float_t, BitWidth::W64>{ using type = float32x2_t; using tag_type = vector_64_tag; }; |
| 121 | template <> struct neon_bitvector<float_t, BitWidth::W128>{ using type = float32x4_t; using tag_type = vector_128_tag; }; |
| 122 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 123 | template <> struct neon_bitvector<float16_t, BitWidth::W64>{ using type = float16x4_t; using tag_type = vector_64_tag; }; |
| 124 | template <> struct neon_bitvector<float16_t, BitWidth::W128>{ using type = float16x8_t; using tag_type = vector_128_tag; }; |
| 125 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 126 | |
| 127 | |
Viet-Hoa Do | bcf9552 | 2023-11-01 11:27:32 +0000 | [diff] [blame^] | 128 | #if defined(ARM_COMPUTE_ENABLE_SVE) && defined(__ARM_FEATURE_SVE) |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 129 | /** Create the appropriate SVE vector given its type */ |
| 130 | template <typename T> struct sve_vector; |
| 131 | |
| 132 | template <> struct sve_vector<uint8_t>{ using scalar_type = uint8_t; using type = svuint8_t; }; |
| 133 | template <> struct sve_vector<int8_t>{ using scalar_type = int8_t; using type = svint8_t; }; |
Viet-Hoa Do | bcf9552 | 2023-11-01 11:27:32 +0000 | [diff] [blame^] | 134 | #endif /* defined(ARM_COMPUTE_ENABLE_SVE) && defined(__ARM_FEATURE_SVE) */ |
Michalis Spyrou | b5a450a | 2021-01-06 17:40:30 +0000 | [diff] [blame] | 135 | |
Georgios Pinitas | 52ebf42 | 2018-12-17 18:21:02 +0000 | [diff] [blame] | 136 | #endif /* DOXYGEN_SKIP_THIS */ |
| 137 | |
| 138 | /** Helper type template to get the type of a neon vector */ |
| 139 | template <typename T, BitWidth BW> using neon_bitvector_t = typename neon_bitvector<T, BW>::type; |
| 140 | /** Helper type template to get the tag type of a neon vector */ |
| 141 | template <typename T, BitWidth BW> using neon_bitvector_tag_t = typename neon_bitvector<T, BW>::tag_type; |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 142 | |
| 143 | /** Promote a type */ |
| 144 | template <typename T> struct promote { }; |
| 145 | template <> struct promote<uint8_t> { using type = uint16_t; }; |
| 146 | template <> struct promote<int8_t> { using type = int16_t; }; |
| 147 | template <> struct promote<uint16_t> { using type = uint32_t; }; |
| 148 | template <> struct promote<int16_t> { using type = int32_t; }; |
| 149 | template <> struct promote<uint32_t> { using type = uint64_t; }; |
| 150 | template <> struct promote<int32_t> { using type = int64_t; }; |
| 151 | template <> struct promote<float> { using type = float; }; |
| 152 | template <> struct promote<half> { using type = half; }; |
| 153 | |
| 154 | /** Get promoted type */ |
| 155 | template <typename T> |
| 156 | using promote_t = typename promote<T>::type; |
| 157 | |
Georgios Pinitas | 82833b8 | 2018-01-30 12:14:24 +0000 | [diff] [blame] | 158 | // clang-format on |
| 159 | // *INDENT-ON* |
Georgios Pinitas | 57c033b | 2018-02-15 12:29:44 +0000 | [diff] [blame] | 160 | } // namespace traits |
| 161 | } // namespace wrapper |
| 162 | } // namespace arm_compute |
Gunes Bayir | c071328 | 2023-09-14 15:14:48 +0100 | [diff] [blame] | 163 | #endif // ACL_SRC_CORE_NEON_WRAPPER_TRAITS_H |