about summary refs log tree commit diff stats
path: root/miasm2/expression/modint.py
diff options
context:
space:
mode:
authorAjax <commial@gmail.com>2017-01-13 17:26:04 +0100
committerAjax <commial@gmail.com>2017-01-13 17:26:47 +0100
commit49d009a0fabd96e930a7f0233b3b5525bdec2957 (patch)
tree6c83406a5204823027bfe76542487cd6e471d12b /miasm2/expression/modint.py
parent632f95b8ce745ef77ecb9f46054c1964ddc59e3e (diff)
downloadmiasm-49d009a0fabd96e930a7f0233b3b5525bdec2957.tar.gz
miasm-49d009a0fabd96e930a7f0233b3b5525bdec2957.zip
Div: fix modint operation
Diffstat (limited to 'miasm2/expression/modint.py')
-rw-r--r--miasm2/expression/modint.py16
1 files changed, 10 insertions, 6 deletions
diff --git a/miasm2/expression/modint.py b/miasm2/expression/modint.py
index 90dabfac..b6a0e4ee 100644
--- a/miasm2/expression/modint.py
+++ b/miasm2/expression/modint.py
@@ -41,11 +41,15 @@ class moduint(object):
             return self.__class__(self.arg & y)
 
     def __div__(self, y):
+        # Python: 8 / -7 == -2 (C-like: -1)
+        # int(float) trick cannot be used, due to information loss
+        den = int(y)
+        num = int(self)
+        result_sign = 1 if (den * num) >= 0 else -1
+        cls = self.__class__
         if isinstance(y, moduint):
             cls = self.maxcast(y)
-            return cls(int(float(self.arg) / y.arg))
-        else:
-            return self.__class__(int(float(self.arg) / y))
+        return ((abs(num) / abs(den)) * result_sign)
 
     def __int__(self):
         return int(self.arg)
@@ -64,11 +68,11 @@ class moduint(object):
             return self.__class__(self.arg << y)
 
     def __mod__(self, y):
+        # See __div__ for implementation choice
+        cls = self.__class__
         if isinstance(y, moduint):
             cls = self.maxcast(y)
-            return cls(self.arg - (y.arg * int(float(self.arg)/y.arg)))
-        else:
-            return self.__class__(self.arg - (y * int(float(self.arg)/y)))
+        return cls(self.arg - (y * (self / y)))
 
     def __mul__(self, y):
         if isinstance(y, moduint):