arm_compute v17.04
[platform/upstream/armcl.git] / src / core / NEON / kernels / NEGaussian3x3Kernel.cpp
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/NEGaussian3x3Kernel.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/NEON/INEKernel.h"
30 #include "arm_compute/core/Validate.h"
31
32 #include <arm_neon.h>
33
34 using namespace arm_compute;
35
36 BorderSize NEGaussian3x3Kernel::border_size() const
37 {
38     return BorderSize(1);
39 }
40
41 void NEGaussian3x3Kernel::configure(const ITensor *input, ITensor *output, bool border_undefined)
42 {
43     ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
44     ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
45
46     _input  = input;
47     _output = output;
48
49     // Configure kernel window
50     constexpr unsigned int num_elems_processed_per_iteration = 8;
51     constexpr unsigned int num_elems_read_per_iteration      = 16;
52     constexpr unsigned int num_elems_written_per_iteration   = 8;
53     constexpr unsigned int num_rows_read_per_iteration       = 3;
54
55     Window                 win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
56     AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
57
58     update_window_and_padding(win,
59                               AccessWindowRectangle(input->info(), -border_size().left, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration),
60                               output_access);
61
62     output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
63
64     INEKernel::configure(win);
65 }
66
67 void NEGaussian3x3Kernel::run(const Window &window)
68 {
69     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
70     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window);
71
72     Iterator input(_input, window);
73     Iterator output(_output, window);
74
75     const uint8_t *input_bot_ptr = _input->ptr_to_element(Coordinates(-1, -1));
76     const uint8_t *input_mid_ptr = _input->ptr_to_element(Coordinates(-1, 0));
77     const uint8_t *input_top_ptr = _input->ptr_to_element(Coordinates(-1, +1));
78
79     static const int16x8_t two  = vdupq_n_s16(2);
80     static const int16x8_t four = vdupq_n_s16(4);
81
82     execute_window_loop(window, [&](const Coordinates & id)
83     {
84         uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
85         uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset());
86         uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
87
88         const int16x8x2_t top_s16 =
89         {
90             {
91                 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))),
92                 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data)))
93             }
94         };
95         const int16x8x2_t mid_s16 =
96         {
97             {
98                 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mid_data))),
99                 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mid_data)))
100             }
101         };
102         const int16x8x2_t bot_s16 =
103         {
104             {
105                 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))),
106                 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data)))
107             }
108         };
109
110         //top left
111         int16x8_t out = top_s16.val[0];
112         //top mid
113         out = vmlaq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 1), two);
114         //top right
115         out = vaddq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 2));
116         //mid left
117         out = vmlaq_s16(out, mid_s16.val[0], two);
118         //mid mid
119         out = vmlaq_s16(out, vextq_s16(mid_s16.val[0], mid_s16.val[1], 1), four);
120         //mid right
121         out = vmlaq_s16(out, vextq_s16(mid_s16.val[0], mid_s16.val[1], 2), two);
122         //bot left
123         out = vaddq_s16(out, bot_s16.val[0]);
124         //bot mid
125         out = vmlaq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 1), two);
126         //bot right
127         out = vaddq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2));
128
129         vst1_u8(output.ptr(), vqshrun_n_s16(out, 4));
130     },
131     input, output);
132 }