Skip to content

Commit

Permalink
avoid #8231, bitwise move to mul,div (#15070)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
bung87 authored Aug 25, 2020
1 parent 15ff89c commit 7cee63b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
12 changes: 10 additions & 2 deletions compiler/jsgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down
3 changes: 3 additions & 0 deletions tests/js/t8231.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import strutils

doAssert formatSize(2462056448, '.', bpIEC, false) == "2.293GiB"

0 comments on commit 7cee63b

Please sign in to comment.