Skip to content

Commit

Permalink
Change: Optimise trivial right shift operations on variables
Browse files Browse the repository at this point in the history
  • Loading branch information
JGRennison authored and FLHerne committed Apr 3, 2023
1 parent 30842d3 commit 1e26f14
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
18 changes: 18 additions & 0 deletions nml/expression/binop.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,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

0 comments on commit 1e26f14

Please sign in to comment.