mirror of
https://github.com/Sei-Lisa/LSL-PyOptimizer
synced 2025-07-01 15:48:21 +00:00
Add getMin() and getMax(), which can detect constants
This commit is contained in:
parent
bf505220ac
commit
d2e3b9a3bd
2 changed files with 38 additions and 15 deletions
|
@ -29,6 +29,18 @@ from strutil import xrange, unicode
|
||||||
|
|
||||||
class foldconst(object):
|
class foldconst(object):
|
||||||
|
|
||||||
|
def getMin(self, node):
|
||||||
|
assert node.t in ('integer', 'float')
|
||||||
|
if node.nt == 'CONST':
|
||||||
|
return node.value
|
||||||
|
return getattr(node, 'min', None)
|
||||||
|
|
||||||
|
def getMax(self, node):
|
||||||
|
assert node.t in ('integer', 'float')
|
||||||
|
if node.nt == 'CONST':
|
||||||
|
return node.value
|
||||||
|
return getattr(node, 'max', None)
|
||||||
|
|
||||||
def isLocalVar(self, node):
|
def isLocalVar(self, node):
|
||||||
name = node.name
|
name = node.name
|
||||||
scope = node.scope
|
scope = node.scope
|
||||||
|
@ -1322,33 +1334,35 @@ class foldconst(object):
|
||||||
if child[0].nt == 'CONST' and child[1].SEF:
|
if child[0].nt == 'CONST' and child[1].SEF:
|
||||||
# when CONST >= second.max: always false
|
# when CONST >= second.max: always false
|
||||||
# when CONST < second.min: always true
|
# when CONST < second.min: always true
|
||||||
if (hasattr(child[1], 'max')
|
rmin = self.getMin(child[1])
|
||||||
and not lslfuncs.less(child[0].value, child[1].max)
|
rmax = self.getMax(child[1])
|
||||||
|
if (rmax is not None
|
||||||
|
and not lslfuncs.less(child[0].value, rmax)
|
||||||
):
|
):
|
||||||
parent[index] = nr(nt='CONST', t='integer',
|
parent[index] = nr(nt='CONST', t='integer',
|
||||||
value=0, SEF=True)
|
value=0, SEF=True)
|
||||||
return
|
return
|
||||||
if (hasattr(child[1], 'min')
|
if rmin is not None and lslfuncs.less(child[0].value, rmin):
|
||||||
and lslfuncs.less(child[0].value, child[1].min)
|
|
||||||
):
|
|
||||||
parent[index] = nr(nt='CONST', t='integer',
|
parent[index] = nr(nt='CONST', t='integer',
|
||||||
value=1, SEF=True)
|
value=1, SEF=True)
|
||||||
return
|
return
|
||||||
|
del rmin, rmax
|
||||||
if child[1].nt == 'CONST' and child[0].SEF:
|
if child[1].nt == 'CONST' and child[0].SEF:
|
||||||
# when first.max < CONST: always true
|
# when first.max < CONST: always true
|
||||||
# when first.min >= CONST: always false
|
# when first.min >= CONST: always false
|
||||||
if (hasattr(child[0], 'max')
|
lmin = self.getMin(child[0])
|
||||||
and lslfuncs.less(child[0].max, child[1].value)
|
lmax = self.getMax(child[0])
|
||||||
):
|
if lmax is not None and lslfuncs.less(lmax, child[1].value):
|
||||||
parent[index] = nr(nt='CONST', t='integer',
|
parent[index] = nr(nt='CONST', t='integer',
|
||||||
value=1, SEF=True)
|
value=1, SEF=True)
|
||||||
return
|
return
|
||||||
if (hasattr(child[0], 'min')
|
if (lmin is not None
|
||||||
and not lslfuncs.less(child[0].min, child[1].value)
|
and not lslfuncs.less(lmin, child[1].value)
|
||||||
):
|
):
|
||||||
parent[index] = nr(nt='CONST', t='integer',
|
parent[index] = nr(nt='CONST', t='integer',
|
||||||
value=0, SEF=True)
|
value=0, SEF=True)
|
||||||
return
|
return
|
||||||
|
del lmin, lmax
|
||||||
|
|
||||||
# Convert 2147483647<i and i<-2147483648 to i&0
|
# Convert 2147483647<i and i<-2147483648 to i&0
|
||||||
if (child[0].t == child[1].t == 'integer'
|
if (child[0].t == child[1].t == 'integer'
|
||||||
|
|
|
@ -528,11 +528,20 @@ def OptimizeFunc(self, parent, index):
|
||||||
parent[index] = nr(nt='CONST', t='list', value=[], SEF=True)
|
parent[index] = nr(nt='CONST', t='list', value=[], SEF=True)
|
||||||
return
|
return
|
||||||
|
|
||||||
if name == 'llFrand' and child[0].nt == 'CONST':
|
if name == 'llFrand':
|
||||||
# We can set a range when the input is a constant
|
# We can set a range when the input is known to be positive or negative
|
||||||
value = child[0].value
|
argmin = self.getMin(child[0])
|
||||||
node.min = lslfuncs.F32(min(value, 0) * 0.9999999403953552)
|
argmax = self.getMax(child[0])
|
||||||
node.max = lslfuncs.F32(max(value, 0) * 0.9999999403953552)
|
if argmin is not None and argmin >= 0:
|
||||||
|
# Positive argument
|
||||||
|
node.min = 0.
|
||||||
|
if argmax is not None:
|
||||||
|
node.max = lslfuncs.F32(argmax * 0.9999999403953552)
|
||||||
|
elif argmax is not None and argmax <= 0:
|
||||||
|
# Negative argument
|
||||||
|
node.max = 0.
|
||||||
|
if argmin is not None:
|
||||||
|
node.min = lslfuncs.F32(argmin * 0.9999999403953552)
|
||||||
|
|
||||||
def FuncOptSetup():
|
def FuncOptSetup():
|
||||||
# Patch the default values list for LSO
|
# Patch the default values list for LSO
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue