summaryrefslogtreecommitdiff
path: root/cffi
diff options
context:
space:
mode:
authorArmin Rigo <arigo@tunes.org>2019-04-26 14:15:51 +0200
committerArmin Rigo <arigo@tunes.org>2019-04-26 14:15:51 +0200
commitf611fd0f3e9c4e8397d3b22cbeece0436fd23811 (patch)
treebe926eabe0df530e68708535965b4921e602e4db /cffi
parent027ca7570f39680c02745e428f92a8711bd57642 (diff)
downloadcffi-f611fd0f3e9c4e8397d3b22cbeece0436fd23811.tar.gz
Fix C integer division. Add modulo.
Diffstat (limited to 'cffi')
-rw-r--r--cffi/cparser.py11
1 files changed, 9 insertions, 2 deletions
diff --git a/cffi/cparser.py b/cffi/cparser.py
index b23238a..e8ea23a 100644
--- a/cffi/cparser.py
+++ b/cffi/cparser.py
@@ -868,8 +868,9 @@ class Parser(object):
elif exprnode.op == '*':
return left * right
elif exprnode.op == '/':
- # do integer division!
- return left // right
+ return self._c_div(left, right)
+ elif exprnode.op == '%':
+ return left - self._c_div(left, right) * right
elif exprnode.op == '<<':
return left << right
elif exprnode.op == '>>':
@@ -884,6 +885,12 @@ class Parser(object):
raise FFIError(":%d: unsupported expression: expected a "
"simple numeric constant" % exprnode.coord.line)
+ def _c_div(self, a, b):
+ result = a // b
+ if ((a < 0) ^ (b < 0)) and (a % b) != 0:
+ result += 1
+ return result
+
def _build_enum_type(self, explicit_name, decls):
if decls is not None:
partial = False