COMPMID-791: Adds support of QASYMM8 in NEDepthwiseConvolution3x3

Change-Id: I1a9ed6c3420ddf8978aeaad48d9915333b006b49
Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/116374
Tested-by: Jenkins <bsgcomp@arm.com>
Reviewed-by: Anthony Barbier <anthony.barbier@arm.com>
diff --git a/arm_compute/core/NEON/kernels/convolution/NEDirectConvolutionDetail.h b/arm_compute/core/NEON/kernels/convolution/NEDirectConvolutionDetail.h
index c358558..908fa13 100644
--- a/arm_compute/core/NEON/kernels/convolution/NEDirectConvolutionDetail.h
+++ b/arm_compute/core/NEON/kernels/convolution/NEDirectConvolutionDetail.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -36,12 +36,14 @@
 {
 /** Loads a 3x3 matrix as a row  (float).
  *
- * @param[in] ptr Pointer to a float 3x3 matrix.
+ * @param[in] ptr            Pointer to a float 3x3 matrix.
+ * @param[in] weights_offset (Optional) Weights quantization offset.
  *
  * @return The loaded matrix.
  */
-inline float32x4x3_t load_matrix_row(const float *ptr)
+inline float32x4x3_t load_matrix_row(const float *ptr, int weights_offset = 0)
 {
+    ARM_COMPUTE_UNUSED(weights_offset);
     const float32x4x3_t r =
     {
         {
@@ -55,12 +57,14 @@
 
 /** Loads a 3x3 matrix as a row  (qint8_t).
  *
- * @param[in] ptr Pointer to a qint8 3x3 matrix.
+ * @param[in] ptr            Pointer to a qint8 3x3 matrix.
+ * @param[in] weights_offset (Optional) Weights quantization offset.
  *
  * @return The loaded matrix.
  */
-inline qint8x8x3_t load_matrix_row(const qint8_t *ptr)
+inline qint8x8x3_t load_matrix_row(const qint8_t *ptr, int weights_offset = 0)
 {
+    ARM_COMPUTE_UNUSED(weights_offset);
     /* ptr is a pointer to a row in a 3x3 matrix, the function returns 3 vectors holding exactly the same value in all lanes:
        r.val[0] contains the first element, r.val[1] the second element and r.val[2] the third element (in all lanes) */
     const qint8x8x3_t r =
@@ -74,6 +78,30 @@
     return r;
 }
 
+/** Loads a 3x3 matrix as a row  (uint8_t).
+ *
+ * @param[in] ptr            Pointer to a uint8_t 3x3 matrix.
+ * @param[in] weights_offset (Optional) Weights quantization offset.
+ *
+ * @return The loaded matrix.
+ */
+inline int32x4x3_t load_matrix_row(const uint8_t *ptr, int weights_offset = 0)
+{
+    const int32x4_t v_weights_offset = vdupq_n_s32(weights_offset);
+
+    /* ptr is a pointer to a row in a 3x3 matrix, the function returns 3 vectors holding exactly the same value in all lanes:
+       r.val[0] contains the first element, r.val[1] the second element and r.val[2] the third element (in all lanes) */
+    int32x4x3_t r =
+    {
+        {
+            vaddq_s32(v_weights_offset, vdupq_n_s32(*ptr)),
+            vaddq_s32(v_weights_offset, vdupq_n_s32(*(ptr + 1))),
+            vaddq_s32(v_weights_offset, vdupq_n_s32(*(ptr + 2)))
+        }
+    };
+    return r;
+}
+
 /** Perform a convolve3x3 on float32.
  *
  * @param[in] in_top               Pointer to the first row of the input.
@@ -83,15 +111,21 @@
  * @param[in] m1                   Second row of the filter.
  * @param[in] m2                   Third row of the filter.
  * @param[in] fixed_point_position (Optional) Fixed point position.
+ * @param[in] input_offset         (Optional) Input quantization offset.
  *
  */
 template <unsigned int stridex>
-float32x4x2_t convolve_3x3(const float *in_top, const float *in_mid, const float *in_low, const float32x4x3_t &m0, const float32x4x3_t &m1, const float32x4x3_t &m2, int fixed_point_position);
+float32x4x2_t convolve_3x3(const float *in_top, const float *in_mid, const float *in_low,
+                           const float32x4x3_t &m0, const float32x4x3_t &m1, const float32x4x3_t &m2,
+                           int fixed_point_position, int input_offset = 0);
 
 template <>
-inline float32x4x2_t convolve_3x3<1>(const float *in_top, const float *in_mid, const float *in_low, const float32x4x3_t &m0, const float32x4x3_t &m1, const float32x4x3_t &m2, int fixed_point_position)
+inline float32x4x2_t convolve_3x3<1>(const float *in_top, const float *in_mid, const float *in_low,
+                                     const float32x4x3_t &m0, const float32x4x3_t &m1, const float32x4x3_t &m2,
+                                     int fixed_point_position, int input_offset)
 {
     ARM_COMPUTE_UNUSED(fixed_point_position);
+    ARM_COMPUTE_UNUSED(input_offset);
 
     const float32x4x3_t vtop =
     {
@@ -149,9 +183,13 @@
 }
 
 template <>
-inline float32x4x2_t convolve_3x3<2>(const float *in_top, const float *in_mid, const float *in_low, const float32x4x3_t &m0, const float32x4x3_t &m1, const float32x4x3_t &m2, int fixed_point_position)
+inline float32x4x2_t convolve_3x3<2>(const float *in_top, const float *in_mid, const float *in_low,
+                                     const float32x4x3_t &m0, const float32x4x3_t &m1, const float32x4x3_t &m2,
+                                     int fixed_point_position, int input_offset)
 {
-    float32x4x2_t out = convolve_3x3<1>(in_top, in_mid, in_low, m0, m1, m2, fixed_point_position);
+    ARM_COMPUTE_UNUSED(input_offset);
+
+    float32x4x2_t out = convolve_3x3<1>(in_top, in_mid, in_low, m0, m1, m2, fixed_point_position, input_offset);
     out.val[0]        = vsetq_lane_f32(vgetq_lane_f32(out.val[0], 2), out.val[0], 1);
     out.val[0]        = vsetq_lane_f32(vgetq_lane_f32(out.val[1], 0), out.val[0], 2);
     out.val[0]        = vsetq_lane_f32(vgetq_lane_f32(out.val[1], 2), out.val[0], 3);
@@ -159,9 +197,13 @@
 }
 
 template <>
-inline float32x4x2_t convolve_3x3<3>(const float *in_top, const float *in_mid, const float *in_low, const float32x4x3_t &m0, const float32x4x3_t &m1, const float32x4x3_t &m2, int fixed_point_position)
+inline float32x4x2_t convolve_3x3<3>(const float *in_top, const float *in_mid, const float *in_low,
+                                     const float32x4x3_t &m0, const float32x4x3_t &m1, const float32x4x3_t &m2,
+                                     int fixed_point_position, int input_offset)
 {
-    float32x4x2_t out = convolve_3x3<1>(in_top, in_mid, in_low, m0, m1, m2, fixed_point_position);
+    ARM_COMPUTE_UNUSED(input_offset);
+
+    float32x4x2_t out = convolve_3x3<1>(in_top, in_mid, in_low, m0, m1, m2, fixed_point_position, input_offset);
     out.val[0]        = vsetq_lane_f32(vgetq_lane_f32(out.val[0], 3), out.val[0], 1);
     return out;
 }
@@ -175,15 +217,21 @@
  * @param[in] m1                   Second row of the filter.
  * @param[in] m2                   Third row of the filter.
  * @param[in] fixed_point_position (Optional) Fixed point position.
+ * @param[in] input_offset         (Optional) Input quantization offset.
  *
  */
 template <unsigned int stridex>
-qint16x8x2_t convolve_3x3(const qint8_t *in_top, const qint8_t *in_mid, const qint8_t *in_low, const qint8x8x3_t &m0, const qint8x8x3_t &m1, const qint8x8x3_t &m2, int fixed_point_position);
+qint16x8x2_t convolve_3x3(const qint8_t *in_top, const qint8_t *in_mid, const qint8_t *in_low,
+                          const qint8x8x3_t &m0, const qint8x8x3_t &m1, const qint8x8x3_t &m2,
+                          int fixed_point_position, int input_offset = 0);
 
 template <>
-inline qint16x8x2_t convolve_3x3<1>(const qint8_t *in_top, const qint8_t *in_mid, const qint8_t *in_low, const qint8x8x3_t &m0, const qint8x8x3_t &m1, const qint8x8x3_t &m2, int fixed_point_position)
+inline qint16x8x2_t convolve_3x3<1>(const qint8_t *in_top, const qint8_t *in_mid, const qint8_t *in_low,
+                                    const qint8x8x3_t &m0, const qint8x8x3_t &m1, const qint8x8x3_t &m2,
+                                    int fixed_point_position, int input_offset)
 {
     ARM_COMPUTE_UNUSED(fixed_point_position);
+    ARM_COMPUTE_UNUSED(input_offset);
 
     const qint8x8x3_t vtop =
     {
@@ -236,9 +284,13 @@
 }
 
 template <>
-inline qint16x8x2_t convolve_3x3<2>(const qint8_t *in_top, const qint8_t *in_mid, const qint8_t *in_low, const qint8x8x3_t &m0, const qint8x8x3_t &m1, const qint8x8x3_t &m2, int fixed_point_position)
+inline qint16x8x2_t convolve_3x3<2>(const qint8_t *in_top, const qint8_t *in_mid, const qint8_t *in_low,
+                                    const qint8x8x3_t &m0, const qint8x8x3_t &m1, const qint8x8x3_t &m2,
+                                    int fixed_point_position, int input_offset)
 {
-    qint16x8x2_t out = convolve_3x3<1>(in_top, in_mid, in_low, m0, m1, m2, fixed_point_position);
+    ARM_COMPUTE_UNUSED(input_offset);
+
+    qint16x8x2_t out = convolve_3x3<1>(in_top, in_mid, in_low, m0, m1, m2, fixed_point_position, input_offset);
     out.val[0]       = vsetq_lane_s16(vgetq_lane_s16(out.val[0], 2), out.val[0], 1);
     out.val[0]       = vsetq_lane_s16(vgetq_lane_s16(out.val[0], 4), out.val[0], 2);
     out.val[0]       = vsetq_lane_s16(vgetq_lane_s16(out.val[0], 6), out.val[0], 3);
@@ -250,15 +302,153 @@
 }
 
 template <>
-inline qint16x8x2_t convolve_3x3<3>(const qint8_t *in_top, const qint8_t *in_mid, const qint8_t *in_low, const qint8x8x3_t &m0, const qint8x8x3_t &m1, const qint8x8x3_t &m2, int fixed_point_position)
+inline qint16x8x2_t convolve_3x3<3>(const qint8_t *in_top, const qint8_t *in_mid, const qint8_t *in_low,
+                                    const qint8x8x3_t &m0, const qint8x8x3_t &m1, const qint8x8x3_t &m2,
+                                    int fixed_point_position, int input_offset)
 {
-    qint16x8x2_t out = convolve_3x3<1>(in_top, in_mid, in_low, m0, m1, m2, fixed_point_position);
+    ARM_COMPUTE_UNUSED(input_offset);
+
+    qint16x8x2_t out = convolve_3x3<1>(in_top, in_mid, in_low, m0, m1, m2, fixed_point_position, input_offset);
     out.val[0]       = vsetq_lane_s16(vgetq_lane_s16(out.val[0], 3), out.val[0], 1);
     out.val[0]       = vsetq_lane_s16(vgetq_lane_s16(out.val[0], 6), out.val[0], 2);
     out.val[0]       = vsetq_lane_s16(vgetq_lane_s16(out.val[1], 1), out.val[0], 3);
     return out;
 }
 
+/** Perform a convolve3x3 on uint8_t
+ *
+ * @param[in] in_top               Pointer to the first row of the input.
+ * @param[in] in_mid               Pointer to the second row of the input.
+ * @param[in] in_low               Pointer to the third row of the input.
+ * @param[in] m0                   First row of the filter.
+ * @param[in] m1                   Second row of the filter.
+ * @param[in] m2                   Third row of the filter.
+ * @param[in] fixed_point_position (Optional) Fixed point position.
+ * @param[in] input_offset         (Optional) Input quantization offset.
+ *
+ */
+template <unsigned int stridex>
+int32x4x2_t convolve_3x3(const uint8_t *in_top, const uint8_t *in_mid, const uint8_t *in_low,
+                         const int32x4x3_t &m0, const int32x4x3_t &m1, const int32x4x3_t &m2,
+                         int fixed_point_position, int input_offset);
+
+template <>
+inline int32x4x2_t convolve_3x3<1>(const uint8_t *in_top, const uint8_t *in_mid, const uint8_t *in_low, const int32x4x3_t &m0, const int32x4x3_t &m1, const int32x4x3_t &m2,
+                                   int fixed_point_position, int input_offset)
+{
+    ARM_COMPUTE_UNUSED(fixed_point_position);
+
+    const int32x4_t v_input_offset = vdupq_n_s32(input_offset);
+
+    const uint8x8x2_t vtop =
+    {
+        {
+            vld1_u8(in_top),
+            vld1_u8(in_top + 8)
+        }
+    };
+    const uint8x8x2_t vmid =
+    {
+        {
+            vld1_u8(in_mid),
+            vld1_u8(in_mid + 8)
+        }
+    };
+    const uint8x8x2_t vlow =
+    {
+        {
+            vld1_u8(in_low),
+            vld1_u8(in_low + 8)
+        }
+    };
+
+    const int32x4x3_t vtop_s32 =
+    {
+        {
+            vaddw_s16(v_input_offset, vreinterpret_s16_u16(vget_low_u16(vmovl_u8(vtop.val[0])))),
+            vaddw_s16(v_input_offset, vreinterpret_s16_u16(vget_high_u16(vmovl_u8(vtop.val[0])))),
+            vaddw_s16(v_input_offset, vreinterpret_s16_u16(vget_low_u16(vmovl_u8(vtop.val[1])))),
+        }
+    };
+    const int32x4x3_t vmid_s32 =
+    {
+        {
+            vaddw_s16(v_input_offset, vreinterpret_s16_u16(vget_low_u16(vmovl_u8(vmid.val[0])))),
+            vaddw_s16(v_input_offset, vreinterpret_s16_u16(vget_high_u16(vmovl_u8(vmid.val[0])))),
+            vaddw_s16(v_input_offset, vreinterpret_s16_u16(vget_low_u16(vmovl_u8(vmid.val[1])))),
+        }
+    };
+    const int32x4x3_t vlow_s32 =
+    {
+        {
+            vaddw_s16(v_input_offset, vreinterpret_s16_u16(vget_low_u16(vmovl_u8(vlow.val[0])))),
+            vaddw_s16(v_input_offset, vreinterpret_s16_u16(vget_high_u16(vmovl_u8(vlow.val[0])))),
+            vaddw_s16(v_input_offset, vreinterpret_s16_u16(vget_low_u16(vmovl_u8(vlow.val[1])))),
+        }
+    };
+
+    int32x4x2_t out
+    {
+        {
+            vdupq_n_s32(0),
+            vdupq_n_s32(0),
+        }
+    };
+
+    // 0
+    out.val[0] = vmlaq_s32(out.val[0], vtop_s32.val[0], m0.val[0]);
+    out.val[0] = vmlaq_s32(out.val[0], vextq_s32(vtop_s32.val[0], vtop_s32.val[1], 1), m0.val[1]);
+    out.val[0] = vmlaq_s32(out.val[0], vextq_s32(vtop_s32.val[0], vtop_s32.val[1], 2), m0.val[2]);
+
+    out.val[0] = vmlaq_s32(out.val[0], vmid_s32.val[0], m1.val[0]);
+    out.val[0] = vmlaq_s32(out.val[0], vextq_s32(vmid_s32.val[0], vmid_s32.val[1], 1), m1.val[1]);
+    out.val[0] = vmlaq_s32(out.val[0], vextq_s32(vmid_s32.val[0], vmid_s32.val[1], 2), m1.val[2]);
+
+    out.val[0] = vmlaq_s32(out.val[0], vlow_s32.val[0], m2.val[0]);
+    out.val[0] = vmlaq_s32(out.val[0], vextq_s32(vlow_s32.val[0], vlow_s32.val[1], 1), m2.val[1]);
+    out.val[0] = vmlaq_s32(out.val[0], vextq_s32(vlow_s32.val[0], vlow_s32.val[1], 2), m2.val[2]);
+
+    // 1
+    out.val[1] = vmlaq_s32(out.val[1], vtop_s32.val[1], m0.val[0]);
+    out.val[1] = vmlaq_s32(out.val[1], vextq_s32(vtop_s32.val[1], vtop_s32.val[2], 1), m0.val[1]);
+    out.val[1] = vmlaq_s32(out.val[1], vextq_s32(vtop_s32.val[1], vtop_s32.val[2], 2), m0.val[2]);
+
+    out.val[1] = vmlaq_s32(out.val[1], vmid_s32.val[1], m1.val[0]);
+    out.val[1] = vmlaq_s32(out.val[1], vextq_s32(vmid_s32.val[1], vmid_s32.val[2], 1), m1.val[1]);
+    out.val[1] = vmlaq_s32(out.val[1], vextq_s32(vmid_s32.val[1], vmid_s32.val[2], 2), m1.val[2]);
+
+    out.val[1] = vmlaq_s32(out.val[1], vlow_s32.val[1], m2.val[0]);
+    out.val[1] = vmlaq_s32(out.val[1], vextq_s32(vlow_s32.val[1], vlow_s32.val[2], 1), m2.val[1]);
+    out.val[1] = vmlaq_s32(out.val[1], vextq_s32(vlow_s32.val[1], vlow_s32.val[2], 2), m2.val[2]);
+
+    return out;
+}
+
+template <>
+inline int32x4x2_t convolve_3x3<2>(const uint8_t *in_top, const uint8_t *in_mid, const uint8_t *in_low,
+                                   const int32x4x3_t &m0, const int32x4x3_t &m1, const int32x4x3_t &m2,
+                                   int fixed_point_position, int input_offset)
+{
+    ARM_COMPUTE_UNUSED(fixed_point_position);
+
+    int32x4x2_t out = convolve_3x3<1>(in_top, in_mid, in_low, m0, m1, m2, fixed_point_position, input_offset);
+    out.val[0]      = vsetq_lane_s32(vgetq_lane_s32(out.val[0], 2), out.val[0], 1);
+    out.val[0]      = vsetq_lane_s32(vgetq_lane_s32(out.val[1], 0), out.val[0], 2);
+    out.val[0]      = vsetq_lane_s32(vgetq_lane_s32(out.val[1], 2), out.val[0], 3);
+    return out;
+}
+
+template <>
+inline int32x4x2_t convolve_3x3<3>(const uint8_t *in_top, const uint8_t *in_mid, const uint8_t *in_low,
+                                   const int32x4x3_t &m0, const int32x4x3_t &m1, const int32x4x3_t &m2,
+                                   int fixed_point_position, int input_offset)
+{
+    ARM_COMPUTE_UNUSED(fixed_point_position);
+    int32x4x2_t out = convolve_3x3<1>(in_top, in_mid, in_low, m0, m1, m2, fixed_point_position, input_offset);
+    out.val[0]      = vsetq_lane_s32(vgetq_lane_s32(out.val[0], 3), out.val[0], 1);
+    return out;
+}
+
 /** Stores a float32x4x2_t array into a memory location.
  *
  * @param[in] buffer Pointer to the memory location where the values will be stored.
@@ -315,6 +505,34 @@
     vst1_qs16(buffer, vget_low_s16(values.val[0]));
 }
 
+/** Stores a uint32_t array into a memory location.
+ *
+ * @param[in] buffer Pointer to the memory location where the values will be stored.
+ * @param[in] values Values that will be stored.
+ *
+ */
+template <unsigned int stridex>
+void store_results(int32_t *buffer, const int32x4x2_t &values);
+
+template <>
+inline void store_results<1>(int32_t *buffer, const int32x4x2_t &values)
+{
+    vst1q_s32(buffer, values.val[0]);
+    vst1q_s32(buffer + 4, values.val[1]);
+}
+
+template <>
+inline void store_results<2>(int32_t *buffer, const int32x4x2_t &values)
+{
+    vst1q_s32(buffer, values.val[0]);
+}
+
+template <>
+inline void store_results<3>(int32_t *buffer, const int32x4x2_t &values)
+{
+    vst1_s32(buffer, vget_low_s32(values.val[0]));
+}
+
 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
 /** Loads a 3x3 matrix as a row (float16_t).
  *