From 1bdaff32ab4f5be5e69706b680ac6fb868a39a7c Mon Sep 17 00:00:00 2001 From: Sei Lisa Date: Tue, 2 Jul 2019 15:40:20 +0200 Subject: [PATCH] When optimizing nonconst * const, cast the result to the type of the operator. Per bug report by @Welcius. Fixes #8. --- lslopt/lslfoldconst.py | 15 ++++++++++----- unit_tests/expr.suite/casts.lsl | 3 +++ unit_tests/expr.suite/casts.out | 3 +++ unit_tests/regression.suite/issue-8.lsl | 14 ++++++++++++++ unit_tests/regression.suite/issue-8.out | 16 ++++++++++++++++ 5 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 unit_tests/regression.suite/issue-8.lsl create mode 100644 unit_tests/regression.suite/issue-8.out diff --git a/lslopt/lslfoldconst.py b/lslopt/lslfoldconst.py index ce3a0a7..28f9659 100644 --- a/lslopt/lslfoldconst.py +++ b/lslopt/lslfoldconst.py @@ -1204,24 +1204,29 @@ class foldconst(object): and b == 1 and val in (-1, 1) ): if val == 1: - parent[index] = child[a] + parent[index] = self.Cast(child[a], optype) + self.FoldTree(parent, index) return if val == 0: if child[a].SEF: - parent[index] = child[b] + parent[index] = self.Cast(child[b], optype) + self.FoldTree(parent, index) return if val == -1: # Note 0.0*-1 equals -0.0 in LSL, so this is safe node = parent[index] = nr(nt='NEG', t=node.t, - ch=[child[a]], SEF=child[a].SEF) + ch=[self.Cast(child[a], optype)], + SEF=child[a].SEF) + self.FoldTree(parent, index) return # only -2, 2 remain if child[a].nt == 'IDENT' and self.isLocalVar(child[a]): - child[b] = child[a].copy() + child[b] = self.Cast(child[a].copy(), optype) node.nt = '+' if val == -2: - parent[index] = nr(nt='NEG', t=node.t, + parent[index] = nr(nt='NEG', t=optype, ch=[node], SEF=node.SEF) + self.FoldTree(parent, index) return return diff --git a/unit_tests/expr.suite/casts.lsl b/unit_tests/expr.suite/casts.lsl index fae727d..5f51835 100644 --- a/unit_tests/expr.suite/casts.lsl +++ b/unit_tests/expr.suite/casts.lsl @@ -132,4 +132,7 @@ , llSin(-2147483648) , llSin(2147483647) , llSin(2147483647.0) +, 2147483647 * 1.0 * 2 +, 3 * 1.0 / 2 +, 3 / 2 ] diff --git a/unit_tests/expr.suite/casts.out b/unit_tests/expr.suite/casts.out index a337a2d..27efc0e 100644 --- a/unit_tests/expr.suite/casts.out +++ b/unit_tests/expr.suite/casts.out @@ -128,4 +128,7 @@ , 0.9713102 , -0.9713102 , -0.9713102 +, 4294967296. +, 1.5 +, 1 ] \ No newline at end of file diff --git a/unit_tests/regression.suite/issue-8.lsl b/unit_tests/regression.suite/issue-8.lsl new file mode 100644 index 0000000..a94fa4b --- /dev/null +++ b/unit_tests/regression.suite/issue-8.lsl @@ -0,0 +1,14 @@ +default{timer(){ + + integer x = llGetNumberOfPrims(); + integer y = llGetUnixTime(); + llOwnerSay((string)(x * 1.0 / y)); + llOwnerSay((string)(x / 1.0 / y)); + llOwnerSay((string)(x / (-1.0) / y)); + llOwnerSay((string)(x * 1.0 / y)); + llOwnerSay((string)(x * (-1.0) / y)); + llOwnerSay((string)(x * -2.0)); + llOwnerSay((string)(x * 0.0 + y)); + llOwnerSay((string)(x + 0.0 + y)); + +}} diff --git a/unit_tests/regression.suite/issue-8.out b/unit_tests/regression.suite/issue-8.out new file mode 100644 index 0000000..1982dd6 --- /dev/null +++ b/unit_tests/regression.suite/issue-8.out @@ -0,0 +1,16 @@ +default +{ + timer() + { + integer x = llGetNumberOfPrims(); + integer y = llGetUnixTime(); + llOwnerSay((string)((float)x / y)); + llOwnerSay((string)((float)x / y)); + llOwnerSay((string)(-(float)x / y)); + llOwnerSay((string)((float)x / y)); + llOwnerSay((string)(-(float)x / y)); + llOwnerSay((string)(-(x + (float)x))); + llOwnerSay((string)((float)y)); + llOwnerSay((string)((float)x + y)); + } +}