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/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 | |
| 36 | using namespace arm_compute; |
| 37 | |
| 38 | void 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 | |
| 64 | BorderSize NEIntegralImageKernel::border_size() const |
| 65 | { |
| 66 | return BorderSize(1, 0, 0, 1); |
| 67 | } |
| 68 | |
| 69 | bool NEIntegralImageKernel::is_parallelisable() const |
| 70 | { |
| 71 | return false; |
| 72 | } |
| 73 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 74 | void NEIntegralImageKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 75 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 76 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 77 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 78 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window); |
| 79 | |
| 80 | Iterator input(_input, window); |
| 81 | Iterator output(_output, window); |
| 82 | |
| 83 | const auto output_top_left = reinterpret_cast<const uint32_t *>(_output->ptr_to_element(Coordinates(-1, -1))); |
| 84 | const auto output_top_mid = reinterpret_cast<const uint32_t *>(_output->ptr_to_element(Coordinates(0, -1))); |
| 85 | |
| 86 | execute_window_loop(window, [&](const Coordinates & id) |
| 87 | { |
| 88 | const uint8x16_t input_pixels = vld1q_u8(input.ptr()); |
| 89 | |
| 90 | const uint16x8x2_t tmp = |
| 91 | { |
| 92 | { |
| 93 | vmovl_u8(vget_low_u8(input_pixels)), |
| 94 | vmovl_u8(vget_high_u8(input_pixels)) |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | uint32x4x4_t pixels = |
| 99 | { |
| 100 | { |
| 101 | vmovl_u16(vget_low_u16(tmp.val[0])), |
| 102 | vmovl_u16(vget_high_u16(tmp.val[0])), |
| 103 | vmovl_u16(vget_low_u16(tmp.val[1])), |
| 104 | vmovl_u16(vget_high_u16(tmp.val[1])) |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | // Divide by four as pointer is now uint32 instead of uint8! |
| 109 | const size_t off = output.offset() / 4; |
| 110 | |
| 111 | // Add top mid pixel values |
| 112 | const uint32_t *const top_mid_ptr = output_top_mid + off; |
| 113 | |
| 114 | pixels.val[0] = vaddq_u32(vld1q_u32(top_mid_ptr), pixels.val[0]); |
| 115 | pixels.val[1] = vaddq_u32(vld1q_u32(top_mid_ptr + 4), pixels.val[1]); |
| 116 | pixels.val[2] = vaddq_u32(vld1q_u32(top_mid_ptr + 8), pixels.val[2]); |
| 117 | pixels.val[3] = vaddq_u32(vld1q_u32(top_mid_ptr + 12), pixels.val[3]); |
| 118 | |
| 119 | // Subtract top left diagonal values |
| 120 | const auto outptr = reinterpret_cast<uint32_t *>(output.ptr()); |
| 121 | const uint32_t *const top_left_ptr = output_top_left + off; |
| 122 | |
| 123 | pixels.val[0] = vsubq_u32(pixels.val[0], vld1q_u32(top_left_ptr)); |
| 124 | vst1q_u32(outptr, pixels.val[0]); |
| 125 | |
| 126 | pixels.val[1] = vsubq_u32(pixels.val[1], vld1q_u32(top_left_ptr + 4)); |
| 127 | vst1q_u32(outptr + 4, pixels.val[1]); |
| 128 | |
| 129 | pixels.val[2] = vsubq_u32(pixels.val[2], vld1q_u32(top_left_ptr + 8)); |
| 130 | vst1q_u32(outptr + 8, pixels.val[2]); |
| 131 | |
| 132 | pixels.val[3] = vsubq_u32(pixels.val[3], vld1q_u32(top_left_ptr + 12)); |
| 133 | vst1q_u32(outptr + 12, pixels.val[3]); |
| 134 | |
| 135 | // Perform prefix summation |
| 136 | for(auto i = 0; i < 16; ++i) |
| 137 | { |
| 138 | outptr[i] += outptr[i - 1]; |
| 139 | } |
| 140 | }, |
| 141 | input, output); |
| 142 | } |