1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! Conversion from `u128` into [`U512`].
use super::U512;
/// Creates a [`U512`] from a `u128` value, placing the high 64 bits in
/// limb `w6` and the low 64 bits in limb `w7`, with the upper six limbs
/// set to zero.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u512::U512;
///
/// let v = U512::from(0x1_0000_0000_0000_0000u128);
/// assert_eq!(v, U512::from_be_limbs([0, 0, 0, 0, 0, 0, 1, 0]));
/// ```
impl From<u128> for U512 {
#[inline]
fn from(value: u128) -> Self {
Self::from_be_limbs([0, 0, 0, 0, 0, 0, (value >> 64) as u64, value as u64])
}
}
#[cfg(test)]
mod ai_tests {
use super::*;
/// Converting zero produces U512::ZERO.
#[test]
fn zero() {
assert_eq!(U512::from(0u128), U512::ZERO);
}
/// Converting one produces U512::ONE.
#[test]
fn one() {
assert_eq!(U512::from(1u128), U512::ONE);
}
/// Converting u128::MAX fills the lower two limbs.
#[test]
fn max_u128() {
assert_eq!(
U512::from(u128::MAX),
U512::from_be_limbs([0, 0, 0, 0, 0, 0, u64::MAX, u64::MAX]),
);
}
/// Value spanning both u64 halves of the u128.
#[test]
fn cross_limb() {
let v = U512::from(((5u128) << 64) | 7);
assert_eq!(v, U512::from_be_limbs([0, 0, 0, 0, 0, 0, 5, 7]));
}
}