Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add i256 (#2637) #2781

Merged
merged 10 commits into from Oct 3, 2022
Merged

Add i256 (#2637) #2781

merged 10 commits into from Oct 3, 2022

Conversation

tustvold
Copy link
Contributor

Which issue does this PR close?

Part of #2637

Rationale for this change

This adds an i256 that can be used as the basis for a Decimal256Array, and implements common arithmetic for it.

What changes are included in this PR?

Are there any user-facing changes?

}

impl Ord for i256 {
fn cmp(&self, other: &Self) -> Ordering {
Copy link
Contributor Author

@tustvold tustvold Sep 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is related to #2360, performing the comparison this way is significantly faster than using memcmp, which is itself significantly faster than using BigInt.

FYI @liukun4515

See https://rust.godbolt.org/z/h9fjEP1b4

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

genius method.

Comment on lines 133 to 187
/// Performs wrapping multiplication
#[inline]
pub fn wrapping_mul(self, other: Self) -> Self {
let l = BigInt::from_signed_bytes_le(&self.0);
let r = BigInt::from_signed_bytes_le(&other.0);
Self::from_bigint_with_overflow(l * r).0
}

/// Performs checked multiplication
#[inline]
pub fn checked_mul(self, other: Self) -> Option<Self> {
let l = BigInt::from_signed_bytes_le(&self.0);
let r = BigInt::from_signed_bytes_le(&other.0);
let (val, overflow) = Self::from_bigint_with_overflow(l * r);
(!overflow).then(|| val)
}

/// Performs wrapping division
#[inline]
pub fn wrapping_div(self, other: Self) -> Self {
let l = BigInt::from_signed_bytes_le(&self.0);
let r = BigInt::from_signed_bytes_le(&other.0);
Self::from_bigint_with_overflow(l / r).0
}

/// Performs checked division
#[inline]
pub fn checked_div(self, other: Self) -> Option<Self> {
let l = BigInt::from_signed_bytes_le(&self.0);
let r = BigInt::from_signed_bytes_le(&other.0);
let (val, overflow) = Self::from_bigint_with_overflow(l / r);
(!overflow).then(|| val)
}

/// Performs wrapping remainder
#[inline]
pub fn wrapping_rem(self, other: Self) -> Self {
let l = BigInt::from_signed_bytes_le(&self.0);
let r = BigInt::from_signed_bytes_le(&other.0);
Self::from_bigint_with_overflow(l % r).0
}

/// Performs checked division
#[inline]
pub fn checked_rem(self, other: Self) -> Option<Self> {
if other.0 == [0; 32] {
return None;
}

let l = BigInt::from_signed_bytes_le(&self.0);
let r = BigInt::from_signed_bytes_le(&other.0);
let (val, overflow) = Self::from_bigint_with_overflow(l % r);
(!overflow).then(|| val)
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theoretically these would all benefit from an implementation not using BigInt, as this elides allocations and branches, but unlike the addition and subtraction kernels these are significantly more complicated to implement. I leave this as a potential future optimisation for someone who really cares about decimal256 performance

Copy link
Member

@viirya viirya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @tustvold. I'm a bit busy since last week. I will find some time to look at this in the weekend.

@liukun4515
Copy link
Contributor

I need more time to review it tomorrow.

Thanks @tustvold

match overflow {
true => assert!(checked.is_none()),
false => assert_eq!(checked.unwrap(), actual),
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't include tests for div, rem because they are implemented using BigInt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I figured that would be a bit circular

}

#[inline]
fn mulx(a: u128, b: u128) -> (u128, u128) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A simple doc might be nicer.

@tustvold tustvold merged commit 4df1f3c into apache:master Oct 3, 2022
@ursabot
Copy link

ursabot commented Oct 3, 2022

Benchmark runs are scheduled for baseline = 9b59081 and contender = 4df1f3c. 4df1f3c is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ec2-t3-xlarge-us-east-2] ec2-t3-xlarge-us-east-2
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on test-mac-arm] test-mac-arm
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ursa-i9-9960x] ursa-i9-9960x
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ursa-thinkcentre-m75q] ursa-thinkcentre-m75q
Buildkite builds:
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java

@tustvold
Copy link
Contributor Author

tustvold commented Oct 3, 2022

There appears to be a mistake somewhere in this 😢 will look into https://github.com/apache/arrow-rs/actions/runs/3174304664/jobs/5170929134

@tustvold tustvold added the arrow Changes to the arrow crate label Oct 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrow Changes to the arrow crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants