blob: 6e402ae604cb2257e7b80b3ca620df39eb3a0a2b [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/NEHistogramKernel.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/IDistribution1D.h"
29#include "arm_compute/core/ITensor.h"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Types.h"
32#include "arm_compute/core/Window.h"
33
34#include <algorithm>
35#include <arm_neon.h>
36#include <array>
37
38using namespace arm_compute;
39
40namespace arm_compute
41{
42class Coordinates;
43} // namespace arm_compute
44
45inline void NEHistogramKernel::merge_histogram(uint32_t *global_hist, const uint32_t *local_hist, size_t bins)
46{
Michalis Spyrou07781ac2017-08-31 15:11:41 +010047 std::lock_guard<arm_compute::Mutex> lock(_hist_mtx);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048
49 const unsigned int v_end = (bins / 4) * 4;
50
51 for(unsigned int b = 0; b < v_end; b += 4)
52 {
53 const uint32x4_t tmp_global = vld1q_u32(global_hist + b);
54 const uint32x4_t tmp_local = vld1q_u32(local_hist + b);
55 vst1q_u32(global_hist + b, vaddq_u32(tmp_global, tmp_local));
56 }
57
58 for(unsigned int b = v_end; b < bins; ++b)
59 {
60 global_hist[b] += local_hist[b];
61 }
62}
63
64NEHistogramKernel::NEHistogramKernel()
65 : _func(nullptr), _input(nullptr), _output(nullptr), _local_hist(nullptr), _window_lut(nullptr), _hist_mtx()
66{
67}
68
Moritz Pflanzerc186b572017-09-07 09:48:04 +010069void NEHistogramKernel::histogram_U8(Window win, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010070{
71 ARM_COMPUTE_ERROR_ON(_output->buffer() == nullptr);
72
73 const size_t bins = _output->num_bins();
74 const int32_t offset = _output->offset();
75 const uint32_t offrange = offset + _output->range();
76 const uint32_t *const w_lut = _window_lut;
Moritz Pflanzerc186b572017-09-07 09:48:04 +010077 uint32_t *const local_hist = _local_hist + info.thread_id * bins;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078
79 // Clear local_histogram
80 std::fill_n(local_hist, bins, 0);
81
82 auto update_local_hist = [&](uint8_t p)
83 {
84 if(offset <= p && p < offrange)
85 {
86 ++local_hist[w_lut[p]];
87 }
88 };
89
90 const unsigned int x_start = win.x().start();
91 const unsigned int x_end = win.x().end();
92
93 // Handle X dimension manually to split into two loops
94 // First one will use vector operations, second one processes the left over
95 // pixels
96 win.set(Window::DimX, Window::Dimension(0, 1, 1));
97
98 Iterator input(_input, win);
99
100 // Calculate local histogram
101 execute_window_loop(win, [&](const Coordinates &)
102 {
103 unsigned int x = x_start;
104
105 // Vector loop
106 for(; x <= x_end - 8; x += 8)
107 {
108 const uint8x8_t pixels = vld1_u8(input.ptr() + x);
109
110 update_local_hist(vget_lane_u8(pixels, 0));
111 update_local_hist(vget_lane_u8(pixels, 1));
112 update_local_hist(vget_lane_u8(pixels, 2));
113 update_local_hist(vget_lane_u8(pixels, 3));
114 update_local_hist(vget_lane_u8(pixels, 4));
115 update_local_hist(vget_lane_u8(pixels, 5));
116 update_local_hist(vget_lane_u8(pixels, 6));
117 update_local_hist(vget_lane_u8(pixels, 7));
118 }
119
120 // Process leftover pixels
121 for(; x < x_end; ++x)
122 {
123 update_local_hist(input.ptr()[x]);
124 }
125 },
126 input);
127
128 // Merge histograms
129 merge_histogram(_output->buffer(), local_hist, bins);
130}
131
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100132void NEHistogramKernel::histogram_fixed_U8(Window win, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100133{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100134 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135 ARM_COMPUTE_ERROR_ON(_output->buffer() == nullptr);
136
137 std::array<uint32_t, _max_range_size> local_hist{ { 0 } };
138
139 const unsigned int x_start = win.x().start();
140 const unsigned int x_end = win.x().end();
141
142 // Handle X dimension manually to split into two loops
143 // First one will use vector operations, second one processes the left over
144 // pixels
145 win.set(Window::DimX, Window::Dimension(0, 1, 1));
146
147 Iterator input(_input, win);
148
149 // Calculate local histogram
150 execute_window_loop(win, [&](const Coordinates &)
151 {
152 unsigned int x = x_start;
153
154 // Vector loop
155 for(; x <= x_end - 8; x += 8)
156 {
157 const uint8x8_t pixels = vld1_u8(input.ptr() + x);
158
159 ++local_hist[vget_lane_u8(pixels, 0)];
160 ++local_hist[vget_lane_u8(pixels, 1)];
161 ++local_hist[vget_lane_u8(pixels, 2)];
162 ++local_hist[vget_lane_u8(pixels, 3)];
163 ++local_hist[vget_lane_u8(pixels, 4)];
164 ++local_hist[vget_lane_u8(pixels, 5)];
165 ++local_hist[vget_lane_u8(pixels, 6)];
166 ++local_hist[vget_lane_u8(pixels, 7)];
167 }
168
169 // Process leftover pixels
170 for(; x < x_end; ++x)
171 {
172 ++local_hist[input.ptr()[x]];
173 }
174 },
175 input);
176
177 // Merge histograms
178 merge_histogram(_output->buffer(), local_hist.data(), _max_range_size);
179}
180
181void NEHistogramKernel::calculate_window_lut() const
182{
183 const int32_t offset = _output->offset();
184 const size_t bins = _output->num_bins();
185 const uint32_t range = _output->range();
186
187 std::fill_n(_window_lut, offset, 0);
188
189 for(unsigned int p = offset; p < _max_range_size; ++p)
190 {
191 _window_lut[p] = ((p - offset) * bins) / range;
192 }
193}
194
195void NEHistogramKernel::configure(const IImage *input, IDistribution1D *output, uint32_t *local_hist, uint32_t *window_lut)
196{
197 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
198 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
199 ARM_COMPUTE_ERROR_ON(nullptr == output);
200 ARM_COMPUTE_ERROR_ON(nullptr == local_hist);
201 ARM_COMPUTE_ERROR_ON(nullptr == window_lut);
202
203 _input = input;
204 _output = output;
205 _local_hist = local_hist;
206 _window_lut = window_lut;
207
208 //Check offset
209 ARM_COMPUTE_ERROR_ON_MSG(0 > _output->offset() || _output->offset() > static_cast<int32_t>(_max_range_size), "Offset is larger than the image value range.");
210
211 //Check range
212 ARM_COMPUTE_ERROR_ON_MSG(static_cast<int32_t>(_output->range()) > static_cast<int32_t>(_max_range_size) /* max range */, "Range larger than the image value range.");
213
214 // Calculate LUT
215 calculate_window_lut();
216
217 // Set appropriate function
218 _func = &NEHistogramKernel::histogram_U8;
219
220 constexpr unsigned int num_elems_processed_per_iteration = 1;
221
222 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
223
224 INEKernel::configure(win);
225}
226
227void NEHistogramKernel::configure(const IImage *input, IDistribution1D *output)
228{
229 ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input);
230 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
231 ARM_COMPUTE_ERROR_ON(nullptr == output);
232
233 _input = input;
234 _output = output;
235
236 // Set appropriate function
237 _func = &NEHistogramKernel::histogram_fixed_U8;
238
239 constexpr unsigned int num_elems_processed_per_iteration = 1;
240
241 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
242
243 INEKernel::configure(win);
244}
245
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100246void NEHistogramKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100247{
248 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
249 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
250 ARM_COMPUTE_ERROR_ON(_func == nullptr);
251
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100252 (this->*_func)(window, info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100253}