xref: /aosp_15_r20/external/ComputeLibrary/src/core/NEON/kernels/NEFFTScaleKernel.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2019-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 #include "src/core/NEON/kernels/NEFFTScaleKernel.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 #include "src/core/NEON/wrapper/wrapper.h"
32 #include "src/core/helpers/AutoConfiguration.h"
33 #include "src/core/helpers/WindowHelpers.h"
34 
35 #include <arm_neon.h>
36 
37 namespace arm_compute
38 {
39 namespace
40 {
scale_complex(float * c_in,float * c_out,bool is_conjugate,float scale)41 void scale_complex(float *c_in, float *c_out, bool is_conjugate, float scale)
42 {
43     const auto a = wrapper::vload(c_in);
44     auto       b = wrapper::vdiv(a, float32x2_t{ scale, scale });
45     if(is_conjugate)
46     {
47         const float img_part = wrapper::vgetlane(b, 1);
48         b                    = wrapper::vsetlane(-img_part, b, 1);
49     }
50 
51     wrapper::vstore(c_out, b);
52 }
53 
validate_arguments(const ITensorInfo * input,const ITensorInfo * output)54 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
55 {
56     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 2, DataType::F32);
57 
58     // Checks performed when output is configured
59     if((output != nullptr) && (output->total_size() != 0))
60     {
61         ARM_COMPUTE_RETURN_ERROR_ON(output->num_channels() != 1 && output->num_channels() != 2);
62         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
63         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
64     }
65 
66     return Status{};
67 }
68 
validate_and_configure_window(ITensorInfo * input,ITensorInfo * output)69 std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
70 {
71     // Configure kernel window
72     Window win = calculate_max_window(*input, Steps());
73 
74     if(output != nullptr)
75     {
76         // Output auto inizialitation if not yet initialized
77         auto_init_if_empty(*output, *input->clone());
78 
79         // NEFFTScaleKernel doesn't need padding so update_window_and_padding() can be skipped
80     }
81 
82     return std::make_pair(Status{}, win);
83 }
84 } // namespace
85 
NEFFTScaleKernel()86 NEFFTScaleKernel::NEFFTScaleKernel()
87     : _input(nullptr), _output(nullptr), _scale(), _run_in_place(false), _is_conj(false)
88 {
89 }
90 
configure(ITensor * input,ITensor * output,const FFTScaleKernelInfo & config)91 void NEFFTScaleKernel::configure(ITensor *input, ITensor *output, const FFTScaleKernelInfo &config)
92 {
93     ARM_COMPUTE_ERROR_ON_NULLPTR(input);
94     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr));
95 
96     _input        = input;
97     _output       = output;
98     _run_in_place = (output == nullptr) || (output == input);
99     _is_conj      = config.conjugate;
100     _scale        = config.scale;
101 
102     // Configure kernel window
103     auto win_config = validate_and_configure_window(input->info(), _run_in_place ? nullptr : output->info());
104     ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
105     INEKernel::configure(win_config.second);
106 }
107 
validate(const ITensorInfo * input,const ITensorInfo * output,const FFTScaleKernelInfo & config)108 Status NEFFTScaleKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const FFTScaleKernelInfo &config)
109 {
110     ARM_COMPUTE_UNUSED(config);
111     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
112     ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
113 
114     return Status{};
115 }
116 
run(const Window & window,const ThreadInfo & info)117 void NEFFTScaleKernel::run(const Window &window, const ThreadInfo &info)
118 {
119     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
120     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
121     ARM_COMPUTE_UNUSED(info);
122 
123     Window input_window = window;
124     input_window.set(Window::DimX, 0);
125 
126     Iterator in(_input, input_window);
127     Iterator out(_run_in_place ? _input : _output, input_window);
128 
129     execute_window_loop(window, [&](const Coordinates &)
130     {
131         scale_complex(reinterpret_cast<float *>(in.ptr()), reinterpret_cast<float *>(out.ptr()), _is_conj, _scale);
132     },
133     in, out);
134 }
135 } // namespace arm_compute
136