Commit 7e43ee41279bcd374a65e4d69a698c99d729a5eb

Thomas de Grivel 2024-08-05T18:20:53

fix tag_neg for return tag_integer_reduce

diff --git a/libkc3/tag_neg.c b/libkc3/tag_neg.c
index 9ef6b64..b10986a 100644
--- a/libkc3/tag_neg.c
+++ b/libkc3/tag_neg.c
@@ -15,75 +15,60 @@
 #include "ratio.h"
 #include "tag.h"
 
-s_tag * tag_neg (const s_tag *tag, s_tag *result)
+s_tag * tag_neg (const s_tag *tag, s_tag *dest)
 {
-  s16 i_s16;
   s_integer tmp = {0};
   switch (tag->type) {
   case TAG_INTEGER:
-    result->type = TAG_INTEGER;
-    integer_neg(&tag->data.integer, &result->data.integer);
-    return result;
+    dest->type = TAG_INTEGER;
+    integer_neg(&tag->data.integer, &dest->data.integer);
+    return tag_integer_reduce(dest);
   case TAG_RATIO:
-    result->type = TAG_RATIO;
-    ratio_neg(&tag->data.ratio, &result->data.ratio);
-    return result;
+    dest->type = TAG_RATIO;
+    ratio_neg(&tag->data.ratio, &dest->data.ratio);
+    return dest;
   case TAG_SW:
-    if (tag->data.sw == SW_MIN) {
-      integer_init_sw(&tmp, tag->data.sw);
-      result->type = TAG_INTEGER;
-      integer_neg(&tmp, &result->data.integer);
-      integer_clean(&tmp);
-      return result;
-    }
-    return tag_init_sw(result, -tag->data.sw);
+    integer_init_sw(&tmp, tag->data.sw);
+    dest->type = TAG_INTEGER;
+    integer_neg(&tmp, &dest->data.integer);
+    integer_clean(&tmp);
+    return tag_integer_reduce(dest);
   case TAG_S64:
-    if (tag->data.s64 == S64_MIN) {
-      integer_init_s64(&tmp, tag->data.s64);
-      result->type = TAG_INTEGER;
-      integer_neg(&tmp, &result->data.integer);
-      integer_clean(&tmp);
-      return result;
-    }
-    return tag_init_s64(result, -tag->data.s64);
+    integer_init_s64(&tmp, tag->data.s64);
+    dest->type = TAG_INTEGER;
+    integer_neg(&tmp, &dest->data.integer);
+    integer_clean(&tmp);
+    return tag_integer_reduce(dest);
   case TAG_S32:
-    return tag_init_s64(result, - (s64) tag->data.s32);
+    tag_init_s64(dest, - (s64) tag->data.s32);
+    return tag_integer_reduce(dest);
   case TAG_S16:
-    return tag_init_s32(result, - (s32) tag->data.s16);
+    tag_init_s32(dest, - (s32) tag->data.s16);
+    return tag_integer_reduce(dest);
   case TAG_S8:
-    i_s16 = - (s16) tag->data.s8;
-    if (i_s16 < S8_MIN)
-      return tag_init_s16(result, i_s16);
-    if (i_s16 < 0)
-      return tag_init_s8(result, i_s16);
-    if (i_s16 <= U8_MAX)
-      return tag_init_u8(result, i_s16);
-    return tag_init_u16(result, i_s16);
+    tag_init_s16(dest, - (s16) tag->data.s8);
+    return tag_integer_reduce(dest);
   case TAG_U8:
-    i_s16 = - (s16) tag->data.u8;
-    if (i_s16 < S8_MIN)
-      return tag_init_s16(result, i_s16);
-    if (i_s16 < 0)
-      return tag_init_s8(result, i_s16);
-    if (i_s16 <= U8_MAX)
-      return tag_init_u8(result, i_s16);
-    return tag_init_u16(result, i_s16);
+    tag_init_s16(dest, - (s16) tag->data.u8);
+    return tag_integer_reduce(dest);
   case TAG_U16:
-    return tag_init_s32(result, - (s32) tag->data.u16);
+    tag_init_s32(dest, - (s32) tag->data.u16);
+    return tag_integer_reduce(dest);
   case TAG_U32:
-    return tag_init_s64(result, - (s64) tag->data.u32);
+    tag_init_s64(dest, - (s64) tag->data.u32);
+    return tag_integer_reduce(dest);
   case TAG_U64:
     integer_init_u64(&tmp, tag->data.u64);
-    result->type = TAG_INTEGER;
-    integer_neg(&tmp, &result->data.integer);
+    dest->type = TAG_INTEGER;
+    integer_neg(&tmp, &dest->data.integer);
     integer_clean(&tmp);
-    return result;
+    return tag_integer_reduce(dest);
   case TAG_UW:
     integer_init_uw(&tmp, tag->data.uw);
-    result->type = TAG_INTEGER;
-    integer_neg(&tmp, &result->data.integer);
+    dest->type = TAG_INTEGER;
+    integer_neg(&tmp, &dest->data.integer);
     integer_clean(&tmp);
-    return result;
+    return tag_integer_reduce(dest);
   default:
     err_write_1("tag_neg: invalid tag type: ");
     err_puts(tag_type_to_string(tag->type));