aboutsummaryrefslogtreecommitdiff
path: root/src/core/NEON/kernels/NESobel5x5Kernel.cpp
blob: fc8ccc803d6e5c1a14fac3cc0fc07bea6c835644 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
 * Copyright (c) 2016-2019 Arm Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include "arm_compute/core/NEON/kernels/NESobel5x5Kernel.h"

#include "arm_compute/core/Coordinates.h"
#include "arm_compute/core/Error.h"
#include "arm_compute/core/Helpers.h"
#include "arm_compute/core/ITensor.h"
#include "arm_compute/core/Types.h"
#include "arm_compute/core/Validate.h"
#include "arm_compute/core/Window.h"

#include <arm_neon.h>
#include <cstddef>
#include <cstdint>

using namespace arm_compute;

NESobel5x5HorKernel::NESobel5x5HorKernel()
    : _input(nullptr), _output_x(nullptr), _output_y(nullptr), _run_sobel_x(false), _run_sobel_y(false), _border_size(0)
{
}

BorderSize NESobel5x5HorKernel::border_size() const
{
    return _border_size;
}

void NESobel5x5HorKernel::configure(const ITensor *input, ITensor *output_x, ITensor *output_y, bool border_undefined)
{
    ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
    ARM_COMPUTE_ERROR_ON((output_x == nullptr) && (output_y == nullptr));

    _run_sobel_x = output_x != nullptr;
    _run_sobel_y = output_y != nullptr;

    if(_run_sobel_x)
    {
        ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_x, 1, DataType::S16);
    }

    if(_run_sobel_y)
    {
        ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output_y, 1, DataType::S16);
    }

    _input       = input;
    _output_x    = output_x;
    _output_y    = output_y;
    _border_size = BorderSize(border_undefined ? 0 : 2, 2);

    // Configure kernel window
    constexpr unsigned int num_elems_processed_per_iteration = 8;
    constexpr unsigned int num_elems_read_per_iteration      = 16;
    constexpr unsigned int num_elems_written_per_iteration   = 8;

    Window                 win = calculate_max_window_horizontal(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
    AccessWindowHorizontal output_x_access(output_x == nullptr ? nullptr : output_x->info(), 0, num_elems_written_per_iteration);
    AccessWindowHorizontal output_y_access(output_y == nullptr ? nullptr : output_y->info(), 0, num_elems_written_per_iteration);

    update_window_and_padding(win,
                              AccessWindowHorizontal(input->info(), -border_size().left, num_elems_read_per_iteration),
                              output_x_access,
                              output_y_access);

    output_x_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
    output_y_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());

    INEKernel::configure(win);
}

