* fixes #19615; emit better code for integer divisions when the divisor is known at compile-time * proper bugfix: unsigned numbers cannot be -1
This commit is contained in:
parent
731eabc930
commit
c4a0d4c5e3
1 changed files with 16 additions and 6 deletions
|
|
@ -584,13 +584,23 @@ proc binaryArithOverflow(p: BProc, e: PNode, d: var TLoc, m: TMagic) =
|
||||||
else:
|
else:
|
||||||
# we handle div by zero here so that we know that the compilerproc's
|
# we handle div by zero here so that we know that the compilerproc's
|
||||||
# result is only for overflows.
|
# result is only for overflows.
|
||||||
|
var needsOverflowCheck = true
|
||||||
if m in {mDivI, mModI}:
|
if m in {mDivI, mModI}:
|
||||||
linefmt(p, cpsStmts, "if ($1 == 0){ #raiseDivByZero(); $2}$n",
|
var canBeZero = true
|
||||||
[rdLoc(b), raiseInstr(p)])
|
if e[2].kind in {nkIntLit..nkUInt64Lit}:
|
||||||
|
canBeZero = e[2].intVal == 0
|
||||||
let res = binaryArithOverflowRaw(p, t, a, b,
|
if e[2].kind in {nkIntLit..nkInt64Lit}:
|
||||||
if t.kind == tyInt64: prc64[m] else: prc[m])
|
needsOverflowCheck = e[2].intVal == -1
|
||||||
putIntoDest(p, d, e, "($#)($#)" % [getTypeDesc(p.module, e.typ), res])
|
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) =
|
proc unaryArithOverflow(p: BProc, e: PNode, d: var TLoc, m: TMagic) =
|
||||||
var
|
var
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue