Skip to main content

fastnum/bint/int/
checked.rs

1use crate::bint::{
2    checked::checked_impl, doc, intrinsics::ExpType, utils::tuple_to_option, Int, UInt,
3};
4
5checked_impl!(Int, I);
6
7impl<const N: usize> Int<N> {
8    #[doc = doc::checked::checked_add_unsigned!(I 256)]
9    #[must_use = doc::must_use_op!()]
10    #[inline(always)]
11    pub const fn checked_add_unsigned(self, rhs: UInt<N>) -> Option<Self> {
12        tuple_to_option(self.overflowing_add_unsigned(rhs))
13    }
14
15    #[doc = doc::checked::checked_sub_unsigned!(I 256)]
16    #[must_use = doc::must_use_op!()]
17    #[inline(always)]
18    pub const fn checked_sub_unsigned(self, rhs: UInt<N>) -> Option<Self> {
19        tuple_to_option(self.overflowing_sub_unsigned(rhs))
20    }
21
22    #[doc = doc::checked::checked_mul!(I 256)]
23    #[must_use = doc::must_use_op!()]
24    #[inline(always)]
25    pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
26        tuple_to_option(self.overflowing_mul(rhs))
27    }
28
29    #[doc = doc::checked::checked_ilog2!(I 256)]
30    #[must_use = doc::must_use_op!()]
31    #[inline(always)]
32    pub const fn checked_ilog2(self) -> Option<ExpType> {
33        if self.is_negative() {
34            None
35        } else {
36            self.to_bits().checked_ilog2()
37        }
38    }
39
40    #[doc = doc::checked::checked_ilog10!(I 256)]
41    #[must_use = doc::must_use_op!()]
42    #[inline(always)]
43    pub const fn checked_ilog10(self) -> Option<ExpType> {
44        if self.is_negative() {
45            None
46        } else {
47            self.to_bits().checked_ilog10()
48        }
49    }
50
51    #[doc = doc::checked::checked_ilog!(I 256)]
52    #[must_use = doc::must_use_op!()]
53    #[inline(always)]
54    pub const fn checked_ilog(self, base: Self) -> Option<ExpType> {
55        if base.is_negative() || self.is_negative() {
56            None
57        } else {
58            self.to_bits().checked_ilog(base.to_bits())
59        }
60    }
61
62    #[doc = doc::checked::checked_abs!(I 256)]
63    #[must_use = doc::must_use_op!()]
64    #[inline]
65    pub const fn checked_abs(self) -> Option<Self> {
66        tuple_to_option(self.overflowing_abs())
67    }
68}