Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 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/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 | |
| 33 | using namespace arm_compute; |
| 34 | |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | class Coordinates; |
| 38 | } // namespace arm_compute |
| 39 | |
| 40 | NEThresholdKernel::NEThresholdKernel() |
| 41 | : _func(nullptr), _input(nullptr), _output(nullptr), _threshold(0), _false_value(0), _true_value(0), _upper(0) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | void NEThresholdKernel::configure(const ITensor *input, ITensor *output, uint8_t threshold, uint8_t false_value, uint8_t true_value, ThresholdType type, uint8_t upper) |
| 46 | { |
| 47 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 48 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8); |
| 49 | |
| 50 | _input = input; |
| 51 | _output = output; |
| 52 | _threshold = threshold; |
| 53 | _false_value = false_value; |
| 54 | _true_value = true_value; |
| 55 | _upper = upper; |
| 56 | |
| 57 | switch(type) |
| 58 | { |
| 59 | case ThresholdType::BINARY: |
| 60 | _func = &NEThresholdKernel::run_binary; |
| 61 | break; |
| 62 | case ThresholdType::RANGE: |
| 63 | _func = &NEThresholdKernel::run_range; |
| 64 | break; |
| 65 | default: |
| 66 | ARM_COMPUTE_ERROR("Thresholding type not recognized"); |
| 67 | break; |
| 68 | } |
| 69 | |
| 70 | const unsigned int num_elems_processed_per_iteration = 16; |
| 71 | |
| 72 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 73 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
| 74 | update_window_and_padding(win, AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration), output_access); |
| 75 | output_access.set_valid_region(win, input->info()->valid_region()); |
| 76 | |
| 77 | INEKernel::configure(win); |
| 78 | } |
| 79 | |
| 80 | inline void NEThresholdKernel::run_binary(const Window &window) |
| 81 | { |
| 82 | const uint8x16_t threshold = vdupq_n_u8(_threshold); |
| 83 | const uint8x16_t true_value = vdupq_n_u8(_true_value); |
| 84 | const uint8x16_t false_value = vdupq_n_u8(_false_value); |
| 85 | |
| 86 | Iterator input(_input, window); |
| 87 | Iterator output(_output, window); |
| 88 | |
| 89 | execute_window_loop(window, [&](const Coordinates & id) |
| 90 | { |
| 91 | const uint8x16_t data = vld1q_u8(input.ptr()); |
| 92 | const uint8x16_t mask = vcgtq_u8(data, threshold); |
| 93 | |
| 94 | vst1q_u8(output.ptr(), vbslq_u8(mask, true_value, false_value)); |
| 95 | }, |
| 96 | input, output); |
| 97 | } |
| 98 | |
| 99 | inline void NEThresholdKernel::run_range(const Window &window) |
| 100 | { |
| 101 | const uint8x16_t lower_threshold = vdupq_n_u8(_threshold); |
| 102 | const uint8x16_t upper_threshold = vdupq_n_u8(_upper); |
| 103 | const uint8x16_t true_value = vdupq_n_u8(_true_value); |
| 104 | const uint8x16_t false_value = vdupq_n_u8(_false_value); |
| 105 | |
| 106 | Iterator input(_input, window); |
| 107 | Iterator output(_output, window); |
| 108 | |
| 109 | execute_window_loop(window, [&](const Coordinates & id) |
| 110 | { |
| 111 | const uint8x16_t data = vld1q_u8(input.ptr()); |
| 112 | |
| 113 | uint8x16_t mask = vcleq_u8(data, upper_threshold); |
| 114 | |
| 115 | mask = vandq_u8(vcgeq_u8(data, lower_threshold), mask); |
| 116 | |
| 117 | vst1q_u8(output.ptr(), vbslq_u8(mask, true_value, false_value)); |
| 118 | }, |
| 119 | input, output); |
| 120 | } |
| 121 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 122 | void NEThresholdKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 123 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 124 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 125 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 126 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 127 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 128 | |
| 129 | (this->*_func)(window); |
| 130 | } |