diff options
author | Arne Döring <arne.doering@gmx.net> | 2019-10-27 11:34:33 +0100 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2019-10-27 11:34:33 +0100 |
commit | 3c567bcf20f06dd6e9be8fc0b09bffec3bfeb0a4 (patch) | |
tree | 8127fa468b64f458b2dfb9fb34c666b610d2e618 /compiler | |
parent | 3a62cf29d8312375e94524f67dcd4125da994af7 (diff) | |
download | Nim-3c567bcf20f06dd6e9be8fc0b09bffec3bfeb0a4.tar.gz |
fixes #12514 (#12520) [backport]
Diffstat (limited to 'compiler')
-rw-r--r-- | compiler/int128.nim | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/compiler/int128.nim b/compiler/int128.nim index 44f77b8e8..361f854f2 100644 --- a/compiler/int128.nim +++ b/compiler/int128.nim @@ -407,11 +407,17 @@ proc divMod*(dividend, divisor: Int128): tuple[quotient, remainder: Int128] = if divisor > dividend: result.quotient = Zero - result.remainder = dividend + if isNegativeA: + result.remainder = -dividend + else: + result.remainder = dividend return if divisor == dividend: - result.quotient = One + if isNegativeA xor isNegativeB: + result.quotient = NegOne + else: + result.quotient = One result.remainder = Zero return @@ -685,3 +691,13 @@ when isMainModule: doAssert divMod(toInt128(-ma),toInt128( mb)) == (toInt128(-ma div mb), toInt128(-ma mod mb)) doAssert divMod(toInt128( ma),toInt128(-mb)) == (toInt128( ma div -mb), toInt128( ma mod -mb)) doAssert divMod(toInt128(-ma),toInt128(-mb)) == (toInt128(-ma div -mb), toInt128(-ma mod -mb)) + + doAssert divMod(toInt128( mb),toInt128( mb)) == (toInt128( mb div mb), toInt128( mb mod mb)) + doAssert divMod(toInt128(-mb),toInt128( mb)) == (toInt128(-mb div mb), toInt128(-mb mod mb)) + doAssert divMod(toInt128( mb),toInt128(-mb)) == (toInt128( mb div -mb), toInt128( mb mod -mb)) + doAssert divMod(toInt128(-mb),toInt128(-mb)) == (toInt128(-mb div -mb), toInt128(-mb mod -mb)) + + doAssert divMod(toInt128( mb),toInt128( ma)) == (toInt128( mb div ma), toInt128( mb mod ma)) + doAssert divMod(toInt128(-mb),toInt128( ma)) == (toInt128(-mb div ma), toInt128(-mb mod ma)) + doAssert divMod(toInt128( mb),toInt128(-ma)) == (toInt128( mb div -ma), toInt128( mb mod -ma)) + doAssert divMod(toInt128(-mb),toInt128(-ma)) == (toInt128(-mb div -ma), toInt128(-mb mod -ma)) |