Skip to main content

fastnum/bint/uint/
num.rs

1use crate::bint::{
2    doc,
3    intrinsics::last_digit_index,
4    num::num_impl,
5    uint::{
6        intrinsics,
7        intrinsics::{Digit, Digits, ExpType},
8        math, powers,
9    },
10    Int, UInt,
11};
12
13num_impl!(UInt, U);
14
15impl<const N: usize> UInt<N> {
16    #[doc = doc::num::mul!(U 256)]
17    #[must_use = doc::must_use_op!()]
18    #[inline(always)]
19    pub const fn mul(self, rhs: Self) -> Self {
20        #[cfg(debug_assertions)]
21        return self.strict_mul(rhs);
22
23        #[cfg(not(debug_assertions))]
24        self.wrapping_mul(rhs)
25    }
26
27    #[doc = doc::num::div!(U 256)]
28    #[must_use = doc::must_use_op!()]
29    #[inline(always)]
30    pub const fn div(self, rhs: Self) -> Self {
31        math::div::div(self, rhs)
32    }
33
34    #[doc = doc::num::neg!(U 256)]
35    #[must_use = doc::must_use_op!()]
36    #[inline(always)]
37    pub const fn neg(self) -> Self {
38        #[cfg(debug_assertions)]
39        return self.strict_neg();
40
41        #[cfg(not(debug_assertions))]
42        self.wrapping_neg()
43    }
44
45    #[doc = doc::num::digits!(U 256)]
46    #[must_use = doc::must_use_op!()]
47    #[inline(always)]
48    pub const fn digits(&self) -> &Digits<N> {
49        self.0.digits()
50    }
51
52    #[doc = doc::num::digits_mut!(U 256)]
53    #[must_use = doc::must_use_op!()]
54    #[inline(always)]
55    pub fn digits_mut(&mut self) -> &mut Digits<N> {
56        self.0.digits_mut()
57    }
58
59    #[doc = doc::num::cast_signed!(256)]
60    #[must_use = doc::must_use_op!()]
61    #[inline(always)]
62    pub const fn cast_signed(self) -> Int<N> {
63        Int(self.0.cast_signed())
64    }
65
66    #[doc = doc::num::div_digit!(U 256)]
67    #[must_use = doc::must_use_op!()]
68    #[inline(always)]
69    pub const fn div_digit(self, rhs: Digit) -> Self {
70        math::div::div_digit(self, rhs)
71    }
72
73    #[doc = doc::num::div_rem!(U 256)]
74    #[must_use = doc::must_use_op!()]
75    #[inline(always)]
76    pub const fn div_rem(self, rhs: Self) -> (Self, Self) {
77        math::div::div_rem(self, rhs)
78    }
79
80    #[doc = doc::num::div_rem_digit!(U 256)]
81    #[must_use = doc::must_use_op!()]
82    #[inline(always)]
83    pub const fn div_rem_digit(self, rhs: Digit) -> (Self, Digit) {
84        math::div::div_rem_digit(self, rhs)
85    }
86
87    #[doc = doc::num::mul_div_rem!(U 256)]
88    #[must_use = doc::must_use_op!()]
89    #[inline(always)]
90    pub const fn mul_div_rem(self, rhs: Self, divisor: Self) -> (Self, Self) {
91        math::div::mul_div_rem(self, rhs, divisor)
92    }
93
94    #[doc = doc::num::mul_div_rem!(U 256)]
95    #[must_use = doc::must_use_op!()]
96    #[inline(always)]
97    pub const fn mul_div(self, rhs: Self, divisor: Self) -> Self {
98        math::div::mul_div(self, rhs, divisor)
99    }
100
101    #[doc = doc::num::from_digits!(U 256)]
102    #[must_use = doc::must_use_op!()]
103    #[inline(always)]
104    pub const fn from_digits(digits: Digits<N>) -> Self {
105        Self(bnum::BUint::from_digits(digits))
106    }
107
108    #[doc = doc::num::from_digit!(U 256)]
109    #[must_use = doc::must_use_op!()]
110    #[inline(always)]
111    pub const fn from_digit(digit: Digit) -> Self {
112        Self(bnum::BUint::from_digit(digit))
113    }
114
115    #[doc = doc::num::power_of_two!(U 256)]
116    #[must_use = doc::must_use_op!()]
117    #[inline(always)]
118    pub const fn power_of_two(power: ExpType) -> Self {
119        Self(bnum::BUint::power_of_two(power))
120    }
121
122    #[doc = doc::num::power_of_five!(U 256)]
123    #[must_use = doc::must_use_op!()]
124    #[inline(always)]
125    pub const fn power_of_five(power: ExpType) -> Self {
126        Self::strict_power_of_five(power)
127    }
128
129    #[doc = doc::num::power_of_ten!(U 256)]
130    #[must_use = doc::must_use_op!()]
131    #[inline(always)]
132    pub const fn power_of_ten(power: ExpType) -> Self {
133        Self::strict_power_of_ten(power)
134    }
135
136    #[doc = doc::num::abs_diff!(U 256)]
137    #[must_use = doc::must_use_op!()]
138    #[inline(always)]
139    pub const fn abs_diff(self, other: Self) -> Self {
140        Self(self.0.abs_diff(other.0))
141    }
142
143    #[doc = doc::num::mul_digit!(U 256)]
144    #[must_use = doc::must_use_op!()]
145    #[inline(always)]
146    pub const fn mul_digit(self, digit: u64) -> Self {
147        #[cfg(debug_assertions)]
148        return self.strict_mul_digit(digit);
149
150        #[cfg(not(debug_assertions))]
151        self.wrapping_mul_digit(digit)
152    }
153
154    #[doc = doc::num::decimal_digits!(U 256)]
155    #[must_use = doc::must_use_op!()]
156    #[inline(always)]
157    pub const fn decimal_digits(&self) -> ExpType {
158        powers::decimal_digits(self)
159    }
160
161    #[doc = doc::num::remaining_decimal_digits!(U 256)]
162    #[must_use = doc::must_use_op!()]
163    #[inline(always)]
164    pub const fn remaining_decimal_digits(&self) -> ExpType {
165        powers::remaining_decimal_digits(self)
166    }
167
168    #[doc = doc::num::can_scaled_by_power_of_ten!(U 256)]
169    #[must_use = doc::must_use_op!()]
170    #[inline(always)]
171    pub const fn can_scaled_by_power_of_ten(&self, power: ExpType) -> bool {
172        powers::can_scaled_by_power_of_ten(self, power)
173    }
174
175    #[inline(always)]
176    pub(crate) const fn last_digit_index(&self) -> usize {
177        last_digit_index(self.digits())
178    }
179
180    #[allow(unsafe_code)]
181    #[inline(always)]
182    pub(crate) const unsafe fn _transmute<const M: usize>(self) -> UInt<M> {
183        intrinsics::transmute(self)
184    }
185}