[COMPMID-1229] Implementing Pad on OpenCL -FP32/FP16

Change-Id: Ideead99410e5e0bda1035030af1bbcd0a65ea15e
Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/144792
Tested-by: bsgcomp <bsgcomp@arm.com>
Reviewed-by: Georgios Pinitas <georgios.pinitas@arm.com>
diff --git a/arm_compute/core/CL/CLKernels.h b/arm_compute/core/CL/CLKernels.h
index 2fd6ab2..8865801 100644
--- a/arm_compute/core/CL/CLKernels.h
+++ b/arm_compute/core/CL/CLKernels.h
@@ -91,6 +91,7 @@
 #include "arm_compute/core/CL/kernels/CLMagnitudePhaseKernel.h"
 #include "arm_compute/core/CL/kernels/CLMeanStdDevKernel.h"
 #include "arm_compute/core/CL/kernels/CLMedian3x3Kernel.h"
+#include "arm_compute/core/CL/kernels/CLMemsetKernel.h"
 #include "arm_compute/core/CL/kernels/CLMinMaxLayerKernel.h"
 #include "arm_compute/core/CL/kernels/CLMinMaxLocationKernel.h"
 #include "arm_compute/core/CL/kernels/CLNonLinearFilterKernel.h"
diff --git a/arm_compute/core/CL/kernels/CLCopyKernel.h b/arm_compute/core/CL/kernels/CLCopyKernel.h
index 2aeb488..9fc3759 100644
--- a/arm_compute/core/CL/kernels/CLCopyKernel.h
+++ b/arm_compute/core/CL/kernels/CLCopyKernel.h
@@ -47,18 +47,20 @@
     CLCopyKernel &operator=(CLCopyKernel &&) = default;
     /** Initialize the kernel's input, output.
      *
-     * @param[in]  input  Source tensor. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32.
-     * @param[out] output Destination tensor. Data types supported: same as @p input.
+     * @param[in]  input   Source tensor. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32.
+     * @param[out] output  Destination tensor. Data types supported: same as @p input.
+     * @param[in]  padding (Optional) Padding to be applied to the input tensor
      */
-    void configure(const ICLTensor *input, ICLTensor *output);
+    void configure(const ICLTensor *input, ICLTensor *output, const PaddingList &padding = PaddingList());
     /** Static function to check if given info will lead to a valid configuration of @ref CLCopyKernel
      *
-     * @param[in] input  Source tensor. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32.
-     * @param[in] output Destination tensor. Data types supported: same as @p input.
+     * @param[in] input   Source tensor info. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32.
+     * @param[in] output  Destination tensor info. Data types supported: same as @p input.
+     * @param[in] padding (Optional) Padding to be applied to the input tensor
      *
      * @return a status
      */
-    static Status validate(const ITensorInfo *input, const ITensorInfo *output);
+    static Status validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding = PaddingList());
 
     // Inherited methods overridden:
     void run(const Window &window, cl::CommandQueue &queue) override;
diff --git a/arm_compute/core/CL/kernels/CLMemsetKernel.h b/arm_compute/core/CL/kernels/CLMemsetKernel.h
new file mode 100644
index 0000000..e35a9f1
--- /dev/null
+++ b/arm_compute/core/CL/kernels/CLMemsetKernel.h
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2018 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.
+ */
+#ifndef __ARM_COMPUTE_CLMEMSETKERNEL_H__
+#define __ARM_COMPUTE_CLMEMSETKERNEL_H__
+
+#include "arm_compute/core/CL/ICLKernel.h"
+#include "arm_compute/core/PixelValue.h"
+#include "arm_compute/core/Types.h"
+
+namespace arm_compute
+{
+class ICLTensor;
+
+/** Interface for filling the planes of a tensor */
+class CLMemsetKernel : public ICLKernel
+{
+public:
+    /** Default constructor */
+    CLMemsetKernel();
+    /** Prevent instances of this class from being copied (As this class contains pointers) */
+    CLMemsetKernel(const CLMemsetKernel &) = delete;
+    /** Prevent instances of this class from being copied (As this class contains pointers) */
+    CLMemsetKernel &operator=(const CLMemsetKernel &) = delete;
+    /** Allow instances of this class to be moved */
+    CLMemsetKernel(CLMemsetKernel &&) = default;
+    /** Allow instances of this class to be moved */
+    CLMemsetKernel &operator=(CLMemsetKernel &&) = default;
+    /** Default destructor */
+    ~CLMemsetKernel() = default;
+
+    /** Initialise the kernel's tensor and filling value
+     *
+     * @param[in,out] tensor         Input tensor to fill. Supported data types: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32
+     * @param[in]     constant_value The value used to fill the planes of the tensor
+     */
+    void configure(ICLTensor *tensor, const PixelValue &constant_value);
+    /** Static function to check if given info will lead to a valid configuration of @ref CLMemsetKernel
+     *
+     * @param[in] tensor         Source tensor info. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32
+     * @param[in] constant_value The value used to fill the planes of the tensor
+     *
+     * @return a status
+     */
+    static Status validate(const ITensorInfo *tensor, const PixelValue &constant_value);
+
+    // Inherited methods overridden:
+    void run(const Window &window, cl::CommandQueue &queue) override;
+
+private:
+    ICLTensor *_tensor;
+};
+} // namespace arm_compute
+#endif /*__ARM_COMPUTE_CLMEMSETRKERNEL_H__ */
diff --git a/arm_compute/core/Types.h b/arm_compute/core/Types.h
index 31f2c88..4c81e1f 100644
--- a/arm_compute/core/Types.h
+++ b/arm_compute/core/Types.h
@@ -470,6 +470,12 @@
     uint32_t z; /**< Z coordinates */
 };
 
