blob: 6123f7e25abb67bc1138d127d45be415f7452e6c [file] [log] [blame]
Pablo Marquez Tello67773592023-10-06 13:49:44 +01001/*
2 * Copyright (c) 2021-2023 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#ifndef ACL_SRC_CPU_KERNELS_SUB_NEON_IMPL_H
26#define ACL_SRC_CPU_KERNELS_SUB_NEON_IMPL_H
27
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/ITensor.h"
30#include "arm_compute/core/Types.h"
31#include "arm_compute/core/utils/misc/Traits.h"
32
33#include "src/core/helpers/WindowHelpers.h"
34#include "src/core/NEON/wrapper/intrinsics/intrinsics.h"
35#include "src/core/NEON/wrapper/scalar/sub.h"
36
37namespace arm_compute
38{
39namespace cpu
40{
41template <typename T>
42void sub_same_neon(
43 const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
44{
45 /** SIMD vector tag type. */
46 using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t<T, wrapper::traits::BitWidth::W128>;
47
48 bool is_sat = policy == ConvertPolicy::SATURATE;
49
50 // Create input windows
51 Window input1_win = window.broadcast_if_dimension_le_one(src0->info()->tensor_shape());
52 Window input2_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape());
53
54 // Clear X Dimension on execution window as we handle manually
55 Window win = window;
56 win.set(Window::DimX, Window::Dimension(0, 1, 1));
57
58 constexpr int window_step_x = 16 / sizeof(T);
59 const auto window_start_x = static_cast<int>(window.x().start());
60 const auto window_end_x = static_cast<int>(window.x().end());
61 const bool is_broadcast_across_x = src0->info()->tensor_shape().x() != src1->info()->tensor_shape().x();
62
63 Iterator input1(src0, window.broadcast_if_dimension_le_one(src0->info()->tensor_shape()));
64 Iterator input2(src1, window.broadcast_if_dimension_le_one(src1->info()->tensor_shape()));
65 Iterator output(dst, window);
66
67 if (is_broadcast_across_x)
68 {
69 const bool is_broadcast_input_2 = input2_win.x().step() == 0;
70 Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win;
71 Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win;
72 const ITensor *broadcast_tensor = is_broadcast_input_2 ? src1 : src0;
73 const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? src1 : src0;
74
75 // Clear X Dimension on execution window as we handle manually
76 non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1));
77
78 Iterator broadcast_input(broadcast_tensor, broadcast_win);
79 Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win);
80 Iterator output(dst, win);
81
82 execute_window_loop(
83 win,
84 [&](const Coordinates &)
85 {
86 const auto non_broadcast_input_ptr = reinterpret_cast<const T *>(non_broadcast_input.ptr());
87 const auto output_ptr = reinterpret_cast<T *>(output.ptr());
88
89 const T broadcast_value = *reinterpret_cast<const T *>(broadcast_input.ptr());
90 const auto broadcast_value_vec = wrapper::vdup_n(broadcast_value, ExactTagType{});
91
92 // Compute S elements per iteration
93 int x = window_start_x;
94 for (; x <= (window_end_x - window_step_x); x += window_step_x)
95 {
96 const auto non_broadcast_v = wrapper::vloadq(non_broadcast_input_ptr + x);
97 auto res = is_sat ? wrapper::vqsub(broadcast_value_vec, non_broadcast_v)
98 : wrapper::vsub(broadcast_value_vec, non_broadcast_v);
99 if (is_broadcast_input_2)
100 {
101 res = wrapper::vmul(res, wrapper::vdup_n(static_cast<T>(-1), ExactTagType{}));
102 }
103 wrapper::vstore(output_ptr + x, res);
104 }
105
106 // Compute left-over elements
107 for (; x < window_end_x; ++x)
108 {
109 const auto non_broadcast_v = *(non_broadcast_input_ptr + x);
110 auto res =
111 is_sat ? wrapper::sub_sat(broadcast_value, non_broadcast_v) : broadcast_value - non_broadcast_v;
112 if (is_broadcast_input_2)
113 {
114 res = static_cast<T>(-1) * res;
115 }
116
117 *(output_ptr + x) = res;
118 }
119 },
120 broadcast_input, non_broadcast_input, output);
121 }
122 else
123 {
124 // Clear X Dimension on execution window as we handle manually
125 input1_win.set(Window::DimX, Window::Dimension(0, 1, 1));
126 input2_win.set(Window::DimX, Window::Dimension(0, 1, 1));
127
128 Iterator input1(src0, input1_win);
129 Iterator input2(src1, input2_win);
130 Iterator output(dst, win);
131
132 execute_window_loop(
133 win,
134 [&](const Coordinates &)
135 {
136 const auto input1_ptr = reinterpret_cast<const T *>(input1.ptr());
137 const auto input2_ptr = reinterpret_cast<const T *>(input2.ptr());
138 const auto output_ptr = reinterpret_cast<T *>(output.ptr());
139
140 // Compute S elements per iteration
141 int x = window_start_x;
142 for (; x <= (window_end_x - window_step_x); x += window_step_x)
143 {
144 const auto val1 = wrapper::vloadq(input1_ptr + x);
145 const auto val2 = wrapper::vloadq(input2_ptr + x);
146 const auto res = is_sat ? wrapper::vqsub(val1, val2) : wrapper::vsub(val1, val2);
147 wrapper::vstore(output_ptr + x, res);
148 }
149
150 // Compute left-over elements
151 for (; x < window_end_x; ++x)
152 {
153 const auto val1 = *(input1_ptr + x);
154 const auto val2 = *(input2_ptr + x);
155 *(output_ptr + x) = is_sat ? wrapper::sub_sat(val1, val2) : val1 - val2;
156 }
157 },
158 input1, input2, output);
159 }
160}
161} // namespace cpu
162} // namespace arm_compute
163
164#endif // ACL_SRC_CPU_KERNELS_SUB_NEON_IMPL_H