void NESobel5x5HorKernel::run(const Window &window, const ThreadInfo &info)
{
    ARM_COMPUTE_UNUSED(info);
    ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
    ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);

    Window win_in(window);
    win_in.shift(Window::DimX, -2);

    Iterator input(_input, win_in);
    Iterator output_x;
    Iterator output_y;

    if(_run_sobel_x)
    {
        output_x = Iterator(_output_x, window);
    }

    if(_run_sobel_y)
    {
        output_y = Iterator(_output_y, window);
    }

    if(_run_sobel_y && _run_sobel_x)
    {
        static const int16x8_t six      = vdupq_n_s16(6);
        static const int16x8_t four     = vdupq_n_s16(4);
        static const int16x8_t two      = vdupq_n_s16(2);
        static const int16x8_t minustwo = vdupq_n_s16(-2);

        execute_window_loop(window, [&](const Coordinates &)
        {
            const uint8x16_t data = vld1q_u8(input.ptr());

            const int16x8x2_t data_s16 =
            {
                {
                    vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(data))),
                    vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(data)))
                }
            };

            int16x8_t out_y = data_s16.val[0];
            out_y           = vmlaq_s16(out_y, vextq_s16(data_s16.val[0], data_s16.val[1], 1), four);
            out_y           = vmlaq_s16(out_y, vextq_s16(data_s16.val[0], data_s16.val[1], 2), six);
            out_y           = vmlaq_s16(out_y, vextq_s16(data_s16.val[0], data_s16.val[1], 3), four);
            out_y           = vaddq_s16(out_y, vextq_s16(data_s16.val[0], data_s16.val[1], 4));

            vst1q_s16(reinterpret_cast<int16_t *>(output_y.ptr()), out_y);

            int16x8_t out_x = vnegq_s16(data_s16.val[0]);
            out_x           = vmlaq_s16(out_x, vextq_s16(data_s16.val[0], data_s16.val[1], 1), minustwo);
            out_x           = vmlaq_s16(out_x, vextq_s16(data_s16.val[0], data_s16.val[1], 3), two);
            out_x           = vaddq_s16(out_x, vextq_s16(data_s16.val[0], data_s16.val[1], 4));

            vst1q_s16(reinterpret_cast<int16_t *>(output_x.ptr()), out_x);
        },
        input, output_x, output_y);
    }
    else if(_run_sobel_x)
    {
        static const int16x8_t two      = vdupq_n_s16(2);
        static const int16x8_t minustwo = vdupq_n_s16(-2);

        execute_window_loop(window, [&](const Coordinates &)
        {
            const uint8x16_t data = vld1q_u8(input.ptr());

            const int16x8x2_t data_s16 =
            {
                {
                    vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(data))),
                    vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(data)))
                }
            };

            int16x8_t out = vnegq_s16(data_s16.val[0]);
            out           = vmlaq_s16(out, vextq_s16(data_s16.val[0], data_s16.val[1], 1), minustwo);
            out           = vmlaq_s16(out, vextq_s16(data_s16.val[0], data_s16.val[1], 3), two);
            out           = vaddq_s16(out, vextq_s16(data_s16.val[0], data_s16.val[1], 4));

            vst1q_s16(reinterpret_cast<int16_t *>(output_x.ptr()), out);
        },
        input, output_x);
    }
    else if(_run_sobel_y)
    {
        static const int16x8_t six  = vdupq_n_s16(6);
        static const int16x8_t four = vdupq_n_s16(4);

        execute_window_loop(window, [&](const Coordinates &)
        {
            const uint8x16_t data = vld1q_u8(input.ptr());

            const int16x8x2_t data_s16 =
            {
                {
                    vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(data))),
                    vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(data)))
                }
            };

            int16x8_t out = data_s16.val[0];
            out           = vmlaq_s16(out, vextq_s16(data_s16.val[0], data_s16.val[1], 1), four);
            out           = vmlaq_s16(out, vextq_s16(data_s16.val[0], data_s16.val[1], 2), six);
            out           = vmlaq_s16(out, vextq_s16(data_s16.val[0], data_s16.val[1], 3), four);
            out           = vaddq_s16(out, vextq_s16(data_s16.val[0], data_s16.val[1], 4));

            vst1q_s16(reinterpret_cast<int16_t *>(output_y.ptr()), out);
        },
        input, output_y);
    }
}

NESobel5x5VertKernel::NESobel5x5VertKernel()
    : _input_x(nullptr), _input_y(nullptr), _output_x(nullptr), _output_y(nullptr), _run_sobel_x(false), _run_sobel_y(false)
{
}

BorderSize NESobel5x5VertKernel::border_size() const
{
    return BorderSize{ 2, 0 };
}

