blob: 71dd875ad852198f9308036f863cecd31ee00d97 [file] [log] [blame]
Michalis Spyroua3c9a3b2020-12-08 21:02:16 +00001/*
2 * Copyright (c) 2020-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 */
Sheri Zhang61243902021-01-12 18:25:16 +000024#ifndef SRC_CORE_SVE_KERNELS_ADD_LIST_H
25#define SRC_CORE_SVE_KERNELS_ADD_LIST_H
Michalis Spyroua3c9a3b2020-12-08 21:02:16 +000026
27#if defined(__ARM_FEATURE_SVE)
28#include "arm_compute/core/Types.h"
29#include "arm_compute/core/utils/misc/Traits.h"
30#include "src/core/NEON/SVEMath.h"
31#include "src/core/NEON/wrapper/intrinsics/intrinsics.h"
32#include <arm_sve.h>
33
34namespace arm_compute
35{
36namespace cpu
37{
Sheri Zhang61243902021-01-12 18:25:16 +000038#define DECLARE_ADD_KERNEL(func_name) \
39 void func_name(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
Michalis Spyroua3c9a3b2020-12-08 21:02:16 +000040
Sheri Zhang61243902021-01-12 18:25:16 +000041DECLARE_ADD_KERNEL(add_qasymm8_sve);
42DECLARE_ADD_KERNEL(add_qasymm8_signed_sve);
43DECLARE_ADD_KERNEL(add_qsymm16_sve);
44DECLARE_ADD_KERNEL(add_s16_u8_s16_sve);
45DECLARE_ADD_KERNEL(add_u8_s16_s16_sve);
46DECLARE_ADD_KERNEL(add_u8_u8_s16_sve);
Michalis Spyroua3c9a3b2020-12-08 21:02:16 +000047
Sheri Zhang61243902021-01-12 18:25:16 +000048#undef DECLARE_ADD_KERNEL
Michalis Spyroua3c9a3b2020-12-08 21:02:16 +000049
50template <typename ScalarType>
Sheri Zhang61243902021-01-12 18:25:16 +000051void add_same_sve(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
Michalis Spyroua3c9a3b2020-12-08 21:02:16 +000052{
53 const auto all_true_pg = wrapper::svptrue<ScalarType>();
54 const auto window_start_x = static_cast<int>(window.x().start());
55 const auto window_end_x = static_cast<int>(window.x().end());
Sheri Zhang61243902021-01-12 18:25:16 +000056 const bool is_broadcast_across_x = src0->info()->tensor_shape().x() != src1->info()->tensor_shape().x();
Michalis Spyroua3c9a3b2020-12-08 21:02:16 +000057 const bool is_sat = (policy == ConvertPolicy::SATURATE);
58
59 // Clear X Dimension on execution window as we handle manually
60 Window win = window;
61 win.set(Window::DimX, Window::Dimension(0, 1, 1));
62
63 // Create input windows
Sheri Zhang61243902021-01-12 18:25:16 +000064 Window input1_win = window.broadcast_if_dimension_le_one(src0->info()->tensor_shape());
65 Window input2_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape());
Michalis Spyroua3c9a3b2020-12-08 21:02:16 +000066
Sheri Zhang61243902021-01-12 18:25:16 +000067 Iterator input1(src0, window.broadcast_if_dimension_le_one(src0->info()->tensor_shape()));
68 Iterator input2(src1, window.broadcast_if_dimension_le_one(src1->info()->tensor_shape()));
69 Iterator output(dst, window);
Michalis Spyroua3c9a3b2020-12-08 21:02:16 +000070
71 if(is_broadcast_across_x)
72 {
73 const bool is_broadcast_input_2 = input2_win.x().step() == 0;
74 Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win;
75 Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win;
Sheri Zhang61243902021-01-12 18:25:16 +000076 const ITensor *broadcast_tensor = is_broadcast_input_2 ? src1 : src0;
77 const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? src1 : src0;
Michalis Spyroua3c9a3b2020-12-08 21:02:16 +000078
79 // Clear X Dimension on execution window as we handle manually
80 non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1));
81
82 Iterator broadcast_input(broadcast_tensor, broadcast_win);
83 Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win);
Sheri Zhang61243902021-01-12 18:25:16 +000084 Iterator output(dst, win);
Michalis Spyroua3c9a3b2020-12-08 21:02:16 +000085
86 execute_window_loop(win, [&](const Coordinates &)
87 {
88 const auto non_broadcast_input_ptr = reinterpret_cast<const ScalarType *>(non_broadcast_input.ptr());
89 const auto output_ptr = reinterpret_cast<ScalarType *>(output.ptr());
90
91 const ScalarType broadcast_value = *reinterpret_cast<const ScalarType *>(broadcast_input.ptr());
92 const auto broadcast_value_vec = wrapper::svdup_n(broadcast_value);
93
94 int x = window_start_x;
95 svbool_t pg = wrapper::svwhilelt<ScalarType>(x, window_end_x);
96 do
97 {
98 const auto non_broadcast_v = svld1(pg, non_broadcast_input_ptr + x);
99 auto res = is_sat ? wrapper::svqadd(broadcast_value_vec, non_broadcast_v) : svadd_z(pg, broadcast_value_vec, non_broadcast_v);
100 svst1(pg, output_ptr + x, res);
101
102 x += wrapper::svcnt<ScalarType>();
103 pg = wrapper::svwhilelt<ScalarType>(x, window_end_x);
104 }
105 while(svptest_any(all_true_pg, pg));
106 },
107 broadcast_input, non_broadcast_input, output);
108 }
109 else
110 {
111 // Clear X Dimension on execution window as we handle manually
112 input1_win.set(Window::DimX, Window::Dimension(0, 1, 1));
113 input2_win.set(Window::DimX, Window::Dimension(0, 1, 1));
114
Sheri Zhang61243902021-01-12 18:25:16 +0000115 Iterator input1(src0, input1_win);
116 Iterator input2(src1, input2_win);
117 Iterator output(dst, win);
Michalis Spyroua3c9a3b2020-12-08 21:02:16 +0000118
119 execute_window_loop(win, [&](const Coordinates &)
120 {
121 const auto input1_ptr = reinterpret_cast<const ScalarType *>(input1.ptr());
122 const auto input2_ptr = reinterpret_cast<const ScalarType *>(input2.ptr());
123 const auto output_ptr = reinterpret_cast<ScalarType *>(output.ptr());
124
125 int x = window_start_x;
126 svbool_t pg = wrapper::svwhilelt<ScalarType>(x, window_end_x);
127 do
128 {
129 const auto val1 = svld1(pg, input1_ptr + x);
130 const auto val2 = svld1(pg, input2_ptr + x);
131 const auto res = is_sat ? wrapper::svqadd(val1, val2) : svadd_z(pg, val1, val2);
132 svst1(pg, output_ptr + x, res);
133
134 x += wrapper::svcnt<ScalarType>();
135 pg = wrapper::svwhilelt<ScalarType>(x, window_end_x);
136 }
137 while(svptest_any(all_true_pg, pg));
138 },
139 input1, input2, output);
140 }
141}
142} // namespace cpu
143} // namespace arm_compute
144#endif // defined(__ARM_FEATURE_SVE)
Sheri Zhang61243902021-01-12 18:25:16 +0000145#endif // SRC_CORE_SVE_KERNELS_ADD_LIST_H