Hash :
77f26ec2
Author :
Thomas de Grivel
Date :
2025-09-17T16:56:09
fix tag_neg and tag_div
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
/* kc3
* Copyright from 2022 to 2025 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include "assert.h"
#include "integer.h"
#include "ratio.h"
#include "tag.h"
s_tag * tag_neg (s_tag *src, s_tag *dest)
{
s_tag tag = {0};
s_integer tmp = {0};
switch (src->type) {
case TAG_INTEGER:
tag.type = TAG_INTEGER;
integer_neg(&src->data.integer, &tag.data.integer);
goto integer_reduce;
case TAG_RATIO:
dest->type = TAG_RATIO;
ratio_neg(&src->data.ratio, &dest->data.ratio);
return dest;
case TAG_SW:
integer_init_sw(&tmp, src->data.sw);
tag.type = TAG_INTEGER;
integer_neg(&tmp, &tag.data.integer);
integer_clean(&tmp);
goto integer_reduce;
case TAG_S64:
integer_init_s64(&tmp, src->data.s64);
tag.type = TAG_INTEGER;
integer_neg(&tmp, &tag.data.integer);
integer_clean(&tmp);
goto integer_reduce;
case TAG_S32:
tag_init_s64(&tag, - (s64) src->data.s32);
goto integer_reduce;
case TAG_S16:
tag_init_s32(&tag, - (s32) src->data.s16);
goto integer_reduce;
case TAG_S8:
tag_init_s16(&tag, - (s16) src->data.s8);
goto integer_reduce;
case TAG_U8:
tag_init_s16(&tag, - (s16) src->data.u8);
goto integer_reduce;
case TAG_U16:
tag_init_s32(&tag, - (s32) src->data.u16);
goto integer_reduce;
case TAG_U32:
tag_init_s64(&tag, - (s64) src->data.u32);
goto integer_reduce;
case TAG_U64:
integer_init_u64(&tmp, src->data.u64);
tag.type = TAG_INTEGER;
integer_neg(&tmp, &tag.data.integer);
integer_clean(&tmp);
goto integer_reduce;
case TAG_UW:
integer_init_uw(&tmp, src->data.uw);
tag.type = TAG_INTEGER;
integer_neg(&tmp, &tag.data.integer);
integer_clean(&tmp);
goto integer_reduce;
default:
err_write_1("tag_neg: invalid tag type: ");
err_puts(tag_type_to_string(src->type));
}
return NULL;
integer_reduce:
if (! tag_integer_reduce(&tag, dest)) {
tag_clean(&tag);
return NULL;
}
tag_clean(&tag);
return dest;
}