blob: cf77345da704699b6521f2377af7ae89100361c9 [file] [log] [blame]
giuros0114c4e0f2019-03-26 17:44:40 +00001/*
2 * Copyright (c) 2019 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#include "arm_compute/core/NEON/kernels/NEFFTDigitReverseKernel.h"
25
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"
31
giuros01154bc1c2019-03-26 17:44:40 +000032#include <set>
33
giuros0114c4e0f2019-03-26 17:44:40 +000034namespace arm_compute
35{
36namespace
37{
giuros01154bc1c2019-03-26 17:44:40 +000038Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *idx, const FFTDigitReverseKernelInfo &config)
giuros0114c4e0f2019-03-26 17:44:40 +000039{
giuros01154bc1c2019-03-26 17:44:40 +000040 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() != DataType::F32);
41 ARM_COMPUTE_RETURN_ERROR_ON(input->num_channels() > 2);
giuros0114c4e0f2019-03-26 17:44:40 +000042 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(idx, 1, DataType::U32);
giuros01154bc1c2019-03-26 17:44:40 +000043 ARM_COMPUTE_RETURN_ERROR_ON(std::set<unsigned int>({ 0, 1 }).count(config.axis) == 0);
44 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[config.axis] != idx->tensor_shape().x());
giuros0114c4e0f2019-03-26 17:44:40 +000045
46 // Checks performed when output is configured
47 if((output != nullptr) && (output->total_size() != 0))
48 {
giuros01154bc1c2019-03-26 17:44:40 +000049 ARM_COMPUTE_RETURN_ERROR_ON(output->num_channels() != 2);
giuros0114c4e0f2019-03-26 17:44:40 +000050 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
51 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
52 }
53
54 return Status{};
55}
56
giuros01154bc1c2019-03-26 17:44:40 +000057std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, ITensorInfo *idx, const FFTDigitReverseKernelInfo &config)
giuros0114c4e0f2019-03-26 17:44:40 +000058{
giuros01154bc1c2019-03-26 17:44:40 +000059 ARM_COMPUTE_UNUSED(idx, config);
giuros0114c4e0f2019-03-26 17:44:40 +000060
giuros01154bc1c2019-03-26 17:44:40 +000061 auto_init_if_empty(*output, input->clone()->set_num_channels(2));
giuros0114c4e0f2019-03-26 17:44:40 +000062
giuros01154bc1c2019-03-26 17:44:40 +000063 Window win = calculate_max_window(*input, Steps());
64 input->set_valid_region(ValidRegion(Coordinates(), input->tensor_shape()));
giuros0114c4e0f2019-03-26 17:44:40 +000065
66 return std::make_pair(Status{}, win);
67}
68} // namespace
69
70NEFFTDigitReverseKernel::NEFFTDigitReverseKernel()
giuros01154bc1c2019-03-26 17:44:40 +000071 : _func(nullptr), _input(nullptr), _output(nullptr), _idx(nullptr)
giuros0114c4e0f2019-03-26 17:44:40 +000072{
73}
74
giuros01154bc1c2019-03-26 17:44:40 +000075void NEFFTDigitReverseKernel::configure(const ITensor *input, ITensor *output, const ITensor *idx, const FFTDigitReverseKernelInfo &config)
giuros0114c4e0f2019-03-26 17:44:40 +000076{
77 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, idx);
giuros01154bc1c2019-03-26 17:44:40 +000078 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), idx->info(), config));
giuros0114c4e0f2019-03-26 17:44:40 +000079
80 _input = input;
81 _output = output;
82 _idx = idx;
giuros01154bc1c2019-03-26 17:44:40 +000083
84 const size_t axis = config.axis;
85 const bool is_conj = config.conjugate;
86 const bool is_input_complex = (input->info()->num_channels() == 2);
giuros0114c4e0f2019-03-26 17:44:40 +000087
88 // Configure kernel window
giuros01154bc1c2019-03-26 17:44:40 +000089 auto win_config = validate_and_configure_window(input->info(), output->info(), idx->info(), config);
giuros0114c4e0f2019-03-26 17:44:40 +000090 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
91 INEKernel::configure(win_config.second);
giuros01154bc1c2019-03-26 17:44:40 +000092
93 if(axis == 0)
94 {
95 if(is_input_complex)
96 {
97 if(is_conj)
98 {
99 _func = &NEFFTDigitReverseKernel::digit_reverse_kernel_axis_0<true, true>;
100 }
101 else
102 {
103 _func = &NEFFTDigitReverseKernel::digit_reverse_kernel_axis_0<true, false>;
104 }
105 }
106 else
107 {
108 _func = &NEFFTDigitReverseKernel::digit_reverse_kernel_axis_0<false, false>;
109 }
110 }
111 else if(axis == 1)
112 {
113 if(is_input_complex)
114 {
115 if(is_conj)
116 {
117 _func = &NEFFTDigitReverseKernel::digit_reverse_kernel_axis_1<true, true>;
118 }
119 else
120 {
121 _func = &NEFFTDigitReverseKernel::digit_reverse_kernel_axis_1<true, false>;
122 }
123 }
124 else
125 {
126 _func = &NEFFTDigitReverseKernel::digit_reverse_kernel_axis_1<false, false>;
127 }
128 }
129 else
130 {
131 ARM_COMPUTE_ERROR("Not supported");
132 }
giuros0114c4e0f2019-03-26 17:44:40 +0000133}
134
giuros01154bc1c2019-03-26 17:44:40 +0000135Status NEFFTDigitReverseKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *idx, const FFTDigitReverseKernelInfo &config)
giuros0114c4e0f2019-03-26 17:44:40 +0000136{
giuros01154bc1c2019-03-26 17:44:40 +0000137 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, idx, config));
138 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), idx->clone().get(), config).first);
giuros0114c4e0f2019-03-26 17:44:40 +0000139 return Status{};
140}
141
giuros01154bc1c2019-03-26 17:44:40 +0000142template <bool is_input_complex, bool is_conj>
143void NEFFTDigitReverseKernel::digit_reverse_kernel_axis_0(const Window &window)
144{
145 const size_t N = _input->info()->dimension(0);
146
147 // Copy the look-up buffer to a local array
148 std::vector<unsigned int> buffer_idx(N);
149 std::copy_n(reinterpret_cast<unsigned int *>(_idx->buffer()), N, buffer_idx.data());
150
151 // Input/output iterators
152 Window slice = window;
153 slice.set(0, Window::DimX);
154 Iterator in(_input, slice);
155 Iterator out(_output, slice);
156
157 // Row buffers
158 std::vector<float> buffer_row_out(2 * N);
159 std::vector<float> buffer_row_in(2 * N);
160
161 execute_window_loop(slice, [&](const Coordinates &)
162 {
163 if(is_input_complex)
164 {
165 // Load
166 memcpy(buffer_row_in.data(), reinterpret_cast<float *>(in.ptr()), 2 * N * sizeof(float));
167
168 // Shuffle
169 for(size_t x = 0; x < 2 * N; x += 2)
170 {
171 size_t idx = buffer_idx[x / 2];
172 buffer_row_out[x] = buffer_row_in[2 * idx];
173 buffer_row_out[x + 1] = (is_conj ? -buffer_row_in[2 * idx + 1] : buffer_row_in[2 * idx + 1]);
174 }
175 }
176 else
177 {
178 // Load
179 memcpy(buffer_row_in.data(), reinterpret_cast<float *>(in.ptr()), N * sizeof(float));
180
181 // Shuffle
182 for(size_t x = 0; x < N; ++x)
183 {
184 size_t idx = buffer_idx[x];
185 buffer_row_out[2 * x] = buffer_row_in[idx];
186 }
187 }
188
189 // Copy back
190 memcpy(reinterpret_cast<float *>(out.ptr()), buffer_row_out.data(), 2 * N * sizeof(float));
191 },
192 in, out);
193}
194
195template <bool is_input_complex, bool is_conj>
196void NEFFTDigitReverseKernel::digit_reverse_kernel_axis_1(const Window &window)
197{
198 const size_t Nx = _input->info()->dimension(0);
199 const size_t Ny = _input->info()->dimension(1);
200
201 // Copy the look-up buffer to a local array
202 std::vector<unsigned int> buffer_idx(Ny);
203 std::copy_n(reinterpret_cast<unsigned int *>(_idx->buffer()), Ny, buffer_idx.data());
204
205 // Output iterator
206 Window slice = window;
207 slice.set(0, Window::DimX);
208 Iterator out(_output, slice);
209
210 // Row buffer
211 std::vector<float> buffer_row(Nx);
212
213 // Strides
214 const size_t stride_z = _input->info()->strides_in_bytes()[2];
215 const size_t stride_w = _input->info()->strides_in_bytes()[3];
216
217 execute_window_loop(slice, [&](const Coordinates & id)
218 {
219 auto *out_ptr = reinterpret_cast<float *>(out.ptr());
220 auto *in_ptr = reinterpret_cast<float *>(_input->buffer() + id.z() * stride_z + id[3] * stride_w);
221 const size_t y_shuffled = buffer_idx[id.y()];
222
223 if(is_input_complex)
224 {
225 // Shuffle the entire row into the output
226 memcpy(out_ptr, in_ptr + 2 * Nx * y_shuffled, 2 * Nx * sizeof(float));
227
228 // Conjugate if necessary
229 if(is_conj)
230 {
231 for(size_t x = 0; x < 2 * Nx; x += 2)
232 {
233 out_ptr[x + 1] = -out_ptr[x + 1];
234 }
235 }
236 }
237 else
238 {
239 // Shuffle the entire row into the buffer
240 memcpy(buffer_row.data(), in_ptr + Nx * y_shuffled, Nx * sizeof(float));
241
242 // Copy the buffer to the output, with a zero imaginary part
243 for(size_t x = 0; x < 2 * Nx; x += 2)
244 {
245 out_ptr[x] = buffer_row[x / 2];
246 }
247 }
248 },
249 out);
250}
251
giuros0114c4e0f2019-03-26 17:44:40 +0000252void NEFFTDigitReverseKernel::run(const Window &window, const ThreadInfo &info)
253{
giuros0114c4e0f2019-03-26 17:44:40 +0000254 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
255 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
giuros01154bc1c2019-03-26 17:44:40 +0000256 ARM_COMPUTE_UNUSED(info);
257 (this->*_func)(window);
giuros0114c4e0f2019-03-26 17:44:40 +0000258}
giuros01154bc1c2019-03-26 17:44:40 +0000259
giuros0114c4e0f2019-03-26 17:44:40 +0000260} // namespace arm_compute