<HTML><BODY><div>Hi, Sergey!</div><div>Thanks for the patch!</div><div>LGTM</div><div data-signature-widget="container"><div data-signature-widget="content"><div>--<br>Best regards,</div><div>Maxim Kokryashkin</div></div></div><div> </div><div> </div><blockquote style="border-left:1px solid #0857A6; margin:10px; padding:0 0 0 10px;">Вторник, 31 октября 2023, 15:49 +03:00 от Sergey Kaplun <skaplun@tarantool.org>:<br> <div id=""><div class="js-helper js-readmsg-msg"><div><div id="style_16987565611498006775_BODY">From: Mike Pall <mike><br><br>Reported by XmiliaH.<br><br>(cherry-picked from commit 7b994e0ee0399caf6319865bbac88ddf62129a36)<br><br>Fold optimization x - (-0) ==> x is INVALID for x = -0 in FP arithmetic.<br>Its result is -0 instead of +0. This patch allows only x - (+0) ==> x<br>optimization.<br><br>Sergey Kaplun:<br>* added the description and the test for the problem<br><br>Part of tarantool/tarantool#9145<br>---<br><br>Branch: <a href="https://github.com/tarantool/luajit/tree/skaplun/lj-783-fix-fold-x-0" target="_blank">https://github.com/tarantool/luajit/tree/skaplun/lj-783-fix-fold-x-0</a><br>Tarantool PR: <a href="https://github.com/tarantool/tarantool/pull/9320" target="_blank">https://github.com/tarantool/tarantool/pull/9320</a><br>Related Issues:<br>* <a href="https://github.com/tarantool/tarantool/issues/9145" target="_blank">https://github.com/tarantool/tarantool/issues/9145</a><br>* <a href="https://github.com/LuaJIT/LuaJIT/pull/783" target="_blank">https://github.com/LuaJIT/LuaJIT/pull/783</a><br><br> src/lj_opt_fold.c | 3 +--<br> test/tarantool-tests/lj-783-fold--0.test.lua | 28 ++++++++++++++++++++<br> 2 files changed, 29 insertions(+), 2 deletions(-)<br> create mode 100644 test/tarantool-tests/lj-783-fold--0.test.lua<br><br>diff --git a/src/lj_opt_fold.c b/src/lj_opt_fold.c<br>index 09e6c87b..944a9ecc 100644<br>--- a/src/lj_opt_fold.c<br>+++ b/src/lj_opt_fold.c<br>@@ -1005,8 +1005,7 @@ LJFOLDF(simplify_numadd_xneg)<br> LJFOLD(SUB any KNUM)<br> LJFOLDF(simplify_numsub_k)<br> {<br>- lua_Number n = knumright;<br>- if (n == 0.0) /* x - (+-0) ==> x */<br>+ if (ir_knum(fright)->u64 == 0) /* x - (+0) ==> x */<br> return LEFTFOLD;<br> return NEXTFOLD;<br> }<br>diff --git a/test/tarantool-tests/lj-783-fold--0.test.lua b/test/tarantool-tests/lj-783-fold--0.test.lua<br>new file mode 100644<br>index 00000000..d6b4b493<br>--- /dev/null<br>+++ b/test/tarantool-tests/lj-783-fold--0.test.lua<br>@@ -0,0 +1,28 @@<br>+local tap = require('tap')<br>+<br>+-- Test file to demonstrate LuaJIT's incorrect fold optimization<br>+-- x - (-0) ==> x.<br>+-- See also <a href="https://github.com/LuaJIT/LuaJIT/issues/783" target="_blank">https://github.com/LuaJIT/LuaJIT/issues/783</a>.<br>+local test = tap.test('lj-783-fold--0'):skipcond({<br>+ ['Test requires JIT enabled'] = not jit.status(),<br>+})<br>+<br>+test:plan(2)<br>+<br>+-- XXX: Use the variable to avoid folding during parsing.<br>+local minus_zero = -0<br>+local results = {}<br>+<br>+jit.opt.start('hotloop=1')<br>+<br>+for i = 1, 4 do<br>+ results[i] = tostring(minus_zero - (-0))<br>+end<br>+<br>+-- Fold optimization x - (-0) ==> x is INVALID for x = -0 in FP<br>+-- arithmetic. Its result is -0 instead of +0.<br>+<br>+test:is(results[1], '0', 'correct VM value for -0 - (-0)')<br>+test:samevalues(results, '-0 folding in simplify_numsub_k')<br>+<br>+test:done(true)<br>--<br>2.42.0</div></div></div></div></blockquote><div> </div></BODY></HTML>