diff options
| author | dam <dam@gudinoff> | 2023-05-20 10:46:41 +0100 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2023-05-20 10:46:41 +0100 |
| commit | 1b278877b6605322d214b1faf9aeca5760415f90 (patch) | |
| tree | 0bf2a1eda67328c0702086e5d03095a376bf6c5f | |
| parent | e988d2b7eb253fabcd6e57c37f1c8c385032f03c (diff) | |
| download | task-time-tracker-1b278877b6605322d214b1faf9aeca5760415f90.tar.zst task-time-tracker-1b278877b6605322d214b1faf9aeca5760415f90.zip | |
Cleanup non x64 implementation of signed add.
| -rw-r--r-- | Math_Ext.jai | 86 |
1 files changed, 48 insertions, 38 deletions
diff --git a/Math_Ext.jai b/Math_Ext.jai index b4c8879..1eef02b 100644 --- a/Math_Ext.jai +++ b/Math_Ext.jai @@ -23,40 +23,47 @@ test_math_ext :: () { set_build_options_dc(.{do_output=false}); */ #import "Random"; - test_add :: (x: $Tx, y: $Ty, r: $Tr, t: Type, o: bool) { + test_add :: (x: $Tx, y: $Ty, r: $Tr, t: Type, o: bool) -> errors_found: int { tr, to := add(cast(Tx)x, cast(Ty)y); print("add(%): % + % = % : %\n", t, x, y, r, o); - if r != tr print(" > incorrect result value: got % expected %\n", tr, r); - if t != type_of(tr) print(" > incorrect result type: got % expected %\n", type_of(tr), t); - if o != to print(" > incorrect overflow flag: got % expected %\n", to, o); + error := 0; + if r != tr { error += 1; print(" > incorrect result value: got % expected %\n", tr, r); }; + if t != type_of(tr) { error += 1; print(" > incorrect result type: got % expected %\n", type_of(tr), t); }; + if o != to { error += 1; print(" > incorrect overflow flag: got % expected %\n", to, o); }; + return error; } + errors := 0; + // test_add(cast(u8)1, cast(u8)2, 3, u8, false); // test_add(cast(u8)255, cast(u8)1, 255, u8, true); - test_add(cast( s8) S8_MAX, cast( s8)1, S8_MAX, s8, true); - test_add(cast(s16)S16_MAX, cast( u8)1, S16_MAX, s16, true); - test_add(cast(s32)S32_MAX, cast(s32)1, S32_MAX, s32, true); - test_add(cast(s64)S64_MAX, cast(u32)1, S64_MAX, s64, true); + errors += test_add(cast( s8) S8_MAX, cast( s8)1, S8_MAX, s8, true); + errors += test_add(cast(s16)S16_MAX, cast( u8)1, S16_MAX, s16, true); + errors += test_add(cast(s32)S32_MAX, cast(s32)1, S32_MAX, s32, true); + errors += test_add(cast(s64)S64_MAX, cast(u32)1, S64_MAX, s64, true); - test_add(cast( u8) U8_MAX, cast( u8)1, U8_MAX, u8, true); - test_add(cast(u16)U16_MAX, cast(u16)1, U16_MAX, u16, true); - test_add(cast(u32)U32_MAX, cast(u32)1, U32_MAX, u32, true); - test_add(cast(u64)U64_MAX, cast(u64)1, U64_MAX, u64, true); + errors += test_add(cast( u8) U8_MAX, cast( u8)1, U8_MAX, u8, true); + errors += test_add(cast(u16)U16_MAX, cast(u16)1, U16_MAX, u16, true); + errors += test_add(cast(u32)U32_MAX, cast(u32)1, U32_MAX, u32, true); + errors += test_add(cast(u64)U64_MAX, cast(u64)1, U64_MAX, u64, true); - // test_add(cast(s32)66, cast(s64)-2, 64, s64, false); - // test_add(cast(u32)66, cast(s64)4, 70, s64, false); - // test_add(cast(s32)S32_MAX, cast(s64)1, 2147483648, s64, false); - // test_add(cast(s32)S32_MAX, cast(s32)1, S32_MAX, s32, true); - // test_add(cast(s64)S64_MAX, cast(s64)0, S64_MAX, s64, false); - // test_add(cast(s64)9223372036854775806, cast(s64)1, S64_MAX, s64, false); - // test_add(cast(s64)9223372036854775806, cast(s64)2, S64_MAX, s64, true); - - // test_add(cast(u8)7, cast(u8)1, 8, u8, false); - // test_add(cast(u8)U8_MAX, cast(u8)1, U8_MAX, u8, true); - - // test_add(cast(u16)10, cast(u8)3, 13, u16, false); - // test_add(cast(u8)1, cast(u16)U16_MAX, U16_MAX, u16, true); + // errors += test_add(cast(s32)66, cast(s64)-2, 64, s64, false); + // errors += test_add(cast(u32)66, cast(s64)4, 70, s64, false); + // errors += test_add(cast(s32)S32_MAX, cast(s64)1, 2147483648, s64, false); + // errors += test_add(cast(s32)S32_MAX, cast(s32)1, S32_MAX, s32, true); + // errors += test_add(cast(s64)S64_MAX, cast(s64)0, S64_MAX, s64, false); + // errors += test_add(cast(s64)9223372036854775806, cast(s64)1, S64_MAX, s64, false); + // errors += test_add(cast(s64)9223372036854775806, cast(s64)2, S64_MAX, s64, true); + + // errors += test_add(cast(u8)7, cast(u8)1, 8, u8, false); + // errors += test_add(cast(u8)U8_MAX, cast(u8)1, U8_MAX, u8, true); + + // errors += test_add(cast(u16)10, cast(u8)3, 13, u16, false); + // errors += test_add(cast(u8)1, cast(u16)U16_MAX, U16_MAX, u16, true); + + if errors > 0 print("# Found % %!\n", errors, ifx errors == 1 then "error" else "errors"); else print(" No errors found.\n"); + /* PERFORMANCE TEST best_generic: float; @@ -98,7 +105,7 @@ test_math_ext :: () { set_build_options_dc(.{do_output=false}); */ } -add :: (x: $Tx, y: $Ty) -> result: $Tr, saturated: bool #dump +add :: (x: $Tx, y: $Ty) -> result: $Tr, saturated: bool //#dump #modify { type_info_x := cast(*Type_Info)Tx; type_info_y := cast(*Type_Info)Ty; @@ -133,26 +140,29 @@ add :: (x: $Tx, y: $Ty) -> result: $Tr, saturated: bool #dump } { - // TODO Are these constants really needed? - #if Tr == s8 { MAX :: S8_MAX; MIN :: S8_MIN; BITS :: 7; } - #if Tr == s16 { MAX :: S16_MAX; MIN :: S16_MIN; BITS :: 15; } - #if Tr == s32 { MAX :: S32_MAX; MIN :: S32_MIN; BITS :: 31; } - #if Tr == s64 { MAX :: S64_MAX; MIN :: S64_MIN; BITS :: 63; } - - #if Tr == u8 { MAX :: U8_MAX; MIN :: 0; BITS :: 8; } - #if Tr == u16 { MAX :: U16_MAX; MIN :: 0; BITS :: 16; } - #if Tr == u32 { MAX :: U32_MAX; MIN :: 0; BITS :: 32; } - #if Tr == u64 { MAX :: U64_MAX; MIN :: 0; BITS :: 64; } - #if CPU != .X64 { - + #if Tr == s8 || Tr == s16 || Tr == s32 || Tr == s64 { + + #if Tr == s8 { MAX :: S8_MAX; MIN :: S8_MIN; } + #if Tr == s16 { MAX :: S16_MAX; MIN :: S16_MIN; } + #if Tr == s32 { MAX :: S32_MAX; MIN :: S32_MIN; } + #if Tr == s64 { MAX :: S64_MAX; MIN :: S64_MIN; } + if (y > 0 && x > MAX - y) then return MAX, true; if (y < 0 && x < MIN - y) then return MIN, true; return x + y, false; + } else { + + #if Tr == u8 { MAX :: U8_MAX; } + #if Tr == u16 { MAX :: U16_MAX; } + #if Tr == u32 { MAX :: U32_MAX; } + #if Tr == u64 { MAX :: U64_MAX; } + if (x > MAX - y) then return MAX, true; return x + y, false; + } } else { |
