blob: 3b09a1bdbb9d584166c867e0b374d029c0a556e7 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 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#include "arm_compute/core/NEON/kernels/NEIntegralImageKernel.h"
25
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"
31
32#include <arm_neon.h>
33#include <cstddef>
34#include <cstdint>
35
36using namespace arm_compute;
37
38void NEIntegralImageKernel::configure(const ITensor *input, ITensor *output)
39{
40 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
41 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U32);
42
43 _input = input;
44 _output = output;
45
46 constexpr unsigned int num_elems_processed_per_iteration = 16;
47
48 // Configure kernel window
49 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
50 // The kernel is effectively reading 17 values from -1 as it loads 16
51 // starting at -1 and also 16 starting at 0
52 AccessWindowRectangle output_read_access(output->info(), -1, -1, num_elems_processed_per_iteration + 1, 1);
53 AccessWindowHorizontal output_write_access(output->info(), 0, num_elems_processed_per_iteration);
54
55 update_window_and_padding(win,
56 AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration),
57 output_read_access, output_write_access);
58
59 output_write_access.set_valid_region(win, input->info()->valid_region());
60
61 IKernel::configure(win);
62}
63
64BorderSize NEIntegralImageKernel::border_size() const
65{
66 return BorderSize(1, 0, 0, 1);
67}
68
69bool NEIntegralImageKernel::is_parallelisable() const
70{
71 return false;
72}
73
74void NEIntegralImageKernel::run(const Window &window)
75{
76 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
77 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window);
78
79 Iterator input(_input, window);
80 Iterator output(_output, window);
81
82 const auto output_top_left = reinterpret_cast<const uint32_t *>(_output->ptr_to_element(Coordinates(-1, -1)));
83 const auto output_top_mid = reinterpret_cast<const uint32_t *>(_output->ptr_to_element(Coordinates(0, -1)));
84
85 execute_window_loop(window, [&](const Coordinates & id)
86 {
87 const uint8x16_t input_pixels = vld1q_u8(input.ptr());
88
89 const uint16x8x2_t tmp =
90 {
91 {
92 vmovl_u8(vget_low_u8(input_pixels)),
93 vmovl_u8(vget_high_u8(input_pixels))
94 }
95 };
96
97 uint32x4x4_t pixels =
98 {
99 {
100 vmovl_u16(vget_low_u16(tmp.val[0])),
101 vmovl_u16(vget_high_u16(tmp.val[0])),
102 vmovl_u16(vget_low_u16(tmp.val[1])),
103 vmovl_u16(vget_high_u16(tmp.val[1]))
104 }
105 };
106
107 // Divide by four as pointer is now uint32 instead of uint8!
108 const size_t off = output.offset() / 4;
109
110 // Add top mid pixel values
111 const uint32_t *const top_mid_ptr = output_top_mid + off;
112
113 pixels.val[0] = vaddq_u32(vld1q_u32(top_mid_ptr), pixels.val[0]);
114 pixels.val[1] = vaddq_u32(vld1q_u32(top_mid_ptr + 4), pixels.val[1]);
115 pixels.val[2] = vaddq_u32(vld1q_u32(top_mid_ptr + 8), pixels.val[2]);
116 pixels.val[3] = vaddq_u32(vld1q_u32(top_mid_ptr + 12), pixels.val[3]);
117
118 // Subtract top left diagonal values
119 const auto outptr = reinterpret_cast<uint32_t *>(output.ptr());
120 const uint32_t *const top_left_ptr = output_top_left + off;
121
122 pixels.val[0] = vsubq_u32(pixels.val[0], vld1q_u32(top_left_ptr));
123 vst1q_u32(outptr, pixels.val[0]);
124
125 pixels.val[1] = vsubq_u32(pixels.val[1], vld1q_u32(top_left_ptr + 4));
126 vst1q_u32(outptr + 4, pixels.val[1]);
127
128 pixels.val[2] = vsubq_u32(pixels.val[2], vld1q_u32(top_left_ptr + 8));
129 vst1q_u32(outptr + 8, pixels.val[2]);
130
131 pixels.val[3] = vsubq_u32(pixels.val[3], vld1q_u32(top_left_ptr + 12));
132 vst1q_u32(outptr + 12, pixels.val[3]);
133
134 // Perform prefix summation
135 for(auto i = 0; i < 16; ++i)
136 {
137 outptr[i] += outptr[i - 1];
138 }
139 },
140 input, output);
141}