blob: a5969cd4972825620cf28a23d085dd3a67ee4f1b [file] [log] [blame]
giuros0114c4e0f2019-03-26 17:44:40 +00001/*
SiCongLib88272e2021-02-24 15:40:57 +00002 * Copyright (c) 2019-2021 Arm Limited.
giuros0114c4e0f2019-03-26 17:44:40 +00003 *
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 */
Michalis Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEFFTDigitReverseKernel.h"
giuros0114c4e0f2019-03-26 17:44:40 +000025
26#include "arm_compute/core/ITensor.h"
27#include "arm_compute/core/TensorInfo.h"
28#include "arm_compute/core/Types.h"
29#include "arm_compute/core/Validate.h"
30#include "arm_compute/core/Window.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/helpers/AutoConfiguration.h"
33#include "src/core/helpers/WindowHelpers.h"
giuros0114c4e0f2019-03-26 17:44:40 +000034
giuros01154bc1c2019-03-26 17:44:40 +000035#include <set>
36
giuros0114c4e0f2019-03-26 17:44:40 +000037namespace arm_compute
38{
39namespace
40{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010041Status validate_arguments(const ITensorInfo *input,
42 const ITensorInfo *output,
43 const ITensorInfo *idx,
44 const FFTDigitReverseKernelInfo &config)
giuros0114c4e0f2019-03-26 17:44:40 +000045{
giuros01154bc1c2019-03-26 17:44:40 +000046 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() != DataType::F32);
47 ARM_COMPUTE_RETURN_ERROR_ON(input->num_channels() > 2);
giuros0114c4e0f2019-03-26 17:44:40 +000048 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(idx, 1, DataType::U32);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010049 ARM_COMPUTE_RETURN_ERROR_ON(std::set<unsigned int>({0, 1}).count(config.axis) == 0);
giuros01154bc1c2019-03-26 17:44:40 +000050 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[config.axis] != idx->tensor_shape().x());
giuros0114c4e0f2019-03-26 17:44:40 +000051
52 // Checks performed when output is configured
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010053 if ((output != nullptr) && (output->total_size() != 0))
giuros0114c4e0f2019-03-26 17:44:40 +000054 {
giuros01154bc1c2019-03-26 17:44:40 +000055 ARM_COMPUTE_RETURN_ERROR_ON(output->num_channels() != 2);
giuros0114c4e0f2019-03-26 17:44:40 +000056 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
58 }
59
60 return Status{};
61}
62
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010063std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input,
64 ITensorInfo *output,
65 ITensorInfo *idx,
66 const FFTDigitReverseKernelInfo &config)
giuros0114c4e0f2019-03-26 17:44:40 +000067{
giuros01154bc1c2019-03-26 17:44:40 +000068 ARM_COMPUTE_UNUSED(idx, config);
giuros0114c4e0f2019-03-26 17:44:40 +000069
giuros01154bc1c2019-03-26 17:44:40 +000070 auto_init_if_empty(*output, input->clone()->set_num_channels(2));
giuros0114c4e0f2019-03-26 17:44:40 +000071
giuros01154bc1c2019-03-26 17:44:40 +000072 Window win = calculate_max_window(*input, Steps());
giuros0114c4e0f2019-03-26 17:44:40 +000073
74 return std::make_pair(Status{}, win);
75}
76} // namespace
77
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010078NEFFTDigitReverseKernel::NEFFTDigitReverseKernel() : _func(nullptr), _input(nullptr), _output(nullptr), _idx(nullptr)
giuros0114c4e0f2019-03-26 17:44:40 +000079{
80}
81
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010082void NEFFTDigitReverseKernel::configure(const ITensor *input,
83 ITensor *output,
84 const ITensor *idx,
85 const FFTDigitReverseKernelInfo &config)
giuros0114c4e0f2019-03-26 17:44:40 +000086{
87 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, idx);
giuros01154bc1c2019-03-26 17:44:40 +000088 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), idx->info(), config));
giuros0114c4e0f2019-03-26 17:44:40 +000089
90 _input = input;
91 _output = output;
92 _idx = idx;
giuros01154bc1c2019-03-26 17:44:40 +000093
94 const size_t axis = config.axis;
95 const bool is_conj = config.conjugate;
96 const bool is_input_complex = (input->info()->num_channels() == 2);
giuros0114c4e0f2019-03-26 17:44:40 +000097
98 // Configure kernel window
giuros01154bc1c2019-03-26 17:44:40 +000099 auto win_config = validate_and_configure_window(input->info(), output->info(), idx->info(), config);
giuros0114c4e0f2019-03-26 17:44:40 +0000100 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
101 INEKernel::configure(win_config.second);
giuros01154bc1c2019-03-26 17:44:40 +0000102
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103 if (axis == 0)
giuros01154bc1c2019-03-26 17:44:40 +0000104 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100105 if (is_input_complex)
giuros01154bc1c2019-03-26 17:44:40 +0000106 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100107 if (is_conj)
giuros01154bc1c2019-03-26 17:44:40 +0000108 {
109 _func = &NEFFTDigitReverseKernel::digit_reverse_kernel_axis_0<true, true>;
110 }
111 else
112 {
113 _func = &NEFFTDigitReverseKernel::digit_reverse_kernel_axis_0<true, false>;
114 }
115 }
116 else
117 {
118 _func = &NEFFTDigitReverseKernel::digit_reverse_kernel_axis_0<false, false>;
119 }
120 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100121 else if (axis == 1)
giuros01154bc1c2019-03-26 17:44:40 +0000122 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100123 if (is_input_complex)
giuros01154bc1c2019-03-26 17:44:40 +0000124 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100125 if (is_conj)
giuros01154bc1c2019-03-26 17:44:40 +0000126 {
127 _func = &NEFFTDigitReverseKernel::digit_reverse_kernel_axis_1<true, true>;
128 }
129 else
130 {
131 _func = &NEFFTDigitReverseKernel::digit_reverse_kernel_axis_1<true, false>;
132 }
133 }
134 else
135 {
136 _func = &NEFFTDigitReverseKernel::digit_reverse_kernel_axis_1<false, false>;
137 }
138 }
139 else
140 {
141 ARM_COMPUTE_ERROR("Not supported");
142 }
giuros0114c4e0f2019-03-26 17:44:40 +0000143}
144
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100145Status NEFFTDigitReverseKernel::validate(const ITensorInfo *input,
146 const ITensorInfo *output,
147 const ITensorInfo *idx,
148 const FFTDigitReverseKernelInfo &config)
giuros0114c4e0f2019-03-26 17:44:40 +0000149{
giuros01154bc1c2019-03-26 17:44:40 +0000150 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, idx, config));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100151 ARM_COMPUTE_RETURN_ON_ERROR(
152 validate_and_configure_window(input->clone().get(), output->clone().get(), idx->clone().get(), config).first);
giuros0114c4e0f2019-03-26 17:44:40 +0000153 return Status{};
154}
155
giuros01154bc1c2019-03-26 17:44:40 +0000156template <bool is_input_complex, bool is_conj>
157void NEFFTDigitReverseKernel::digit_reverse_kernel_axis_0(const Window &window)
158{
159 const size_t N = _input->info()->dimension(0);
160
161 // Copy the look-up buffer to a local array
162 std::vector<unsigned int> buffer_idx(N);
163 std::copy_n(reinterpret_cast<unsigned int *>(_idx->buffer()), N, buffer_idx.data());
164
165 // Input/output iterators
166 Window slice = window;
167 slice.set(0, Window::DimX);
168 Iterator in(_input, slice);
169 Iterator out(_output, slice);
170
171 // Row buffers
172 std::vector<float> buffer_row_out(2 * N);
173 std::vector<float> buffer_row_in(2 * N);
174
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100175 execute_window_loop(
176 slice,
177 [&](const Coordinates &)
giuros01154bc1c2019-03-26 17:44:40 +0000178 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100179 if (is_input_complex)
giuros01154bc1c2019-03-26 17:44:40 +0000180 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100181 // Load
182 memcpy(buffer_row_in.data(), reinterpret_cast<float *>(in.ptr()), 2 * N * sizeof(float));
giuros01154bc1c2019-03-26 17:44:40 +0000183
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100184 // Shuffle
185 for (size_t x = 0; x < 2 * N; x += 2)
186 {
187 size_t idx = buffer_idx[x / 2];
188 buffer_row_out[x] = buffer_row_in[2 * idx];
189 buffer_row_out[x + 1] = (is_conj ? -buffer_row_in[2 * idx + 1] : buffer_row_in[2 * idx + 1]);
190 }
191 }
192 else
giuros01154bc1c2019-03-26 17:44:40 +0000193 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100194 // Load
195 memcpy(buffer_row_in.data(), reinterpret_cast<float *>(in.ptr()), N * sizeof(float));
giuros01154bc1c2019-03-26 17:44:40 +0000196
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100197 // Shuffle
198 for (size_t x = 0; x < N; ++x)
199 {
200 size_t idx = buffer_idx[x];
201 buffer_row_out[2 * x] = buffer_row_in[idx];
202 }
203 }
204
205 // Copy back
206 memcpy(reinterpret_cast<float *>(out.ptr()), buffer_row_out.data(), 2 * N * sizeof(float));
207 },
208 in, out);
giuros01154bc1c2019-03-26 17:44:40 +0000209}
210
211template <bool is_input_complex, bool is_conj>
212void NEFFTDigitReverseKernel::digit_reverse_kernel_axis_1(const Window &window)
213{
214 const size_t Nx = _input->info()->dimension(0);
215 const size_t Ny = _input->info()->dimension(1);
216
217 // Copy the look-up buffer to a local array
218 std::vector<unsigned int> buffer_idx(Ny);
219 std::copy_n(reinterpret_cast<unsigned int *>(_idx->buffer()), Ny, buffer_idx.data());
220
221 // Output iterator
222 Window slice = window;
223 slice.set(0, Window::DimX);
224 Iterator out(_output, slice);
225
226 // Row buffer
227 std::vector<float> buffer_row(Nx);
228
229 // Strides
230 const size_t stride_z = _input->info()->strides_in_bytes()[2];
231 const size_t stride_w = _input->info()->strides_in_bytes()[3];
232
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100233 execute_window_loop(
234 slice,
235 [&](const Coordinates &id)
giuros01154bc1c2019-03-26 17:44:40 +0000236 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100237 auto *out_ptr = reinterpret_cast<float *>(out.ptr());
238 auto *in_ptr = reinterpret_cast<float *>(_input->buffer() + id.z() * stride_z + id[3] * stride_w);
239 const size_t y_shuffled = buffer_idx[id.y()];
giuros01154bc1c2019-03-26 17:44:40 +0000240
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100241 if (is_input_complex)
giuros01154bc1c2019-03-26 17:44:40 +0000242 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100243 // Shuffle the entire row into the output
244 memcpy(out_ptr, in_ptr + 2 * Nx * y_shuffled, 2 * Nx * sizeof(float));
245
246 // Conjugate if necessary
247 if (is_conj)
giuros01154bc1c2019-03-26 17:44:40 +0000248 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100249 for (size_t x = 0; x < 2 * Nx; x += 2)
250 {
251 out_ptr[x + 1] = -out_ptr[x + 1];
252 }
giuros01154bc1c2019-03-26 17:44:40 +0000253 }
254 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100255 else
giuros01154bc1c2019-03-26 17:44:40 +0000256 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100257 // Shuffle the entire row into the buffer
258 memcpy(buffer_row.data(), in_ptr + Nx * y_shuffled, Nx * sizeof(float));
259
260 // Copy the buffer to the output, with a zero imaginary part
261 for (size_t x = 0; x < 2 * Nx; x += 2)
262 {
263 out_ptr[x] = buffer_row[x / 2];
264 }
giuros01154bc1c2019-03-26 17:44:40 +0000265 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100266 },
267 out);
giuros01154bc1c2019-03-26 17:44:40 +0000268}
269
giuros0114c4e0f2019-03-26 17:44:40 +0000270void NEFFTDigitReverseKernel::run(const Window &window, const ThreadInfo &info)
271{
giuros0114c4e0f2019-03-26 17:44:40 +0000272 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
273 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
giuros01154bc1c2019-03-26 17:44:40 +0000274 ARM_COMPUTE_UNUSED(info);
275 (this->*_func)(window);
giuros0114c4e0f2019-03-26 17:44:40 +0000276}
giuros01154bc1c2019-03-26 17:44:40 +0000277
giuros0114c4e0f2019-03-26 17:44:40 +0000278} // namespace arm_compute