From 480f600ad3e9ff6e7ea1519bb80c54a826412d43 Mon Sep 17 00:00:00 2001 From: Rowan Torbitzky-Lane Date: Thu, 13 Feb 2025 00:39:52 -0600 Subject: [PATCH] add commutative opposites where it applies --- src/HushGP/Instructions/FloatInstructions.hs | 11 +++++++++++ src/HushGP/Instructions/IntInstructions.hs | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/HushGP/Instructions/FloatInstructions.hs b/src/HushGP/Instructions/FloatInstructions.hs index 2495b31..45a0c84 100644 --- a/src/HushGP/Instructions/FloatInstructions.hs +++ b/src/HushGP/Instructions/FloatInstructions.hs @@ -41,6 +41,11 @@ instructionFloatSub :: State -> State instructionFloatSub state@(State {_float = f1 : f2 : fs}) = state {_float = f2 - f1 : fs} instructionFloatSub state = state +-- |Subtracts the second float from the first float and pushes the result to the float stack. +instructionFloatSubOpp :: State -> State +instructionFloatSubOpp state@(State {_float = i1 : i2 : is}) = state {_float = i1 - i2 : is} +instructionFloatSubOpp state = state + -- |Multiplies the top two floats on the float stack. instructionFloatMul :: State -> State instructionFloatMul state@(State {_float = f1 : f2 : fs}) = state {_float = f2 * f1 : fs} @@ -51,6 +56,12 @@ instructionFloatDiv :: State -> State instructionFloatDiv state@(State {_float = f1 : f2 : fs}) = state {_float = if f1 /= 0 then f2 / f1 : fs else f1 : f2 : fs} instructionFloatDiv state = state +-- |Divides the second float from the first float and pushes the result to the float stack. +-- This does truncate. +instructionFloatDivOpp :: State -> State +instructionFloatDivOpp state@(State {_float = i1 : i2 : is}) = state {_float = if i2 /= 0 then (i1 / i2) : is else i1 : i2 : is} +instructionFloatDivOpp state = state + -- |Mods the first float from the second float on the float stack. instructionFloatMod :: State -> State instructionFloatMod state@(State {_float = f1 : f2 : fs}) = state {_float = if f1 /= 0 then f2 `mod'` f1 : fs else f1 : f2 : fs} diff --git a/src/HushGP/Instructions/IntInstructions.hs b/src/HushGP/Instructions/IntInstructions.hs index 543f0a7..43e8877 100644 --- a/src/HushGP/Instructions/IntInstructions.hs +++ b/src/HushGP/Instructions/IntInstructions.hs @@ -39,6 +39,11 @@ instructionIntSub :: State -> State instructionIntSub state@(State {_int = i1 : i2 : is}) = state {_int = i2 - i1 : is} instructionIntSub state = state +-- |Subtracts the second int from the first int and pushes the result to the int stack. +instructionIntSubOpp :: State -> State +instructionIntSubOpp state@(State {_int = i1 : i2 : is}) = state {_int = i1 - i2 : is} +instructionIntSubOpp state = state + -- |Multiplies the top two ints from the int stack and pushes the result to the int stack. instructionIntMul :: State -> State instructionIntMul state@(State {_int = i1 : i2 : is}) = state {_int = i2 * i1 : is} @@ -50,6 +55,12 @@ instructionIntDiv :: State -> State instructionIntDiv state@(State {_int = i1 : i2 : is}) = state {_int = if i1 /= 0 then (i2 `div` i1) : is else i1 : i2 : is} instructionIntDiv state = state +-- |Divides the second int from the first int and pushes the result to the int stack. +-- This does truncate. +instructionIntDivOpp :: State -> State +instructionIntDivOpp state@(State {_int = i1 : i2 : is}) = state {_int = if i2 /= 0 then (i1 `div` i2) : is else i1 : i2 : is} +instructionIntDivOpp state = state + -- |Mods the first float from the second float and pushes the result to the int stack. -- This does truncate. instructionIntMod :: State -> State