blob: b1c84599dcce9f8baec489c5a001619c2c9eff79 [file] [log] [blame]
Viet-Hoa Do47370942023-11-13 17:20:45 +00001/*
2 * Copyright (c) 2023 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
25#include "arm_compute/core/Error.h"
26
27#include <cstdint>
28#include <cstring>
29
30namespace arm_compute
31{
32namespace cpu
33{
34
35void depth_to_space_nhwc_any( //
36 const uint8_t *src,
37 uint8_t *dst,
38 const uintptr_t src_shape[4],
39 const uintptr_t src_strides[4],
40 const uintptr_t dst_strides[4],
41 uintptr_t element_size,
42 uintptr_t block_size)
43{
44 ARM_COMPUTE_ERROR_ON(src_strides[0] != element_size);
45 ARM_COMPUTE_ERROR_ON(dst_strides[0] != element_size);
46
47 const auto src_block_row_stride = (src_shape[0] / block_size) * element_size;
48 const auto dst_width_block_stride = block_size * dst_strides[1];
49
50 auto *src_batch_ptr = src;
51 auto *dst_batch_ptr = dst;
52
53 for (uintptr_t batch = 0; batch < src_shape[3]; ++batch)
54 {
55 auto *src_height_block_ptr = src_batch_ptr;
56 auto *dst_row_ptr = dst_batch_ptr;
57
58 for (uintptr_t height_block = 0; height_block < src_shape[2]; ++height_block)
59 {
60 auto *src_block_row_ptr = src_height_block_ptr;
61
62 for (uintptr_t block_row = 0; block_row < block_size; ++block_row)
63 {
64 auto *src_width_block_ptr = src_block_row_ptr;
65 auto *dst_width_block_ptr = dst_row_ptr;
66
67 for (uintptr_t width_block = 0; width_block < src_shape[1]; ++width_block)
68 {
69 // The source pointer is accumulated as:
70 //
71 // src_width_block_ptr =
72 // src +
73 // batch * src_strides[3] +
74 // height_block * src_strides[2] +
75 // width_block * src_strides[1] +
76 // block_row * (src_shape[0] / block_size) * element_size;
77 //
78 // The destination pointer is accumulated as:
79 //
80 // dst_width_block_ptr =
81 // dst +
82 // batch * dst_strides[3] +
83 // (height_block * block_size + block_row) * dst_strides[2] +
84 // width_block * block_size * dst_strides[1];
85
86 std::memcpy(dst_width_block_ptr, src_width_block_ptr, src_block_row_stride);
87
88 src_width_block_ptr += src_strides[1];
89 dst_width_block_ptr += dst_width_block_stride;
90 }
91
92 src_block_row_ptr += src_block_row_stride;
93 dst_row_ptr += dst_strides[2];
94 }
95
96 src_height_block_ptr += src_strides[2];
97 }
98
99 src_batch_ptr += src_strides[3];
100 dst_batch_ptr += dst_strides[3];
101 }
102}
103
104} // namespace cpu
105} // namespace arm_compute