From 96fb676be757725830d354e2d92cc3a947b5ea31 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Tue, 15 Mar 2022 15:19:19 +0100
Subject: [PATCH] =?UTF-8?q?Bytecode:=20fix=20stack=5Fsolve=5Falgo=3D4=20wh?=
 =?UTF-8?q?en=20a=20=E2=80=9Csolve=20backward/forward=E2=80=9D=20block=20f?=
 =?UTF-8?q?ollows=20a=20=E2=80=9Csolve=20two=20boundaries=E2=80=9D=20block?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

When solving a “Solve two boundarise” block with stack_solve_algo=4, the
“slowc” variable is modified. This would affect the resolution of further
“solve backward/forward” blocks, which would yield results.

The fix consists in saving and restoring “slowc”.
---
 mex/sources/bytecode/Interpreter.cc | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/mex/sources/bytecode/Interpreter.cc b/mex/sources/bytecode/Interpreter.cc
index d7bc98326b..a301d69c6c 100644
--- a/mex/sources/bytecode/Interpreter.cc
+++ b/mex/sources/bytecode/Interpreter.cc
@@ -310,6 +310,7 @@ Interpreter::simulate_a_block(const vector_table_conditional_local_type &vector_
   max_res_idx = 0;
   bool cvg;
   double *y_save;
+  double another_slowc_save;
 #ifdef DEBUG
   mexPrintf("simulate_a_block type = %d, periods=%d, y_kmin=%d, y_kmax=%d\n", type, periods, y_kmin, y_kmax);
   mexEvalString("drawnow;");
@@ -429,6 +430,7 @@ Interpreter::simulate_a_block(const vector_table_conditional_local_type &vector_
       test_mxMalloc(y_save, __LINE__, __FILE__, __func__, y_size*sizeof(double)*(periods+y_kmax+y_kmin));
       start_code = it_code;
       iter = 0;
+      another_slowc_save = slowc; // slowc is modified when stack_solve_algo=4, so save it
       if (!is_linear
           || stack_solve_algo == 4) // On linear blocks, stack_solve_algo=4 may
                                     // need more than one iteration to find the
@@ -485,6 +487,7 @@ Interpreter::simulate_a_block(const vector_table_conditional_local_type &vector_
           Simulate_Newton_Two_Boundaries(block_num, symbol_table_endo_nbr, y_kmin, y_kmax, size, periods, cvg, minimal_solving_periods, stack_solve_algo, endo_name_length, P_endo_names, vector_table_conditional_local);
           max_res = 0; max_res_idx = 0;
         }
+      slowc = another_slowc_save; // slowc is modified when stack_solve_algo=4, so restore it
       it_code = end_code;
       if (r)
         mxFree(r);
-- 
GitLab