Compute Library
 21.02
integer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 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 namespace
36 {
37 void sub_s16_u8_s16_impl(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window, bool is_swapped)
38 {
39  // Create input windows
40  Window win = window;
41  Window input1_win = window.broadcast_if_dimension_le_one(src0->info()->tensor_shape());
42  Window input2_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape());
43 
44  // Clear X Dimension on execution window as we handle manually
45  win.set(Window::DimX, Window::Dimension(0, 1, 1));
46  input1_win.set(Window::DimX, Window::Dimension(0, 1, 1));
47  input2_win.set(Window::DimX, Window::Dimension(0, 1, 1));
48 
49  Iterator input1(src0, input1_win);
50  Iterator input2(src1, input2_win);
51  Iterator output(dst, win);
52 
53  const int window_step_x = 8;
54  const auto window_start_x = static_cast<int>(window.x().start());
55  const auto window_end_x = static_cast<int>(window.x().end());
56 
57  execute_window_loop(win, [&](const Coordinates &)
58  {
59  const auto input1_ptr = reinterpret_cast<const int16_t *>(input1.ptr());
60  const auto input2_ptr = reinterpret_cast<const uint8_t *>(input2.ptr());
61  const auto output_ptr = reinterpret_cast<int16_t *>(output.ptr());
62 
63  if(policy == ConvertPolicy::WRAP)
64  {
65  // Compute S elements per iteration
66  int x = window_start_x;
67  for(; x <= (window_end_x - window_step_x); x += window_step_x)
68  {
69  const auto vin1 = wrapper::vloadq(input1_ptr + x);
70  const auto vin2 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input2_ptr + x)));
71  const auto res = is_swapped ? wrapper::vsub(vin2, vin1) : wrapper::vsub(vin1, vin2);
72  wrapper::vstore(output_ptr + x, res);
73  }
74 
75  // Compute left-over elements
76  for(; x < window_end_x; ++x)
77  {
78  const auto res = is_swapped ? static_cast<int16_t>(*(input2_ptr + x)) - *(input1_ptr + x) : *(input1_ptr + x) - static_cast<int16_t>(*(input2_ptr + x));
79  *(output_ptr + x) = res;
80  }
81  }
82  else
83  {
84  // Compute S elements per iteration
85  int x = window_start_x;
86  for(; x <= (window_end_x - window_step_x); x += window_step_x)
87  {
88  const auto vin1 = wrapper::vloadq(input1_ptr + x);
89  const auto vin2 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input2_ptr + x)));
90  const auto res = is_swapped ? wrapper::vqsub(vin2, vin1) : wrapper::vqsub(vin1, vin2);
91  wrapper::vstore(output_ptr + x, res);
92  }
93 
94  // Compute left-over elements
95  for(; x < window_end_x; ++x)
96  {
97  const auto res = is_swapped ? wrapper::sub_sat(static_cast<int16_t>(*(input2_ptr + x)), *(input1_ptr + x)) : wrapper::sub_sat(*(input1_ptr + x), static_cast<int16_t>(*(input2_ptr + x)));
98  *(output_ptr + x) = res;
99  }
100  }
101  },
102  input1, input2, output);
103 }
104 }
105 
106 void sub_s16_u8_s16_neon(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
107 {
108  sub_s16_u8_s16_impl(src1, src0, dst, policy, window, false);
109 }
110 
111 void sub_u8_s16_s16_neon(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
112 {
113  // Swap arguments
114  sub_s16_u8_s16_impl(src1, src0, dst, policy, window, true);
115 }
116 
117 void sub_u8_u8_s16_neon(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
118 {
119  // Create input windows
120  Window win = window;
121  Window input1_win = window.broadcast_if_dimension_le_one(src0->info()->tensor_shape());
122  Window input2_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape());
123 
124  // Clear X Dimension on execution window as we handle manually
125  win.set(Window::DimX, Window::Dimension(0, 1, 1));
126  input1_win.set(Window::DimX, Window::Dimension(0, 1, 1));
127  input2_win.set(Window::DimX, Window::Dimension(0, 1, 1));
128 
129  Iterator input1(src0, input1_win);
130  Iterator input2(src1, input2_win);
131  Iterator output(dst, win);
132 
133  const int window_step_x = 8;
134  const auto window_start_x = static_cast<int>(window.x().start());
135  const auto window_end_x = static_cast<int>(window.x().end());
136 
137  execute_window_loop(win, [&](const Coordinates &)
138  {
139  const auto input1_ptr = reinterpret_cast<const uint8_t *>(input1.ptr());
140  const auto input2_ptr = reinterpret_cast<const uint8_t *>(input2.ptr());
141  const auto output_ptr = reinterpret_cast<int16_t *>(output.ptr());
142 
143  if(policy == ConvertPolicy::WRAP)
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 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input1_ptr + x)));
150  const auto vin2 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input2_ptr + x)));
151  wrapper::vstore(output_ptr + x, wrapper::vsub(vin1, vin2));
152  }
153 
154  // Compute left-over elements
155  for(; x < window_end_x; ++x)
156  {
157  *(output_ptr + x) = static_cast<int16_t>(*(input1_ptr + x)) - static_cast<int16_t>(*(input2_ptr + x));
158  }
159  }
160  else
161  {
162  // Compute S elements per iteration
163  int x = window_start_x;
164  for(; x <= (window_end_x - window_step_x); x += window_step_x)
165  {
166  const auto vin1 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input1_ptr + x)));
167  const auto vin2 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input2_ptr + x)));
168  wrapper::vstore(output_ptr + x, wrapper::vqsub(vin1, vin2));
169  }
170 
171  // Compute left-over elements
172  for(; x < window_end_x; ++x)
173  {
174  *(output_ptr + x) = wrapper::sub_sat(static_cast<int16_t>(*(input1_ptr + x)),
175  static_cast<int16_t>(*(input2_ptr + x)));
176  }
177  }
178  },
179  input1, input2, output);
180 }
181 
182 } // namespace cpu
183 } // namespace arm_compute
uint8x16_t vloadq(const uint8_t *ptr)
Definition: load.h:58
uint8x8_t vsub(const uint8x8_t &a, const uint8x8_t &b)
Definition: sub.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
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Coordinates of an item.
Definition: Coordinates.h:37
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
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 sub_u8_u8_s16_neon(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
Definition: integer.cpp:117
uint8x8_t vqsub(const uint8x8_t &a, const uint8x8_t &b)
Definition: sub.h:74
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
uint8_t sub_sat(const uint8_t &a, const uint8_t &b)
Definition: sub.h:33
void sub_u8_s16_s16_neon(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
Definition: integer.cpp:111
Includes all wrapper headers at once.
void sub_s16_u8_s16_neon(const ITensor *src0, const ITensor *src1, ITensor *dst, const ConvertPolicy &policy, const Window &window)
Definition: integer.cpp:106
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