arm_compute v17.04
[platform/upstream/armcl.git] / src / core / NEON / kernels / NEBox3x3Kernel.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/NEBox3x3Kernel.h"
25
26 #include "arm_compute/core/Coordinates.h"
27 #include "arm_compute/core/Helpers.h"
28 #include "arm_compute/core/IAccessWindow.h"
29 #include "arm_compute/core/ITensor.h"
30 #include "arm_compute/core/NEON/INEKernel.h"
31 #include "arm_compute/core/Validate.h"
32 #include <arm_neon.h>
33
34 using namespace arm_compute;
35
36 #ifdef ARM_COMPUTE_ENABLE_FP16
37 void NEBox3x3FP16Kernel::run(const Window &window)
38 {
39     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
40     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window);
41
42     Iterator input(_input, window);
43     Iterator output(_output, window);
44
45     unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-1, -1));
46     unsigned char *const input_mid_ptr = _input->ptr_to_element(Coordinates(-1, 0));
47     unsigned char *const input_bot_ptr = _input->ptr_to_element(Coordinates(-1, +1));
48
49     const float16x8_t oneovernine = vdupq_n_f16(1.0f / 9.0f);
50
51     execute_window_loop(window, [&](const Coordinates & id)
52     {
53         const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
54         const uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset());
55         const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
56
57         const float16x8x2_t top_f16 =
58         {
59             {
60                 vcvtq_f16_u16(vmovl_u8(vget_low_u8(top_data))),
61                 vcvtq_f16_u16(vmovl_u8(vget_high_u8(top_data)))
62             }
63         };
64
65         const float16x8x2_t mid_f16 =
66         {
67             {
68                 vcvtq_f16_u16(vmovl_u8(vget_low_u8(mid_data))),
69                 vcvtq_f16_u16(vmovl_u8(vget_high_u8(mid_data)))
70             }
71         };
72
73         const float16x8x2_t bot_f16 =
74         {
75             {
76                 vcvtq_f16_u16(vmovl_u8(vget_low_u8(bot_data))),
77                 vcvtq_f16_u16(vmovl_u8(vget_high_u8(bot_data)))
78             }
79         };
80
81         //top left
82         float16x8_t out = top_f16.val[0];
83         //top mid
84         out = vaddq_f16(out, vextq_f16(top_f16.val[0], top_f16.val[1], 1));
85         //top right
86         out = vaddq_f16(out, vextq_f16(top_f16.val[0], top_f16.val[1], 2));
87         //mid left
88         out = vaddq_f16(out, mid_f16.val[0]);
89         //mid mid
90         out = vaddq_f16(out, vextq_f16(mid_f16.val[0], mid_f16.val[1], 1));
91         //mid right
92         out = vaddq_f16(out, vextq_f16(mid_f16.val[0], mid_f16.val[1], 2));
93         //bot left
94         out = vaddq_f16(out, bot_f16.val[0]);
95         //bot mid
96         out = vaddq_f16(out, vextq_f16(bot_f16.val[0], bot_f16.val[1], 1));
97         //bot right
98         out = vaddq_f16(out, vextq_f16(bot_f16.val[0], bot_f16.val[1], 2));
99
100         out = vmulq_f16(out, oneovernine);
101
102         vst1_u8(output.ptr(), vqmovun_s16(vcvtq_s16_f16(out)));
103     },
104     input, output);
105 }
106 #endif
107
108 BorderSize NEBox3x3Kernel::border_size() const
109 {
110     return BorderSize(1);
111 }
112
113 void NEBox3x3Kernel::configure(const ITensor *input, ITensor *output, bool border_undefined)
114 {
115     ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
116     ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8);
117
118     _input  = input;
119     _output = output;
120
121     constexpr unsigned int num_elems_processed_per_iteration = 8;
122     constexpr unsigned int num_elems_read_per_iteration      = 16;
123     constexpr unsigned int num_elems_written_per_iteration   = 8;
124     constexpr unsigned int num_rows_read_per_iteration       = 3;
125     constexpr int          rect_offset_xy                    = -1;
126
127     // Configure kernel window
128     Window                 win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
129     AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
130
131     update_window_and_padding(win, AccessWindowRectangle(input->info(), rect_offset_xy, rect_offset_xy, num_elems_read_per_iteration, num_rows_read_per_iteration), output_access);
132
133     output_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
134
135     INEKernel::configure(win);
136 }
137
138 void NEBox3x3Kernel::run(const Window &window)
139 {
140     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
141     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window);
142
143     Iterator input(_input, window);
144     Iterator output(_output, window);
145
146     unsigned char *const input_top_ptr = _input->ptr_to_element(Coordinates(-1, -1));
147     unsigned char *const input_mid_ptr = _input->ptr_to_element(Coordinates(-1, 0));
148     unsigned char *const input_bot_ptr = _input->ptr_to_element(Coordinates(-1, +1));
149
150     const float32x4_t oneovernine = vdupq_n_f32(1.0f / 9.0f);
151
152     execute_window_loop(window, [&](const Coordinates & id)
153     {
154         const uint8x16_t top_data = vld1q_u8(input_top_ptr + input.offset());
155         const uint8x16_t mid_data = vld1q_u8(input_mid_ptr + input.offset());
156         const uint8x16_t bot_data = vld1q_u8(input_bot_ptr + input.offset());
157
158         const int16x8x2_t top_s16 =
159         {
160             {
161                 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(top_data))),
162                 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(top_data)))
163             }
164         };
165         const int16x8x2_t mid_s16 =
166         {
167             {
168                 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mid_data))),
169                 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mid_data)))
170             }
171         };
172         const int16x8x2_t bot_s16 =
173         {
174             {
175                 vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(bot_data))),
176                 vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(bot_data)))
177             }
178         };
179
180         //top left
181         int16x8_t out = top_s16.val[0];
182         //top mid
183         out = vaddq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 1));
184         //top right
185         out = vaddq_s16(out, vextq_s16(top_s16.val[0], top_s16.val[1], 2));
186         //mid left
187         out = vaddq_s16(out, mid_s16.val[0]);
188         //mid mid
189         out = vaddq_s16(out, vextq_s16(mid_s16.val[0], mid_s16.val[1], 1));
190         //mid right
191         out = vaddq_s16(out, vextq_s16(mid_s16.val[0], mid_s16.val[1], 2));
192         //bot left
193         out = vaddq_s16(out, bot_s16.val[0]);
194         //bot mid
195         out = vaddq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 1));
196         //bot right
197         out = vaddq_s16(out, vextq_s16(bot_s16.val[0], bot_s16.val[1], 2));
198
199         float32x4_t outfloathigh = vcvtq_f32_s32(vmovl_s16(vget_high_s16(out)));
200         float32x4_t outfloatlow  = vcvtq_f32_s32(vmovl_s16(vget_low_s16(out)));
201
202         outfloathigh = vmulq_f32(outfloathigh, oneovernine);
203         outfloatlow  = vmulq_f32(outfloatlow, oneovernine);
204
205         out = vcombine_s16(vqmovn_s32(vcvtq_s32_f32(outfloatlow)),
206                            vqmovn_s32(vcvtq_s32_f32(outfloathigh)));
207
208         vst1_u8(output.ptr(), vqmovun_s16(out));
209     },
210     input, output);
211 }