COMPMID-401: Implement FixedPointPosition conversion for NEON.

Adds support of changing the fixed point position of a tensor in
DepthConvert.

Change-Id: Ic3b50a4628fac7497a0217d92941c9d6f64d21cb
Reviewed-on: http://mpd-gerrit.cambridge.arm.com/80438
Reviewed-by: Anthony Barbier <anthony.barbier@arm.com>
Tested-by: Kaizen <jeremy.johnson+kaizengerrit@arm.com>
diff --git a/src/core/NEON/kernels/NEDepthConvertKernel.cpp b/src/core/NEON/kernels/NEDepthConvertKernel.cpp
index 3c1a94d..f720370 100644
--- a/src/core/NEON/kernels/NEDepthConvertKernel.cpp
+++ b/src/core/NEON/kernels/NEDepthConvertKernel.cpp
@@ -40,24 +40,53 @@
 } // namespace arm_compute
 
 NEDepthConvertKernel::NEDepthConvertKernel()
-    : _policy(), _shift(0)
+    : _input(nullptr), _output(nullptr), _policy(), _shift(0), _fixed_point_position_input(0), _fixed_point_position_output(0)
 {
 }
 
-void NEDepthConvertKernel::configure(const ITensor *input, ITensor *output, ConvertPolicy policy, uint32_t shift)
+void NEDepthConvertKernel::configure(ITensor *input, ITensor *output, ConvertPolicy policy, uint32_t shift)
 {
     ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::QS8, DataType::S16, DataType::U16, DataType::QS16, DataType::F32);
-    ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::QS8, DataType::S16, DataType::U16, DataType::QS16, DataType::U32, DataType::S32, DataType::F32);
-    ARM_COMPUTE_ERROR_ON(shift >= 8);
-    ARM_COMPUTE_ERROR_ON(input == output);
-    ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == output->info()->data_type(), "Input and output data_types must be different");
+
+    _input  = input;
+    _output = input;
+    _policy = policy;
+    _shift  = shift;
+
+    if(output != nullptr)
+    {
+        // Auto initialize output shape if not initialized (We can only auto-configure the shape, datatype must be given)
+        set_shape_if_empty(*output->info(), input->info()->tensor_shape());
+
+        ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::QS8, DataType::S16, DataType::U16, DataType::QS16, DataType::U32, DataType::S32, DataType::F32);
+        ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
+
+        // Set output
+        _output = output;
+    }
+
+    // Set initial fixed point position of input and output
+    _fixed_point_position_input  = input->info()->fixed_point_position();
+    _fixed_point_position_output = _output->info()->fixed_point_position();
+
+    // Set the fixed point position to the output tensor if needed
+    if(is_data_type_fixed_point(input->info()->data_type()) && is_data_type_fixed_point(_output->info()->data_type()))
+    {
+        // If in-place set the fixed point position of the output tensor to be equal to shift
+        _fixed_point_position_output = (_input == _output) ? static_cast<int>(_shift) : _fixed_point_position_output;
+        // Set fixed point position to output tensor
+        _output->info()->set_fixed_point_position(_fixed_point_position_output);
+    }
+
+    ARM_COMPUTE_ERROR_ON(shift >= 8 && (!is_data_type_fixed_point(input->info()->data_type()) && !is_data_type_fixed_point(output->info()->data_type())));
+    ARM_COMPUTE_ERROR_ON(input == output && (data_size_from_type(input->info()->data_type()) != data_size_from_type(output->info()->data_type())));
 
     ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::U8 && (output->info()->data_type() != DataType::S16 && output->info()->data_type() != DataType::U16
                                                                             && output->info()->data_type() != DataType::S32),
                              "Only data_types supported [in] U8 -> [out] U16, S16, S32");
 
-    ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::QS8 && output->info()->data_type() != DataType::F32,
-                             "Only data_types supported [in] QS8 ->  [out] F32");
+    ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::QS8 && (output->info()->data_type() != DataType::QS8 && output->info()->data_type() != DataType::F32),
+                             "Only data_types supported [in] QS8 ->  [out] QS8, F32");
 
     ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::U16 && (output->info()->data_type() != DataType::U8 && output->info()->data_type() != DataType::U32),
                              "Only data_types supported [in] U16 ->  [out] U8, U32");
@@ -65,28 +94,36 @@
     ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::S16 && (output->info()->data_type() != DataType::U8 && output->info()->data_type() != DataType::S32),
                              "Only data_types supported [in] S16 ->  [out] U8, S32");
 
-    ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::QS16 && output->info()->data_type() != DataType::F32,
-                             "Only data_types supported [in] QS16 ->  [out] F32");
+    ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::QS16 && (output->info()->data_type() != DataType::QS16 && output->info()->data_type() != DataType::F32),
+                             "Only data_types supported [in] QS16 ->  [out] QS16, F32");
 
     ARM_COMPUTE_ERROR_ON_MSG(input->info()->data_type() == DataType::F32 && (output->info()->data_type() != DataType::QS8 && output->info()->data_type() != DataType::QS16),
                              "Only data_types supported [in] F32 ->  [out] QS8, QS16");
 
