viirya commented on code in PR #4303:
URL: https://github.com/apache/arrow-rs/pull/4303#discussion_r1211126905


##########
arrow-buffer/src/bigint.rs:
##########
@@ -396,42 +407,93 @@ impl i256 {
             .then_some(Self { low, high })
     }
 
+    /// Return the least number of bits needed to represent the number
+    #[inline]
+    fn bits_required(&self) -> usize {
+        let arr = self.to_le_bytes();
+        let iter = arr.iter().rev().take(32 - 1);
+        if self.is_negative() {
+            let ctr = iter.take_while(|&&b| b == ::core::u8::MAX).count();
+            (8 * (32 - ctr)) + 1 - (!arr[32 - ctr - 1]).leading_zeros() as 
usize
+        } else {
+            let ctr = iter.take_while(|&&b| b == ::core::u8::MIN).count();
+            (8 * (32 - ctr)) + 1 - arr[32 - ctr - 1].leading_zeros() as usize
+        }
+    }
+
+    /// divmod like operation, returns (quotient, remainder)
+    #[inline]
+    fn div_rem(self, other: Self) -> Result<(Self, Self), I256Error> {
+        if other == Self::ZERO {
+            return Err(I256Error::DivideByZero);
+        }
+        if other.is_negative() && self == Self::MIN && other == 
Self::ONE.wrapping_neg() {
+            return Err(I256Error::DivideOverflow);
+        }
+
+        if self == Self::MIN || other == Self::MIN {
+            let l = BigInt::from_signed_bytes_le(&self.to_le_bytes());
+            let r = BigInt::from_signed_bytes_le(&other.to_le_bytes());
+            let d = i256::from_bigint_with_overflow(&l / &r).0;
+            let r = i256::from_bigint_with_overflow(&l % &r).0;
+            return Ok((d, r));
+        }
+
+        let mut me = self.checked_abs().unwrap();
+        let mut you = other.checked_abs().unwrap();
+        let mut ret = [0u8; 32];
+        if me < you {
+            return Ok((Self::from_le_bytes(ret), self));
+        }
+

Review Comment:
   The low component is u128. If the high component is zero, it might be 
possibly over i128 range, no?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to