User talk:PROXiCiDE/MathUtil
Jump to navigation
Jump to search
Some Small Efficiency Tweaks[edit source]
This is looking good PROXiCiDE, but I've noticed there's a small efficiency tweak that can be made to the IsNumberEven and IsNumberOdd functions. At the moment, here's the assembly generated by the IsNumberEven function:
.function IsNumberEven
.userFlags 0
.docString ""
.return Bool
.paramTable
.param num Int
.endParamTable
.localTable
.local ::temp0 int
.local ::temp1 float
.local ::temp2 bool
.endLocalTable
.code
IMOD ::temp0 num 2 ;@line 4
CAST ::temp1 ::temp0 ;@line 4
CALLSTATIC math Floor ::temp0 ::temp1 ;@line 4
COMPAREEQ ::temp2 ::temp0 0 ;@line 4
JUMPF ::temp2 label1 ;@line 4
RETURN True ;@line 5
JUMP label0
label1:
RETURN False ;@line 7
label0:
.endCode
.endFunction
By changing the function to this, the generated assembly can be reduced so that it's essentially exactly the same except without the COMPAREEQ operation, and the True/False return statements are switched around:
Bool Function IsNumberEven (Int num)
If Math.Floor(num % 2)
Return False
Else
Return True
EndIf
EndFunctionThe functionality hasn't changed, and now the assembly code generated for this function looks like this:
.code IMOD ::temp3 num 2 ;@line 12 CAST ::temp4 ::temp3 ;@line 12 CALLSTATIC math Floor ::temp3 ::temp4 ;@line 12 JUMPF ::temp3 label3 ;@line 12 RETURN False ;@line 13 JUMP label2 label3: RETURN True ;@line 15 label2: .endCode
Of course, the same optimisation can also be applied to the IsNumberOdd function:
Bool Function IsNumberEven (Int num)
If Math.Floor(num % 2)
Return True
Else
Return False
EndIf
EndFunctionAnother tweak can be made to make InRange significantly shorter and more efficient too:
Bool Function InRange(Float x,Float min,Float max)
Return (min <= x && x <= max)
EndFunction