fuel-vm icon indicating copy to clipboard operation
fuel-vm copied to clipboard

Narrow integer arithmetic instructions

Open Dentosal opened this issue 9 months ago • 2 comments

Spec PR: https://github.com/FuelLabs/fuel-specs/pull/628

Adds NIOP r r r imm, an instruction to perform narrow integer (u8, u16, u32) operations with exact overflow checking. The instruction only supports operations for which the current u64 operations do not check overflow correctly.

Alternatives

We could instead add an operation that truncates the result of the previous u64 operation, asserting the flags-related conditions. Not sure if that would be any cleaner. The current approach is more compact, but further complicates the instruction set.

Checklist

  • [x] Breaking changes are clearly marked as such in the PR description and changelog: none!
  • [x] New behavior is reflected in tests
  • [ ] If performance characteristic of an instruction change, update gas costs as well or make a follow-up PR for that
  • [x] The specification matches the implemented behavior: https://github.com/FuelLabs/fuel-specs/pull/628

Before requesting review

  • [ ] I have reviewed the code myself
  • [ ] I have created follow-up issues caused by this PR and linked them here

Dentosal avatar Mar 06 '25 01:03 Dentosal

What is the state of this PR? Also, do we have any benchmarks that shows how it improves the size of contracts? OR does it requires re-work of STD library to actually start using it?

xgreenx avatar Apr 06 '25 02:04 xgreenx

What is the state of this PR? Also, do we have any benchmarks that shows how it improves the size of contracts? OR does it requires re-work of STD library to actually start using it?

This PR is waiting for #923. Requires some minor rework of sway std, which I haven't finished yet. But the purpose of this PR isn't really to reduce code size of contracts, but to get the edge cases right. The sway team has had trouble implementing all overflow checking and FLAGS-related complexities cleanly.

Dentosal avatar Apr 07 '25 07:04 Dentosal