+/** Padding information as a pair of unsigned int start/end */
+using PaddingInfo = std::pair<uint32_t, uint32_t>;
+
+/** List of padding information */
+using PaddingList = std::vector<PaddingInfo>;
+
 /** Region of interest */
 struct ROI
 {
diff --git a/arm_compute/core/Utils.h b/arm_compute/core/Utils.h
index 222f867..c742ebc 100644
--- a/arm_compute/core/Utils.h
+++ b/arm_compute/core/Utils.h
@@ -25,6 +25,7 @@
 #define __ARM_COMPUTE_UTILS_H__
 
 #include "arm_compute/core/Error.h"
+#include "arm_compute/core/PixelValue.h"
 #include "arm_compute/core/Rounding.h"
 #include "arm_compute/core/Types.h"
 
@@ -938,6 +939,14 @@
  * @return The string describing the pooling type.
  */
 const std::string &string_from_pooling_type(PoolingType type);
+/** Convert a PixelValue to a string, represented through the specific data type
+ *
+ * @param[in] value     The PixelValue to convert
+ * @param[in] data_type The type to be used to convert the @p value
+ *
+ * @return String representation of the PixelValue through the given data type.
+ */
+std::string string_from_pixel_value(const PixelValue &value, const DataType data_type);
 /** Lower a given string.
  *
  * @param[in] val Given string to lower.
diff --git a/arm_compute/core/utils/misc/ShapeCalculator.h b/arm_compute/core/utils/misc/ShapeCalculator.h
index 4ae97f7..2db7b28 100644
--- a/arm_compute/core/utils/misc/ShapeCalculator.h
+++ b/arm_compute/core/utils/misc/ShapeCalculator.h
@@ -573,6 +573,16 @@
     return output_shape;
 }
 
+inline TensorShape compute_padded_shape(const TensorShape &input_shape, const PaddingList &padding)
+{
+    TensorShape padded_shape = input_shape;
+    for(size_t dim = 0; dim < padding.size(); ++dim)
+    {
+        padded_shape.set(dim, padding[dim].first + input_shape[dim] + padding[dim].second);
+    }
+    return padded_shape;
+}
+
 template <typename T>
 inline TensorShape extract_shape(T *data)
 {
diff --git a/arm_compute/runtime/CL/CLFunctions.h b/arm_compute/runtime/CL/CLFunctions.h
index cf8911b..4619aa5 100644
--- a/arm_compute/runtime/CL/CLFunctions.h
+++ b/arm_compute/runtime/CL/CLFunctions.h
@@ -95,6 +95,7 @@
 #include "arm_compute/runtime/CL/functions/CLNormalizationLayer.h"
 #include "arm_compute/runtime/CL/functions/CLNormalizePlanarYUVLayer.h"
 #include "arm_compute/runtime/CL/functions/CLOpticalFlow.h"
+#include "arm_compute/runtime/CL/functions/CLPadLayer.h"
 #include "arm_compute/runtime/CL/functions/CLPermute.h"
 #include "arm_compute/runtime/CL/functions/CLPhase.h"
 #include "arm_compute/runtime/CL/functions/CLPixelWiseMultiplication.h"
diff --git a/arm_compute/runtime/CL/functions/CLPadLayer.h b/arm_compute/runtime/CL/functions/CLPadLayer.h
new file mode 100644
index 0000000..b9dca665
--- /dev/null
+++ b/arm_compute/runtime/CL/functions/CLPadLayer.h
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2018 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.
+ */
+#ifndef __ARM_COMPUTE_CLPADLAYER_H__
+#define __ARM_COMPUTE_CLPADLAYER_H__
+
+#include "arm_compute/core/CL/kernels/CLCopyKernel.h"
+#include "arm_compute/core/CL/kernels/CLFillBorderKernel.h"
+#include "arm_compute/core/CL/kernels/CLMemsetKernel.h"
+#include "arm_compute/core/Types.h"
+#include "arm_compute/runtime/CL/CLScheduler.h"
+#include "arm_compute/runtime/IFunction.h"
+
+namespace arm_compute
+{
+class ICLTensor;
+
+/** Basic function to pad a tensor. This function calls the following OpenCL kernels:
+ *
+ *  -# @ref CLMemsetKernel
+ *  -# @ref CLFillBorderKernel
+ *  -# @ref CLCopyKernel
+ */
+class CLPadLayer : public IFunction
+{
+public:
+    /** Default constructor*/
+    CLPadLayer();
+
+    /** Initialize the function
+     *
+     * @param[in]  input   Source tensor. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32.
+     * @param[out] output  Output tensor. Data type supported: same as @p input
+     * @param[in]  padding The padding for each spatial dimension of the input tensor. The pair padding[i]
+     *                     specifies the front and the end padding in the i-th dimension.
+     */
+    void configure(ICLTensor *input, ICLTensor *output, const PaddingList &padding);
+
+    /**  Static function to check if given info will lead to a valid configuration of @ref CLPadLayer.
+     *
+     * @param[in] input   Source tensor info. Data types supported: U8/S8/QASYMM8/U16/S16/F16/U32/S32/F32.
+     * @param[in] output  Output tensor info. Data type supported: same as @p input
+     * @param[in] padding The padding for each spatial dimension of the input tensor. The pair padding[i]
+     *                     specifies the front and the end padding in the i-th dimension.
+     */
+    static Status validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding);
+
+    // Inherited methods overridden:
+    void run() override;
+
+private:
+    CLCopyKernel       _copy_kernel;
+    CLFillBorderKernel _fillborder_kernel;
+    CLMemsetKernel     _memset_kernel;
+};
+} // namespace arm_compute
+#endif /*__ARM_COMPUTE_PADLAYER_H__ */