Skip to content

Conversation

@Dentosal
Copy link
Member

@Dentosal Dentosal commented Mar 7, 2025

Closes #626. VM PRs: FuelLabs/fuel-vm#922 and FuelLabs/fuel-vm#923.

Implementing correct overflow handling for narrow integer types (u8, u16, u32) is tedious, error prone, and needs lots of instructions. Likewise, memory load/store operations with these involve lots of expensive bitmasking. This change provides the functionality on the vm level.

Only operations for which the overflow handling is different with narrow integers are implemented. For instance, multiplication is definitely different as 256 * 2 overflows u8 but doesn't overflow u64. In comparison, division is not, it never creates results larger than the original value.

The operations silently truncate the input operands to the correct bit width, meaning that niop $ra $rb $rc NIOP_U8 | NIOP_ADD with $rb = 0xff01, $rc = 0x02 sets $ra = 3 without panicking. Same goes for store operations, and that was already the case with SB. It's up to the compiler or user to take this into account.

As always I'm trying to sneak in cool new features. Here it's the XNOR operating mode, which is extremely useful for generating bitmasks. For instance niop $ra $zero $zero NIOP_U32 | NIOP_XNOR sets $ra to 0xffffffff.

Before requesting review

  • I have reviewed the code myself

After merging, notify other teams

@Dentosal Dentosal added the comp:FVM Component: FuelVM label Mar 7, 2025
@Dentosal Dentosal self-assigned this Mar 7, 2025
@Dentosal Dentosal requested review from a team March 7, 2025 14:35
@Dentosal Dentosal merged commit cad8e69 into master Mar 11, 2025
6 checks passed
@Dentosal Dentosal deleted the dento/narrow-int-instructions branch March 11, 2025 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp:FVM Component: FuelVM

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New instructions for manipulating mid-to-small sized types

3 participants