Thank you!
FWIW:
Onchain:
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.19;
contract Test {
uint256 public gasUsed;
function cAdd(uint256 x, uint256 y) external returns (uint256 z) { uint256 gasLeft = gasleft(); z = x + y; gasUsed = gasLeft - gasleft(); }
function cSub(uint256 x, uint256 y) external returns (uint256 z) { uint256 gasLeft = gasleft(); z = x - y; gasUsed = gasLeft - gasleft(); }
function cMul(uint256 x, uint256 y) external returns (uint256 z) { uint256 gasLeft = gasleft(); z = x * y; gasUsed = gasLeft - gasleft(); }
function cDiv(uint256 x, uint256 y) external returns (uint256 z) { uint256 gasLeft = gasleft(); z = x / y; gasUsed = gasLeft - gasleft(); }
function uAdd(uint256 x, uint256 y) external returns (uint256 z) {unchecked{uint256 gasLeft = gasleft(); z = x + y; gasUsed = gasLeft - gasleft();}}
function uSub(uint256 x, uint256 y) external returns (uint256 z) {unchecked{uint256 gasLeft = gasleft(); z = x - y; gasUsed = gasLeft - gasleft();}}
function uMul(uint256 x, uint256 y) external returns (uint256 z) {unchecked{uint256 gasLeft = gasleft(); z = x * y; gasUsed = gasLeft - gasleft();}}
function uDiv(uint256 x, uint256 y) external returns (uint256 z) {unchecked{uint256 gasLeft = gasleft(); z = x / y; gasUsed = gasLeft - gasleft();}}
function cAnd(uint256 x, uint256 y) external returns (uint256 z) { uint256 gasLeft = gasleft(); z = x & y; gasUsed = gasLeft - gasleft(); }
function cOr (uint256 x, uint256 y) external returns (uint256 z) { uint256 gasLeft = gasleft(); z = x | y; gasUsed = gasLeft - gasleft(); }
function cXor(uint256 x, uint256 y) external returns (uint256 z) { uint256 gasLeft = gasleft(); z = x ^ y; gasUsed = gasLeft - gasleft(); }
function cShl(uint256 x, uint8 y) external returns (uint256 z) { uint256 gasLeft = gasleft(); z = x << y; gasUsed = gasLeft - gasleft(); }
function cShr(uint256 x, uint8 y) external returns (uint256 z) { uint256 gasLeft = gasleft(); z = x >> y; gasUsed = gasLeft - gasleft(); }
function uAnd(uint256 x, uint256 y) external returns (uint256 z) {unchecked{uint256 gasLeft = gasleft(); z = x & y; gasUsed = gasLeft - gasleft();}}
function uOr (uint256 x, uint256 y) external returns (uint256 z) {unchecked{uint256 gasLeft = gasleft(); z = x | y; gasUsed = gasLeft - gasleft();}}
function uXor(uint256 x, uint256 y) external returns (uint256 z) {unchecked{uint256 gasLeft = gasleft(); z = x ^ y; gasUsed = gasLeft - gasleft();}}
function uShl(uint256 x, uint8 y) external returns (uint256 z) {unchecked{uint256 gasLeft = gasleft(); z = x << y; gasUsed = gasLeft - gasleft();}}
function uShr(uint256 x, uint8 y) external returns (uint256 z) {unchecked{uint256 gasLeft = gasleft(); z = x >> y; gasUsed = gasLeft - gasleft();}}
}
Offchain:
describe('Test', () => {
for (const test of [
{func: 'cAdd', x: 255, y: 255},
{func: 'cSub', x: 255, y: 255},
{func: 'cMul', x: 128, y: 128},
{func: 'cDiv', x: 255, y: 255},
{func: 'uAdd', x: 255, y: 255},
{func: 'uSub', x: 255, y: 255},
{func: 'uMul', x: 128, y: 128},
{func: 'uDiv', x: 255, y: 255},
{func: 'cAnd', x: 255, y: 255},
{func: 'cOr' , x: 255, y: 255},
{func: 'cXor', x: 255, y: 255},
{func: 'cShl', x: 255, y: 7},
{func: 'cShr', x: 255, y: 7},
{func: 'uAnd', x: 255, y: 255},
{func: 'uOr' , x: 255, y: 255},
{func: 'uXor', x: 255, y: 255},
{func: 'uShl', x: 255, y: 7},
{func: 'uShr', x: 255, y: 7},
]) {
it(`${test.func} gas cost`, async () => {
const contract = await Test.deploy();
const receipt = await contract[test.func](test.x, test.y);
const gasUsed = await contract.gasUsed();
console.log(gasUsed.toString());
});
}
});