blob: 5c3b2a7540251cecdb6210a1cadaa5ce9e1ef394 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michalis Spyroua4f378d2019-04-26 14:54:54 +01002 * Copyright (c) 2016-2019 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 */
24#include "arm_compute/core/NEON/kernels/NEThresholdKernel.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
29#include "arm_compute/core/Validate.h"
30
31#include <arm_neon.h>
32
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033namespace arm_compute
34{
35class Coordinates;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
37NEThresholdKernel::NEThresholdKernel()
38 : _func(nullptr), _input(nullptr), _output(nullptr), _threshold(0), _false_value(0), _true_value(0), _upper(0)
39{
40}
41
42void NEThresholdKernel::configure(const ITensor *input, ITensor *output, uint8_t threshold, uint8_t false_value, uint8_t true_value, ThresholdType type, uint8_t upper)
43{
44 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
45 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
46
47 _input = input;
48 _output = output;
49 _threshold = threshold;
50 _false_value = false_value;
51 _true_value = true_value;
52 _upper = upper;
53
54 switch(type)
55 {
56 case ThresholdType::BINARY:
57 _func = &NEThresholdKernel::run_binary;
58 break;
59 case ThresholdType::RANGE:
60 _func = &NEThresholdKernel::run_range;
61 break;
62 default:
63 ARM_COMPUTE_ERROR("Thresholding type not recognized");
64 break;
65 }
66
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +010067 constexpr unsigned int num_elems_processed_per_iteration = 16;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068
69 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
70 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
71 update_window_and_padding(win, AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration), output_access);
72 output_access.set_valid_region(win, input->info()->valid_region());
73
74 INEKernel::configure(win);
75}
76
77inline void NEThresholdKernel::run_binary(const Window &window)
78{
79 const uint8x16_t threshold = vdupq_n_u8(_threshold);
80 const uint8x16_t true_value = vdupq_n_u8(_true_value);
81 const uint8x16_t false_value = vdupq_n_u8(_false_value);
82
83 Iterator input(_input, window);
84 Iterator output(_output, window);
85
Michalis Spyroua4f378d2019-04-26 14:54:54 +010086 execute_window_loop(window, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087 {
88 const uint8x16_t data = vld1q_u8(input.ptr());
89 const uint8x16_t mask = vcgtq_u8(data, threshold);
90
91 vst1q_u8(output.ptr(), vbslq_u8(mask, true_value, false_value));
92 },
93 input, output);
94}
95
96inline void NEThresholdKernel::run_range(const Window &window)
97{
98 const uint8x16_t lower_threshold = vdupq_n_u8(_threshold);
99 const uint8x16_t upper_threshold = vdupq_n_u8(_upper);
100 const uint8x16_t true_value = vdupq_n_u8(_true_value);
101 const uint8x16_t false_value = vdupq_n_u8(_false_value);
102
103 Iterator input(_input, window);
104 Iterator output(_output, window);
105
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100106 execute_window_loop(window, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107 {
108 const uint8x16_t data = vld1q_u8(input.ptr());
109
110 uint8x16_t mask = vcleq_u8(data, upper_threshold);
111
112 mask = vandq_u8(vcgeq_u8(data, lower_threshold), mask);
113
114 vst1q_u8(output.ptr(), vbslq_u8(mask, true_value, false_value));
115 },
116 input, output);
117}
118
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100119void NEThresholdKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100121 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
123 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
124 ARM_COMPUTE_ERROR_ON(_func == nullptr);
125
126 (this->*_func)(window);
127}
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +0100128} // namespace arm_compute