diff --git a/pystencils/nbackend/types/basic_types.py b/pystencils/nbackend/types/basic_types.py index 0a56f2f574cb18d70db633923b48f35ffc46f6f4..42feb18b99a64faa25a885d81abaf3e3ca874866 100644 --- a/pystencils/nbackend/types/basic_types.py +++ b/pystencils/nbackend/types/basic_types.py @@ -358,13 +358,19 @@ T = TypeVar("T", bound=PsAbstractType) def constify(t: T) -> T: """Adds the const qualifier to a given type.""" - t_copy = copy(t) - t_copy._const = True - return t_copy + if not t.const: + t_copy = copy(t) + t_copy._const = True + return t_copy + else: + return t def deconstify(t: T) -> T: """Removes the const qualifier from a given type.""" - t_copy = copy(t) - t_copy._const = False - return t_copy + if t.const: + t_copy = copy(t) + t_copy._const = False + return t_copy + else: + return t diff --git a/pystencils_tests/nbackend/test_constant_folding.py b/pystencils_tests/nbackend/test_constant_folding.py index 12149c3ce8b933831a55a5ae8caa1594a530de8d..7f395aaf7259f98b99fc03bacdff264256645402 100644 --- a/pystencils_tests/nbackend/test_constant_folding.py +++ b/pystencils_tests/nbackend/test_constant_folding.py @@ -27,6 +27,16 @@ def test_constant_folding_int(width): assert folder(expr) == PsTypedConstant(-53, SInt(width)) + expr = pb.Product( + ( + PsTypedConstant(2, SInt(width)), + PsTypedConstant(-3, SInt(width)), + PsTypedConstant(4, SInt(width)) + ) + ) + + assert folder(expr) == PsTypedConstant(-24, SInt(width)) + @pytest.mark.parametrize("width", (32, 64)) def test_constant_folding_float(width):