2e21f78ea1e10017678a3fa3b055dfca1c73a4d4
[platform/upstream/armcl.git] / src / core / NEON / kernels / NEDepthConvertKernel.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/NEDepthConvertKernel.h"
25
26 #include "arm_compute/core/Error.h"
27 #include "arm_compute/core/Helpers.h"
28 #include "arm_compute/core/ITensor.h"
29 #include "arm_compute/core/TensorInfo.h"
30 #include "arm_compute/core/Validate.h"
31
32 #include <arm_neon.h>
33
34 using namespace arm_compute;
35
36 namespace arm_compute
37 {
38 class Coordinates;
39 } // namespace arm_compute
40
41 NEDepthConvertKernel::NEDepthConvertKernel()
42     : _policy(), _shift(0)
43 {
44 }
45
46 void NEDepthConvertKernel::configure(const ITensor *input, ITensor *output, ConvertPolicy policy, uint32_t shift)
47 {
48     ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S16, DataType::U16, DataType::U32, DataType::S32);
49     ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::S16, DataType::U16, DataType::U32, DataType::S32);
50     ARM_COMPUTE_ERROR_ON(shift >= 8);
51     ARM_COMPUTE_ERROR_ON(input == output);
52     ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == output->info()->data_type(), "Input and output data_types must be different");
53
54     ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::U8 && (output->info()->data_type() != DataType::S16 && output->info()->data_type() != DataType::U16
55                                                                             && output->info()->data_type() != DataType::U32
56                                                                             && output->info()->data_type() != DataType::S32),
57                              "Only data_types supported [in] U8 -> [out] U16, S16, U32, S32");
58
59     ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::U16 && (output->info()->data_type() != DataType::U8 && output->info()->data_type() != DataType::U32
60                                                                              && output->info()->data_type() != DataType::S32),
61                              "Only data_types supported [in] U16 ->  [out] U8, U32, S32");
62
63     ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::S16 && (output->info()->data_type() != DataType::U8 && output->info()->data_type() != DataType::U32
64                                                                              && output->info()->data_type() != DataType::S32),
65                              "Only data_types supported [in] S16 ->  [out] U8, U32, S32");
66
67     ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::U32 && (output->info()->data_type() != DataType::U8 && output->info()->data_type() != DataType::U16
68                                                                              && output->info()->data_type() != DataType::S16),
69                              "Only data_types supported [in] S16 ->  [out] U8, U16, S16");
70
71     ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::S32 && (output->info()->data_type() != DataType::U8 && output->info()->data_type() != DataType::U16
72                                                                              && output->info()->data_type() != DataType::S16),
73                              "Only data_types supported [in] S16 ->  [out] U8, U16, S16");
74
75     _policy = policy;
76     _shift  = shift;
77
78     constexpr unsigned int num_elems_processed_per_iteration(16);
79     INESimpleKernel::configure(input, output, num_elems_processed_per_iteration);
80 }
81
82 void NEDepthConvertKernel::run(const Window &window)
83 {
84     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
85     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window);
86     ARM_COMPUTE_ERROR_ON(nullptr == _input);
87     ARM_COMPUTE_ERROR_ON(nullptr == _output);
88     ARM_COMPUTE_ERROR_ON(_input == _output);
89
90     Iterator input(_input, window);
91     Iterator output(_output, window);
92
93     switch(_input->info()->data_type())
94     {
95         case DataType::U8:
96         {
97             const int16x8_t b = vdupq_n_s16(_shift);
98
99             switch(_output->info()->data_type())
100             {
101                 case DataType::S16:
102                 {
103                     /* Up-conversion U8 -> S16 */
104                     execute_window_loop(window, [&](const Coordinates & id)
105                     {
106                         const uint8x16_t texels_u8 = vld1q_u8(input.ptr());
107
108                         const int16x8x2_t texels =
109                         {
110                             {
111                                 vshlq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(texels_u8))), b),
112                                 vshlq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(texels_u8))), b)
113                             }
114                         };
115
116                         vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()), texels.val[0]);
117                         vst1q_s16(reinterpret_cast<int16_t *>(output.ptr()) + 8, texels.val[1]);
118                     },
119                     input, output);
120                     break;
121                 }
122                 case DataType::S32:
123                 {
124                     /* Up-conversion S16 -> S32 */
125                     execute_window_loop(window, [&](const Coordinates & id)
126                     {
127                         const uint8x16_t texels_u8 = vld1q_u8(input.ptr());
128
129                         const int16x8x2_t texels =
130                         {
131                             {
132                                 vshlq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(texels_u8))), b),
133                                 vshlq_s16(vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(texels_u8))), b)
134                             }
135                         };
136
137                         vst1q_s32(reinterpret_cast<int32_t *>(output.ptr()), vmovl_s16(vget_low_s16(texels.val[0])));
138                         vst1q_s32(reinterpret_cast<int32_t *>(output.ptr()) + 4, vmovl_s16(vget_high_s16(texels.val[0])));
139                         vst1q_s32(reinterpret_cast<int32_t *>(output.ptr()) + 8, vmovl_s16(vget_low_s16(texels.val[1])));
140                         vst1q_s32(reinterpret_cast<int32_t *>(output.ptr()) + 12, vmovl_s16(vget_high_s16(texels.val[1])));
141                     },
142                     input, output);
143                     break;
144                 }
145                 case DataType::U16:
146                 {
147                     /* Up-conversion U8 -> U16 */
148                     execute_window_loop(window, [&](const Coordinates & id)
149                     {
150                         const uint8x16_t texels_u8 = vld1q_u8(input.ptr());
151
152                         const uint16x8x2_t texels =
153                         {
154                             {
155                                 vshlq_u16(vmovl_u8(vget_low_u8(texels_u8)), b),
156                                 vshlq_u16(vmovl_u8(vget_high_u8(texels_u8)), b)
157                             }
158                         };
159
160                         vst1q_u16(reinterpret_cast<uint16_t *>(output.ptr()), texels.val[0]);
161                         vst1q_u16(reinterpret_cast<uint16_t *>(output.ptr()) + 8, texels.val[1]);
162                     },
163                     input, output);
164                     break;
165                 }
166                 default:
167                     ARM_COMPUTE_ERROR("Output data type not supported");
168             }
169             break;
170         }
171         case DataType::S16:
172         {
173             switch(_output->info()->data_type())
174             {
175                 case DataType::U8:
176                 {
177                     const int16x8_t b = vdupq_n_s16(-static_cast<int16_t>(_shift));
178
179                     /* Down-conversion S16 -> U8 */
180                     if(ConvertPolicy::SATURATE == _policy)
181                     {
182                         execute_window_loop(window, [&](const Coordinates & id)
183                         {
184                             const int16x8x2_t texels =
185                             {
186                                 {
187                                     vqshlq_s16(vld1q_s16(reinterpret_cast<int16_t *>(input.ptr())), b),
188                                     vqshlq_s16(vld1q_s16(reinterpret_cast<int16_t *>(input.ptr()) + 8), b)
189                                 }
190                             };
191
192                             vst1q_u8(output.ptr(), vcombine_u8(vqmovun_s16(texels.val[0]), vqmovun_s16(texels.val[1])));
193                         },
194                         input, output);
195                     }
196                     else
197                     {
198                         execute_window_loop(window, [&](const Coordinates & id)
199                         {
200                             const int16x8x2_t texels =
201                             {
202                                 {
203                                     vshlq_s16(vld1q_s16(reinterpret_cast<int16_t *>(input.ptr())), b),
204                                     vshlq_s16(vld1q_s16(reinterpret_cast<int16_t *>(input.ptr()) + 8), b)
205                                 }
206                             };
207
208                             vst1q_u8(output.ptr(), vcombine_u8(vmovn_u16(vreinterpretq_u16_s16(texels.val[0])),
209                                                                vmovn_u16(vreinterpretq_u16_s16(texels.val[1]))));
210                         },
211                         input, output);
212                     }
213                     break;
214                 }
215                 case DataType::S32:
216                 {
217                     const int16x8_t b = vdupq_n_s16(_shift);
218
219                     /* Up-conversion S16 -> S32 */
220                     execute_window_loop(window, [&](const Coordinates & id)
221                     {
222                         const int16x8x2_t texels =
223                         {
224                             {
225                                 vshlq_s16(vld1q_s16(reinterpret_cast<int16_t *>(input.ptr())), b),
226                                 vshlq_s16(vld1q_s16(reinterpret_cast<int16_t *>(input.ptr()) + 8), b)
227                             }
228                         };
229
230                         vst1q_s32(reinterpret_cast<int32_t *>(output.ptr()), vmovl_s16(vget_low_s16(texels.val[0])));
231                         vst1q_s32(reinterpret_cast<int32_t *>(output.ptr()) + 4, vmovl_s16(vget_high_s16(texels.val[0])));
232                         vst1q_s32(reinterpret_cast<int32_t *>(output.ptr()) + 8, vmovl_s16(vget_low_s16(texels.val[1])));
233                         vst1q_s32(reinterpret_cast<int32_t *>(output.ptr()) + 12, vmovl_s16(vget_high_s16(texels.val[1])));
234                     },
235                     input, output);
236                     break;
237                 }
238                 default:
239                     ARM_COMPUTE_ERROR("Output data type not supported");
240             }
241             break;
242         }
243         case DataType::U16:
244         {
245             switch(_output->info()->data_type())
246             {
247                 case DataType::U8:
248                 {
249                     const int16x8_t b = vdupq_n_s16(-static_cast<int16_t>(_shift));
250
251                     /* Down-conversion U16 -> U8 */
252                     if(ConvertPolicy::SATURATE == _policy)
253                     {
254                         execute_window_loop(window, [&](const Coordinates & id)
255                         {
256                             const uint16x8x2_t texels =
257                             {
258                                 {
259                                     vqshlq_u16(vld1q_u16(reinterpret_cast<uint16_t *>(input.ptr())), b),
260                                     vqshlq_u16(vld1q_u16(reinterpret_cast<uint16_t *>(input.ptr()) + 8), b)
261                                 }
262                             };
263
264                             vst1q_u8(output.ptr(), vcombine_u8(vqmovn_u16(texels.val[0]), vqmovn_u16(texels.val[1])));
265                         },
266                         input, output);
267                     }
268                     else
269                     {
270                         execute_window_loop(window, [&](const Coordinates & id)
271                         {
272                             const uint16x8x2_t texels =
273                             {
274                                 {
275                                     vshlq_u16(vld1q_u16(reinterpret_cast<uint16_t *>(input.ptr())), b),
276                                     vshlq_u16(vld1q_u16(reinterpret_cast<uint16_t *>(input.ptr()) + 8), b)
277                                 }
278                             };
279
280                             vst1q_u8(output.ptr(), vcombine_u8(vqmovn_u16(texels.val[0]), vqmovn_u16(texels.val[1])));
281                         },
282                         input, output);
283                     }
284                     break;
285                 }
286                 case DataType::U32:
287                 {
288                     const int32x4_t b = vdupq_n_s32(_shift);
289
290                     /* Up-conversion U16 -> U32 */
291                     execute_window_loop(window, [&](const Coordinates & id)
292                     {
293                         const uint16x8x2_t texels =
294                         {
295                             {
296                                 vld1q_u16(reinterpret_cast<uint16_t *>(input.ptr())),
297                                 vld1q_u16(reinterpret_cast<uint16_t *>(input.ptr()) + 8)
298                             }
299                         };
300
301                         vst1q_u32(reinterpret_cast<uint32_t *>(output.ptr()), vshlq_u32(vmovl_u16(vget_low_u16(texels.val[0])), b));
302                         vst1q_u32(reinterpret_cast<uint32_t *>(output.ptr()) + 4, vshlq_u32(vmovl_u16(vget_high_u16(texels.val[0])), b));
303                         vst1q_u32(reinterpret_cast<uint32_t *>(output.ptr()) + 8, vshlq_u32(vmovl_u16(vget_low_u16(texels.val[1])), b));
304                         vst1q_u32(reinterpret_cast<uint32_t *>(output.ptr()) + 12, vshlq_u32(vmovl_u16(vget_high_u16(texels.val[1])), b));
305                     },
306                     input, output);
307                     break;
308                 }
309                 default:
310                     ARM_COMPUTE_ERROR("Output data type not supported");
311             }
312             break;
313         }
314         default:
315             ARM_COMPUTE_ERROR("Not supported");
316     }
317 }