|
1 | 1 | use int::{Int, LargeInt}; |
2 | 2 |
|
3 | | -macro_rules! udivmod_inner { |
4 | | - ($n:expr, $d:expr, $rem:expr, $ty:ty) => {{ |
5 | | - let (n, d, rem) = ($n, $d, $rem); |
6 | | - // NOTE X is unknown, K != 0 |
7 | | - if n.high() == 0 { |
8 | | - if d.high() == 0 { |
9 | | - // 0 X |
10 | | - // --- |
11 | | - // 0 X |
12 | | - |
13 | | - if let Some(rem) = rem { |
14 | | - *rem = <$ty>::from(n.low().aborting_rem(d.low())); |
15 | | - } |
16 | | - return <$ty>::from(n.low().aborting_div(d.low())) |
17 | | - } else { |
18 | | - // 0 X |
19 | | - // --- |
20 | | - // K X |
21 | | - if let Some(rem) = rem { |
22 | | - *rem = n; |
23 | | - } |
24 | | - return 0; |
25 | | - }; |
26 | | - } |
27 | | - |
28 | | - let mut sr; |
29 | | - let mut q; |
30 | | - let mut r; |
31 | | - |
32 | | - if d.low() == 0 { |
33 | | - if d.high() == 0 { |
34 | | - // K X |
35 | | - // --- |
36 | | - // 0 0 |
37 | | - // NOTE This should be unreachable in safe Rust because the program will panic before |
38 | | - // this intrinsic is called |
39 | | - ::abort(); |
40 | | - } |
41 | | - |
42 | | - if n.low() == 0 { |
43 | | - // K 0 |
44 | | - // --- |
45 | | - // K 0 |
46 | | - if let Some(rem) = rem { |
47 | | - *rem = <$ty>::from_parts(0, n.high().aborting_rem(d.high())); |
48 | | - } |
49 | | - return <$ty>::from(n.high().aborting_div(d.high())) |
50 | | - } |
51 | | - |
52 | | - // K K |
53 | | - // --- |
54 | | - // K 0 |
55 | | - |
56 | | - if d.high().is_power_of_two() { |
57 | | - if let Some(rem) = rem { |
58 | | - *rem = <$ty>::from_parts(n.low(), n.high() & (d.high() - 1)); |
59 | | - } |
60 | | - return <$ty>::from(n.high() >> d.high().trailing_zeros()); |
61 | | - } |
62 | | - |
63 | | - sr = d.high().leading_zeros().wrapping_sub(n.high().leading_zeros()); |
64 | | - |
65 | | - // D > N |
66 | | - if sr > <hty!($ty)>::BITS - 2 { |
67 | | - if let Some(rem) = rem { |
68 | | - *rem = n; |
69 | | - } |
70 | | - return 0; |
71 | | - } |
72 | | - |
73 | | - sr += 1; |
74 | | - |
75 | | - // 1 <= sr <= <hty!($ty)>::BITS - 1 |
76 | | - q = n << (<$ty>::BITS - sr); |
77 | | - r = n >> sr; |
78 | | - } else if d.high() == 0 { |
79 | | - // K X |
80 | | - // --- |
81 | | - // 0 K |
82 | | - if d.low().is_power_of_two() { |
83 | | - if let Some(rem) = rem { |
84 | | - *rem = <$ty>::from(n.low() & (d.low() - 1)); |
85 | | - } |
86 | | - |
87 | | - if d.low() == 1 { |
88 | | - return n; |
89 | | - } else { |
90 | | - let sr = d.low().trailing_zeros(); |
91 | | - return n >> sr; |
92 | | - }; |
93 | | - } |
94 | | - |
95 | | - sr = 1 + <hty!($ty)>::BITS + d.low().leading_zeros() - n.high().leading_zeros(); |
96 | | - |
97 | | - // 2 <= sr <= u64::BITS - 1 |
98 | | - q = n << (<$ty>::BITS - sr); |
99 | | - r = n >> sr; |
100 | | - } else { |
101 | | - // K X |
102 | | - // --- |
103 | | - // K K |
104 | | - sr = d.high().leading_zeros().wrapping_sub(n.high().leading_zeros()); |
105 | | - |
106 | | - // D > N |
107 | | - if sr > <hty!($ty)>::BITS - 1 { |
108 | | - if let Some(rem) = rem { |
109 | | - *rem = n; |
110 | | - } |
111 | | - return 0; |
112 | | - } |
113 | | - |
114 | | - sr += 1; |
115 | | - |
116 | | - // 1 <= sr <= <hty!($ty)>::BITS |
117 | | - q = n << (<$ty>::BITS - sr); |
118 | | - r = n >> sr; |
119 | | - } |
120 | | - |
121 | | - // Not a special case |
122 | | - // q and r are initialized with |
123 | | - // q = n << (u64::BITS - sr) |
124 | | - // r = n >> sr |
125 | | - // 1 <= sr <= u64::BITS - 1 |
126 | | - let mut carry = 0; |
127 | | - |
128 | | - // Don't use a range because they may generate references to memcpy in unoptimized code |
129 | | - let mut i = 0; |
130 | | - while i < sr { |
131 | | - i += 1; |
132 | | - |
133 | | - // r:q = ((r:q) << 1) | carry |
134 | | - r = (r << 1) | (q >> (<$ty>::BITS - 1)); |
135 | | - q = (q << 1) | carry as $ty; |
136 | | - |
137 | | - // carry = 0 |
138 | | - // if r >= d { |
139 | | - // r -= d; |
140 | | - // carry = 1; |
141 | | - // } |
142 | | - let s = (d.wrapping_sub(r).wrapping_sub(1)) as os_ty!($ty) >> (<$ty>::BITS - 1); |
143 | | - carry = (s & 1) as hty!($ty); |
144 | | - r -= d & s as $ty; |
145 | | - } |
146 | | - |
147 | | - if let Some(rem) = rem { |
148 | | - *rem = r; |
149 | | - } |
150 | | - (q << 1) | carry as $ty |
151 | | - }} |
152 | | -} |
153 | | - |
154 | 3 | intrinsics! { |
155 | 4 | #[maybe_use_optimized_c_shim] |
156 | 5 | #[arm_aeabi_alias = __aeabi_uidiv] |
|
0 commit comments