aboutsummaryrefslogtreecommitdiff
path: root/src/cpu/kernels/depth_to_space/nhwc/any/impl.cpp
blob: b1c84599dcce9f8baec489c5a001619c2c9eff79 (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
/*
 * Copyright (c) 2023 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/Error.h"

#include <cstdint>
#include <cstring>

namespace arm_compute
{
namespace cpu
{

void depth_to_space_nhwc_any( //
    const uint8_t  *src,
    uint8_t        *dst,
    const uintptr_t src_shape[4],
    const uintptr_t src_strides[4],
    const uintptr_t dst_strides[4],
    uintptr_t       element_size,
    uintptr_t       block_size)
{
    ARM_COMPUTE_ERROR_ON(src_strides[0] != element_size);
    ARM_COMPUTE_ERROR_ON(dst_strides[0] != element_size);

    const auto src_block_row_stride   = (src_shape[0] / block_size) * element_size;
    const auto dst_width_block_stride = block_size * dst_strides[1];

    auto *src_batch_ptr = src;
    auto *dst_batch_ptr = dst;

    for (uintptr_t batch = 0; batch < src_shape[3]; ++batch)
    {
        auto *src_height_block_ptr = src_batch_ptr;
        auto *dst_row_ptr          = dst_batch_ptr;

        for (uintptr_t height_block = 0; height_block < src_shape[2]; ++height_block)
        {
            auto *src_block_row_ptr = src_height_block_ptr;

            for (uintptr_t block_row = 0; block_row < block_size; ++block_row)
            {
                auto *src_width_block_ptr = src_block_row_ptr;
                auto *dst_width_block_ptr = dst_row_ptr;

                for (uintptr_t width_block = 0; width_block < src_shape[1]; ++width_block)
                {
                    // The source pointer is accumulated as:
                    //
                    // src_width_block_ptr =
                    //   src +
                    //   batch * src_strides[3] +
                    //   height_block * src_strides[2] +
                    //   width_block * src_strides[1] +
                    //   block_row * (src_shape[0] / block_size) * element_size;
                    //
                    // The destination pointer is accumulated as:
                    //
                    // dst_width_block_ptr =
                    //     dst +
                    //     batch * dst_strides[3] +
                    //     (height_block * block_size + block_row) * dst_strides[2] +
                    //     width_block * block_size * dst_strides[1];

                    std::memcpy(dst_width_block_ptr, src_width_block_ptr, src_block_row_stride);

                    src_width_block_ptr += src_strides[1];
                    dst_width_block_ptr += dst_width_block_stride;
                }

                src_block_row_ptr += src_block_row_stride;
                dst_row_ptr += dst_strides[2];
            }

            src_height_block_ptr += src_strides[2];
        }

        src_batch_ptr += src_strides[3];
        dst_batch_ptr += dst_strides[3];
    }
}

} // namespace cpu
} // namespace arm_compute