arm_compute v17.04
[platform/upstream/armcl.git] / src / core / CL / kernels / CLDepthConvertKernel.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/CL/kernels/CLDepthConvertKernel.h"
25
26 #include "arm_compute/core/CL/CLHelpers.h"
27 #include "arm_compute/core/CL/CLKernelLibrary.h"
28 #include "arm_compute/core/CL/ICLTensor.h"
29 #include "arm_compute/core/CL/OpenCL.h"
30 #include "arm_compute/core/Error.h"
31 #include "arm_compute/core/TensorInfo.h"
32 #include "arm_compute/core/Utils.h"
33 #include "arm_compute/core/Validate.h"
34
35 #include <cstddef>
36 #include <set>
37 #include <string>
38
39 using namespace arm_compute;
40
41 void CLDepthConvertKernel::configure(const ICLTensor *input, ICLTensor *output, ConvertPolicy policy, uint32_t shift)
42 {
43     ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S16, DataType::U16, DataType::U32, DataType::S32);
44     ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::S16, DataType::U16, DataType::U32, DataType::S32);
45     ARM_COMPUTE_ERROR_ON(input == output);
46     ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == output->info()->data_type(), "Input and output data types must be different");
47     ARM_COMPUTE_ERROR_ON(shift >= 8);
48
49     // Check if convertion is supported
50     ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::U8 && (output->info()->data_type() != DataType::U16 && output->info()->data_type() != DataType::S16
51                                                                             && output->info()->data_type() != DataType::U32 && output->info()->data_type() != DataType::S32),
52                              "Only data types supported [in] U8 -> [out] U16, S16, U32, S32");
53
54     ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::U16 && (output->info()->data_type() != DataType::U8 && output->info()->data_type() != DataType::U32
55                                                                              && output->info()->data_type() != DataType::S32),
56                              "Only data types supported [in] U16 ->  [out] U8, U32, S32");
57
58     ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::S16 && (output->info()->data_type() != DataType::U8 && output->info()->data_type() != DataType::U32
59                                                                              && output->info()->data_type() != DataType::S32),
60                              "Only data types supported [in] S16 ->  [out] U8, U32, S32");
61
62     ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::U32 && (output->info()->data_type() != DataType::U8 && output->info()->data_type() != DataType::U16
63                                                                              && output->info()->data_type() != DataType::S16),
64                              "Only data types supported [in] U32 ->  [out] U8, U16, S16");
65
66     ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::S32 && (output->info()->data_type() != DataType::U8 && output->info()->data_type() != DataType::U16
67                                                                              && output->info()->data_type() != DataType::S16),
68                              "Only data types supported [in] S32 ->  [out] U8, U16, S16");
69
70     // Get data sizes
71     const size_t input_size  = data_size_from_type(input->info()->data_type());
72     const size_t output_size = data_size_from_type(output->info()->data_type());
73
74     // Construct kernel name and build options
75     std::string           kernel_name = "convert_depth";
76     std::set<std::string> build_opts;
77     if(input_size > output_size)
78     {
79         kernel_name += "_down";
80         build_opts.insert((policy == ConvertPolicy::WRAP) ? "-DWRAP" : "-DSATURATE");
81     }
82     else
83     {
84         kernel_name += "_up";
85     }
86     build_opts.insert("-DDATA_TYPE_IN=" + get_cl_type_from_data_type(input->info()->data_type()));
87     build_opts.insert("-DDATA_TYPE_OUT=" + get_cl_type_from_data_type(output->info()->data_type()));
88
89     // Create kernel
90     _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
91
92     // Set shift arg
93     unsigned int idx = 2 * num_arguments_per_2D_tensor(); //Skip the input and output parameters
94     _kernel.setArg(idx++, shift);
95
96     // Configure kernel
97     constexpr unsigned int num_elems_processed_per_iteration = 16;
98     ICLSimple2DKernel::configure(input, output, num_elems_processed_per_iteration);
99 }