Compute Library
 21.02
NEIntegralImageKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2020 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  */
25 
29 #include "arm_compute/core/Types.h"
33 
34 #include <arm_neon.h>
35 #include <cstddef>
36 #include <cstdint>
37 
38 using namespace arm_compute;
39 
41 {
44 
45  _input = input;
46  _output = output;
47 
48  constexpr unsigned int num_elems_processed_per_iteration = 16;
49 
50  // Configure kernel window
51  Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
52  // The kernel is effectively reading 17 values from -1 as it loads 16
53  // starting at -1 and also 16 starting at 0
54  AccessWindowRectangle output_read_access(output->info(), -1, -1, num_elems_processed_per_iteration + 1, 1);
55  AccessWindowHorizontal output_write_access(output->info(), 0, num_elems_processed_per_iteration);
56 
59  output_read_access, output_write_access);
60 
61  output_write_access.set_valid_region(win, input->info()->valid_region());
62 
63  IKernel::configure(win);
64 }
65 
67 {
68  return BorderSize{ 1, 0, 0, 1 };
69 }
70 
72 {
73  return false;
74 }
75 
77 {
78  ARM_COMPUTE_UNUSED(info);
81 
82  Iterator input(_input, window);
83  Iterator output(_output, window);
84 
85  const auto output_top_left = reinterpret_cast<const uint32_t *>(_output->ptr_to_element(Coordinates(-1, -1)));
86  const auto output_top_mid = reinterpret_cast<const uint32_t *>(_output->ptr_to_element(Coordinates(0, -1)));
87 
88  execute_window_loop(window, [&](const Coordinates &)
89  {
90  const uint8x16_t input_pixels = vld1q_u8(input.ptr());
91 
92  const uint16x8x2_t tmp =
93  {
94  {
95  vmovl_u8(vget_low_u8(input_pixels)),
96  vmovl_u8(vget_high_u8(input_pixels))
97  }
98  };
99 
100  uint32x4x4_t pixels =
101  {
102  {
103  vmovl_u16(vget_low_u16(tmp.val[0])),
104  vmovl_u16(vget_high_u16(tmp.val[0])),
105  vmovl_u16(vget_low_u16(tmp.val[1])),
106  vmovl_u16(vget_high_u16(tmp.val[1]))
107  }
108  };
109 
110  // Divide by four as pointer is now uint32 instead of uint8!
111  const size_t off = output.offset() / 4;
112 
113  // Add top mid pixel values
114  const uint32_t *const top_mid_ptr = output_top_mid + off;
115 
116  pixels.val[0] = vaddq_u32(vld1q_u32(top_mid_ptr), pixels.val[0]);
117  pixels.val[1] = vaddq_u32(vld1q_u32(top_mid_ptr + 4), pixels.val[1]);
118  pixels.val[2] = vaddq_u32(vld1q_u32(top_mid_ptr + 8), pixels.val[2]);
119  pixels.val[3] = vaddq_u32(vld1q_u32(top_mid_ptr + 12), pixels.val[3]);
120 
121  // Subtract top left diagonal values
122  const auto outptr = reinterpret_cast<uint32_t *>(output.ptr());
123  const uint32_t *const top_left_ptr = output_top_left + off;
124 
125  pixels.val[0] = vsubq_u32(pixels.val[0], vld1q_u32(top_left_ptr));
126  vst1q_u32(outptr, pixels.val[0]);
127 
128  pixels.val[1] = vsubq_u32(pixels.val[1], vld1q_u32(top_left_ptr + 4));
129  vst1q_u32(outptr + 4, pixels.val[1]);
130 
131  pixels.val[2] = vsubq_u32(pixels.val[2], vld1q_u32(top_left_ptr + 8));
132  vst1q_u32(outptr + 8, pixels.val[2]);
133 
134  pixels.val[3] = vsubq_u32(pixels.val[3], vld1q_u32(top_left_ptr + 12));
135  vst1q_u32(outptr + 12, pixels.val[3]);
136 
137  // Perform prefix summation
138  for(auto i = 0; i < 16; ++i)
139  {
140  outptr[i] += outptr[i - 1];
141  }
142  },
143  input, output);
144 }
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
uint8_t * ptr_to_element(const Coordinates &id) const
Return a pointer to the element at the passed coordinates.
Definition: ITensor.h:63
void configure(const ITensor *input, ITensor *output)
Set the source, destination and border mode of the kernel.
Container for 2D border size.
Definition: Types.h:273
1 channel, 1 U8 per channel
BorderSize border_size() const override
The size of the border for that kernel.
Interface for Neon tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
virtual ValidRegion valid_region() const =0
Valid region of the tensor.
Implementation of a rectangular access pattern.
bool update_window_and_padding(Window &win, Ts &&... patterns)
Update window and padding size for each of the access patterns.
Definition: WindowHelpers.h:46
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
1 channel, 1 U32 per channel
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
Coordinates of an item.
Definition: Coordinates.h:37
Implementation of a row access pattern.
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
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
#define ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:790
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:235
bool is_parallelisable() const override
Indicates whether or not the kernel is parallelisable.
unsigned int num_elems_processed_per_iteration
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
constexpr size_t offset() const
Return the offset in bytes from the first element to the current position of the iterator.
Definition: Helpers.inl:134
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205