1 /*
2 * Copyright (c) 2022 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
25 #include "arm_compute/core/TensorInfo.h"
26 #include "src/cpu/kernels/select/generic/neon/impl.h"
27 #include "src/core/NEON/NEAsymm.h"
28
29 #include <arm_neon.h>
30 #include <map>
31 #include <string>
32
33 namespace arm_compute
34 {
35 namespace cpu
36 {
37 template <typename ScalarType, typename VectorType>
select_op(const ITensor * cond,const ITensor * in1,const ITensor * in2,ITensor * out,const Window & window,const int window_step_x,const int window_start_x,const int window_end_x,const int limit,VectorType (* condition_conversion)(const uint8_t *))38 void select_op(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window,
39 const int window_step_x, const int window_start_x, const int window_end_x, const int limit, VectorType (*condition_conversion)(const uint8_t *))
40 {
41 Window win = window;
42 win.set(Window::DimX, Window::Dimension(0, 1, 1));
43
44 Iterator condition(cond, win);
45 Iterator input1(in1, win);
46 Iterator input2(in2, win);
47 Iterator output(out, win);
48
49 execute_window_loop(win, [&](const Coordinates &)
50 {
51 auto output_ptr = reinterpret_cast<ScalarType *>(output.ptr());
52 const auto condition_ptr = reinterpret_cast<const uint8_t *>(condition.ptr());
53 const auto input1_ptr = reinterpret_cast<const ScalarType *>(input1.ptr());
54 const auto input2_ptr = reinterpret_cast<const ScalarType *>(input2.ptr());
55
56 int x = window_start_x;
57 for(; x <= limit; x += window_step_x)
58 {
59 const auto c = (*condition_conversion)(condition_ptr + x);
60 const auto a = wrapper::vloadq(input1_ptr + x);
61 const auto b = wrapper::vloadq(input2_ptr + x);
62 wrapper::vstore(output_ptr + x, wrapper::vbsl(c, a, b));
63 }
64 for(; x < window_end_x; ++x)
65 {
66 const auto c = *(condition_ptr + x);
67 const auto a = *(input1_ptr + x);
68 const auto b = *(input2_ptr + x);
69 *(output_ptr + x) = static_cast<bool>(c) ? a : b;
70 }
71 },
72 condition, input1, input2, output);
73 }
74
75 template <typename ScalarType, typename VectorType>
select_op_8(const ITensor * cond,const ITensor * in1,const ITensor * in2,ITensor * out,const Window & window)76 void select_op_8(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
77 {
78 const auto window_step_x = 16 / sizeof(ScalarType);
79 const auto window_start_x = static_cast<int>(window.x().start());
80 const auto window_end_x = static_cast<int>(window.x().end());
81
82 select_op<ScalarType, VectorType>(cond, in1, in2, out, window, window_step_x, window_start_x, window_end_x, window_end_x - window_step_x, [](const uint8_t *condition_ptr) -> VectorType
83 {
84 static const auto zero = wrapper::vdup_n(static_cast<uint8_t>(0), arm_compute::wrapper::traits::vector_128_tag());
85 return wrapper::vcgt(wrapper::vloadq(condition_ptr), zero);
86 });
87 }
88
89 template <typename ScalarType, typename VectorType>
select_op_16(const ITensor * cond,const ITensor * in1,const ITensor * in2,ITensor * out,const Window & window)90 void select_op_16(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
91 {
92 const auto window_step_x = 16 / sizeof(ScalarType);
93 const auto window_start_x = static_cast<int>(window.x().start());
94 const auto window_end_x = static_cast<int>(window.x().end());
95
96 select_op<ScalarType, VectorType>(cond, in1, in2, out, window, window_step_x, window_start_x, window_end_x, window_end_x - window_step_x, [](const uint8_t *condition_ptr) -> VectorType
97 {
98 static const auto zero = wrapper::vdup_n(static_cast<uint16_t>(0), arm_compute::wrapper::traits::vector_128_tag());
99 return wrapper::vcgt(wrapper::vmovl(wrapper::vload(condition_ptr)), zero);
100 });
101 }
102
103 template <typename ScalarType, typename VectorType>
select_op_32(const ITensor * cond,const ITensor * in1,const ITensor * in2,ITensor * out,const Window & window)104 void select_op_32(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
105 {
106 const auto window_step_x = 16 / sizeof(ScalarType);
107 const auto window_start_x = static_cast<int>(window.x().start());
108 const auto window_end_x = static_cast<int>(window.x().end());
109
110 select_op<ScalarType, VectorType>(cond, in1, in2, out, window, window_step_x, window_start_x, window_end_x, window_end_x - window_step_x, [](const uint8_t *condition_ptr) -> VectorType
111 {
112 static const auto zero = wrapper::vdup_n(static_cast<uint32_t>(0), arm_compute::wrapper::traits::vector_128_tag());
113 return wrapper::vcgt(wrapper::vmovl(wrapper::vgetlow(wrapper::vmovl(wrapper::vload(condition_ptr)))), zero);
114 });
115 }
116
117 template <typename ScalarType>
select_op_not_same_rank(const ITensor * cond,const ITensor * in1,const ITensor * in2,ITensor * out,const Window & window)118 void select_op_not_same_rank(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window)
119 {
120 ARM_COMPUTE_UNUSED(window);
121
122 auto output_ptr = reinterpret_cast<ScalarType *>(out->buffer());
123 const auto condition_ptr = reinterpret_cast<const uint8_t *>(cond->buffer());
124 const auto input1_ptr = reinterpret_cast<const ScalarType *>(in1->buffer());
125 const auto input2_ptr = reinterpret_cast<const ScalarType *>(in2->buffer());
126
127 const int outer_size = cond->info()->total_size() / cond->info()->element_size();
128 const int inner_size = (in1->info()->total_size() / in1->info()->element_size()) / outer_size;
129 int offset = 0;
130 const int step = 16 / in1->info()->element_size();
131
132 for(int i = 0; i < outer_size; ++i)
133 {
134 int x = offset;
135 const auto input_ptr = static_cast<bool>(*(condition_ptr + i)) ? input1_ptr : input2_ptr;
136 for(; x <= offset + inner_size - step; x += step)
137 {
138 wrapper::vstore(output_ptr + x, wrapper::vloadq(input_ptr + x));
139 }
140 if(x <= offset + inner_size - (step / 2))
141 {
142 wrapper::vstore(output_ptr + x, wrapper::vload(input_ptr + x));
143 x += step / 2;
144 }
145 for(; x < offset + inner_size; ++x)
146 {
147 *(output_ptr + x) = *(input_ptr + x);
148 }
149 offset += inner_size;
150 }
151 }
152
153 template void select_op_32<float, uint32x4_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
154
155 template void select_op_not_same_rank<float>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
156
157 template void select_op_8<int8_t, uint8x16_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
158
159 template void select_op_16<int16_t, uint16x8_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
160
161 #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
162 template void select_op_16<float16_t, uint16x8_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
163 #endif /* (__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS) */
164
165 template void select_op_32<int32_t, uint32x4_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
166
167 template void select_op_not_same_rank<int8_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
168
169 template void select_op_not_same_rank<int16_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
170
171 #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
172 template void select_op_not_same_rank<float16_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
173 #endif /* (__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS) */
174
175 template void select_op_not_same_rank<int32_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
176
177 template void select_op_8<uint8_t, uint8x16_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
178
179 template void select_op_16<uint16_t, uint16x8_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
180
181 template void select_op_32<uint32_t, uint32x4_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
182
183 template void select_op_not_same_rank<uint8_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
184
185 template void select_op_not_same_rank<uint16_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
186
187 template void select_op_not_same_rank<uint32_t>(const ITensor *cond, const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window);
188
189 } // namespace cpu
190
191 } // namespace arm_compute
192