From b04d1295cd5ee8d753279607d8056a56be446bf4 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Thu, 18 Aug 2022 19:24:01 +0100 Subject: [PATCH] VarAction2: Infer that unsigned mod 2 results in 0 or 1 --- src/newgrf_optimiser.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/newgrf_optimiser.cpp b/src/newgrf_optimiser.cpp index 283c5142af..eec236eb3a 100644 --- a/src/newgrf_optimiser.cpp +++ b/src/newgrf_optimiser.cpp @@ -1003,6 +1003,12 @@ void OptimiseVarAction2Adjust(VarAction2OptimiseState &state, const GrfSpecFeatu group->dsg_flags |= DSGF_CHECK_INSERT_JUMP; } } + if (adjust.operation == DSGA_OP_RST && adjust.type == DSGA_TYPE_MOD && adjust.divmod_val == 2) { + /* Non-negative value % 2 implies VA2AIF_ONE_OR_ZERO */ + if ((uint64)adjust.and_mask + (uint64)adjust.add_val < (uint64)get_sign_bit()) { + state.inference |= VA2AIF_SIGNED_NON_NEGATIVE | VA2AIF_ONE_OR_ZERO; + } + } } else { if (adjust.and_mask == 0 && IsEvalAdjustWithZeroRemovable(adjust.operation)) { /* Delete useless zero operations */