From 7f6bb1303bc7b533d6f75a27352ac4ff440d4c9c Mon Sep 17 00:00:00 2001 From: Frederik Hennig <frederik.hennig@fau.de> Date: Wed, 3 Jul 2024 09:29:19 +0200 Subject: [PATCH] move c-intdiv implementations to pystencils.utils --- src/pystencils/backend/ast/expressions.py | 4 ++-- src/pystencils/backend/ast/util.py | 11 ----------- .../backend/transformations/eliminate_constants.py | 2 +- src/pystencils/sympyextensions/integer_functions.py | 7 ++++--- src/pystencils/utils.py | 11 +++++++++++ 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/pystencils/backend/ast/expressions.py b/src/pystencils/backend/ast/expressions.py index 67fd6b27e..908f31052 100644 --- a/src/pystencils/backend/ast/expressions.py +++ b/src/pystencils/backend/ast/expressions.py @@ -636,7 +636,7 @@ class PsIntDiv(PsBinOp, PsIntOpTrait): @property def python_operator(self) -> Callable[[Any, Any], Any]: - from .util import c_intdiv + from ...utils import c_intdiv return c_intdiv @@ -646,7 +646,7 @@ class PsRem(PsBinOp, PsIntOpTrait): @property def python_operator(self) -> Callable[[Any, Any], Any]: - from .util import c_rem + from ...utils import c_rem return c_rem diff --git a/src/pystencils/backend/ast/util.py b/src/pystencils/backend/ast/util.py index 2fdf6078d..0d3b78629 100644 --- a/src/pystencils/backend/ast/util.py +++ b/src/pystencils/backend/ast/util.py @@ -36,14 +36,3 @@ class AstEqWrapper: # TODO: consider replacing this with smth. more performant # TODO: Check that repr is implemented by all AST nodes return hash(repr(self._node)) - - -def c_intdiv(num, denom): - """C-style integer division""" - return int(num / denom) - - -def c_rem(num, denom): - """C-style integer remainder""" - div = c_intdiv(num, denom) - return num - div * denom diff --git a/src/pystencils/backend/transformations/eliminate_constants.py b/src/pystencils/backend/transformations/eliminate_constants.py index 15a6d5c5c..bd3b2bb58 100644 --- a/src/pystencils/backend/transformations/eliminate_constants.py +++ b/src/pystencils/backend/transformations/eliminate_constants.py @@ -316,7 +316,7 @@ class EliminateConstants: ) elif isinstance(expr, PsDiv): if is_int: - from ..ast.util import c_intdiv + from ...utils import c_intdiv folded = PsConstant(c_intdiv(v1, v2), dtype) elif isinstance(dtype, PsIeeeFloatType): folded = PsConstant(v1 / v2, dtype) diff --git a/src/pystencils/sympyextensions/integer_functions.py b/src/pystencils/sympyextensions/integer_functions.py index 600562ad1..3b215266e 100644 --- a/src/pystencils/sympyextensions/integer_functions.py +++ b/src/pystencils/sympyextensions/integer_functions.py @@ -48,15 +48,16 @@ class int_div(IntegerFunctionTwoArgsMixIn): """C-style round-to-zero integer division""" def _eval_op(self, arg1, arg2): - return int(arg1 / arg2) + from ..utils import c_intdiv + return c_intdiv(arg1, arg2) class int_rem(IntegerFunctionTwoArgsMixIn): """C-style round-to-zero integer remainder""" def _eval_op(self, arg1, arg2): - div = int(arg1 / arg2) - return arg1 - div * arg2 + from ..utils import c_rem + return c_rem(arg1, arg2) # noinspection PyPep8Naming diff --git a/src/pystencils/utils.py b/src/pystencils/utils.py index f872ae48a..98331e7e5 100644 --- a/src/pystencils/utils.py +++ b/src/pystencils/utils.py @@ -250,3 +250,14 @@ class ContextVar: def get(self): return self.stack[-1] + + +def c_intdiv(num, denom): + """C-style integer division""" + return int(num / denom) + + +def c_rem(num, denom): + """C-style integer remainder""" + div = c_intdiv(num, denom) + return num - div * denom -- GitLab