From 7cee63bba3805bd49f0c03b94d65f02dea04f946 Mon Sep 17 00:00:00 2001 From: Bung Date: Tue, 25 Aug 2020 15:58:32 +0800 Subject: [PATCH] avoid #8231, bitwise move to mul,div (#15070) * avoid #8231, bitwise move to mul,div * add test for #8231 * fix bitwise move when div result is float * bitwise move depends on typ.size --- compiler/jsgen.nim | 12 ++++++++++-- tests/js/t8231.nim | 3 +++ 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 tests/js/t8231.nim diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index ede4d14e0..bea5225e6 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -602,8 +602,16 @@ proc arithAux(p: PProc, n: PNode, r: var TCompRes, op: TMagic) = of mMulF64: applyFormat("($1 * $2)", "($1 * $2)") of mDivF64: applyFormat("($1 / $2)", "($1 / $2)") of mShrI: applyFormat("", "") - of mShlI: applyFormat("($1 << $2)", "($1 << $2)") - of mAshrI: applyFormat("($1 >> $2)", "($1 >> $2)") + of mShlI: + if n[1].typ.size <= 4: + applyFormat("($1 << $2)", "($1 << $2)") + else: + applyFormat("($1 * Math.pow(2,$2))", "($1 * Math.pow(2,$2))") + of mAshrI: + if n[1].typ.size <= 4: + applyFormat("($1 >> $2)", "($1 >> $2)") + else: + applyFormat("Math.floor($1 / Math.pow(2,$2))", "Math.floor($1 / Math.pow(2,$2))") of mBitandI: applyFormat("($1 & $2)", "($1 & $2)") of mBitorI: applyFormat("($1 | $2)", "($1 | $2)") of mBitxorI: applyFormat("($1 ^ $2)", "($1 ^ $2)") diff --git a/tests/js/t8231.nim b/tests/js/t8231.nim new file mode 100644 index 000000000..b0625a621 --- /dev/null +++ b/tests/js/t8231.nim @@ -0,0 +1,3 @@ +import strutils + +doAssert formatSize(2462056448, '.', bpIEC, false) == "2.293GiB" \ No newline at end of file