Skip to content

Commit

Permalink
Add arithmetic function abs_diff
Browse files Browse the repository at this point in the history
  • Loading branch information
akubera committed Nov 9, 2024
1 parent eb77071 commit 9ed0395
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ fn main() {
let ac = autocfg::new();
ac.emit_rustc_version(1, 70);

// abs_diff
ac.emit_rustc_version(1, 60);

// slice::fill
ac.emit_rustc_version(1, 50);

Expand Down
13 changes: 13 additions & 0 deletions src/arithmetic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ pub(crate) fn diff_usize(a: usize, b: usize) -> (Ordering, usize) {
}
}

/// Return absolute difference between two numbers
#[cfg(rustc_1_60)]
#[allow(clippy::incompatible_msrv)]
#[allow(dead_code)]
pub(crate) fn abs_diff(x: i64, y: i64) -> u64 {
x.abs_diff(y)
}

#[cfg(not(rustc_1_60))]
pub(crate) fn abs_diff(x: i64, y: i64) -> u64 {
(x as i128 - y as i128).to_u64().unwrap_or(0)
}


/// Add carry to given number, returning trimmed value and storing overflow back in carry
///
Expand Down

0 comments on commit 9ed0395

Please sign in to comment.