xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/kernels/scale/sve/integer.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2021-2022 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 
25 #include "arm_compute/core/Helpers.h"
26 #include "arm_compute/core/ITensorPack.h"
27 #include "arm_compute/core/Window.h"
28 #include "src/core/NEON/NEMath.h"
29 #include "src/core/NEON/wrapper/wrapper.h"
30 #include "src/core/helpers/ScaleHelpers.h"
31 #include "src/core/utils/ScaleUtils.h"
32 #include "support/Rounding.h"
33 
34 #include <arm_sve.h>
35 #include <cmath>
36 #include <cstddef>
37 
38 namespace arm_compute
39 {
40 namespace
41 {
u8_sve_scale_nearest(const ITensor * src,ITensor * dst,const ITensor * offsets,float sampling_offset,bool align_corners,const Window & window)42 void u8_sve_scale_nearest(const ITensor *src, ITensor *dst, const ITensor *offsets,
43                           float sampling_offset, bool align_corners, const Window &window)
44 {
45     const size_t in_stride_c  = src->info()->dimension(0) + src->info()->padding().left + src->info()->padding().right;
46     const size_t in_stride_w  = src->info()->dimension(1) + src->info()->padding().top + src->info()->padding().bottom;
47     const size_t in_stride_wc = in_stride_w * in_stride_c;
48     const size_t in_dim_h     = src->info()->dimension(2);
49 
50     // Compute the ratio between source height and destination height
51     const auto hr             = scale_utils::calculate_resize_ratio(in_dim_h, dst->info()->dimension(2), align_corners);
52     const auto window_start_x = static_cast<int32_t>(window.x().start());
53     const auto window_end_x   = static_cast<int32_t>(window.x().end());
54 
55     Window win(window);
56     win.set(Window::DimX, Window::Dimension(0, 1, 1));
57     Iterator out(dst, win);
58 
59     const uint8_t     *in_ptr_start        = src->buffer() + src->info()->offset_first_element_in_bytes();
60     const unsigned int in_stride_bytes_hwc = src->info()->strides_in_bytes()[3];
61 
62     execute_window_loop(win, [&](const Coordinates & id)
63     {
64         const int32_t offset     = *reinterpret_cast<const int32_t *>(offsets->ptr_to_element(Coordinates(id.y(), id.z()))) * in_stride_c;
65         const auto    in_hi      = static_cast<int>(align_corners ? utils::rounding::round_half_away_from_zero((id.z() + sampling_offset) * hr) : std::floor((id.z() + sampling_offset) * hr));
66         const int     offset_row = in_hi * in_stride_wc;
67         const auto    in_ptr     = reinterpret_cast<const uint8_t *>(in_ptr_start + in_stride_bytes_hwc * id[3]);
68         const auto    out_ptr    = reinterpret_cast<uint8_t *>(out.ptr());
69 
70         // Compute S elements per iteration
71         int      x  = window_start_x;
72         svbool_t pg = svwhilelt_b8(x, window_end_x);
73         do
74         {
75             // Store results
76             svst1_u8(pg, out_ptr + x, svld1_u8(pg, in_ptr + offset + offset_row + x));
77 
78             x += svcntw();
79             pg = svwhilelt_b8(x, window_end_x);
80         }
81         while(svptest_any(svptrue_b8(), pg));
82     },
83     out);
84 }
85 
s16_sve_scale_nearest(const ITensor * src,ITensor * dst,const ITensor * offsets,float sampling_offset,bool align_corners,const Window & window)86 void s16_sve_scale_nearest(const ITensor *src, ITensor *dst, const ITensor *offsets,
87                            float sampling_offset, bool align_corners, const Window &window)
88 {
89     const size_t in_stride_c  = src->info()->dimension(0) + src->info()->padding().left + src->info()->padding().right;
90     const size_t in_stride_w  = src->info()->dimension(1) + src->info()->padding().top + src->info()->padding().bottom;
91     const size_t in_stride_wc = in_stride_w * in_stride_c;
92     const size_t in_dim_h     = src->info()->dimension(2);
93 
94     // Compute the ratio between source height and destination height
95     const auto hr             = scale_utils::calculate_resize_ratio(in_dim_h, dst->info()->dimension(2), align_corners);
96     const auto window_start_x = static_cast<int32_t>(window.x().start());
97     const auto window_end_x   = static_cast<int32_t>(window.x().end());
98 
99     Window win(window);
100     win.set(Window::DimX, Window::Dimension(0, 1, 1));
101     Iterator out(dst, win);
102 
103     const uint8_t     *in_ptr_start        = src->buffer() + src->info()->offset_first_element_in_bytes();
104     const unsigned int in_stride_bytes_hwc = src->info()->strides_in_bytes()[3];
105 
106     execute_window_loop(win, [&](const Coordinates & id)
107     {
108         const int32_t offset     = *reinterpret_cast<const int32_t *>(offsets->ptr_to_element(Coordinates(id.y(), id.z()))) * in_stride_c;
109         const auto    in_hi      = static_cast<int>(align_corners ? utils::rounding::round_half_away_from_zero((id.z() + sampling_offset) * hr) : std::floor((id.z() + sampling_offset) * hr));
110         const int     offset_row = in_hi * in_stride_wc;
111         const auto    in_ptr     = reinterpret_cast<const int16_t *>(in_ptr_start + in_stride_bytes_hwc * id[3]);
112         const auto    out_ptr    = reinterpret_cast<int16_t *>(out.ptr());
113 
114         // Compute S elements per iteration
115         int      x  = window_start_x;
116         svbool_t pg = svwhilelt_b16(x, window_end_x);
117         do
118         {
119             // Store results
120             svst1_s16(pg, out_ptr + x, svld1_s16(pg, in_ptr + offset + offset_row + x));
121 
122             x += svcntw();
123             pg = svwhilelt_b16(x, window_end_x);
124         }
125         while(svptest_any(svptrue_b16(), pg));
126     },
127     out);
128 }
129 }
130 namespace cpu
131 {
u8_sve_scale(const ITensor * src,ITensor * dst,const ITensor * offsets,const ITensor * dx,const ITensor * dy,InterpolationPolicy policy,BorderMode border_mode,PixelValue constant_border_value,float sampling_offset,bool align_corners,const Window & window)132 void u8_sve_scale(const ITensor *src, ITensor *dst, const ITensor *offsets, const ITensor *dx, const ITensor *dy,
133                   InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, float sampling_offset,
134                   bool align_corners, const Window &window)
135 {
136     ARM_COMPUTE_UNUSED(dx, dy, border_mode, constant_border_value);
137     if(policy == InterpolationPolicy::NEAREST_NEIGHBOR)
138     {
139         u8_sve_scale_nearest(src, dst, offsets, sampling_offset, align_corners, window);
140     }
141     else
142     {
143         ARM_COMPUTE_ERROR("Not Implemented");
144     }
145 }
146 
s16_sve_scale(const ITensor * src,ITensor * dst,const ITensor * offsets,const ITensor * dx,const ITensor * dy,InterpolationPolicy policy,BorderMode border_mode,PixelValue constant_border_value,float sampling_offset,bool align_corners,const Window & window)147 void s16_sve_scale(const ITensor *src, ITensor *dst, const ITensor *offsets, const ITensor *dx, const ITensor *dy,
148                    InterpolationPolicy policy, BorderMode border_mode, PixelValue constant_border_value, float sampling_offset,
149                    bool align_corners, const Window &window)
150 {
151     ARM_COMPUTE_UNUSED(dx, dy, border_mode, constant_border_value);
152     if(policy == InterpolationPolicy::NEAREST_NEIGHBOR)
153     {
154         s16_sve_scale_nearest(src, dst, offsets, sampling_offset, align_corners, window);
155     }
156     else
157     {
158         ARM_COMPUTE_ERROR("Not Implemented");
159     }
160 }
161 } // namespace cpu
162 } // namespace arm_compute
163