Skip to content

Commit 422eb29

Browse files
authored
arrow-buffer: add i256::trailing_zeros (#8969)
# Which issue does this PR close? - Closes #8968. # Rationale for this change - Add `i256::trailing_zeros` to align with other integer types - e.g. [`i128::trailing_zeros`](https://doc.rust-lang.org/std/primitive.i128.html#method.trailing_zeros) - Add function documentation - Add unit tests # What changes are included in this PR? Function, docs, unit tests # Are these changes tested? New unit tests # Are there any user-facing changes? New function in `i256` struct
1 parent 5375411 commit 422eb29

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

arrow-buffer/src/bigint/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,14 @@ impl i256 {
590590
}
591591
}
592592

593+
/// Returns the number of trailing zeros in the binary representation of this [`i256`].
594+
pub const fn trailing_zeros(&self) -> u32 {
595+
match self.low {
596+
0 => u128::BITS + self.high.trailing_zeros(),
597+
_ => self.low.trailing_zeros(),
598+
}
599+
}
600+
593601
fn redundant_leading_sign_bits_i256(n: i256) -> u8 {
594602
let mask = n >> 255; // all ones or all zeros
595603
((n ^ mask).leading_zeros() - 1) as u8 // we only need one sign bit
@@ -1327,4 +1335,20 @@ mod tests {
13271335
let out = big_neg.to_f64().unwrap();
13281336
assert!(out.is_finite() && out.is_sign_negative());
13291337
}
1338+
1339+
#[test]
1340+
fn test_trailing_zeros() {
1341+
// Without high part
1342+
assert_eq!(i256::from(0).trailing_zeros(), 256);
1343+
assert_eq!(i256::from(2).trailing_zeros(), 1);
1344+
assert_eq!(i256::from(16).trailing_zeros(), 4);
1345+
assert_eq!(i256::from(17).trailing_zeros(), 0);
1346+
// With high part
1347+
assert_eq!(i256::from_parts(0, i128::MAX).trailing_zeros(), 128);
1348+
assert_eq!(i256::from_parts(0, 16).trailing_zeros(), 128 + 4);
1349+
assert_eq!(i256::from_parts(2, i128::MAX).trailing_zeros(), 1);
1350+
1351+
assert_eq!(i256::MAX.trailing_zeros(), 0);
1352+
assert_eq!(i256::from(-1).trailing_zeros(), 0);
1353+
}
13301354
}

0 commit comments

Comments
 (0)