Skip to content

Commit d6b4494

Browse files
authored
UintRef { limbs: ... } (#1129)
Changes `UintRef` from a tuple struct to having a `limbs` field like every other `*Uint` type. I don't know why I made it a tuple struct in the first place but now the inconsistency is driving me insane.
1 parent 8e3092c commit d6b4494

14 files changed

Lines changed: 206 additions & 164 deletions

File tree

src/uint/encoding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ impl RadixDivisionParams {
845845
remain_mut
846846
.leading_mut(RADIX_ENCODING_LIMBS_LARGE - 1)
847847
.copy_from(limbs_rem);
848-
remain_mut.0[RADIX_ENCODING_LIMBS_LARGE - 1] = rem_high;
848+
remain_mut.limbs[RADIX_ENCODING_LIMBS_LARGE - 1] = rem_high;
849849
remain_mut.shr_assign_limb_vartime(self.shift_large);
850850

851851
(remain_mut, out_idx.saturating_sub(self.digits_large))

src/uint/mul.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,14 @@ pub(crate) const fn wrapping_mul_overflow(
261261
let mut j = lhs.nlimbs();
262262
let mut k = rhs.nlimbs().saturating_sub(1);
263263
while k > j {
264-
rhs_tail = rhs_tail.bitor(rhs.0[k]);
264+
rhs_tail = rhs_tail.bitor(rhs.limbs[k]);
265265
k -= 1;
266266
}
267267
while i < lhs.nlimbs() {
268268
j = lhs.nlimbs() - i;
269269
if j < rhs.nlimbs() {
270-
rhs_tail = rhs_tail.bitor(rhs.0[j]);
271-
overflow = overflow.or(lhs.0[i].is_nonzero().and(rhs_tail.is_nonzero()));
270+
rhs_tail = rhs_tail.bitor(rhs.limbs[j]);
271+
overflow = overflow.or(lhs.limbs[i].is_nonzero().and(rhs_tail.is_nonzero()));
272272
}
273273
i += 1;
274274
}

src/uint/mul/karatsuba.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ pub const fn widening_mul_fixed<const LHS: usize, const RHS: usize>(
6464
let (mut l0c, mut l1c) = (Limb::ZERO, Limb::ZERO);
6565
let mut i = 0;
6666
while i < HALF {
67-
(l0.limbs[i], l0c) = x0.0[i].carrying_add(x1.0[i], l0c);
68-
(l1.limbs[i], l1c) = y0.0[i].carrying_add(y1.0[i], l1c);
67+
(l0.limbs[i], l0c) = x0.limbs[i].carrying_add(x1.limbs[i], l0c);
68+
(l1.limbs[i], l1c) = y0.limbs[i].carrying_add(y1.limbs[i], l1c);
6969
i += 1;
7070
}
7171
let z1 = widening_mul_fixed(l0.as_uint_ref(), l1.as_uint_ref());
@@ -456,7 +456,7 @@ pub(crate) const fn wrapping_square(uint: &UintRef, out: &mut UintRef) -> Limb {
456456
if tail.is_empty() {
457457
Limb::ZERO
458458
} else {
459-
tail.0[0]
459+
tail.limbs[0]
460460
}
461461
} else {
462462
let (z01, z2) = hi.split_at_mut(LIMBS);

src/uint/ref_type.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ use {crate::BoxedUint, alloc::borrow::ToOwned};
2727
/// thus provides an abstraction for writing shared implementations.
2828
#[repr(transparent)]
2929
#[derive(PartialEq, Eq)]
30-
pub struct UintRef(pub(crate) [Limb]);
30+
pub struct UintRef {
31+
/// Inner limb array. Stored from least significant to most significant.
32+
// TODO(tarcieri): maintain an invariant of at least one limb?
33+
pub(crate) limbs: [Limb],
34+
}
3135

3236
impl UintRef {
3337
/// Create a [`UintRef`] reference type from a [`Limb`] slice.
@@ -70,55 +74,55 @@ impl UintRef {
7074
#[inline]
7175
#[must_use]
7276
pub const fn as_limbs(&self) -> &[Limb] {
73-
&self.0
77+
&self.limbs
7478
}
7579

7680
/// Mutably borrow the inner `&mut [Limb]` slice.
7781
#[inline]
7882
pub const fn as_mut_limbs(&mut self) -> &mut [Limb] {
79-
&mut self.0
83+
&mut self.limbs
8084
}
8185

8286
/// Borrow the inner limbs as a slice of [`Word`]s.
8387
#[inline]
8488
#[must_use]
8589
pub const fn as_words(&self) -> &[Word] {
86-
Limb::slice_as_words(&self.0)
90+
Limb::slice_as_words(&self.limbs)
8791
}
8892

8993
/// Borrow the inner limbs as a mutable slice of [`Word`]s.
9094
#[inline]
9195
pub const fn as_mut_words(&mut self) -> &mut [Word] {
92-
Limb::slice_as_mut_words(&mut self.0)
96+
Limb::slice_as_mut_words(&mut self.limbs)
9397
}
9498

9599
/// Get an iterator over the inner limbs.
96100
#[inline]
97101
#[must_use]
98102
pub fn iter(&self) -> impl DoubleEndedIterator<Item = &Limb> {
99-
self.0.iter()
103+
self.limbs.iter()
100104
}
101105

102106
/// Get a mutable iterator over the inner limbs.
103107
#[inline]
104108
#[allow(dead_code)] // TODO(tarcieri): use this
105109
pub fn iter_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut Limb> {
106-
self.0.iter_mut()
110+
self.limbs.iter_mut()
107111
}
108112

109113
/// Access the number of limbs.
110114
#[inline]
111115
#[must_use]
112116
pub const fn nlimbs(&self) -> usize {
113-
self.0.len()
117+
self.limbs.len()
114118
}
115119

116120
/// Conditionally assign all of the limbs to zero.
117121
#[inline(always)]
118122
pub const fn conditional_set_zero(&mut self, choice: Choice) {
119123
let mut i = 0;
120124
while i < self.nlimbs() {
121-
self.0[i] = Limb::select(self.0[i], Limb::ZERO, choice);
125+
self.limbs[i] = Limb::select(self.limbs[i], Limb::ZERO, choice);
122126
i += 1;
123127
}
124128
}
@@ -128,7 +132,7 @@ impl UintRef {
128132
pub const fn conditional_set_max(&mut self, choice: Choice) {
129133
let mut i = 0;
130134
while i < self.nlimbs() {
131-
self.0[i] = Limb::select(self.0[i], Limb::MAX, choice);
135+
self.limbs[i] = Limb::select(self.limbs[i], Limb::MAX, choice);
132136
i += 1;
133137
}
134138
}
@@ -144,7 +148,7 @@ impl UintRef {
144148
};
145149
let mut i = 0;
146150
while i < len {
147-
out.limbs[i] = self.0[i];
151+
out.limbs[i] = self.limbs[i];
148152
i += 1;
149153
}
150154
out
@@ -164,11 +168,11 @@ impl UintRef {
164168
#[inline]
165169
#[must_use]
166170
pub const fn to_mut_nz_ref(&mut self) -> CtOption<&mut NonZero<Self>> {
167-
assert!(!self.0.is_empty(), "cannot be used with empty slices");
171+
assert!(!self.limbs.is_empty(), "cannot be used with empty slices");
168172
let is_nz = self.is_nonzero();
169173

170174
// If `self` is zero, set the first limb to `1` to make it non-zero.
171-
self.0[0] = Limb::select(Limb::ONE, self.0[0], is_nz);
175+
self.limbs[0] = Limb::select(Limb::ONE, self.limbs[0], is_nz);
172176

173177
CtOption::new(self.as_mut_nz_unchecked(), is_nz)
174178
}
@@ -234,11 +238,11 @@ impl UintRef {
234238
#[inline]
235239
#[must_use]
236240
pub const fn to_mut_odd_ref(&mut self) -> CtOption<&mut Odd<Self>> {
237-
assert!(!self.0.is_empty(), "cannot be used with empty slices");
241+
assert!(!self.limbs.is_empty(), "cannot be used with empty slices");
238242
let is_odd = self.is_odd();
239243

240244
// If `self` is even, set the first limb to `1` to make it odd.
241-
self.0[0] = Limb::select(Limb::ONE, self.0[0], is_odd);
245+
self.limbs[0] = Limb::select(Limb::ONE, self.limbs[0], is_odd);
242246

243247
CtOption::new(self.as_mut_odd_unchecked(), is_odd)
244248
}
@@ -296,18 +300,18 @@ impl UintRef {
296300
#[cfg(target_pointer_width = "32")]
297301
{
298302
debug_assert!(self.nlimbs() >= 1);
299-
let mut ret = self.0[0].0 as u64;
303+
let mut ret = self.limbs[0].0 as u64;
300304

301305
if self.nlimbs() >= 2 {
302-
ret |= (self.0[1].0 as u64) << 32;
306+
ret |= (self.limbs[1].0 as u64) << 32;
303307
}
304308

305309
ret
306310
}
307311

308312
#[cfg(target_pointer_width = "64")]
309313
{
310-
self.0[0].0
314+
self.limbs[0].0
311315
}
312316
}
313317
}
@@ -331,14 +335,14 @@ impl Index<usize> for UintRef {
331335

332336
#[inline]
333337
fn index(&self, index: usize) -> &Limb {
334-
self.0.index(index)
338+
self.limbs.index(index)
335339
}
336340
}
337341

338342
impl IndexMut<usize> for UintRef {
339343
#[inline]
340344
fn index_mut(&mut self, index: usize) -> &mut Limb {
341-
self.0.index_mut(index)
345+
self.limbs.index_mut(index)
342346
}
343347
}
344348

src/uint/ref_type/add.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ impl UintRef {
77
#[track_caller]
88
pub const fn add_assign_limb(&mut self, mut rhs: Limb) -> Limb {
99
let mut i = 0;
10-
while i < self.0.len() {
11-
(self.0[i], rhs) = self.0[i].overflowing_add(rhs);
10+
while i < self.limbs.len() {
11+
(self.limbs[i], rhs) = self.limbs[i].overflowing_add(rhs);
1212
i += 1;
1313
}
1414
rhs
@@ -18,7 +18,7 @@ impl UintRef {
1818
#[inline]
1919
#[track_caller]
2020
pub const fn carrying_add_assign(&mut self, rhs: &Self, carry: Limb) -> Limb {
21-
self.carrying_add_assign_slice(&rhs.0, carry)
21+
self.carrying_add_assign_slice(&rhs.limbs, carry)
2222
}
2323

2424
/// Perform an in-place carrying add of another limb slice, returning the carried limb value.
@@ -29,12 +29,12 @@ impl UintRef {
2929
#[track_caller]
3030
pub const fn carrying_add_assign_slice(&mut self, rhs: &[Limb], mut carry: Limb) -> Limb {
3131
assert!(
32-
self.0.len() == rhs.len(),
32+
self.limbs.len() == rhs.len(),
3333
"length mismatch in carrying_add_assign_slice"
3434
);
3535
let mut i = 0;
36-
while i < self.0.len() {
37-
(self.0[i], carry) = self.0[i].carrying_add(rhs[i], carry);
36+
while i < self.limbs.len() {
37+
(self.limbs[i], carry) = self.limbs[i].carrying_add(rhs[i], carry);
3838
i += 1;
3939
}
4040
carry
@@ -65,13 +65,13 @@ impl UintRef {
6565
choice: Choice,
6666
) -> Limb {
6767
assert!(
68-
self.0.len() == rhs.len(),
68+
self.limbs.len() == rhs.len(),
6969
"length mismatch in conditional_add_assign_slice"
7070
);
7171
let mut i = 0;
72-
while i < self.0.len() {
73-
(self.0[i], carry) =
74-
self.0[i].carrying_add(Limb::select(Limb::ZERO, rhs[i], choice), carry);
72+
while i < self.limbs.len() {
73+
(self.limbs[i], carry) =
74+
self.limbs[i].carrying_add(Limb::select(Limb::ZERO, rhs[i], choice), carry);
7575
i += 1;
7676
}
7777
carry

0 commit comments

Comments
 (0)