use fuel_asm::{PanicReason, Word};
use crate::error::RuntimeError;
#[inline(always)]
pub fn checked_add_word(a: Word, b: Word) -> Result<Word, PanicReason> {
a.checked_add(b).ok_or(PanicReason::ArithmeticOverflow)
}
#[inline(always)]
pub fn checked_sub_word(a: Word, b: Word) -> Result<Word, PanicReason> {
a.checked_sub(b).ok_or(PanicReason::ArithmeticOverflow)
}
#[inline(always)]
pub fn checked_add_usize(a: usize, b: usize) -> Result<usize, PanicReason> {
a.checked_add(b).ok_or(PanicReason::ArithmeticOverflow)
}
#[inline(always)]
pub fn checked_sub_usize(a: usize, b: usize) -> Result<usize, PanicReason> {
a.checked_sub(b).ok_or(PanicReason::ArithmeticOverflow)
}
#[inline(always)]
pub fn add_word(a: Word, b: Word) -> Result<Word, RuntimeError> {
#[cfg(feature = "optimized")]
#[allow(clippy::integer_arithmetic)]
return Ok(a + b);
#[cfg(not(feature = "optimized"))]
return a
.checked_add(b)
.ok_or_else(|| RuntimeError::unexpected_behavior("unexpected overflow"));
}
#[inline(always)]
pub fn sub_word(a: Word, b: Word) -> Result<Word, RuntimeError> {
#[cfg(feature = "optimized")]
#[allow(clippy::integer_arithmetic)]
return Ok(a - b);
#[cfg(not(feature = "optimized"))]
return a
.checked_sub(b)
.ok_or_else(|| RuntimeError::unexpected_behavior("unexpected underflow"));
}
#[inline(always)]
#[allow(clippy::integer_arithmetic)]
pub const fn add_usize(a: usize, b: usize) -> usize {
a + b
}