From 5a8701e2e3a19176ec0f6dca504191a264ec4062 Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Sat, 2 Sep 2023 17:37:08 +0000 Subject: [PATCH] improve gen_shift_n --- sot/opcode_translator/executor/pycode_generator.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sot/opcode_translator/executor/pycode_generator.py b/sot/opcode_translator/executor/pycode_generator.py index 2c9065b3a..e12cc2d0b 100644 --- a/sot/opcode_translator/executor/pycode_generator.py +++ b/sot/opcode_translator/executor/pycode_generator.py @@ -826,6 +826,12 @@ def gen_shift_n(self, s: int, n: int): """ if s == 0 or n <= 1: return + + # NOTE(zrr1999): right shift s steps is equal to left shift n-s steps + if abs(s) > n // 2: + new_s = s - n if s > 0 else s + n + self.gen_shift_n(new_s, n) + return if s > 0: # NOTE: s=1, n=3 [1,2,3,4,5] -> [1,2,5,3,4] # s=2, n=3 [1,2,3,4,5] -> [1,2,4,5,3]