aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordam <dam@gudinoff>2023-06-08 01:27:07 +0100
committerdam <dam@gudinoff>2023-06-08 01:27:07 +0100
commit2f758a4e5bcf6c125a5f81155675ccabbde8aeb5 (patch)
treede28eb049b749aea66117af618ca3d15b2447cbf
parent1502bb4d94f742069fa14d5ce5cb21761c5a44ed (diff)
downloadtask-time-tracker-2f758a4e5bcf6c125a5f81155675ccabbde8aeb5.tar.zst
task-time-tracker-2f758a4e5bcf6c125a5f81155675ccabbde8aeb5.zip
Fixed bug on mul. Added performance tests.
-rw-r--r--Math_Ext.jai7
-rw-r--r--Math_Test.jai78
2 files changed, 62 insertions, 23 deletions
diff --git a/Math_Ext.jai b/Math_Ext.jai
index 9ec34d0..ad41c2f 100644
--- a/Math_Ext.jai
+++ b/Math_Ext.jai
@@ -225,6 +225,11 @@ mul :: (x: $Tx, y: $Ty, $USE_GENERIC: bool = false) -> result: $Tr, overflow: bo
} 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;
}
@@ -297,7 +302,7 @@ mul :: (x: $Tx, y: $Ty, $USE_GENERIC: bool = false) -> result: $Tr, overflow: bo
#insert #run replace(U_MUL_ASM, ".SIZE", ".d");
#if Tr == u64
#insert #run replace(U_MUL_ASM, ".SIZE", ".q");
-
+
return result, saturated;
diff --git a/Math_Test.jai b/Math_Test.jai
index 1bdb968..8f3dce1 100644
--- a/Math_Test.jai
+++ b/Math_Test.jai
@@ -30,9 +30,9 @@ main :: () {
}
return str;
}
-
+
#insert #run print_test_call(operation);
-
+
errors := 0;
if result != t_result { errors += 1; print(" > incorrect result value: got % expected %\n", t_result, result); };
if type != type_of(t_result) { errors += 1; print(" > incorrect result type: got % expected %\n", type_of(t_result), type); };
@@ -155,9 +155,8 @@ main :: () {
if errors > 0 print("# Found % %!\n", errors, ifx errors == 1 then "error" else "errors"); else print(" No errors found.\n");
- performance_test :: (operation: string) -> ops_size: s64, time_gen: Apollo_Time, time_asm: Apollo_Time #expand {
-
- type :: s8;
+ performance_test :: (operation: string, $type: Type) -> ops_size: s64, time_gen: Apollo_Time, time_asm: Apollo_Time #expand {
+
SIZE :: 12000;
numbers_x: [..] type;
numbers_y: [..] type;
@@ -165,11 +164,20 @@ main :: () {
numbers_zasm: [SIZE] type;
array_reserve(*numbers_x, SIZE);
array_reserve(*numbers_y, SIZE);
-
+
+ #if type == u8 { MIN :: 0; MAX :: U8_MAX; }
+ #if type == u16 { MIN :: 0; MAX :: U16_MAX; }
+ #if type == u32 { MIN :: 0; MAX :: U32_MAX; }
+ #if type == u64 { MIN :: 0; MAX :: U64_MAX; }
+ #if type == s8 { MIN :: S8_MIN; MAX :: S8_MAX; }
+ #if type == s16 { MIN :: S16_MIN; MAX :: S16_MAX; }
+ #if type == s32 { MIN :: S32_MIN; MAX :: S32_MAX; }
+ #if type == s64 { MIN :: S64_MIN; MAX :: S64_MAX; }
+
for 0..SIZE-1 {
- x := cast(type) random_get_within_range(xx S8_MIN, xx S8_MAX);
- y := cast(type) random_get_within_range(xx S8_MIN, xx S8_MAX);
+ x := cast(type) random_get_within_range(xx MIN, xx MAX);
+ y := cast(type) random_get_within_range(xx MIN, xx MAX);
if y == 0 && operation == "div" {
y = 1;
}
@@ -180,25 +188,19 @@ main :: () {
control_gen: type;
control_asm: type;
- PERFORMANCE_TEST :: #string DONE
control_gen = 1;
time_gen := current_time_monotonic();
- for 0..SIZE-1 numbers_zgen[it] = OP(numbers_x[it], numbers_y[it], true);
+ for 0..SIZE-1 #insert #run replace("numbers_zgen[it] = OP(numbers_x[it], numbers_y[it], true);", "OP", operation);
time_gen = current_time_monotonic() - time_gen;
control_asm = 1;
time_asm := current_time_monotonic();
- for 0..SIZE-1 numbers_zasm[it] = OP(numbers_x[it], numbers_y[it]);
- // for numbers control_asm = OP(control_asm, it);
+ for 0..SIZE-1 #insert #run replace("numbers_zasm[it] = OP(numbers_x[it], numbers_y[it]);", "OP", operation);
time_asm = current_time_monotonic() - time_asm;
for 0..SIZE-1 assert(numbers_zgen[it] == numbers_zasm[it]);
- // assert(control_gen == control_asm);
return SIZE, time_gen, time_asm;
- DONE
-
- #insert #run replace(PERFORMANCE_TEST, "OP", operation);
}
// Performance test.
@@ -210,19 +212,51 @@ main :: () {
best_gen = 0;
best_asm = 0;
for 0..500 {
- size, time_gen, time_asm := performance_test("OP");
+ size, time_gen, time_asm := performance_test("OP", TYPE);
perf_gen := cast(float)size/cast(float)to_microseconds(time_gen);
perf_asm := cast(float)size/cast(float)to_microseconds(time_asm);
best_gen = max(best_gen, perf_gen);
best_asm = max(best_asm, perf_asm);
}
- print("%\n generic : %\n asm : %\n", "OP", best_gen, best_asm);
+ print("% : %\n generic : %\n asm : %\n", "OP", TYPE, best_gen, best_asm);
DONE
print("----- op : ops/usec ---\n");
- #insert #run replace(TEST_STR, "OP", "add");
- #insert #run replace(TEST_STR, "OP", "sub");
- #insert #run replace(TEST_STR, "OP", "mul");
- #insert #run replace(TEST_STR, "OP", "div");
+ types :: string.["s8", "s16", "s32", "s64"];
+ #insert #run replace(replace(TEST_STR, "OP", "add"), "TYPE", "s8");
+ #insert #run replace(replace(TEST_STR, "OP", "add"), "TYPE", "s16");
+ #insert #run replace(replace(TEST_STR, "OP", "add"), "TYPE", "s32");
+ #insert #run replace(replace(TEST_STR, "OP", "add"), "TYPE", "s64");
+ #insert #run replace(replace(TEST_STR, "OP", "add"), "TYPE", "u8");
+ #insert #run replace(replace(TEST_STR, "OP", "add"), "TYPE", "u16");
+ #insert #run replace(replace(TEST_STR, "OP", "add"), "TYPE", "u32");
+ #insert #run replace(replace(TEST_STR, "OP", "add"), "TYPE", "u64");
+
+ #insert #run replace(replace(TEST_STR, "OP", "sub"), "TYPE", "s8");
+ #insert #run replace(replace(TEST_STR, "OP", "sub"), "TYPE", "s16");
+ #insert #run replace(replace(TEST_STR, "OP", "sub"), "TYPE", "s32");
+ #insert #run replace(replace(TEST_STR, "OP", "sub"), "TYPE", "s64");
+ #insert #run replace(replace(TEST_STR, "OP", "sub"), "TYPE", "u8");
+ #insert #run replace(replace(TEST_STR, "OP", "sub"), "TYPE", "u16");
+ #insert #run replace(replace(TEST_STR, "OP", "sub"), "TYPE", "u32");
+ #insert #run replace(replace(TEST_STR, "OP", "sub"), "TYPE", "u64");
+
+ #insert #run replace(replace(TEST_STR, "OP", "mul"), "TYPE", "s8");
+ #insert #run replace(replace(TEST_STR, "OP", "mul"), "TYPE", "s16");
+ #insert #run replace(replace(TEST_STR, "OP", "mul"), "TYPE", "s32");
+ #insert #run replace(replace(TEST_STR, "OP", "mul"), "TYPE", "s64");
+ #insert #run replace(replace(TEST_STR, "OP", "mul"), "TYPE", "u8");
+ #insert #run replace(replace(TEST_STR, "OP", "mul"), "TYPE", "u16");
+ #insert #run replace(replace(TEST_STR, "OP", "mul"), "TYPE", "u32");
+ #insert #run replace(replace(TEST_STR, "OP", "mul"), "TYPE", "u64");
+
+ #insert #run replace(replace(TEST_STR, "OP", "div"), "TYPE", "s8");
+ #insert #run replace(replace(TEST_STR, "OP", "div"), "TYPE", "s16");
+ #insert #run replace(replace(TEST_STR, "OP", "div"), "TYPE", "s32");
+ #insert #run replace(replace(TEST_STR, "OP", "div"), "TYPE", "s64");
+ #insert #run replace(replace(TEST_STR, "OP", "div"), "TYPE", "u8");
+ #insert #run replace(replace(TEST_STR, "OP", "div"), "TYPE", "u16");
+ #insert #run replace(replace(TEST_STR, "OP", "div"), "TYPE", "u32");
+ #insert #run replace(replace(TEST_STR, "OP", "div"), "TYPE", "u64");
}