xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/kernels/add/generic/sve/impl.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 "src/cpu/kernels/add/generic/sve/impl.h"
26 #include "arm_compute/core/Helpers.h"
27 #include "arm_compute/core/utils/misc/Traits.h"
28 #include "src/core/NEON/SVEMath.h"
29 #include "src/core/NEON/wrapper/intrinsics/intrinsics.h"
30 #include <arm_sve.h>
31 namespace arm_compute
32 {
33 namespace cpu
34 {
35 template <typename ScalarType>
add_same_sve(const ITensor * src0,const ITensor * src1,ITensor * dst,const ConvertPolicy & policy,const Window & window)36 void add_same_sve(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
37 {
38     const auto all_true_pg           = wrapper::svptrue<ScalarType>();
39     const auto window_start_x        = static_cast<int>(window.x().start());
40     const auto window_end_x          = static_cast<int>(window.x().end());
41     const bool is_broadcast_across_x = src0->info()->tensor_shape().x() != src1->info()->tensor_shape().x();
42     const bool is_sat                = (policy == ConvertPolicy::SATURATE);
43 
44     // Clear X Dimension on execution window as we handle manually
45     Window win = window;
46     win.set(Window::DimX, Window::Dimension(0, 1, 1));
47 
48     // Create input windows
49     Window input1_win = window.broadcast_if_dimension_le_one(src0->info()->tensor_shape());
50     Window input2_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape());
51 
52     Iterator input1(src0, window.broadcast_if_dimension_le_one(src0->info()->tensor_shape()));
53     Iterator input2(src1, window.broadcast_if_dimension_le_one(src1->info()->tensor_shape()));
54     Iterator output(dst, window);
55 
56     if(is_broadcast_across_x)
57     {
58         const bool     is_broadcast_input_2 = input2_win.x().step() == 0;
59         Window         broadcast_win        = is_broadcast_input_2 ? input2_win : input1_win;
60         Window         non_broadcast_win    = !is_broadcast_input_2 ? input2_win : input1_win;
61         const ITensor *broadcast_tensor     = is_broadcast_input_2 ? src1 : src0;
62         const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? src1 : src0;
63 
64         // Clear X Dimension on execution window as we handle manually
65         non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1));
66 
67         Iterator broadcast_input(broadcast_tensor, broadcast_win);
68         Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win);
69         Iterator output(dst, win);
70 
71         execute_window_loop(win, [&](const Coordinates &)
72         {
73             const auto non_broadcast_input_ptr = reinterpret_cast<const ScalarType *>(non_broadcast_input.ptr());
74             const auto output_ptr              = reinterpret_cast<ScalarType *>(output.ptr());
75 
76             const ScalarType broadcast_value     = *reinterpret_cast<const ScalarType *>(broadcast_input.ptr());
77             const auto       broadcast_value_vec = wrapper::svdup_n(broadcast_value);
78 
79             int      x  = window_start_x;
80             svbool_t pg = wrapper::svwhilelt<ScalarType>(x, window_end_x);
81             do
82             {
83                 const auto non_broadcast_v = svld1(pg, non_broadcast_input_ptr + x);
84                 auto       res             = is_sat ? wrapper::svqadd(broadcast_value_vec, non_broadcast_v) : svadd_z(pg, broadcast_value_vec, non_broadcast_v);
85                 svst1(pg, output_ptr + x, res);
86 
87                 x += wrapper::svcnt<ScalarType>();
88                 pg = wrapper::svwhilelt<ScalarType>(x, window_end_x);
89             }
90             while(svptest_any(all_true_pg, pg));
91         },
92         broadcast_input, non_broadcast_input, output);
93     }
94     else
95     {
96         // Clear X Dimension on execution window as we handle manually
97         input1_win.set(Window::DimX, Window::Dimension(0, 1, 1));
98         input2_win.set(Window::DimX, Window::Dimension(0, 1, 1));
99 
100         Iterator input1(src0, input1_win);
101         Iterator input2(src1, input2_win);
102         Iterator output(dst, win);
103 
104         execute_window_loop(win, [&](const Coordinates &)
105         {
106             const auto input1_ptr = reinterpret_cast<const ScalarType *>(input1.ptr());
107             const auto input2_ptr = reinterpret_cast<const ScalarType *>(input2.ptr());
108             const auto output_ptr = reinterpret_cast<ScalarType *>(output.ptr());
109 
110             int      x  = window_start_x;
111             svbool_t pg = wrapper::svwhilelt<ScalarType>(x, window_end_x);
112             do
113             {
114                 const auto val1 = svld1(pg, input1_ptr + x);
115                 const auto val2 = svld1(pg, input2_ptr + x);
116                 const auto res  = is_sat ? wrapper::svqadd(val1, val2) : svadd_z(pg, val1, val2);
117                 svst1(pg, output_ptr + x, res);
118 
119                 x += wrapper::svcnt<ScalarType>();
120                 pg = wrapper::svwhilelt<ScalarType>(x, window_end_x);
121             }
122             while(svptest_any(all_true_pg, pg));
123         },
124         input1, input2, output);
125     }
126 }
127 template void add_same_sve<float>(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window);
128 template void add_same_sve<uint8_t>(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window);
129 template void add_same_sve<int16_t>(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window);
130 template void add_same_sve<int32_t>(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window);
131 #if defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS)
132 template void add_same_sve<float16_t>(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window);
133 #endif /* (__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && defined(ENABLE_FP16_KERNELS) */
134 } // namespace cpu
135 } // namespace arm_compute
136