[Transform/Arithmetic] Support operand value of floating point type
authorWook Song <wook16.song@samsung.com>
Wed, 10 Oct 2018 08:05:08 +0000 (17:05 +0900)
committerjaeyun-jung <39614140+jaeyun-jung@users.noreply.github.com>
Fri, 12 Oct 2018 10:55:28 +0000 (19:55 +0900)
Since the data type of the operand is fixed to 'int64_t', the Arithmetic
mode cannot properly handle operand values of the floating point type
such as 2.3, -10.5, and -9.900000e-001. This patch supports such
floating-point operand values by using the data type, double.

Signed-off-by: Wook Song <wook16.song@samsung.com>
gst/tensor_transform/tensor_transform.c
gst/tensor_transform/tensor_transform.h

index 68df339..196d524 100644 (file)
@@ -288,12 +288,31 @@ gst_tensor_transform_set_option_data (GstTensor_Transform * filter)
     case GTT_ARITHMETIC:
     {
       gchar **strv = g_strsplit (filter->option, ":", 2);
+      gchar *not_consumed;
+
       if (strv[0] != NULL) {
         filter->data_arithmetic.mode =
             gst_tensor_transform_get_arith_mode (strv[0]);
       }
-      if (strv[1] != NULL)
-        filter->data_arithmetic.value = g_ascii_strtod (strv[1], NULL);
+
+      if (strv[1] != NULL) {
+        if (strchr (strv[1], '.') || strchr (strv[1], 'e') ||
+            strchr (strv[1], 'E')) {
+          filter->data_arithmetic.value.type = ARITH_OPRND_TYPE_DOUBLE;
+          filter->data_arithmetic.value.value_double =
+              g_ascii_strtod (strv[1], &not_consumed);
+        } else {
+          filter->data_arithmetic.value.type = ARITH_OPRND_TYPE_INT64;
+          filter->data_arithmetic.value.value_int64 =
+              g_ascii_strtoll (strv[1], &not_consumed, 10);
+        }
+
+        if (strlen (not_consumed)) {
+          g_printerr ("%s is not a valid integer or floating point value\n",
+              strv[1]);
+          g_assert (0);
+        }
+      }
 
       filter->loaded = TRUE;
       g_strfreev (strv);
@@ -632,7 +651,12 @@ gst_tensor_transform_typecast (GstTensor_Transform * filter,
 #define arithloopcase(typecase, itype, num, mode, value) \
   case typecase: \
   { \
-    itype a = (itype) value; \
+    itype a; \
+    switch (value.type) {\
+    case ARITH_OPRND_TYPE_INT64 : a = (itype) value.value_int64; break; \
+    case ARITH_OPRND_TYPE_DOUBLE : a = (itype) value.value_double; break;\
+    default: g_assert(0); \
+    }; \
     switch (mode) { \
     case ARITH_ADD : arith(itype, num, +, a); break; \
     case ARITH_MUL : arith(itype, num, *, a); break; \
@@ -654,7 +678,8 @@ gst_tensor_transform_arithmetic (GstTensor_Transform * filter,
 {
   uint32_t num = get_tensor_element_count (filter->fromDim);
   tensor_transform_arith_mode mode = filter->data_arithmetic.mode;
-  double value = filter->data_arithmetic.value;
+  tensor_transform_arithmetic_operand value = filter->data_arithmetic.value;
+
   switch (filter->type) {
       arithloopcase (_NNS_INT8, int8_t, num, mode, value);
       arithloopcase (_NNS_INT16, int16_t, num, mode, value);
index 1e0d235..8fd964e 100644 (file)
@@ -89,6 +89,14 @@ typedef enum
   STAND_END,
 } tensor_transform_stand_mode;
 
+typedef enum
+{
+  ARITH_OPRND_TYPE_INT64 = 0,
+  ARITH_OPRND_TYPE_DOUBLE = 1,
+
+  ARITH_OPRND_TYPE_END
+} tensor_transform_arith_oprnd_type;
+
 /**
  * @brief Internal data structure for dimchg mode.
  */
@@ -105,12 +113,22 @@ typedef struct _tensor_transform_typecast {
 } tensor_transform_typecast;
 
 /**
+ * @brief Internal data structure for operand of arithmetic mode.
+ */
+typedef struct _tensor_transform_arithmetic_operand {
+  tensor_transform_arith_oprnd_type type;
+  union {
+    int64_t value_int64;
+    double value_double;
+  };
+} tensor_transform_arithmetic_operand;
+
+/**
  * @brief Internal data structure for arithmetic mode.
  */
 typedef struct _tensor_transform_arithmetic {
   tensor_transform_arith_mode mode;
-  /* TODO : Better to use union for various type*/
-  int64_t value;
+  tensor_transform_arithmetic_operand value;
 } tensor_transform_arithmetic;
 
 /**