blob: 6ee97eea303e9c31f0c4a2d42527816e33571db2 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +01002 * Copyright (c) 2016-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 */
Michalis Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEIntegralImageKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
26#include "arm_compute/core/Coordinates.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
29#include "arm_compute/core/Types.h"
30#include "arm_compute/core/Validate.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010031#include "src/core/helpers/AutoConfiguration.h"
32#include "src/core/helpers/WindowHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033
34#include <arm_neon.h>
35#include <cstddef>
36#include <cstdint>
37
38using namespace arm_compute;
39
40void NEIntegralImageKernel::configure(const ITensor *input, ITensor *output)
41{
42 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
43 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U32);
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
57 update_window_and_padding(win,
58 AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration),
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
66BorderSize NEIntegralImageKernel::border_size() const
67{
Michalis Spyroua4f378d2019-04-26 14:54:54 +010068 return BorderSize{ 1, 0, 0, 1 };
Anthony Barbier6ff3b192017-09-04 18:44:23 +010069}
70
71bool NEIntegralImageKernel::is_parallelisable() const
72{
73 return false;
74}
75
Moritz Pflanzerc186b572017-09-07 09:48:04 +010076void NEIntegralImageKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077{
Moritz Pflanzerc186b572017-09-07 09:48:04 +010078 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010079 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
80 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window);
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
Michalis Spyroua4f378d2019-04-26 14:54:54 +010088 execute_window_loop(window, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089 {
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}