void NESobel5x5VertKernel::configure(ITensor *input_x, ITensor *input_y, ITensor *output_x, ITensor *output_y, bool border_undefined)
{
    ARM_COMPUTE_ERROR_ON((output_x == nullptr) && (output_y == nullptr));

    _run_sobel_x = output_x != nullptr;
    _run_sobel_y = output_y != nullptr;

    if(_run_sobel_x)
    {
        ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input_x, Format::S16);
        ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output_x, Format::S16);
    }

    if(_run_sobel_y)
    {
        ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(input_y, Format::S16);
        ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(output_y, Format::S16);
    }

    _input_x  = input_x;
    _input_y  = input_y;
    _output_x = output_x;
    _output_y = output_y;

    const ITensor *const input = _run_sobel_x ? input_x : input_y;

    // Configure kernel window
    constexpr unsigned int num_elems_processed_per_iteration = 16;
    constexpr unsigned int num_elems_read_per_iteration      = 16;
    constexpr unsigned int num_elems_written_per_iteration   = 16;
    constexpr unsigned int num_rows_read_per_iteration       = 5;

    Window                 win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration), border_undefined, border_size());
    AccessWindowHorizontal output_x_access(output_x == nullptr ? nullptr : output_x->info(), 0, num_elems_written_per_iteration);
    AccessWindowHorizontal output_y_access(output_y == nullptr ? nullptr : output_y->info(), 0, num_elems_written_per_iteration);

    update_window_and_padding(win,
                              AccessWindowRectangle(input_x == nullptr ? nullptr : input_x->info(), 0, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration),
                              AccessWindowRectangle(input_y == nullptr ? nullptr : input_y->info(), 0, -border_size().top, num_elems_read_per_iteration, num_rows_read_per_iteration),
                              output_x_access,
                              output_y_access);

    output_x_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());
    output_y_access.set_valid_region(win, input->info()->valid_region(), border_undefined, border_size());

    INEKernel::configure(win);
}

