mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 05:50:30 +00:00
* fixes #19615; emit better code for integer divisions when the divisor is known at compile-time
* proper bugfix: unsigned numbers cannot be -1
(cherry picked from commit c4a0d4c5e3)
This commit is contained in:
@@ -584,13 +584,23 @@ proc binaryArithOverflow(p: BProc, e: PNode, d: var TLoc, m: TMagic) =
|
||||
else:
|
||||
# we handle div by zero here so that we know that the compilerproc's
|
||||
# result is only for overflows.
|
||||
var needsOverflowCheck = true
|
||||
if m in {mDivI, mModI}:
|
||||
linefmt(p, cpsStmts, "if ($1 == 0){ #raiseDivByZero(); $2}$n",
|
||||
[rdLoc(b), raiseInstr(p)])
|
||||
|
||||
let res = binaryArithOverflowRaw(p, t, a, b,
|
||||
if t.kind == tyInt64: prc64[m] else: prc[m])
|
||||
putIntoDest(p, d, e, "($#)($#)" % [getTypeDesc(p.module, e.typ), res])
|
||||
var canBeZero = true
|
||||
if e[2].kind in {nkIntLit..nkUInt64Lit}:
|
||||
canBeZero = e[2].intVal == 0
|
||||
if e[2].kind in {nkIntLit..nkInt64Lit}:
|
||||
needsOverflowCheck = e[2].intVal == -1
|
||||
if canBeZero:
|
||||
linefmt(p, cpsStmts, "if ($1 == 0){ #raiseDivByZero(); $2}$n",
|
||||
[rdLoc(b), raiseInstr(p)])
|
||||
if needsOverflowCheck:
|
||||
let res = binaryArithOverflowRaw(p, t, a, b,
|
||||
if t.kind == tyInt64: prc64[m] else: prc[m])
|
||||
putIntoDest(p, d, e, "($#)($#)" % [getTypeDesc(p.module, e.typ), res])
|
||||
else:
|
||||
let res = "($1)($2 $3 $4)" % [getTypeDesc(p.module, e.typ), rdLoc(a), rope(opr[m]), rdLoc(b)]
|
||||
putIntoDest(p, d, e, res)
|
||||
|
||||
proc unaryArithOverflow(p: BProc, e: PNode, d: var TLoc, m: TMagic) =
|
||||
var
|
||||
|
||||
Reference in New Issue
Block a user