Compute Library
 21.02
integer.cpp
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  */
26 #include "arm_compute/core/Types.h"
30 
31 namespace arm_compute
32 {
33 namespace cpu
34 {
35 void add_u8_u8_s16_neon(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
36 {
37  // Create input windows
38  Window win = window;
39  Window input1_win = window.broadcast_if_dimension_le_one(src0->info()->tensor_shape());
40  Window input2_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape());
41 
42  // Clear X Dimension on execution window as we handle manually
43  win.set(Window::DimX, Window::Dimension(0, 1, 1));
44  input1_win.set(Window::DimX, Window::Dimension(0, 1, 1));
45  input2_win.set(Window::DimX, Window::Dimension(0, 1, 1));
46 
47  Iterator input1(src0, input1_win);
48  Iterator input2(src1, input2_win);
49  Iterator output(dst, win);
50 
51  const int window_step_x = 8;
52  const auto window_start_x = static_cast<int>(window.x().start());
53  const auto window_end_x = static_cast<int>(window.x().end());
54 
55  execute_window_loop(win, [&](const Coordinates &)
56  {
57  const auto input1_ptr = reinterpret_cast<const uint8_t *>(input1.ptr());
58  const auto input2_ptr = reinterpret_cast<const uint8_t *>(input2.ptr());
59  const auto output_ptr = reinterpret_cast<int16_t *>(output.ptr());
60 
61  if(policy == ConvertPolicy::WRAP)
62  {
63  // Compute S elements per iteration
64  int x = window_start_x;
65  for(; x <= (window_end_x - window_step_x); x += window_step_x)
66  {
67  const auto vin1 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input1_ptr + x)));
68  const auto vin2 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input2_ptr + x)));
69  wrapper::vstore(output_ptr + x, wrapper::vadd(vin1, vin2));
70  }
71 
72  // Compute left-over elements
73  for(; x < window_end_x; ++x)
74  {
75  *(output_ptr + x) = static_cast<int16_t>(*(input1_ptr + x)) + static_cast<int16_t>(*(input2_ptr + x));
76  }
77  }
78  else
79  {
80  // Compute S elements per iteration
81  int x = window_start_x;
82  for(; x <= (window_end_x - window_step_x); x += window_step_x)
83  {
84  const auto vin1 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input1_ptr + x)));
85  const auto vin2 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input2_ptr + x)));
86  wrapper::vstore(output_ptr + x, wrapper::vqadd(vin1, vin2));
87  }
88 
89  // Compute left-over elements
90  for(; x < window_end_x; ++x)
91  {
92  *(output_ptr + x) = wrapper::add_sat(static_cast<int16_t>(*(input1_ptr + x)),
93  static_cast<int16_t>(*(input2_ptr + x)));
94  }
95  }
96  },
97  input1, input2, output);
98 }
99 
100 void add_s16_u8_s16_neon(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
101 {
102  // Create input windows
103  Window win = window;
104  Window input1_win = window.broadcast_if_dimension_le_one(src0->info()->tensor_shape());
105  Window input2_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape());
106 
107  // Clear X Dimension on execution window as we handle manually
108  win.set(Window::DimX, Window::Dimension(0, 1, 1));
109  input1_win.set(Window::DimX, Window::Dimension(0, 1, 1));
110  input2_win.set(Window::DimX, Window::Dimension(0, 1, 1));
111 
112  Iterator input1(src0, input1_win);
113  Iterator input2(src1, input2_win);
114  Iterator output(dst, win);
115 
116  const int window_step_x = 8;
117  const auto window_start_x = static_cast<int>(window.x().start());
118  const auto window_end_x = static_cast<int>(window.x().end());
119 
120  execute_window_loop(win, [&](const Coordinates &)
121  {
122  const auto input1_ptr = reinterpret_cast<const int16_t *>(input1.ptr());
123  const auto input2_ptr = reinterpret_cast<const uint8_t *>(input2.ptr());
124  const auto output_ptr = reinterpret_cast<int16_t *>(output.ptr());
125 
126  if(policy == ConvertPolicy::WRAP)
127  {
128  // Compute S elements per iteration
129  int x = window_start_x;
130  for(; x <= (window_end_x - window_step_x); x += window_step_x)
131  {
132  const auto vin1 = wrapper::vloadq(input1_ptr + x);
133  const auto vin2 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input2_ptr + x)));
134  wrapper::vstore(output_ptr + x, wrapper::vadd(vin1, vin2));
135  }
136 
137  // Compute left-over elements
138  for(; x < window_end_x; ++x)
139  {
140  *(output_ptr + x) = *(input1_ptr + x) + static_cast<int16_t>(*(input2_ptr + x));
141  }
142  }
143  else
144  {
145  // Compute S elements per iteration
146  int x = window_start_x;
147  for(; x <= (window_end_x - window_step_x); x += window_step_x)
148  {
149  const auto vin1 = wrapper::vloadq(input1_ptr + x);
150  const auto vin2 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input2_ptr + x)));
151  wrapper::vstore(output_ptr + x, wrapper::vqadd(vin1, vin2));
152  }
153 
154  // Compute left-over elements
155  for(; x < window_end_x; ++x)
156  {
157  *(output_ptr + x) = wrapper::add_sat(*(input1_ptr + x), static_cast<int16_t>(*(input2_ptr + x)));
158  }
159  }
160  },
161  input1, input2, output);
162 }
163 
164 void add_u8_s16_s16_neon(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
165 {
166  // Simply swap the two input buffers:
167  add_s16_u8_s16_neon(src1, src0, dst, policy, window);
168 }
169 } // namespace cpu
170 } // namespace arm_compute
void add_s16_u8_s16_neon(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
Definition: integer.cpp:100
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.
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
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
uint8x8_t vload(const uint8_t *ptr)
Definition: load.h:39
void vstore(uint8_t *ptr, uint8x8_t val)
Definition: store.h:39
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
uint16x8_t vmovl(const uint8x8_t &a)
Definition: movl.h:39
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