Skip to content

Commit 8cd37a2

Browse files
theirixalamb
andauthored
arrow-buffer: make i256::leading_zeros public and tested (#8964)
# Which issue does this PR close? - Closes #8965. # Rationale for this change `leading_zeros` exists for other integer types, and in wide `i128` as well. This PR makes it public and tested. It's worth having it as a public part of the struct. # What changes are included in this PR? - Make the function public - Unit tests - Function documentatioon # Are these changes tested? Yes, via unit tests. # Are there any user-facing changes? No Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
1 parent 112e581 commit 8cd37a2

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

arrow-buffer/src/bigint/mod.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,8 @@ impl i256 {
583583
self.high.is_positive() || self.high == 0 && self.low != 0
584584
}
585585

586-
fn leading_zeros(&self) -> u32 {
586+
/// Returns the number of leading zeros in the binary representation of this [`i256`].
587+
pub const fn leading_zeros(&self) -> u32 {
587588
match self.high {
588589
0 => u128::BITS + self.low.leading_zeros(),
589590
_ => self.high.leading_zeros(),
@@ -1336,6 +1337,22 @@ mod tests {
13361337
assert!(out.is_finite() && out.is_sign_negative());
13371338
}
13381339

1340+
#[test]
1341+
fn test_leading_zeros() {
1342+
// Without high part
1343+
assert_eq!(i256::from(0).leading_zeros(), 256);
1344+
assert_eq!(i256::from(1).leading_zeros(), 256 - 1);
1345+
assert_eq!(i256::from(16).leading_zeros(), 256 - 5);
1346+
assert_eq!(i256::from(17).leading_zeros(), 256 - 5);
1347+
1348+
// With high part
1349+
assert_eq!(i256::from_parts(2, 16).leading_zeros(), 128 - 5);
1350+
assert_eq!(i256::from_parts(2, i128::MAX).leading_zeros(), 1);
1351+
1352+
assert_eq!(i256::MAX.leading_zeros(), 1);
1353+
assert_eq!(i256::from(-1).leading_zeros(), 0);
1354+
}
1355+
13391356
#[test]
13401357
fn test_trailing_zeros() {
13411358
// Without high part

0 commit comments

Comments
 (0)