Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions ndc_core/src/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ impl Int {
#[must_use]
pub fn abs(&self) -> Self {
match self {
Self::Int64(i) => Self::from(i.abs()),
// `i64::MIN.abs()` overflows, so promote to BigInt in that case.
Self::Int64(i) => i
.checked_abs()
.map_or_else(|| Self::from(BigInt::from(*i).abs()), Self::Int64),
Self::BigInt(b) => Self::from(b.abs()),
}
}
Expand Down Expand Up @@ -218,7 +221,10 @@ impl Neg for Int {

fn neg(self) -> Self::Output {
match self {
Self::Int64(i) => Self::Int64(i.neg()),
// `-i64::MIN` overflows, so promote to BigInt in that case.
Self::Int64(i) => i
.checked_neg()
.map_or_else(|| Self::BigInt(BigInt::from(i).neg()), Self::Int64),
Self::BigInt(i) => Self::BigInt(i.neg()),
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/functional/programs/001_math/032_neg_abs_i64_min.ndc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Negating or taking the absolute value of `i64::MIN` overflows the i64
// range (`-i64::MIN` does not fit in an i64). `Int::neg` and `Int::abs` used
// to call `i.neg()` / `i.abs()` directly, which panics in debug builds and
// silently wraps in release. They now promote to BigInt, yielding the correct
// positive magnitude.
// expect-output: 9223372036854775808 9223372036854775808 9223372036854775808
let min = -9223372036854775807 - 1;
print(-min, abs(min), abs(-min));
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// `abs(i64::MIN)` and `-(i64::MIN)` overflow the i64 range because the
// positive magnitude `2^63` does not fit in an i64. `Int::abs`/`Int::neg`
// used to evaluate `i.abs()`/`i.neg()` directly, panicking with "attempt to
// negate with overflow" in debug builds. They now promote to BigInt.
// expect-output: 9223372036854775808
print(abs(-9223372036854775807 - 1));
Loading