summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorLemonBoy <LemonBoy@users.noreply.github.com>2019-01-22 07:33:27 +0100
committerAndreas Rumpf <rumpf_a@web.de>2019-01-22 07:33:27 +0100
commit7a3f382517cfc0f2b676dc6704b423e7b167b565 (patch)
treeb47126edf98d4406af8e6a43530a6690045153eb
parenta0e8d4a93f68c00ecd9c50465d3209056e7a8358 (diff)
downloadNim-7a3f382517cfc0f2b676dc6704b423e7b167b565.tar.gz
Fix corner case of checked div/mod on x86 (#10406)
Fixes #10377
-rw-r--r--lib/system/arithm.nim44
1 files changed, 29 insertions, 15 deletions
diff --git a/lib/system/arithm.nim b/lib/system/arithm.nim
index 69c558799..a875e95a7 100644
--- a/lib/system/arithm.nim
+++ b/lib/system/arithm.nim
@@ -197,26 +197,40 @@ when asmVersion and not defined(gcc) and not defined(llvm_gcc):
 
   proc divInt(a, b: int): int {.compilerProc, asmNoStackFrame.} =
     asm """
-        mov eax, ecx
-        mov ecx, edx
-        xor edx, edx
-        idiv ecx
-        jno  theEnd
-        call `raiseOverflow`
-      theEnd:
+        test  edx, edx
+        jne   L_NOT_ZERO
+        call  `raiseDivByZero`
+      L_NOT_ZERO:
+        cmp   ecx, 0x80000000
+        jne   L_DO_DIV
+        cmp   edx, -1
+        jne   L_DO_DIV
+        call  `raiseOverflow`
+      L_DO_DIV:
+        mov   eax, ecx
+        mov   ecx, edx
+        cdq
+        idiv  ecx
         ret
     """
 
   proc modInt(a, b: int): int {.compilerProc, asmNoStackFrame.} =
     asm """
-        mov eax, ecx
-        mov ecx, edx
-        xor edx, edx
-        idiv ecx
-        jno theEnd
-        call `raiseOverflow`
-      theEnd:
-        mov eax, edx
+        test  edx, edx
+        jne   L_NOT_ZERO
+        call  `raiseDivByZero`
+      L_NOT_ZERO:
+        cmp   ecx, 0x80000000
+        jne   L_DO_DIV
+        cmp   edx, -1
+        jne   L_DO_DIV
+        call  `raiseOverflow`
+      L_DO_DIV:
+        mov   eax, ecx
+        mov   ecx, edx
+        cdq
+        idiv  ecx
+        mov   eax, edx
         ret
     """