-    // Auto initialize output shape if not initialized (We can only auto-configure the shape, datatype must be given)
-    set_shape_if_empty(*output->info(), input->info()->tensor_shape());
-
-    ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
-
-    _policy = policy;
-    _shift  = shift;
-
     constexpr unsigned int num_elems_processed_per_iteration = 16;
-    INESimpleKernel::configure(input, output, num_elems_processed_per_iteration);
+
+    // Configure kernel window
+    Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
+
+    AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
+    if(output != nullptr)
+    {
+        AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
+        update_window_and_padding(win, input_access, output_access);
+        output_access.set_valid_region(win, input->info()->valid_region());
+    }
+    else
+    {
+        // In-place computation
+        update_window_and_padding(win, input_access);
+    }
+    ICPPKernel::configure(win);
 }
 
 void NEDepthConvertKernel::run(const Window &window)
 {
     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
-    ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window);
+    ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
     ARM_COMPUTE_ERROR_ON(nullptr == _input);
     ARM_COMPUTE_ERROR_ON(nullptr == _output);
     ARM_COMPUTE_ERROR_ON(_input == _output);
@@ -94,37 +131,10 @@
     Iterator input(_input, window);
     Iterator output(_output, window);
 
+    bool in_place = (_input == _output);
+
     switch(_input->info()->data_type())
     {
-        case DataType::QS8:
-        {
-            const int fixed_point_position = _input->info()->fixed_point_position();
-
-            switch(_output->info()->data_type())
-            {
-                case DataType::F32:
-                {
-                    /* Up-conversion QS8 -> F32 */
-                    execute_window_loop(window, [&](const Coordinates & id)
-                    {
-                        const int8x16_t texels_s8 = vld1q_s8(reinterpret_cast<const int8_t *>(input.ptr()));
-
-                        float32x4x2_t texels_low  = vcvt_f32_qs8(vget_low_s8(texels_s8), fixed_point_position);
-                        float32x4x2_t texels_high = vcvt_f32_qs8(vget_high_s8(texels_s8), fixed_point_position);
-
-                        vst1q_f32(reinterpret_cast<float *>(output.ptr()), texels_low.val[0]);
-                        vst1q_f32(reinterpret_cast<float *>(output.ptr()) + 4, texels_low.val[1]);
-                        vst1q_f32(reinterpret_cast<float *>(output.ptr()) + 8, texels_high.val[0]);
-                        vst1q_f32(reinterpret_cast<float *>(output.ptr()) + 12, texels_high.val[1]);
-                    },
-                    input, output);
-                    break;
-                }
-                default:
-                    ARM_COMPUTE_ERROR("Output data type not supported");
-            }
-            break;
-        }
         case DataType::U8:
         {
             const int16x8_t b = vdupq_n_s16(_shift);
@@ -201,6 +211,49 @@
             }
             break;
         }
