1#[cfg(not(integer128))]
23#[cfg_attr(feature = "no-panic", inline)]
24pub fn umul128(a: u64, b: u64) -> (u64, u64) {
25 let a_lo = a as u32;
26 let a_hi = (a >> 32) as u32;
27 let b_lo = b as u32;
28 let b_hi = (b >> 32) as u32;
29
30 let b00 = a_lo as u64 * b_lo as u64;
31 let b01 = a_lo as u64 * b_hi as u64;
32 let b10 = a_hi as u64 * b_lo as u64;
33 let b11 = a_hi as u64 * b_hi as u64;
34
35 let b00_lo = b00 as u32;
36 let b00_hi = (b00 >> 32) as u32;
37
38 let mid1 = b10 + b00_hi as u64;
39 let mid1_lo = mid1 as u32;
40 let mid1_hi = (mid1 >> 32) as u32;
41
42 let mid2 = b01 + mid1_lo as u64;
43 let mid2_lo = mid2 as u32;
44 let mid2_hi = (mid2 >> 32) as u32;
45
46 let p_hi = b11 + mid1_hi as u64 + mid2_hi as u64;
47 let p_lo = ((mid2_lo as u64) << 32) | b00_lo as u64;
48
49 (p_lo, p_hi)
50}
51
52#[cfg(not(integer128))]
53#[cfg_attr(feature = "no-panic", inline)]
54pub fn shiftright128(lo: u64, hi: u64, dist: u32) -> u64 {
55 debug_assert!(dist > 0);
57 debug_assert!(dist < 64);
58 (hi << (64 - dist)) | (lo >> dist)
59}
60
61#[cfg_attr(feature = "no-panic", inline)]
62pub fn div5(x: u64) -> u64 {
63 x / 5
64}
65
66#[cfg_attr(feature = "no-panic", inline)]
67pub fn div10(x: u64) -> u64 {
68 x / 10
69}
70
71#[cfg_attr(feature = "no-panic", inline)]
72pub fn div100(x: u64) -> u64 {
73 x / 100
74}
75
76#[cfg_attr(feature = "no-panic", inline)]
77fn pow5_factor(mut value: u64) -> u32 {
78 let mut count = 0u32;
79 loop {
80 debug_assert!(value != 0);
81 let q = div5(value);
82 let r = (value as u32).wrapping_sub(5u32.wrapping_mul(q as u32));
83 if r != 0 {
84 break;
85 }
86 value = q;
87 count += 1;
88 }
89 count
90}
91
92#[cfg_attr(feature = "no-panic", inline)]
94pub fn multiple_of_power_of_5(value: u64, p: u32) -> bool {
95 pow5_factor(value) >= p
97}
98
99#[cfg_attr(feature = "no-panic", inline)]
101pub fn multiple_of_power_of_2(value: u64, p: u32) -> bool {
102 debug_assert!(value != 0);
103 (value & ((1u64 << p) - 1)) == 0
105}