You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "tustvold (via GitHub)" <gi...@apache.org> on 2023/03/25 23:24:56 UTC
[GitHub] [arrow-rs] tustvold opened a new pull request, #3943: Fix checked i256 arithmetic (#3942) (#3941)
tustvold opened a new pull request, #3943:
URL: https://github.com/apache/arrow-rs/pull/3943
# Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
Closes #3941
Closes #3942
# Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
The previous logic would report overflow erroneously
# What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
# Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!---
If there are any breaking changes to public APIs, please add the `breaking change` label.
-->
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] tustvold merged pull request #3943: Fix checked i256 arithmetic (#3942) (#3941)
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold merged PR #3943:
URL: https://github.com/apache/arrow-rs/pull/3943
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] viirya commented on a diff in pull request #3943: Fix checked i256 arithmetic (#3942) (#3941)
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #3943:
URL: https://github.com/apache/arrow-rs/pull/3943#discussion_r1148453386
##########
arrow-buffer/src/bigint.rs:
##########
@@ -791,14 +803,33 @@ mod tests {
#[test]
fn test_i256() {
let candidates = [
- i256::from_parts(0, 0),
- i256::from_parts(0, 1),
- i256::from_parts(0, -1),
+ i256::ZERO,
+ i256::ONE,
+ i256::MINUS_ONE,
+ i256::from_i128(2),
+ i256::from_i128(-2),
i256::from_parts(u128::MAX, 1),
i256::from_parts(u128::MAX, -1),
i256::from_parts(0, 1),
i256::from_parts(0, -1),
+ i256::from_parts(1, -1),
+ i256::from_parts(1, 1),
+ i256::from_parts(0, i128::MAX),
+ i256::from_parts(0, i128::MIN),
+ i256::from_parts(1, i128::MAX),
+ i256::from_parts(1, i128::MIN),
+ i256::from_parts(u128::MAX, i128::MIN),
i256::from_parts(100, 32),
+ i256::MIN,
+ i256::MAX,
+ i256::MIN >> 1,
+ i256::MAX >> 1,
+ i256::ONE << 127,
+ i256::ONE << 128,
+ i256::ONE << 129,
+ i256::MINUS_ONE << 127,
+ i256::MINUS_ONE << 128,
+ i256::MINUS_ONE << 129,
Review Comment:
Better coverage 👍
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] tustvold commented on a diff in pull request #3943: Fix checked i256 arithmetic (#3942) (#3941)
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #3943:
URL: https://github.com/apache/arrow-rs/pull/3943#discussion_r1148448678
##########
arrow-buffer/src/bigint.rs:
##########
@@ -259,9 +258,8 @@ impl i256 {
/// Performs checked subtraction
#[inline]
pub fn checked_sub(self, other: Self) -> Option<Self> {
- let (low, carry) = self.low.overflowing_sub(other.low);
- let high = self.high.checked_sub(other.high)?.checked_sub(carry as _)?;
- Some(Self { low, high })
Review Comment:
Consider the case
```
i256::from_parts(0, i128::MAX).checked_sub(i256::MINUS_ONE).unwrap();
```
The carry is enough to reduce `i128::MAX` enough that `i125::MINUS_ONE` does not result in overflow
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] tustvold commented on a diff in pull request #3943: Fix checked i256 arithmetic (#3942) (#3941)
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #3943:
URL: https://github.com/apache/arrow-rs/pull/3943#discussion_r1148448583
##########
arrow-buffer/src/bigint.rs:
##########
@@ -243,9 +243,8 @@ impl i256 {
/// Performs checked addition
#[inline]
pub fn checked_add(self, other: Self) -> Option<Self> {
- let (low, carry) = self.low.overflowing_add(other.low);
- let high = self.high.checked_add(other.high)?.checked_add(carry as _)?;
Review Comment:
The mistake this makes is assuming that addition is transitive, in the case of overflow it is not.
In particular consider the case
```
i256::from_parts(u128::MAX, i128::MIN)
.checked_add(i256::from_parts(1, -1))
.unwrap();
```
The carry is enough to increase `i128::MIN` enough that adding `-1` doesn't result in overflow
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] tustvold commented on a diff in pull request #3943: Fix checked i256 arithmetic (#3942) (#3941)
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #3943:
URL: https://github.com/apache/arrow-rs/pull/3943#discussion_r1148448678
##########
arrow-buffer/src/bigint.rs:
##########
@@ -259,9 +258,8 @@ impl i256 {
/// Performs checked subtraction
#[inline]
pub fn checked_sub(self, other: Self) -> Option<Self> {
- let (low, carry) = self.low.overflowing_sub(other.low);
- let high = self.high.checked_sub(other.high)?.checked_sub(carry as _)?;
- Some(Self { low, high })
Review Comment:
Consider the case
```
i256::from_parts(0, i128::MAX).checked_sub(i256::MINUS_ONE).unwrap();
```
The carry is enough to reduce `i128::MAX` enough that subtracting `-1` does not result in overflow
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] tustvold commented on a diff in pull request #3943: Fix checked i256 arithmetic (#3942) (#3941)
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #3943:
URL: https://github.com/apache/arrow-rs/pull/3943#discussion_r1148449023
##########
arrow-buffer/src/bigint.rs:
##########
@@ -303,13 +305,14 @@ impl i256 {
let hl = (l_abs.high as u128).checked_mul(r_abs.low)?;
let lh = l_abs.low.checked_mul(r_abs.high as u128)?;
- let high: i128 = high.checked_add(hl)?.checked_add(lh)?.try_into().ok()?;
Review Comment:
If `high == -i128::MIN` this would fail, as it is performed before reversing the absolute value conversion
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] tustvold commented on a diff in pull request #3943: Fix checked i256 arithmetic (#3942) (#3941)
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #3943:
URL: https://github.com/apache/arrow-rs/pull/3943#discussion_r1148449525
##########
arrow-buffer/src/bigint.rs:
##########
@@ -398,6 +404,12 @@ impl i256 {
// needless overflow.
acc.wrapping_mul(base)
}
+
+ /// Returns `true` if this [`i256`] is negative
+ #[inline]
+ pub fn is_negative(self) -> bool {
Review Comment:
This matches the standard method - https://doc.rust-lang.org/std/primitive.i32.html#method.is_negative
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow-rs] viirya commented on a diff in pull request #3943: Fix checked i256 arithmetic (#3942) (#3941)
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #3943:
URL: https://github.com/apache/arrow-rs/pull/3943#discussion_r1148455758
##########
arrow-buffer/src/bigint.rs:
##########
@@ -243,9 +243,8 @@ impl i256 {
/// Performs checked addition
#[inline]
pub fn checked_add(self, other: Self) -> Option<Self> {
- let (low, carry) = self.low.overflowing_add(other.low);
- let high = self.high.checked_add(other.high)?.checked_add(carry as _)?;
Review Comment:
Oh, this is tricky.
--
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: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org