/// a rewrite of safe add with max type
/// @notice Adds two numbers and reverts on overflow
#define macro SAFE_ADD_TYPE() = takes (3) returns (1) {
// input stack // [num1, num2, typeMax]
dup2 // [num2, num1, num2, typeMax]
add // [result, num2, typeMax]
dup1 // [result, result, num2, typeMax]
swap2 // [num2, result, result, typeMax]
gt // [is_overflow, result, typeMax]
iszero // [is_not_overflow, result, typeMax]
is_not_overflow jumpi // [result, typeMax]
[ARITHMETIC_OVERFLOW] PANIC()
is_not_overflow: // [result, typeMax]
swap1 // [typeMax, result]
dup2 // [result, typeMax, result]
gt // [is_type_overflow, result]
iszero // [is_not_type_overflow, result]
is_not_type_overflow jumpi // [result]
[ARITHMETIC_OVERFLOW] PANIC()
is_not_type_overflow:
}
#include "utils/Constants.huff"
...
#define macro ADD_UINT128() = takes(0) returns(1) {
[__UINT128_MAX] // [Uint128Max]
0x01 // [0x01, uint128Max]
0x02 // [0x02, 0x01, uint128Max]
SAFE_ADD_TYPE() // [result]
}