Compute Library
 21.02
list.h
Go to the documentation of this file.
1 /*
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  */
24 #ifndef SRC_CORE_NEON_KERNELS_ADD_LIST_H
25 #define SRC_CORE_NEON_KERNELS_ADD_LIST_H
26 
27 #include "arm_compute/core/Types.h"
30 
31 namespace arm_compute
32 {
33 namespace cpu
34 {
35 #define DECLARE_ADD_KERNEL(func_name) \
36  void func_name(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
37 
44 
45 #undef DECLARE_ADD_KERNEL
46 
47 template <typename ScalarType>
48 void add_same_neon(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
49 {
50  /** Neon vector tag type. */
52 
53  // Create input windows
54  Window input1_win = window.broadcast_if_dimension_le_one(src0->info()->tensor_shape());
55  Window input2_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape());
56 
57  // Clear X Dimension on execution window as we handle manually
58  Window win = window;
59  win.set(Window::DimX, Window::Dimension(0, 1, 1));
60 
61  constexpr int window_step_x = 16 / sizeof(ScalarType);
62  const auto window_start_x = static_cast<int>(window.x().start());
63  const auto window_end_x = static_cast<int>(window.x().end());
64  const bool is_broadcast_across_x = src0->info()->tensor_shape().x() != src1->info()->tensor_shape().x();
65 
66  if(is_broadcast_across_x)
67  {
68  const bool is_broadcast_input_2 = input2_win.x().step() == 0;
69  Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win;
70  Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win;
71  const ITensor *broadcast_tensor = is_broadcast_input_2 ? src1 : src0;
72  const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? src1 : src0;
73 
74  // Clear X Dimension on execution window as we handle manually
75  non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1));
76 
77  Iterator broadcast_input(broadcast_tensor, broadcast_win);
78  Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win);
79  Iterator output(dst, win);
80 
81  execute_window_loop(win, [&](const Coordinates &)
82  {
83  const auto non_broadcast_input_ptr = reinterpret_cast<const ScalarType *>(non_broadcast_input.ptr());
84  const auto output_ptr = reinterpret_cast<ScalarType *>(output.ptr());
85 
86  const ScalarType broadcast_value = *reinterpret_cast<const ScalarType *>(broadcast_input.ptr());
87  const auto broadcast_value_vec = wrapper::vdup_n(broadcast_value, ExactTagType{});
88 
89  // Compute S elements per iteration
90  int x = window_start_x;
91  for(; x <= (window_end_x - window_step_x); x += window_step_x)
92  {
93  const auto non_broadcast_v = wrapper::vloadq(non_broadcast_input_ptr + x);
94  const auto res = (policy == ConvertPolicy::SATURATE) ? wrapper::vqadd(broadcast_value_vec, non_broadcast_v) : wrapper::vadd(broadcast_value_vec, non_broadcast_v);
95  wrapper::vstore(output_ptr + x, res);
96  }
97 
98  // Compute left-over elements
99  for(; x < window_end_x; ++x)
100  {
101  const auto non_broadcast_v = *(non_broadcast_input_ptr + x);
102  *(output_ptr + x) = (policy == ConvertPolicy::SATURATE) ? wrapper::add_sat(broadcast_value, non_broadcast_v) : broadcast_value + non_broadcast_v;
103  }
104  },
105  broadcast_input, non_broadcast_input, output);
106  }
107  else
108  {
109  // Clear X Dimension on execution window as we handle manually
110  input1_win.set(Window::DimX, Window::Dimension(0, 1, 1));
111  input2_win.set(Window::DimX, Window::Dimension(0, 1, 1));
112 
113  Iterator input1(src0, input1_win);
114  Iterator input2(src1, input2_win);
115  Iterator output(dst, win);
116 
117  execute_window_loop(win, [&](const Coordinates &)
118  {
119  const auto input1_ptr = reinterpret_cast<const ScalarType *>(input1.ptr());
120  const auto input2_ptr = reinterpret_cast<const ScalarType *>(input2.ptr());
121  const auto output_ptr = reinterpret_cast<ScalarType *>(output.ptr());
122 
123  // Compute S elements per iteration
124  int x = window_start_x;
125  for(; x <= (window_end_x - window_step_x); x += window_step_x)
126  {
127  const auto val1 = wrapper::vloadq(input1_ptr + x);
128  const auto val2 = wrapper::vloadq(input2_ptr + x);
129  const auto res = (policy == ConvertPolicy::SATURATE) ? wrapper::vqadd(val1, val2) : wrapper::vadd(val1, val2);
130  wrapper::vstore(output_ptr + x, res);
131  }
132 
133  // Compute left-over elements
134  for(; x < window_end_x; ++x)
135  {
136  const auto val1 = *(input1_ptr + x);
137  const auto val2 = *(input2_ptr + x);
138  *(output_ptr + x) = (policy == ConvertPolicy::SATURATE) ? wrapper::add_sat(val1, val2) : val1 + val2;
139  }
140  },
141  input1, input2, output);
142  }
143 }
144 } // namespace cpu
145 } // namespace arm_compute
146 #endif // SRC_CORE_NEON_KERNELS_ADD_LIST_H
void add_s16_u8_s16_neon(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
Definition: integer.cpp:100
constexpr int step() const
Return the step of the dimension.
Definition: Window.h:104
uint8x16_t vloadq(const uint8_t *ptr)
Definition: load.h:58
uint8x8_t vadd(const uint8x8_t &a, const uint8x8_t &b)
Definition: add.h:39
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:77
Interface for Neon tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
void add_qsymm16_neon(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
Definition: qsymm16.cpp:35
void add_qasymm8_neon(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
Definition: qasymm8.cpp:35
typename neon_bitvector< T, BW >::tag_type neon_bitvector_tag_t
Helper type template to get the tag type of a neon vector.
Definition: traits.h:132
T x() const
Alias to access the size of the first dimension.
Definition: Dimensions.h:87
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
void add_u8_u8_s16_neon(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
Definition: integer.cpp:35
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Coordinates of an item.
Definition: Coordinates.h:37
uint8_t add_sat(const uint8_t &a, const uint8_t &b)
Definition: add.h:33
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:139
uint8x8_t vqadd(const uint8x8_t &a, const uint8x8_t &b)
Definition: add.h:73
#define DECLARE_ADD_KERNEL(func_name)
Definition: list.h:35
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
Window broadcast_if_dimension_le_one(const TensorShape &shape) const
Don&#39;t advance in the dimension where shape is less equal to 1.
Definition: Window.inl:120
void add_qasymm8_signed_neon(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
void vstore(uint8_t *ptr, uint8x8_t val)
Definition: store.h:39
uint8x8_t vdup_n(uint8_t value, traits::vector_64_tag)
Definition: dup_n.h:41
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77
Includes all wrapper headers at once.
constexpr int end() const
Return the end of the dimension.
Definition: Window.h:99
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
void add_same_neon(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
Definition: list.h:48
constexpr int start() const
Return the start of the dimension.
Definition: Window.h:94
Describe a multidimensional execution window.
Definition: Window.h:39
ConvertPolicy
Policy to handle overflow.
Definition: Types.h:385
constexpr const Dimension & x() const
Alias to access the first dimension of the window.
Definition: Window.h:145
void add_u8_s16_s16_neon(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
Definition: integer.cpp:164