xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/kernels/crop/generic/neon/impl.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2018-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/cpu/kernels/crop/generic/neon/impl.h"
25 #include "arm_compute/core/Helpers.h"
26 #include "arm_compute/core/TensorInfo.h"
27 #include "src/core/NEON/wrapper/wrapper.h"
28 #include "src/core/common/Registrars.h"
29 #include "src/cpu/kernels/crop/generic/neon/crop_helper.h"
30 
31 namespace arm_compute
32 {
33 namespace cpu
34 {
35 template <typename T>
in_bounds_crop_window(const ITensor * input,const ITensor * output,float * output_ptr,Coordinates input_offset,int32_t window_step_x,int32_t output_width_start,int32_t output_width_limit,bool input_has_single_channel,bool is_width_flipped)36 void in_bounds_crop_window(const ITensor *input, const ITensor *output, float *output_ptr, Coordinates input_offset,
37                            int32_t window_step_x, int32_t output_width_start, int32_t output_width_limit, bool input_has_single_channel, bool is_width_flipped)
38 {
39     // Reverse elements if width flipped.
40     if(is_width_flipped)
41     {
42         // Collapse first dimension if possible.
43         if(input_has_single_channel)
44         {
45             int32_t     x = output_width_start;
46             Coordinates negative_offset(input_offset);
47             negative_offset.set(1, negative_offset[1] - window_step_x + 1);
48             for(; x <= output_width_limit - window_step_x; x += window_step_x, negative_offset[1] -= window_step_x)
49             {
50                 auto in = load_as_f32(reinterpret_cast<T *>(input->ptr_to_element(negative_offset)));
51 
52                 in = wrapper::vrev64(in);
53                 in = wrapper::vcombine(wrapper::vgethigh(in), wrapper::vgetlow(in));
54 
55                 wrapper::vstore(output_ptr + x, in);
56             }
57             input_offset[1] = negative_offset[1] + window_step_x - 1;
58             for(; x < output_width_limit; ++x, --input_offset[1])
59             {
60                 *(output_ptr + x) = static_cast<float>(*reinterpret_cast<T *>(input->ptr_to_element(input_offset)));
61             }
62         }
63         else
64         {
65             for(int32_t x = output_width_start; x < output_width_limit; ++x, --input_offset[1])
66             {
67                 input_offset.set(0, 0);
68                 int32_t c = 0;
69                 for(; c <= static_cast<int32_t>(input->info()->dimension(0)) - window_step_x; c += window_step_x, input_offset[0] += window_step_x)
70                 {
71                     auto in = load_as_f32(reinterpret_cast<T *>(input->ptr_to_element(input_offset)));
72                     wrapper::vstore(output_ptr + x * output->info()->dimension(0) + c, in);
73                 }
74                 for(; c < static_cast<int32_t>(input->info()->dimension(0)); ++c, ++input_offset[0])
75                 {
76                     *(output_ptr + x * output->info()->dimension(0) + c) = static_cast<float>(*reinterpret_cast<T *>(input->ptr_to_element(input_offset)));
77                 }
78             }
79         }
80     }
81     else
82     {
83         // Use memcpy if the elements don't need converting to float.
84         if(std::is_same<T, float>::value)
85         {
86             memcpy(static_cast<void *>(output_ptr + output_width_start * output->info()->dimension(0)),
87                    reinterpret_cast<const void *>(input->ptr_to_element(input_offset)),
88                    (output_width_limit - output_width_start) * output->info()->dimension(0) * output->info()->element_size());
89         }
90         else
91         {
92             int32_t x                = 0;
93             int32_t limit            = (output_width_limit - output_width_start) * static_cast<int32_t>(output->info()->dimension(0));
94             float *output_start_ptr = output_ptr + output_width_start * output->info()->dimension(0);
95             for(; x <= limit - window_step_x; x += window_step_x, input_offset[0] += window_step_x)
96             {
97                 auto in = load_as_f32(reinterpret_cast<T *>(input->ptr_to_element(input_offset)));
98                 wrapper::vstore(output_start_ptr + x, in);
99             }
100             for(; x < limit; ++x, ++input_offset[0])
101             {
102                 *(output_start_ptr + x) = static_cast<float>(*reinterpret_cast<T *>(input->ptr_to_element(input_offset)));
103             }
104         }
105     }
106 }
107 
108 #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
109 template void in_bounds_crop_window<float16_t>(const ITensor *input, const ITensor *output, float *output_ptr, Coordinates input_offset,
110                                                int32_t window_step_x, int32_t output_width_start, int32_t output_width_limit, bool input_has_single_channel, bool is_width_flipped);
111 #endif /* defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS) */
112 template void in_bounds_crop_window<float32_t>(const ITensor *input, const ITensor *output, float *output_ptr, Coordinates input_offset,
113                                                int32_t window_step_x, int32_t output_width_start, int32_t output_width_limit, bool input_has_single_channel, bool is_width_flipped);
114 template void in_bounds_crop_window<uint8_t>(const ITensor *input, const ITensor *output, float *output_ptr, Coordinates input_offset,
115                                              int32_t window_step_x, int32_t output_width_start, int32_t output_width_limit, bool input_has_single_channel, bool is_width_flipped);
116 template void in_bounds_crop_window<uint16_t>(const ITensor *input, const ITensor *output, float *output_ptr, Coordinates input_offset,
117                                               int32_t window_step_x, int32_t output_width_start, int32_t output_width_limit, bool input_has_single_channel, bool is_width_flipped);
118 template void in_bounds_crop_window<uint32_t>(const ITensor *input, const ITensor *output, float *output_ptr, Coordinates input_offset,
119                                               int32_t window_step_x, int32_t output_width_start, int32_t output_width_limit, bool input_has_single_channel, bool is_width_flipped);
120 template void in_bounds_crop_window<int8_t>(const ITensor *input, const ITensor *output, float *output_ptr, Coordinates input_offset,
121                                             int32_t window_step_x, int32_t output_width_start, int32_t output_width_limit, bool input_has_single_channel, bool is_width_flipped);
122 template void in_bounds_crop_window<int16_t>(const ITensor *input, const ITensor *output, float *output_ptr, Coordinates input_offset,
123                                              int32_t window_step_x, int32_t output_width_start, int32_t output_width_limit, bool input_has_single_channel, bool is_width_flipped);
124 template void in_bounds_crop_window<int32_t>(const ITensor *input, const ITensor *output, float *output_ptr, Coordinates input_offset,
125                                              int32_t window_step_x, int32_t output_width_start, int32_t output_width_limit, bool input_has_single_channel, bool is_width_flipped);
126 }
127 } // namespace arm_compute
128