From e33fd880514a38fa5d6a15d7b7073b19b974e2ea Mon Sep 17 00:00:00 2001 From: dawn Date: Sat, 24 Oct 2026 04:55:53 +0300 Subject: [PATCH] gix-imara-diff: round the square root up like git's xdl_bogosqrt Signed-off-by: dawn --- .../crates/gitmirror-xrpc/src/format_patch.rs | 8 ++++++ knot2/third_party/gix-imara-diff/src/util.rs | 28 +++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/gitmirror/crates/gitmirror-xrpc/src/format_patch.rs b/gitmirror/crates/gitmirror-xrpc/src/format_patch.rs index e2e6bc358..1ae726943 100644 --- a/gitmirror/crates/gitmirror-xrpc/src/format_patch.rs +++ b/gitmirror/crates/gitmirror-xrpc/src/format_patch.rs @@ -1575,6 +1575,14 @@ mod tests { ); } + #[test] + fn the_too_common_limit_rounds_up_like_git() { + edit_reads_like_git( + "\n\n\n\n", + "\tdef __init__ (self) :\n\t\tself.xc = None\n\n\tdef callback (self) :\n\t\tid = self.get_uint()\n\t\tself.xc.medialib_get_info(id, MediainfoRes)\n\nxc = xmmsclient.XMMS ()\nxc.connect()\n\t\nxc.signal_playback_playtime(PlaytimeRes)\n#res = xc.broadcast_playback_current_id(CurrentIDRes)\n#res.xc = xc\n\nxc.python_loop()\n\n", + ); + } + #[test] fn similarity_floors_to_a_percent_like_git() { for max in 1..=2000u32 { diff --git a/knot2/third_party/gix-imara-diff/src/util.rs b/knot2/third_party/gix-imara-diff/src/util.rs index e37f9fb7f..5f77225d6 100644 --- a/knot2/third_party/gix-imara-diff/src/util.rs +++ b/knot2/third_party/gix-imara-diff/src/util.rs @@ -51,9 +51,9 @@ pub fn strip_common_postfix(file1: &mut &[Token], file2: &mut &[Token]) -> u32 { off } -/// Computes an approximation of the square root using bit operations. +/// Computes an approximation of the square root using bit operations, rounding up like git's `xdl_bogosqrt()`. pub fn sqrt(val: usize) -> u32 { - let nbits = (usize::BITS - val.leading_zeros()) / 2; + let nbits = (usize::BITS - val.leading_zeros()).div_ceil(2); 1 << nbits } @@ -109,3 +109,27 @@ pub fn find_hunk_start(changes: &[bool], pos: u32) -> u32 { .take_while(|&&changed| changed) .count() as u32 } + +#[cfg(test)] +mod tests { + use super::sqrt; + + fn bogosqrt(mut n: usize) -> u32 { + let mut i = 1; + while n > 0 { + i <<= 1; + n >>= 2; + } + i + } + + #[test] + fn sqrt_rounds_up_like_xdl_bogosqrt() { + for (n, root) in [(0, 1), (1, 2), (3, 2), (4, 4), (15, 4), (16, 8), (63, 8), (64, 16)] { + assert_eq!(sqrt(n), root, "sqrt({n})"); + } + for n in 0..1 << 16 { + assert_eq!(sqrt(n), bogosqrt(n), "sqrt({n})"); + } + } +} -- 2.54.0