From a0647e47b2445b333ea81885e6a23a529c62cd29 Mon Sep 17 00:00:00 2001 From: Andrey Efremov Date: Tue, 28 Mar 2023 19:55:09 +0300 Subject: [PATCH] Fix inlining compare for doubles Signed-off-by: Andrey Efremov --- irtoc_scripts/interpreter_handlers.irt | 18 +++++++++++++++--- tests/checked/type_resolving.js | 21 +++++++++++++++++---- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/irtoc_scripts/interpreter_handlers.irt b/irtoc_scripts/interpreter_handlers.irt index 261ce4a94..ef65f31d6 100644 --- a/irtoc_scripts/interpreter_handlers.irt +++ b/irtoc_scripts/interpreter_handlers.irt @@ -784,20 +784,32 @@ end # Create cpp functions for compare instructions # [:eq, :ne, :ge, :le, :gt, :lt].each do |op| + doubles_macro = :"#{op}2dyn_double" + macro(doubles_macro) do |l, r| + case op + when :eq, :ne + booltoany(Compare(l, r).CC(:"CC_#{op.upcase}").b) + when :lt, :le + booltoany(Compare(Cmp(l, r).Fcmpg().i32, 0).CC(:"CC_#{op.upcase}").b) + when :gt, :ge + booltoany(Compare(Cmp(l, r).Fcmpl().i32, 0).CC(:"CC_#{op.upcase}").b) + end + end + macro(:"#{op}2dyn_smi_smi") do |l, r| booltoany(Compare(anytoi32(l.any), anytoi32(r.any)).CC(:"CC_#{op.upcase}").b) end macro(:"#{op}2dyn_smi_double") do |l, r| - booltoany(Compare(i32tof64(anytoi32(l.any)), anytof64(r.any)).CC(:"CC_#{op.upcase}").b) + send(doubles_macro, i32tof64(anytoi32(l.any)), anytof64(r.any)) end macro(:"#{op}2dyn_double_smi") do |l, r| - booltoany(Compare(anytof64(l.any), i32tof64(anytoi32(r.any))).CC(:"CC_#{op.upcase}").b) + send(doubles_macro, anytof64(l.any), i32tof64(anytoi32(r.any))) end macro(:"#{op}2dyn_double_double") do |l, r| - booltoany(Compare(anytof64(l.any), anytof64(r.any)).CC(:"CC_#{op.upcase}").b) + send(doubles_macro, anytof64(l.any), anytof64(r.any)) end i_i_macro = :"#{op}2dyn_smi_smi" diff --git a/tests/checked/type_resolving.js b/tests/checked/type_resolving.js index c73befbe5..7979ee64c 100644 --- a/tests/checked/type_resolving.js +++ b/tests/checked/type_resolving.js @@ -90,7 +90,13 @@ //! PASS_AFTER "TypesResolving" //! insts.each do |op, intrinsic| //! INST_NOT "Intrinsic.#{intrinsic}Dyn" -//! INST "Compare #{op.upcase} #{type}" +//! if (['ge', 'le', 'gt', 'lt'].include? op) && type == 'f64' +//! new_op = 'Fcmp' + (op[0] == 'g' ? 'l' : 'g') +//! INST_NEXT new_op +//! INST_NEXT "Compare #{op.upcase} i32" +//! else +//! INST_NEXT "Compare #{op.upcase} #{type}" +//! end //! end //! end //! @@ -582,6 +588,11 @@ function test_cmp_i_f() { if (a <= b) res += 14; if (a > b) res += 15; if (a < b) res += 16; + // comparisons below are false + if (a <= NaN) res += 17; + if (a >= NaN) res += 18; + if (a < NaN) res += 19; + if (a > NaN) res += 20; return res; // 40 } @@ -608,7 +619,9 @@ function test_cmp_f_f() { if (a <= b) res += 34; if (a > b) res += 35; if (a < b) res += 36; - return res; // 102 + if (NaN == NaN) res += 37; // false + if (NaN != NaN) res += 38; // true + return res; // 140 } function test_cmp() { @@ -617,8 +630,8 @@ function test_cmp() { res += test_cmp_i_f(); res += test_cmp_f_i(); res += test_cmp_f_f(); - if (res != 226) { - throw "test_add is failed"; + if (res != 264) { + throw "test_cmp is failed"; } } -- Gitee