/rust/registry/src/index.crates.io-1949cf8c6b5b557f/bnum-0.12.1/src/digit.rs
Line | Count | Source |
1 | | macro_rules! digit_module { |
2 | | ($Digit: ident, $SignedDigit: ty, $DoubleDigit: ty) => { |
3 | | pub mod $Digit { |
4 | | mod types { |
5 | | pub type Digit = $Digit; |
6 | | |
7 | | pub type SignedDigit = $SignedDigit; |
8 | | |
9 | | pub type DoubleDigit = $DoubleDigit; |
10 | | } |
11 | | |
12 | | use crate::ExpType; |
13 | | |
14 | | pub use types::*; |
15 | | |
16 | | pub const BITS: ExpType = $Digit::BITS as ExpType; |
17 | | |
18 | | pub const BITS_U8: u8 = BITS as u8; |
19 | | |
20 | | pub const BITS_MINUS_1: ExpType = BITS - 1; |
21 | | |
22 | | pub const BYTES: ExpType = BITS / 8; |
23 | | |
24 | | // This calculates log2 of BYTES as BYTES is guaranteed to only have one '1' bit, since it must be a power of two. |
25 | | pub const BYTE_SHIFT: ExpType = BYTES.trailing_zeros() as ExpType; |
26 | | |
27 | | pub const BIT_SHIFT: ExpType = BITS.trailing_zeros() as ExpType; |
28 | | |
29 | | #[inline] |
30 | 0 | pub const fn to_double_digit(low: Digit, high: Digit) -> DoubleDigit { |
31 | 0 | ((high as DoubleDigit) << BITS) | low as DoubleDigit |
32 | 0 | } Unexecuted instantiation: bnum::digit::u64::to_double_digit Unexecuted instantiation: bnum::digit::u8::to_double_digit Unexecuted instantiation: bnum::digit::u16::to_double_digit Unexecuted instantiation: bnum::digit::u32::to_double_digit Unexecuted instantiation: bnum::digit::u64::to_double_digit |
33 | | |
34 | | // TODO: these will no longer be necessary once const_bigint_helper_methods is stabilised: https://github.com/rust-lang/rust/issues/85532 |
35 | | |
36 | | #[inline] |
37 | 0 | pub const fn carrying_add(a: Digit, b: Digit, carry: bool) -> (Digit, bool) { |
38 | 0 | let (s1, o1) = a.overflowing_add(b); |
39 | 0 | if carry { |
40 | 0 | let (s2, o2) = s1.overflowing_add(1); |
41 | 0 | (s2, o1 || o2) |
42 | | } else { |
43 | 0 | (s1, o1) |
44 | | } |
45 | 0 | } Unexecuted instantiation: bnum::digit::u64::carrying_add Unexecuted instantiation: bnum::digit::u8::carrying_add Unexecuted instantiation: bnum::digit::u16::carrying_add Unexecuted instantiation: bnum::digit::u32::carrying_add Unexecuted instantiation: bnum::digit::u64::carrying_add |
46 | | |
47 | | #[inline] |
48 | 0 | pub const fn borrowing_sub(a: Digit, b: Digit, borrow: bool) -> (Digit, bool) { |
49 | 0 | let (s1, o1) = a.overflowing_sub(b); |
50 | 0 | if borrow { |
51 | 0 | let (s2, o2) = s1.overflowing_sub(1); |
52 | 0 | (s2, o1 || o2) |
53 | | } else { |
54 | 0 | (s1, o1) |
55 | | } |
56 | 0 | } Unexecuted instantiation: bnum::digit::u64::borrowing_sub Unexecuted instantiation: bnum::digit::u8::borrowing_sub Unexecuted instantiation: bnum::digit::u16::borrowing_sub Unexecuted instantiation: bnum::digit::u32::borrowing_sub Unexecuted instantiation: bnum::digit::u64::borrowing_sub |
57 | | |
58 | | #[inline] |
59 | 0 | pub const fn carrying_add_signed( |
60 | 0 | a: SignedDigit, |
61 | 0 | b: SignedDigit, |
62 | 0 | carry: bool, |
63 | 0 | ) -> (SignedDigit, bool) { |
64 | 0 | let (s1, o1) = a.overflowing_add(b); |
65 | 0 | if carry { |
66 | 0 | let (s2, o2) = s1.overflowing_add(1); |
67 | 0 | (s2, o1 != o2) |
68 | | } else { |
69 | 0 | (s1, o1) |
70 | | } |
71 | 0 | } Unexecuted instantiation: bnum::digit::u8::carrying_add_signed Unexecuted instantiation: bnum::digit::u16::carrying_add_signed Unexecuted instantiation: bnum::digit::u32::carrying_add_signed Unexecuted instantiation: bnum::digit::u64::carrying_add_signed |
72 | | |
73 | | #[inline] |
74 | 0 | pub const fn borrowing_sub_signed( |
75 | 0 | a: SignedDigit, |
76 | 0 | b: SignedDigit, |
77 | 0 | borrow: bool, |
78 | 0 | ) -> (SignedDigit, bool) { |
79 | 0 | let (s1, o1) = a.overflowing_sub(b); |
80 | 0 | if borrow { |
81 | 0 | let (s2, o2) = s1.overflowing_sub(1); |
82 | 0 | (s2, o1 != o2) |
83 | | } else { |
84 | 0 | (s1, o1) |
85 | | } |
86 | 0 | } Unexecuted instantiation: bnum::digit::u8::borrowing_sub_signed Unexecuted instantiation: bnum::digit::u16::borrowing_sub_signed Unexecuted instantiation: bnum::digit::u32::borrowing_sub_signed Unexecuted instantiation: bnum::digit::u64::borrowing_sub_signed |
87 | | |
88 | | #[inline] |
89 | 0 | pub const fn widening_mul(a: Digit, b: Digit) -> (Digit, Digit) { |
90 | 0 | let prod = a as DoubleDigit * b as DoubleDigit; |
91 | 0 | (prod as Digit, (prod >> BITS) as Digit) |
92 | 0 | } Unexecuted instantiation: bnum::digit::u64::widening_mul Unexecuted instantiation: bnum::digit::u8::widening_mul Unexecuted instantiation: bnum::digit::u16::widening_mul Unexecuted instantiation: bnum::digit::u32::widening_mul Unexecuted instantiation: bnum::digit::u64::widening_mul |
93 | | |
94 | | #[inline] |
95 | 0 | pub const fn carrying_mul( |
96 | 0 | a: Digit, |
97 | 0 | b: Digit, |
98 | 0 | carry: Digit, |
99 | 0 | current: Digit, |
100 | 0 | ) -> (Digit, Digit) { |
101 | 0 | let prod = carry as DoubleDigit |
102 | 0 | + current as DoubleDigit |
103 | 0 | + (a as DoubleDigit) * (b as DoubleDigit); |
104 | 0 | (prod as Digit, (prod >> BITS) as Digit) |
105 | 0 | } Unexecuted instantiation: bnum::digit::u64::carrying_mul Unexecuted instantiation: bnum::digit::u8::carrying_mul Unexecuted instantiation: bnum::digit::u16::carrying_mul Unexecuted instantiation: bnum::digit::u32::carrying_mul Unexecuted instantiation: bnum::digit::u64::carrying_mul |
106 | | |
107 | | #[inline] |
108 | 0 | pub const fn div_rem_wide(low: Digit, high: Digit, rhs: Digit) -> (Digit, Digit) { |
109 | 0 | debug_assert!(high < rhs); |
110 | | |
111 | 0 | let a = to_double_digit(low, high); |
112 | 0 | ( |
113 | 0 | (a / rhs as DoubleDigit) as Digit, |
114 | 0 | (a % rhs as DoubleDigit) as Digit, |
115 | 0 | ) |
116 | 0 | } Unexecuted instantiation: bnum::digit::u64::div_rem_wide Unexecuted instantiation: bnum::digit::u8::div_rem_wide Unexecuted instantiation: bnum::digit::u16::div_rem_wide Unexecuted instantiation: bnum::digit::u32::div_rem_wide Unexecuted instantiation: bnum::digit::u64::div_rem_wide |
117 | | |
118 | | pub const HEX_PADDING: usize = BITS as usize / 4; |
119 | | } |
120 | | }; |
121 | | } |
122 | | |
123 | | digit_module!(u8, i8, u16); |
124 | | digit_module!(u16, i16, u32); |
125 | | digit_module!(u32, i32, u64); |
126 | | digit_module!(u64, i64, u128); |