Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimise variational action2 trivial cases #281

Merged
merged 2 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 34 additions & 1 deletion nml/expression/binop.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,22 @@ def reduce(self, id_dicts=None, unknown_id_fatal=True):
if op in (nmlop.DIV, nmlop.DIVU, nmlop.MUL) and isinstance(expr2, ConstantNumeric) and expr2.value == 1:
return expr1

if op in (nmlop.ADD, nmlop.SUB) and isinstance(expr2, ConstantNumeric) and expr2.value == 0:
if (
op
in (
nmlop.ADD,
nmlop.SUB,
nmlop.SHIFTU_LEFT,
nmlop.SHIFT_LEFT,
nmlop.SHIFTU_RIGHT,
nmlop.SHIFT_RIGHT,
nmlop.OR,
nmlop.XOR,
nmlop.ROT_RIGHT,
)
and isinstance(expr2, ConstantNumeric)
and expr2.value == 0
):
return expr1

# - Variables (as used in action2var) can have some computations attached to
Expand Down Expand Up @@ -171,6 +186,24 @@ def reduce(self, id_dicts=None, unknown_id_fatal=True):
expr1.mask = nmlop.SHIFT_LEFT(expr1.mask, expr2).reduce()
return expr1

if (
(
op == nmlop.SHIFTU_RIGHT
or (
op == nmlop.SHIFT_RIGHT
and isinstance(expr1.mask, ConstantNumeric)
and expr1.mask.uvalue < 0x80000000
)
)
and isinstance(expr2, ConstantNumeric)
and expr2.value >= 0
and expr1.shift.value >= 0
and (expr1.shift.value + expr2.value) < 32
):
expr1.shift.value += expr2.value
expr1.mask = nmlop.SHIFTU_RIGHT(expr1.mask, expr2).reduce()
return expr1

# - Try to merge multiple additions/subtractions with constant numbers
if (
op in (nmlop.ADD, nmlop.SUB)
Expand Down
Binary file modified regression/expected/example_train.grf
Binary file not shown.
6 changes: 2 additions & 4 deletions regression/expected/example_train.nfo
Original file line number Diff line number Diff line change
Expand Up @@ -505,10 +505,8 @@ FF \wx00FD // @action3_4;
\wx00FD // default: set_cargo_wagon_load;

// Name: cargo_wagon_switch_graphics
117 * 37 02 00 FD 89
10 20 \dxFFFFFFFF
\2u>> 1A 20 \dx00000008
\2& 1A 00 \dx000000FF
117 * 23 02 00 FD 89
10 08 \dx000000FF
\b1
\wx00F1 \dx00000000 \dx00000000 // 0 .. 0: cargo_wagon_switch_vehicle;
\wx00FD // default: cargo_wagon_switch_load;
Expand Down