From 613500d5ceb6bb6f642a3eaa39d23ba001a40a2f Mon Sep 17 00:00:00 2001 From: Eugenio Date: Fri, 3 May 2024 22:46:31 -0300 Subject: [PATCH] Optimization using BALLS (one swap less) --- src/math/Math.huff | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/math/Math.huff b/src/math/Math.huff index 08996069..41378856 100644 --- a/src/math/Math.huff +++ b/src/math/Math.huff @@ -237,14 +237,26 @@ pop // [min(num1, num2)] } +/* +fn AVG(num1, num2) -> (avg) { + avg = add( + and(num1, num2), + div(xor(num2, num1), 2) + ) +} +*/ /// @notice Returns the average of two values on the stack #define macro AVG() = takes (2) returns (1) { - // input stack: // [num1, num2] - dup2 dup2 and // [num1 & num2, num1, num2] - swap2 // [num2, num1, num1 & num2] - xor // [num2 ^ num1, num1 & num2] - 0x02 swap1 div // [num2 ^ num1 / 2, num1 & num2] - add // [sum] + // takes: [num2, num1] + 0x2 // [num2, num1, 0x2] + dup2 // [num2, num1, 0x2, num1] + dup4 // [num2, num1, 0x2, num1, num2] + xor // [num2, num1, 0x2, xor(num2, num1)] + div // [num2, num1, div(xor(num2, num1), 0x2)] + swap2 // [div(xor(num2, num1), 0x2), num1, num2] + and // [div(xor(num2, num1), 0x2), and(num2, num1)] + add // [avg] + // returns: [avg] } /// @notice Unsafely subtracts 1 from a uint256 using the 2's complement representation