• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2019-2020 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         Coordinates coord;
81         coord.set_num_dimensions(output->num_dimensions());
82         output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
83     }
84 
85     return std::make_pair(Status{}, win);
86 }
87 } // namespace
88 
NEFFTScaleKernel()89 NEFFTScaleKernel::NEFFTScaleKernel()
90     : _input(nullptr), _output(nullptr), _scale(), _run_in_place(false), _is_conj(false)
91 {
92 }
93 
configure(ITensor * input,ITensor * output,const FFTScaleKernelInfo & config)94 void NEFFTScaleKernel::configure(ITensor *input, ITensor *output, const FFTScaleKernelInfo &config)
95 {
96     ARM_COMPUTE_ERROR_ON_NULLPTR(input);
97     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (output != nullptr) ? output->info() : nullptr));
98 
99     _input        = input;
100     _output       = output;
101     _run_in_place = (output == nullptr) || (output == input);
102     _is_conj      = config.conjugate;
103     _scale        = config.scale;
104 
105     // Configure kernel window
106     auto win_config = validate_and_configure_window(input->info(), _run_in_place ? nullptr : output->info());
107     ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
108     INEKernel::configure(win_config.second);
109 }
110 
validate(const ITensorInfo * input,const ITensorInfo * output,const FFTScaleKernelInfo & config)111 Status NEFFTScaleKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const FFTScaleKernelInfo &config)
112 {
113     ARM_COMPUTE_UNUSED(config);
114     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
115     ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
116 
117     return Status{};
118 }
119 
run(const Window & window,const ThreadInfo & info)120 void NEFFTScaleKernel::run(const Window &window, const ThreadInfo &info)
121 {
122     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
123     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
124     ARM_COMPUTE_UNUSED(info);
125 
126     Window input_window = window;
127     input_window.set(Window::DimX, 0);
128 
129     Iterator in(_input, input_window);
130     Iterator out(_run_in_place ? _input : _output, input_window);
131 
132     execute_window_loop(window, [&](const Coordinates &)
133     {
134         scale_complex(reinterpret_cast<float *>(in.ptr()), reinterpret_cast<float *>(out.ptr()), _is_conj, _scale);
135     },
136     in, out);
137 }
138 } // namespace arm_compute
139