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