plugy_core/bitwise.rs
1/// Converts a 64-bit value into a pair of 32-bit values using bitwise operations.
2///
3/// This function takes a 64-bit unsigned integer `value` and extracts two 32-bit
4/// unsigned integers from it using bitwise operations. The lower 32 bits of the
5/// input value are extracted as the first 32-bit value, and the upper 32 bits are
6/// extracted as the second 32-bit value.
7///
8/// # Arguments
9///
10/// * `value` - The 64-bit value from which to extract the 32-bit values.
11///
12/// # Returns
13///
14/// A tuple containing two 32-bit unsigned integers. The first element of the tuple
15/// is the lower 32 bits of the input value, and the second element is the upper 32
16/// bits of the input value.
17///
18/// # Examples
19///
20/// ```
21/// use plugy_core::bitwise::from_bitwise;
22/// let value: u64 = 0x0000_1234_5678_9ABC;
23/// let (lower, upper) = from_bitwise(value);
24/// assert_eq!(lower, 0x5678_9ABC);
25/// assert_eq!(upper, 0x0000_1234);
26/// ```
27#[inline(always)]
28pub const fn from_bitwise(value: u64) -> (u32, u32) {
29 ((value << 32 >> 32) as u32, (value >> 32) as u32)
30}
31
32/// Combines two 32-bit values into a single 64-bit value using bitwise operations.
33///
34/// This function takes two 32-bit unsigned integers, `a` and `b`, and combines them
35/// into a single 64-bit unsigned integer using bitwise operations. The value `a` is
36/// stored in the lower 32 bits of the result, and the value `b` is stored in the upper
37/// 32 bits.
38///
39/// # Arguments
40///
41/// * `a` - The lower 32 bits of the resulting 64-bit value.
42/// * `b` - The upper 32 bits of the resulting 64-bit value.
43///
44/// # Returns
45///
46/// A 64-bit unsigned integer obtained by combining the input values `a` and `b`
47/// using bitwise OR and left shift operations.
48///
49/// # Examples
50///
51/// ```
52/// use plugy_core::bitwise::into_bitwise;
53/// let a: u32 = 0x5678_9ABC;
54/// let b: u32 = 0x0000_1234;
55/// let combined = into_bitwise(a, b);
56/// assert_eq!(combined, 0x0000_1234_5678_9ABC);
57/// ```
58#[inline(always)]
59pub const fn into_bitwise(a: u32, b: u32) -> u64 {
60 (a as u64) | (b as u64) << 32
61}
62
63
64#[cfg(test)]
65mod test {
66 use super::*;
67
68 #[test]
69 fn bitwise() {
70 const DATA: (u32, u32) = (10, 20);
71 const INTO: u64 = into_bitwise(DATA.0, DATA.1);
72 const FROM: (u32, u32) = from_bitwise(INTO);
73 assert_eq!(DATA, FROM)
74 }
75}