void NESobel5x5VertKernel::run(const Window &window, const ThreadInfo &info)
{
    ARM_COMPUTE_UNUSED(info);
    ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
    ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);

    Iterator input_x;
    Iterator input_y;
    Iterator output_x;
    Iterator output_y;

    const int16_t *input_x_low2_ptr = nullptr;
    const int16_t *input_x_low_ptr  = nullptr;
    const int16_t *input_x_mid_ptr  = nullptr;
    const int16_t *input_x_top_ptr  = nullptr;
    const int16_t *input_x_top2_ptr = nullptr;

    const int16_t *input_y_low2_ptr = nullptr;
    const int16_t *input_y_low_ptr  = nullptr;
    const int16_t *input_y_top_ptr  = nullptr;
    const int16_t *input_y_top2_ptr = nullptr;

    if(_run_sobel_x)
    {
        input_x          = Iterator(_input_x, window);
        output_x         = Iterator(_output_x, window);
        input_x_top2_ptr = reinterpret_cast<const int16_t *>(_input_x->ptr_to_element(Coordinates(0, -2)));
        input_x_top_ptr  = reinterpret_cast<const int16_t *>(_input_x->ptr_to_element(Coordinates(0, -1)));
        input_x_mid_ptr  = reinterpret_cast<const int16_t *>(_input_x->ptr_to_element(Coordinates(0, 0)));
        input_x_low_ptr  = reinterpret_cast<const int16_t *>(_input_x->ptr_to_element(Coordinates(0, 1)));
        input_x_low2_ptr = reinterpret_cast<const int16_t *>(_input_x->ptr_to_element(Coordinates(0, 2)));
    }

    if(_run_sobel_y)
    {
        input_y          = Iterator(_input_y, window);
        output_y         = Iterator(_output_y, window);
        input_y_top2_ptr = reinterpret_cast<const int16_t *>(_input_y->ptr_to_element(Coordinates(0, -2)));
        input_y_top_ptr  = reinterpret_cast<const int16_t *>(_input_y->ptr_to_element(Coordinates(0, -1)));
        input_y_low_ptr  = reinterpret_cast<const int16_t *>(_input_y->ptr_to_element(Coordinates(0, 1)));
        input_y_low2_ptr = reinterpret_cast<const int16_t *>(_input_y->ptr_to_element(Coordinates(0, 2)));
    }

    static const int16x8_t six      = vdupq_n_s16(6);
    static const int16x8_t four     = vdupq_n_s16(4);
    static const int16x8_t two      = vdupq_n_s16(2);
    static const int16x8_t minustwo = vdupq_n_s16(-2);

    if(_run_sobel_x)
    {
        execute_window_loop(window, [&](const Coordinates &)
        {
            // Convert offset from uint8_t* to uint16_t*
            const size_t input_offset_high_s16 = input_x.offset() / 2;
            const size_t input_offset_low_s16  = input_offset_high_s16 + 8;

            //HIGH DATA
            //top2
            int16x8_t data_high = vld1q_s16(input_x_top2_ptr + input_offset_high_s16);
            int16x8_t out_high  = data_high;
            //top
            data_high = vld1q_s16(input_x_top_ptr + input_offset_high_s16);
            out_high  = vmlaq_s16(out_high, data_high, four);
            //mid
            data_high = vld1q_s16(input_x_mid_ptr + input_offset_high_s16);
            out_high  = vmlaq_s16(out_high, data_high, six);
            //low
            data_high = vld1q_s16(input_x_low_ptr + input_offset_high_s16);
            out_high  = vmlaq_s16(out_high, data_high, four);
            //low2
            data_high = vld1q_s16(input_x_low2_ptr + input_offset_high_s16);
            out_high  = vaddq_s16(out_high, data_high);

            vst1q_s16((reinterpret_cast<int16_t *>(output_x.ptr())), out_high);

            //LOW DATA
            //top2
            int16x8_t data_low = vld1q_s16(input_x_top2_ptr + input_offset_low_s16);
            int16x8_t out_low  = data_low;
            //top
            data_low = vld1q_s16(input_x_top_ptr + input_offset_low_s16);
            out_low  = vmlaq_s16(out_low, data_low, four);
            //mid
            data_low = vld1q_s16(input_x_mid_ptr + input_offset_low_s16);
            out_low  = vmlaq_s16(out_low, data_low, six);
            //low
            data_low = vld1q_s16(input_x_low_ptr + input_offset_low_s16);
            out_low  = vmlaq_s16(out_low, data_low, four);
            //low2
            data_low = vld1q_s16(input_x_low2_ptr + input_offset_low_s16);
            out_low  = vaddq_s16(out_low, data_low);

            vst1q_s16((reinterpret_cast<int16_t *>(output_x.ptr())) + 8, out_low);
        },
        input_x, output_x);
    }

    if(_run_sobel_y)
    {
        execute_window_loop(window, [&](const Coordinates &)
        {
            // Convert offset from uint8_t* to uint16_t*
            const size_t input_offset_high_s16 = input_y.offset() / 2;
            const size_t input_offset_low_s16  = input_offset_high_s16 + 8;

            //HIGH DATA
            //top2
            int16x8_t data_high = vld1q_s16(input_y_top2_ptr + input_offset_high_s16);
            int16x8_t out_high  = vnegq_s16(data_high);
            //top
            data_high = vld1q_s16(input_y_top_ptr + input_offset_high_s16);
            out_high  = vmlaq_s16(out_high, data_high, minustwo);
            //low
            data_high = vld1q_s16(input_y_low_ptr + input_offset_high_s16);
            out_high  = vmlaq_s16(out_high, data_high, two);
            //low2
            data_high = vld1q_s16(input_y_low2_ptr + input_offset_high_s16);
            out_high  = vaddq_s16(out_high, data_high);

            vst1q_s16((reinterpret_cast<int16_t *>(output_y.ptr())), out_high);

            //LOW DATA
            //top2
            int16x8_t data_low = vld1q_s16(input_y_top2_ptr + input_offset_low_s16);
            int16x8_t out_low  = vnegq_s16(data_low);
            //top
            data_low = vld1q_s16(input_y_top_ptr + input_offset_low_s16);
            out_low  = vmlaq_s16(out_low, data_low, minustwo);
            //low
            data_low = vld1q_s16(input_y_low_ptr + input_offset_low_s16);
            out_low  = vmlaq_s16(out_low, data_low, two);
            //low2
            data_low = vld1q_s16(input_y_low2_ptr + input_offset_low_s16);
            out_low  = vaddq_s16(out_low, data_low);

            vst1q_s16((reinterpret_cast<int16_t *>(output_y.ptr())) + 8, out_low);
        },
        input_y, output_y);
    }
}