/rust/registry/src/index.crates.io-1949cf8c6b5b557f/bnum-0.12.1/src/buint/cast.rs
Line | Count | Source |
1 | | macro_rules! decode_float { |
2 | | ($name: ident, $f: ty, $u: ty) => { |
3 | 0 | pub fn $name(f: $f) -> ($u, i16) { |
4 | | const BITS: u32 = core::mem::size_of::<$f>() as u32 * 8; |
5 | | const MANT_MASK: $u = <$u>::MAX >> (BITS - (<$f>::MANTISSA_DIGITS - 1)); |
6 | | const EXP_MASK: $u = <$u>::MAX >> 1; |
7 | | const BIAS: i16 = <$f>::MAX_EXP as i16 - 1; |
8 | | |
9 | 0 | let bits = f.to_bits(); |
10 | 0 | let exp = ((bits & EXP_MASK) >> (<$f>::MANTISSA_DIGITS - 1)) as i16; |
11 | 0 | let mut mant = bits & MANT_MASK; |
12 | 0 | if exp != 0 { |
13 | 0 | mant |= (1 << (<$f>::MANTISSA_DIGITS - 1)); |
14 | 0 | } |
15 | 0 | (mant, exp - (BIAS + <$f>::MANTISSA_DIGITS as i16 - 1)) |
16 | 0 | } Unexecuted instantiation: bnum::buint::cast::decode_f32 Unexecuted instantiation: bnum::buint::cast::decode_f64 |
17 | | }; |
18 | | } |
19 | | |
20 | | decode_float!(decode_f32, f32, u32); |
21 | | decode_float!(decode_f64, f64, u64); |
22 | | |
23 | | macro_rules! buint_as_int { |
24 | | ($BUint: ident, $Digit: ident; $($int: ty), *) => { |
25 | | $( |
26 | | impl<const N: usize> CastFrom<$BUint<N>> for $int { |
27 | | #[must_use = doc::must_use_op!()] |
28 | | #[inline] |
29 | 0 | fn cast_from(from: $BUint<N>) -> Self { |
30 | 0 | let mut out = 0; |
31 | 0 | let mut i = 0; |
32 | 0 | while i << crate::digit::$Digit::BIT_SHIFT < <$int>::BITS as usize && i < N { |
33 | 0 | out |= from.digits[i] as $int << (i << crate::digit::$Digit::BIT_SHIFT); |
34 | 0 | i += 1; |
35 | 0 | } |
36 | 0 | out |
37 | 0 | } Unexecuted instantiation: <u8 as bnum::cast::CastFrom<bnum::buint::BUintD8<_>>>::cast_from Unexecuted instantiation: <u16 as bnum::cast::CastFrom<bnum::buint::BUintD8<_>>>::cast_from Unexecuted instantiation: <u32 as bnum::cast::CastFrom<bnum::buint::BUintD8<_>>>::cast_from Unexecuted instantiation: <u64 as bnum::cast::CastFrom<bnum::buint::BUintD8<_>>>::cast_from Unexecuted instantiation: <u128 as bnum::cast::CastFrom<bnum::buint::BUintD8<_>>>::cast_from Unexecuted instantiation: <usize as bnum::cast::CastFrom<bnum::buint::BUintD8<_>>>::cast_from Unexecuted instantiation: <i8 as bnum::cast::CastFrom<bnum::buint::BUintD8<_>>>::cast_from Unexecuted instantiation: <i16 as bnum::cast::CastFrom<bnum::buint::BUintD8<_>>>::cast_from Unexecuted instantiation: <i32 as bnum::cast::CastFrom<bnum::buint::BUintD8<_>>>::cast_from Unexecuted instantiation: <i64 as bnum::cast::CastFrom<bnum::buint::BUintD8<_>>>::cast_from Unexecuted instantiation: <i128 as bnum::cast::CastFrom<bnum::buint::BUintD8<_>>>::cast_from Unexecuted instantiation: <isize as bnum::cast::CastFrom<bnum::buint::BUintD8<_>>>::cast_from Unexecuted instantiation: <u8 as bnum::cast::CastFrom<bnum::buint::BUintD16<_>>>::cast_from Unexecuted instantiation: <u16 as bnum::cast::CastFrom<bnum::buint::BUintD16<_>>>::cast_from Unexecuted instantiation: <u32 as bnum::cast::CastFrom<bnum::buint::BUintD16<_>>>::cast_from Unexecuted instantiation: <u64 as bnum::cast::CastFrom<bnum::buint::BUintD16<_>>>::cast_from Unexecuted instantiation: <u128 as bnum::cast::CastFrom<bnum::buint::BUintD16<_>>>::cast_from Unexecuted instantiation: <usize as bnum::cast::CastFrom<bnum::buint::BUintD16<_>>>::cast_from Unexecuted instantiation: <i8 as bnum::cast::CastFrom<bnum::buint::BUintD16<_>>>::cast_from Unexecuted instantiation: <i16 as bnum::cast::CastFrom<bnum::buint::BUintD16<_>>>::cast_from Unexecuted instantiation: <i32 as bnum::cast::CastFrom<bnum::buint::BUintD16<_>>>::cast_from Unexecuted instantiation: <i64 as bnum::cast::CastFrom<bnum::buint::BUintD16<_>>>::cast_from Unexecuted instantiation: <i128 as bnum::cast::CastFrom<bnum::buint::BUintD16<_>>>::cast_from Unexecuted instantiation: <isize as bnum::cast::CastFrom<bnum::buint::BUintD16<_>>>::cast_from Unexecuted instantiation: <u8 as bnum::cast::CastFrom<bnum::buint::BUint<_>>>::cast_from Unexecuted instantiation: <u16 as bnum::cast::CastFrom<bnum::buint::BUint<_>>>::cast_from Unexecuted instantiation: <u32 as bnum::cast::CastFrom<bnum::buint::BUint<_>>>::cast_from Unexecuted instantiation: <u8 as bnum::cast::CastFrom<bnum::buint::BUintD32<_>>>::cast_from Unexecuted instantiation: <u16 as bnum::cast::CastFrom<bnum::buint::BUintD32<_>>>::cast_from Unexecuted instantiation: <u32 as bnum::cast::CastFrom<bnum::buint::BUintD32<_>>>::cast_from Unexecuted instantiation: <u64 as bnum::cast::CastFrom<bnum::buint::BUintD32<_>>>::cast_from Unexecuted instantiation: <u128 as bnum::cast::CastFrom<bnum::buint::BUintD32<_>>>::cast_from Unexecuted instantiation: <usize as bnum::cast::CastFrom<bnum::buint::BUintD32<_>>>::cast_from Unexecuted instantiation: <i8 as bnum::cast::CastFrom<bnum::buint::BUintD32<_>>>::cast_from Unexecuted instantiation: <i16 as bnum::cast::CastFrom<bnum::buint::BUintD32<_>>>::cast_from Unexecuted instantiation: <i32 as bnum::cast::CastFrom<bnum::buint::BUintD32<_>>>::cast_from Unexecuted instantiation: <i64 as bnum::cast::CastFrom<bnum::buint::BUintD32<_>>>::cast_from Unexecuted instantiation: <i128 as bnum::cast::CastFrom<bnum::buint::BUintD32<_>>>::cast_from Unexecuted instantiation: <isize as bnum::cast::CastFrom<bnum::buint::BUintD32<_>>>::cast_from Unexecuted instantiation: <u64 as bnum::cast::CastFrom<bnum::buint::BUint<_>>>::cast_from Unexecuted instantiation: <u128 as bnum::cast::CastFrom<bnum::buint::BUint<_>>>::cast_from Unexecuted instantiation: <usize as bnum::cast::CastFrom<bnum::buint::BUint<_>>>::cast_from Unexecuted instantiation: <i8 as bnum::cast::CastFrom<bnum::buint::BUint<_>>>::cast_from Unexecuted instantiation: <i16 as bnum::cast::CastFrom<bnum::buint::BUint<_>>>::cast_from Unexecuted instantiation: <i32 as bnum::cast::CastFrom<bnum::buint::BUint<_>>>::cast_from Unexecuted instantiation: <i64 as bnum::cast::CastFrom<bnum::buint::BUint<_>>>::cast_from Unexecuted instantiation: <i128 as bnum::cast::CastFrom<bnum::buint::BUint<_>>>::cast_from Unexecuted instantiation: <isize as bnum::cast::CastFrom<bnum::buint::BUint<_>>>::cast_from |
38 | | } |
39 | | )* |
40 | | }; |
41 | | } |
42 | | |
43 | | macro_rules! buint_as_float { |
44 | | ($BUint: ident, $f: ty) => { |
45 | | impl<const N: usize> CastFrom<$BUint<N>> for $f { |
46 | | #[must_use = doc::must_use_op!()] |
47 | | #[inline] |
48 | 0 | fn cast_from(value: $BUint<N>) -> Self { |
49 | 0 | crate::cast::float::cast_float_from_uint(value) |
50 | 0 | } Unexecuted instantiation: <f32 as bnum::cast::CastFrom<bnum::buint::BUintD16<_>>>::cast_from Unexecuted instantiation: <f64 as bnum::cast::CastFrom<bnum::buint::BUintD16<_>>>::cast_from Unexecuted instantiation: <f32 as bnum::cast::CastFrom<bnum::buint::BUintD8<_>>>::cast_from Unexecuted instantiation: <f64 as bnum::cast::CastFrom<bnum::buint::BUintD8<_>>>::cast_from Unexecuted instantiation: <f32 as bnum::cast::CastFrom<bnum::buint::BUintD32<_>>>::cast_from Unexecuted instantiation: <f64 as bnum::cast::CastFrom<bnum::buint::BUintD32<_>>>::cast_from Unexecuted instantiation: <f32 as bnum::cast::CastFrom<bnum::buint::BUint<_>>>::cast_from Unexecuted instantiation: <f64 as bnum::cast::CastFrom<bnum::buint::BUint<_>>>::cast_from |
51 | | } |
52 | | }; |
53 | | } |
54 | | |
55 | | macro_rules! as_buint { |
56 | | ($BUint: ident, $Digit: ident; $($ty: ty), *) => { |
57 | | $( |
58 | | impl<const N: usize> CastFrom<$ty> for $BUint<N> { |
59 | | #[must_use = doc::must_use_op!()] |
60 | | #[inline] |
61 | 0 | fn cast_from(mut from: $ty) -> Self { |
62 | | #[allow(unused_comparisons)] |
63 | 0 | let mut out = if from < 0 { |
64 | 0 | Self::MAX |
65 | | } else { |
66 | 0 | Self::MIN |
67 | | }; |
68 | 0 | let mut i = 0; |
69 | 0 | while from != 0 && i < N { |
70 | 0 | let masked = from as $Digit & $Digit::MAX; |
71 | 0 | out.digits[i] = masked; |
72 | 0 | if <$ty>::BITS <= $Digit::BITS { |
73 | 0 | from = 0; |
74 | 0 | } else { |
75 | 0 | from = from.wrapping_shr($Digit::BITS); |
76 | 0 | } |
77 | 0 | i += 1; |
78 | | } |
79 | 0 | out |
80 | 0 | } Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::cast::CastFrom<i8>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::cast::CastFrom<i16>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::cast::CastFrom<i32>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::cast::CastFrom<i64>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::cast::CastFrom<i128>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::cast::CastFrom<isize>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::cast::CastFrom<i16>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::cast::CastFrom<i32>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::cast::CastFrom<i64>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::cast::CastFrom<i128>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::cast::CastFrom<isize>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::cast::CastFrom<u8>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::cast::CastFrom<u16>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::cast::CastFrom<u32>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::cast::CastFrom<u64>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::cast::CastFrom<u128>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::cast::CastFrom<usize>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::cast::CastFrom<i8>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::cast::CastFrom<u8>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::cast::CastFrom<u16>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::cast::CastFrom<u32>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::cast::CastFrom<u64>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::cast::CastFrom<u128>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::cast::CastFrom<usize>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::cast::CastFrom<i8>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::cast::CastFrom<i16>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::cast::CastFrom<i32>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::cast::CastFrom<i64>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::cast::CastFrom<i128>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::cast::CastFrom<isize>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::cast::CastFrom<u8>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::cast::CastFrom<u16>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::cast::CastFrom<u32>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::cast::CastFrom<u64>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::cast::CastFrom<u128>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::cast::CastFrom<usize>>::cast_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::cast::CastFrom<u8>>::cast_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::cast::CastFrom<u16>>::cast_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::cast::CastFrom<u32>>::cast_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::cast::CastFrom<u64>>::cast_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::cast::CastFrom<u128>>::cast_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::cast::CastFrom<usize>>::cast_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::cast::CastFrom<i8>>::cast_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::cast::CastFrom<i16>>::cast_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::cast::CastFrom<i32>>::cast_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::cast::CastFrom<i64>>::cast_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::cast::CastFrom<i128>>::cast_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::cast::CastFrom<isize>>::cast_from |
81 | | } |
82 | | )* |
83 | | }; |
84 | | } |
85 | | |
86 | | use crate::cast::CastFrom; |
87 | | use crate::doc; |
88 | | use crate::ExpType; |
89 | | #[allow(unused_imports)] |
90 | | use crate::cast::float::{FloatMantissa, CastUintFromFloatHelper, CastFloatFromUintHelper}; |
91 | | |
92 | | macro_rules! cast { |
93 | | ($BUint: ident, $BInt: ident, $Digit: ident) => { |
94 | | // #[cfg(feature = "float")] |
95 | | // impl<const N: usize> FloatMantissa for $BUint<N> { |
96 | | // const ZERO: Self = Self::ZERO; |
97 | | // const ONE: Self = Self::ONE; |
98 | | // const TWO: Self = Self::TWO; |
99 | | // const MAX: Self = Self::MAX; |
100 | | |
101 | | // #[inline] |
102 | | // fn leading_zeros(self) -> ExpType { |
103 | | // Self::leading_zeros(self) |
104 | | // } |
105 | | |
106 | | // #[inline] |
107 | | // fn checked_shr(self, n: ExpType) -> Option<Self> { |
108 | | // Self::checked_shr(self, n) |
109 | | // } |
110 | | |
111 | | // #[inline] |
112 | | // fn is_power_of_two(self) -> bool { |
113 | | // Self::is_power_of_two(self) |
114 | | // } |
115 | | // } |
116 | | |
117 | | impl<const N: usize> CastUintFromFloatHelper for $BUint<N> { |
118 | | const MAX: Self = Self::MAX; |
119 | | const MIN: Self = Self::MIN; |
120 | | } |
121 | | |
122 | | impl<const N: usize> CastFloatFromUintHelper for $BUint<N> { |
123 | 0 | fn trailing_zeros(self) -> ExpType { |
124 | 0 | Self::trailing_zeros(self) |
125 | 0 | } Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::cast::float::float_from_uint::CastFloatFromUintHelper>::trailing_zeros Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::cast::float::float_from_uint::CastFloatFromUintHelper>::trailing_zeros Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::cast::float::float_from_uint::CastFloatFromUintHelper>::trailing_zeros Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::cast::float::float_from_uint::CastFloatFromUintHelper>::trailing_zeros |
126 | | } |
127 | | |
128 | | impl<const N: usize> $BUint<N> { |
129 | | #[inline] |
130 | 0 | const fn cast_up<const M: usize>(self, digit: $Digit) -> $BUint<M> { |
131 | 0 | let mut digits = [digit; M]; |
132 | 0 | let mut i = M - N; |
133 | 0 | while i < M { |
134 | 0 | let index = i - (M - N); |
135 | 0 | digits[index] = self.digits[index]; |
136 | 0 | i += 1; |
137 | 0 | } |
138 | 0 | $BUint::from_digits(digits) |
139 | 0 | } Unexecuted instantiation: <bnum::buint::BUint<_>>::cast_up::<_> Unexecuted instantiation: <bnum::buint::BUintD16<_>>::cast_up::<_> Unexecuted instantiation: <bnum::buint::BUintD8<_>>::cast_up::<_> Unexecuted instantiation: <bnum::buint::BUintD32<_>>::cast_up::<_> |
140 | | |
141 | | #[inline] |
142 | 0 | const fn cast_down<const M: usize>(self) -> $BUint<M> { |
143 | 0 | let mut out = $BUint::ZERO; |
144 | 0 | let mut i = 0; |
145 | 0 | while i < M { |
146 | 0 | out.digits[i] = self.digits[i]; |
147 | 0 | i += 1; |
148 | 0 | } |
149 | 0 | out |
150 | 0 | } Unexecuted instantiation: <bnum::buint::BUint<_>>::cast_down::<_> Unexecuted instantiation: <bnum::buint::BUintD16<_>>::cast_down::<_> Unexecuted instantiation: <bnum::buint::BUintD8<_>>::cast_down::<_> Unexecuted instantiation: <bnum::buint::BUintD32<_>>::cast_down::<_> |
151 | | } |
152 | | |
153 | | buint_as_int!($BUint, $Digit; u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize); |
154 | | |
155 | | buint_as_float!($BUint, f32); |
156 | | buint_as_float!($BUint, f64); |
157 | | |
158 | | as_buint!($BUint, $Digit; u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize); |
159 | | |
160 | | impl<const N: usize> CastFrom<bool> for $BUint<N> { |
161 | | #[must_use = doc::must_use_op!()] |
162 | | #[inline] |
163 | 0 | fn cast_from(from: bool) -> Self { |
164 | 0 | if from { |
165 | 0 | Self::ONE |
166 | | } else { |
167 | 0 | Self::ZERO |
168 | | } |
169 | 0 | } Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::cast::CastFrom<bool>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::cast::CastFrom<bool>>::cast_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::cast::CastFrom<bool>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::cast::CastFrom<bool>>::cast_from |
170 | | } |
171 | | |
172 | | impl<const N: usize> CastFrom<char> for $BUint<N> { |
173 | | #[must_use = doc::must_use_op!()] |
174 | | #[inline] |
175 | 0 | fn cast_from(from: char) -> Self { |
176 | 0 | Self::cast_from(from as u32) |
177 | 0 | } Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::cast::CastFrom<char>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::cast::CastFrom<char>>::cast_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::cast::CastFrom<char>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::cast::CastFrom<char>>::cast_from |
178 | | } |
179 | | |
180 | | impl<const N: usize, const M: usize> CastFrom<$BUint<M>> for $BUint<N> { |
181 | | #[must_use = doc::must_use_op!()] |
182 | | #[inline] |
183 | 0 | fn cast_from(from: $BUint<M>) -> Self { |
184 | 0 | if M < N { |
185 | 0 | from.cast_up(0) |
186 | | } else { |
187 | 0 | from.cast_down() |
188 | | } |
189 | 0 | } Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::cast::CastFrom<bnum::buint::BUintD8<_>>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::cast::CastFrom<bnum::buint::BUintD16<_>>>::cast_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::cast::CastFrom<bnum::buint::BUint<_>>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::cast::CastFrom<bnum::buint::BUintD32<_>>>::cast_from |
190 | | } |
191 | | |
192 | | impl<const N: usize, const M: usize> CastFrom<$BInt<M>> for $BUint<N> { |
193 | | #[must_use = doc::must_use_op!()] |
194 | | #[inline] |
195 | 0 | fn cast_from(from: $BInt<M>) -> Self { |
196 | 0 | if M < N { |
197 | 0 | let padding_digit = if from.is_negative() { |
198 | 0 | $Digit::MAX |
199 | | } else { |
200 | 0 | 0 |
201 | | }; |
202 | 0 | from.to_bits().cast_up(padding_digit) |
203 | | } else { |
204 | 0 | from.to_bits().cast_down() |
205 | | } |
206 | 0 | } Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::cast::CastFrom<bnum::bint::BIntD8<_>>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::cast::CastFrom<bnum::bint::BIntD16<_>>>::cast_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::cast::CastFrom<bnum::bint::BInt<_>>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::cast::CastFrom<bnum::bint::BIntD32<_>>>::cast_from |
207 | | } |
208 | | |
209 | | impl<const N: usize> CastFrom<f32> for $BUint<N> { |
210 | | #[must_use = doc::must_use_op!()] |
211 | | #[inline] |
212 | 0 | fn cast_from(value: f32) -> Self { |
213 | 0 | crate::cast::float::cast_uint_from_float(value) |
214 | 0 | } Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::cast::CastFrom<f32>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::cast::CastFrom<f32>>::cast_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::cast::CastFrom<f32>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::cast::CastFrom<f32>>::cast_from |
215 | | } |
216 | | |
217 | | impl<const N: usize> CastFrom<f64> for $BUint<N> { |
218 | | #[must_use = doc::must_use_op!()] |
219 | | #[inline] |
220 | 0 | fn cast_from(value: f64) -> Self { |
221 | 0 | crate::cast::float::cast_uint_from_float(value) |
222 | 0 | } Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::cast::CastFrom<f64>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::cast::CastFrom<f64>>::cast_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::cast::CastFrom<f64>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::cast::CastFrom<f64>>::cast_from |
223 | | } |
224 | | }; |
225 | | } |
226 | | |
227 | | #[cfg(test)] |
228 | | crate::test::all_digit_tests! { |
229 | | crate::int::cast::tests!(utest); |
230 | | } |
231 | | |
232 | | crate::macro_impl!(cast); |
233 | | |
234 | | macro_rules! buint_as_different_digit_bigint { |
235 | | ($BUint: ident, $BInt: ident, $Digit: ident; $(($OtherBUint: ident, $OtherDigit: ident)), *) => { |
236 | | $( |
237 | | impl<const N: usize, const M: usize> crate::cast::CastFrom<$OtherBUint<M>> for $BUint<N> { |
238 | | #[must_use = doc::must_use_op!()] |
239 | | #[inline] |
240 | 0 | fn cast_from(from: $OtherBUint<M>) -> Self { |
241 | 0 | let mut out = Self::ZERO; |
242 | 0 | if $Digit::BITS < $OtherDigit::BITS { |
243 | | const DIVIDE_COUNT: usize = ($OtherDigit::BITS / $Digit::BITS) as usize; |
244 | 0 | let stop_index: usize = if <$OtherBUint<M>>::BITS > <$BUint<N>>::BITS { |
245 | 0 | N |
246 | | } else { |
247 | 0 | M * DIVIDE_COUNT |
248 | | }; |
249 | 0 | let mut i = 0; |
250 | 0 | while i < stop_index { |
251 | 0 | let wider_digit = from.digits[i / DIVIDE_COUNT]; |
252 | 0 | let mini_shift = i % DIVIDE_COUNT; |
253 | 0 | let digit = (wider_digit >> (mini_shift << digit::$Digit::BIT_SHIFT)) as $Digit; |
254 | 0 | out.digits[i] = digit; |
255 | 0 | i += 1; |
256 | 0 | } |
257 | | } else { |
258 | | const DIVIDE_COUNT: usize = ($Digit::BITS / $OtherDigit::BITS) as usize; |
259 | 0 | let stop_index: usize = if <$OtherBUint<M>>::BITS > <$BUint<N>>::BITS { |
260 | 0 | N * DIVIDE_COUNT |
261 | | } else { |
262 | 0 | M |
263 | | }; |
264 | 0 | let mut current_digit: $Digit = 0; |
265 | 0 | let mut i = 0; |
266 | 0 | while i < stop_index { |
267 | 0 | let mini_shift = i % DIVIDE_COUNT; |
268 | 0 | current_digit |= (from.digits[i] as $Digit) << (mini_shift << digit::$OtherDigit::BIT_SHIFT); |
269 | 0 | if mini_shift == DIVIDE_COUNT - 1 || i == stop_index - 1 { |
270 | 0 | out.digits[i / DIVIDE_COUNT] = current_digit; |
271 | 0 | current_digit = 0; |
272 | 0 | } |
273 | 0 | i += 1; |
274 | | } |
275 | | } |
276 | 0 | out |
277 | 0 | } Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::cast::CastFrom<bnum::buint::BUintD32<_>>>::cast_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::cast::CastFrom<bnum::buint::BUintD16<_>>>::cast_from Unexecuted instantiation: <bnum::buint::BUint<_> as bnum::cast::CastFrom<bnum::buint::BUintD8<_>>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::cast::CastFrom<bnum::buint::BUint<_>>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::cast::CastFrom<bnum::buint::BUintD16<_>>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD32<_> as bnum::cast::CastFrom<bnum::buint::BUintD8<_>>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::cast::CastFrom<bnum::buint::BUint<_>>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::cast::CastFrom<bnum::buint::BUintD32<_>>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD16<_> as bnum::cast::CastFrom<bnum::buint::BUintD8<_>>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::cast::CastFrom<bnum::buint::BUint<_>>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::cast::CastFrom<bnum::buint::BUintD32<_>>>::cast_from Unexecuted instantiation: <bnum::buint::BUintD8<_> as bnum::cast::CastFrom<bnum::buint::BUintD16<_>>>::cast_from |
278 | | } |
279 | | |
280 | | impl<const N: usize, const M: usize> crate::cast::CastFrom<$OtherBUint<M>> for $BInt<N> { |
281 | | #[must_use = doc::must_use_op!()] |
282 | | #[inline] |
283 | 0 | fn cast_from(from: $OtherBUint<M>) -> Self { |
284 | 0 | Self::from_bits($BUint::cast_from(from)) |
285 | 0 | } Unexecuted instantiation: <bnum::bint::BInt<_> as bnum::cast::CastFrom<bnum::buint::BUintD16<_>>>::cast_from Unexecuted instantiation: <bnum::bint::BInt<_> as bnum::cast::CastFrom<bnum::buint::BUintD8<_>>>::cast_from Unexecuted instantiation: <bnum::bint::BIntD32<_> as bnum::cast::CastFrom<bnum::buint::BUint<_>>>::cast_from Unexecuted instantiation: <bnum::bint::BIntD32<_> as bnum::cast::CastFrom<bnum::buint::BUintD16<_>>>::cast_from Unexecuted instantiation: <bnum::bint::BIntD32<_> as bnum::cast::CastFrom<bnum::buint::BUintD8<_>>>::cast_from Unexecuted instantiation: <bnum::bint::BInt<_> as bnum::cast::CastFrom<bnum::buint::BUintD32<_>>>::cast_from Unexecuted instantiation: <bnum::bint::BIntD16<_> as bnum::cast::CastFrom<bnum::buint::BUint<_>>>::cast_from Unexecuted instantiation: <bnum::bint::BIntD16<_> as bnum::cast::CastFrom<bnum::buint::BUintD32<_>>>::cast_from Unexecuted instantiation: <bnum::bint::BIntD16<_> as bnum::cast::CastFrom<bnum::buint::BUintD8<_>>>::cast_from Unexecuted instantiation: <bnum::bint::BIntD8<_> as bnum::cast::CastFrom<bnum::buint::BUint<_>>>::cast_from Unexecuted instantiation: <bnum::bint::BIntD8<_> as bnum::cast::CastFrom<bnum::buint::BUintD32<_>>>::cast_from Unexecuted instantiation: <bnum::bint::BIntD8<_> as bnum::cast::CastFrom<bnum::buint::BUintD16<_>>>::cast_from |
286 | | } |
287 | | )* |
288 | | } |
289 | | } |
290 | | |
291 | | pub(crate) use buint_as_different_digit_bigint; |