1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//! Arithmetic functions for the Fuel VM

use fuel_asm::{
    PanicReason,
    Word,
};

/// Add two unchecked words, returning an error if overflow
#[inline(always)]
pub fn checked_add_word(a: Word, b: Word) -> Result<Word, PanicReason> {
    a.checked_add(b).ok_or(PanicReason::ArithmeticOverflow)
}

/// Subtract two unchecked words, returning an error if overflow
#[inline(always)]
pub fn checked_sub_word(a: Word, b: Word) -> Result<Word, PanicReason> {
    a.checked_sub(b).ok_or(PanicReason::ArithmeticOverflow)
}

/// Add two unchecked numbers, returning an error if overflow
#[inline(always)]
pub fn checked_add_usize(a: usize, b: usize) -> Result<usize, PanicReason> {
    a.checked_add(b).ok_or(PanicReason::ArithmeticOverflow)
}

/// Subtract two unchecked numbers, returning an error if overflow
#[inline(always)]
pub fn checked_sub_usize(a: usize, b: usize) -> Result<usize, PanicReason> {
    a.checked_sub(b).ok_or(PanicReason::ArithmeticOverflow)
}