diff options
author | Bung <crc32@qq.com> | 2020-08-25 15:58:32 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-25 09:58:32 +0200 |
commit | 7cee63bba3805bd49f0c03b94d65f02dea04f946 (patch) | |
tree | c36977d4867d6d5ccab33b1d00f45c126a8b6946 | |
parent | 15ff89cec14ea6d1bc05a5975dedfcb6e712720b (diff) | |
download | Nim-7cee63bba3805bd49f0c03b94d65f02dea04f946.tar.gz |
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
-rw-r--r-- | compiler/jsgen.nim | 12 | ||||
-rw-r--r-- | tests/js/t8231.nim | 3 |
2 files changed, 13 insertions, 2 deletions
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 |