+        case DataType::QS8:
+        {
+            switch(_output->info()->data_type())
+            {
+                case DataType::QS8:
+                {
+                    const int relative_shift = _fixed_point_position_output - _fixed_point_position_input;
+                    /* Fixed point position conversion QS8 -> QS8 */
+                    if(relative_shift != 0 || !in_place)
+                    {
+                        const auto relative_shift_vec = vdupq_n_qs8(relative_shift);
+                        execute_window_loop(window, [&](const Coordinates & id)
+                        {
+                            const qint8x16_t texels_qs8 = vld1q_qs8(reinterpret_cast<const qint8_t *>(input.ptr()));
+                            vst1q_qs8(reinterpret_cast<qint8_t *>(output.ptr()), vqrshlq_s8(texels_qs8, relative_shift_vec));
+                        },
+                        input, output);
+                    }
+                    break;
+                }
+                case DataType::F32:
+                {
+                    /* Up-conversion QS8 -> F32 */
+                    execute_window_loop(window, [&](const Coordinates & id)
+                    {
+                        const qint8x16_t texels_qs8 = vld1q_qs8(reinterpret_cast<const qint8_t *>(input.ptr()));
+
+                        float32x4x2_t texels_low  = vcvt_f32_qs8(vget_low_s8(texels_qs8), _fixed_point_position_input);
+                        float32x4x2_t texels_high = vcvt_f32_qs8(vget_high_s8(texels_qs8), _fixed_point_position_input);
+
+                        vst1q_f32(reinterpret_cast<float *>(output.ptr()), texels_low.val[0]);
+                        vst1q_f32(reinterpret_cast<float *>(output.ptr()) + 4, texels_low.val[1]);
+                        vst1q_f32(reinterpret_cast<float *>(output.ptr()) + 8, texels_high.val[0]);
+                        vst1q_f32(reinterpret_cast<float *>(output.ptr()) + 12, texels_high.val[1]);
+                    },
+                    input, output);
+                    break;
+                }
+                default:
+                    ARM_COMPUTE_ERROR("Output data type not supported");
+            }
+            break;
+        }
         case DataType::S16:
         {
             switch(_output->info()->data_type())
@@ -356,16 +409,37 @@
         }
         case DataType::QS16:
         {
-            const int fixed_point_position = _input->info()->fixed_point_position();
-
             switch(_output->info()->data_type())
             {
+                case DataType::QS16:
+                {
+                    const int relative_shift = _fixed_point_position_output - _fixed_point_position_input;
+                    /* Fixed point position conversion QS16 -> QS16 */
+                    if(relative_shift != 0 || !in_place)
+                    {
+                        const auto relative_shift_vec = vdupq_n_qs16(relative_shift);
+                        execute_window_loop(window, [&](const Coordinates & id)
+                        {
+                            const qint16x8x2_t texels_qs16 =
+                            {
+                                {
+                                    vld1q_qs16(reinterpret_cast<qint16_t *>(input.ptr())),
+                                    vld1q_qs16(reinterpret_cast<qint16_t *>(input.ptr()) + 8)
+                                }
+                            };
+                            vst1q_qs16(reinterpret_cast<qint16_t *>(output.ptr()), vqrshlq_s16(texels_qs16.val[0], relative_shift_vec));
+                            vst1q_qs16(reinterpret_cast<qint16_t *>(output.ptr()) + 8, vqrshlq_s16(texels_qs16.val[1], relative_shift_vec));
+                        },
+                        input, output);
+                    }
+                    break;
+                }
                 case DataType::F32:
                 {
                     /* Up-conversion QS16 -> F32 */
                     execute_window_loop(window, [&](const Coordinates & id)
                     {
-                        const int16x8x2_t texels =
+                        const int16x8x2_t texels_qs16 =
                         {
                             {
                                 vld1q_s16(reinterpret_cast<qint16_t *>(input.ptr())),
@@ -373,10 +447,10 @@
                             }
                         };
 
-                        vst1q_f32(reinterpret_cast<float *>(output.ptr()), vcvt_f32_qs16(vget_low_s16(texels.val[0]), fixed_point_position));
-                        vst1q_f32(reinterpret_cast<float *>(output.ptr()) + 4, vcvt_f32_qs16(vget_high_s16(texels.val[0]), fixed_point_position));
-                        vst1q_f32(reinterpret_cast<float *>(output.ptr()) + 8, vcvt_f32_qs16(vget_low_s16(texels.val[1]), fixed_point_position));
-                        vst1q_f32(reinterpret_cast<float *>(output.ptr()) + 12, vcvt_f32_qs16(vget_high_s16(texels.val[1]), fixed_point_position));
+                        vst1q_f32(reinterpret_cast<float *>(output.ptr()), vcvt_f32_qs16(vget_low_s16(texels_qs16.val[0]), _fixed_point_position_input));
+                        vst1q_f32(reinterpret_cast<float *>(output.ptr()) + 4, vcvt_f32_qs16(vget_high_s16(texels_qs16.val[0]), _fixed_point_position_input));
+                        vst1q_f32(reinterpret_cast<float *>(output.ptr()) + 8, vcvt_f32_qs16(vget_low_s16(texels_qs16.val[1]), _fixed_point_position_input));
+                        vst1q_f32(reinterpret_cast<float *>(output.ptr()) + 12, vcvt_f32_qs16(vget_high_s16(texels_qs16.val[1]), _fixed_point_position_input));
                     },
                     input, output);
                     break;
@@ -392,7 +466,6 @@
             {
                 case DataType::QS8:
                 {
-                    const int fixed_point_position = _output->info()->fixed_point_position();
                     /* Down-conversion F32 -> QS8 */
                     execute_window_loop(window, [&](const Coordinates & id)
                     {
@@ -406,7 +479,7 @@
                             }
                         };
 
-                        const qint8x16_t texels_s8 = vqcvtq_qs8_f32(texels_f32, fixed_point_position);
+                        const qint8x16_t texels_s8 = vqcvtq_qs8_f32(texels_f32, _fixed_point_position_output);
 
                         vst1q_s8(reinterpret_cast<int8_t *>(output.ptr()), texels_s8);
                     },
@@ -415,7 +488,6 @@
                 }
                 case DataType::QS16:
                 {
-                    const int fixed_point_position = _output->info()->fixed_point_position();
                     /* Down-conversion F32 -> QS16 */
                     execute_window_loop(window, [&](const Coordinates & id)
                     {
@@ -434,8 +506,8 @@
                             }
                         };
 
-                        vst1q_s16(reinterpret_cast<qint16_t *>(output.ptr()), vqcvtq_qs16_f32(texels_f32_1, fixed_point_position));
-                        vst1q_s16(reinterpret_cast<qint16_t *>(output.ptr()) + 8, vqcvtq_qs16_f32(texels_f32_2, fixed_point_position));
+                        vst1q_s16(reinterpret_cast<qint16_t *>(output.ptr()), vqcvtq_qs16_f32(texels_f32_1, _fixed_point_position_output));
+                        vst1q_s16(reinterpret_cast<qint16_t *>(output.ptr()) + 8, vqcvtq_qs16_f32(texels_f32_2, _fixed_point_position_output));
                     },
                     input, output);
                     break;