-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add std.math.egcd (Extended GCD). Micro-optimize GCD. #25533
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
Draft
two-horned
wants to merge
2
commits into
ziglang:master
Choose a base branch
from
two-horned:faster-gcd
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+268
−44
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,241 @@ | ||
| //! Extended Greatest Common Divisor (https://mathworld.wolfram.com/ExtendedGreatestCommonDivisor.html) | ||
| const std = @import("../std.zig"); | ||
|
|
||
| /// Result type of `egcd`. | ||
| pub fn ExtendedGreatestCommonDivisor(S: anytype) type { | ||
| const N = switch (S) { | ||
| comptime_int => comptime_int, | ||
| else => |T| std.meta.Int(.unsigned, @bitSizeOf(T)), | ||
| }; | ||
|
|
||
| return struct { | ||
| gcd: N, | ||
| bezout_coeff_1: S, | ||
| bezout_coeff_2: S, | ||
| }; | ||
| } | ||
|
|
||
| /// Returns the Extended Greatest Common Divisor (EGCD) of two signed integers (`a` and `b`) which are not both zero. | ||
| pub fn egcd(a: anytype, b: anytype) ExtendedGreatestCommonDivisor(@TypeOf(a, b)) { | ||
| const S = switch (@TypeOf(a, b)) { | ||
| comptime_int => b: { | ||
| const n = @max(@abs(a), @abs(b)); | ||
| break :b std.math.IntFittingRange(-n, n); | ||
| }, | ||
| else => |T| T, | ||
| }; | ||
| if (@typeInfo(S) != .int or @typeInfo(S).int.signedness != .signed) { | ||
| @compileError("`a` and `b` must be signed integers"); | ||
| } | ||
|
|
||
| std.debug.assert(a != 0 or b != 0); | ||
|
|
||
| if (a == 0) return .{ .gcd = @abs(b), .bezout_coeff_1 = 0, .bezout_coeff_2 = std.math.sign(b) }; | ||
| if (b == 0) return .{ .gcd = @abs(a), .bezout_coeff_1 = std.math.sign(a), .bezout_coeff_2 = 0 }; | ||
|
|
||
| const other: S, const odd: S, const shift, const switch_coeff = b: { | ||
| const xz = @ctz(@as(S, a)); | ||
| const yz = @ctz(@as(S, b)); | ||
| break :b if (xz < yz) .{ b, a, xz, true } else .{ a, b, yz, false }; | ||
| }; | ||
| const toinv = @shrExact(other, @intCast(shift)); | ||
| const ctrl = @shrExact(odd, @intCast(shift)); // Invariant: |s|, |t|, |ctrl| < |MIN_OF(S)| | ||
| const half_ctrl = 1 + @shrExact(ctrl - 1, 1); | ||
| const abs_ctrl = @abs(ctrl); | ||
|
|
||
| var s: S = std.math.sign(toinv); | ||
| var t: S = 0; | ||
|
|
||
| var x = @abs(toinv); | ||
| var y = abs_ctrl; | ||
|
|
||
| { | ||
| const xz = @ctz(x); | ||
| x = @shrExact(x, @intCast(xz)); | ||
| for (0..xz) |_| { | ||
| const half_s = s >> 1; | ||
| if (s & 1 == 0) | ||
| s = half_s | ||
| else | ||
| s = half_s + half_ctrl; | ||
| } | ||
| } | ||
|
|
||
| var y_minus_x = y -% x; | ||
| while (y_minus_x != 0) : (y_minus_x = y -% x) { | ||
| const t_minus_s = t - s; | ||
| const copy_x = x; | ||
| const copy_s = s; | ||
| const xz = @ctz(y_minus_x); | ||
|
|
||
| s -= t; | ||
| const carry = x < y; | ||
| x -%= y; | ||
| if (carry) { | ||
| x = y_minus_x; | ||
| y = copy_x; | ||
| s = t_minus_s; | ||
| t = copy_s; | ||
| } | ||
| x = @shrExact(x, @intCast(xz)); | ||
| for (0..xz) |_| { | ||
| const half_s = s >> 1; | ||
| if (s & 1 == 0) | ||
| s = half_s | ||
| else | ||
| s = half_s + half_ctrl; | ||
| } | ||
|
|
||
| if (s < 0) s = @intCast(abs_ctrl - @abs(s)); | ||
| } | ||
|
|
||
| // Using integer widening is only a temporary solution. | ||
| const W = std.meta.Int(.signed, @bitSizeOf(S) * 2); | ||
| t = @intCast(@divExact(y - @as(W, s) * toinv, ctrl)); | ||
| const final_s, const final_t = if (switch_coeff) .{ t, s } else .{ s, t }; | ||
| return .{ | ||
| .gcd = @shlExact(y, @intCast(shift)), | ||
| .bezout_coeff_1 = final_s, | ||
| .bezout_coeff_2 = final_t, | ||
| }; | ||
| } | ||
|
|
||
| test { | ||
| { | ||
| const a: i2 = 0; | ||
| const b: i2 = 1; | ||
| const r = egcd(a, b); | ||
| const g = r.gcd; | ||
| const s: i2 = r.bezout_coeff_1; | ||
| const t: i2 = r.bezout_coeff_2; | ||
| try std.testing.expect(s * a + t * b == g); | ||
| } | ||
| { | ||
| const a: i8 = -128; | ||
| const b: i8 = 127; | ||
| const r = egcd(a, b); | ||
| const g = r.gcd; | ||
| const s: i16 = r.bezout_coeff_1; | ||
| const t: i16 = r.bezout_coeff_2; | ||
| try std.testing.expect(s * a + t * b == g); | ||
| } | ||
| { | ||
| const a: i16 = -32768; | ||
| const b: i16 = -32768; | ||
| const r = egcd(a, b); | ||
| const g = r.gcd; | ||
| const s: i32 = r.bezout_coeff_1; | ||
| const t: i32 = r.bezout_coeff_2; | ||
| try std.testing.expect(s * a + t * b == g); | ||
| } | ||
| { | ||
| const a: i32 = 128; | ||
| const b: i32 = 112; | ||
| const r = egcd(a, b); | ||
| const g = r.gcd; | ||
| const s: i64 = r.bezout_coeff_1; | ||
| const t: i64 = r.bezout_coeff_2; | ||
| try std.testing.expect(s * a + t * b == g); | ||
| } | ||
| { | ||
| const a: i32 = 4 * 89; | ||
| const b: i32 = 2 * 17; | ||
| const r = egcd(a, b); | ||
| const g = r.gcd; | ||
| const s: i64 = r.bezout_coeff_1; | ||
| const t: i64 = r.bezout_coeff_2; | ||
| try std.testing.expect(s * a + t * b == g); | ||
| } | ||
| { | ||
| const a: i8 = 127; | ||
| const b: i8 = 126; | ||
| const r = egcd(a, b); | ||
| const g = r.gcd; | ||
| const s: i16 = r.bezout_coeff_1; | ||
| const t: i16 = r.bezout_coeff_2; | ||
| try std.testing.expect(s * a + t * b == g); | ||
| } | ||
| { | ||
| const a: i4 = -8; | ||
| const b: i4 = 1; | ||
| const r = egcd(a, b); | ||
| const g = r.gcd; | ||
| const s = r.bezout_coeff_1; | ||
| const t = r.bezout_coeff_2; | ||
| try std.testing.expect(s * a + t * b == g); | ||
| } | ||
| { | ||
| const a: i4 = -8; | ||
| const b: i4 = 5; | ||
| const r = egcd(a, b); | ||
| const g = r.gcd; | ||
| // Avoid overflow in assert. | ||
| const s: i8 = r.bezout_coeff_1; | ||
| const t: i8 = r.bezout_coeff_2; | ||
| try std.testing.expect(s * a + t * b == g); | ||
| } | ||
| { | ||
| const a: i32 = 0; | ||
| const b: i32 = 5; | ||
| const r = egcd(a, b); | ||
| const g = r.gcd; | ||
| const s = r.bezout_coeff_1; | ||
| const t = r.bezout_coeff_2; | ||
| try std.testing.expect(s * a + t * b == g); | ||
| } | ||
| { | ||
| const a: i32 = 5; | ||
| const b: i32 = 0; | ||
| const r = egcd(a, b); | ||
| const g = r.gcd; | ||
| const s = r.bezout_coeff_1; | ||
| const t = r.bezout_coeff_2; | ||
| try std.testing.expect(s * a + t * b == g); | ||
| } | ||
|
|
||
| { | ||
| const a: i32 = 21; | ||
| const b: i32 = 15; | ||
| const r = egcd(a, b); | ||
| const g = r.gcd; | ||
| const s = r.bezout_coeff_1; | ||
| const t = r.bezout_coeff_2; | ||
| try std.testing.expect(s * a + t * b == g); | ||
| } | ||
| { | ||
| const a: i32 = -21; | ||
| const b: i32 = 15; | ||
| const r = egcd(a, b); | ||
| const g = r.gcd; | ||
| const s = r.bezout_coeff_1; | ||
| const t = r.bezout_coeff_2; | ||
| try std.testing.expect(s * a + t * b == g); | ||
| } | ||
| { | ||
| const a = -21; | ||
| const b = 15; | ||
| const r = egcd(a, b); | ||
| const g = r.gcd; | ||
| const s = r.bezout_coeff_1; | ||
| const t = r.bezout_coeff_2; | ||
| try std.testing.expect(s * a + t * b == g); | ||
| } | ||
| { | ||
| const a = 927372692193078999176; | ||
| const b = 573147844013817084101; | ||
| const r = egcd(a, b); | ||
| const g = r.gcd; | ||
| const s = r.bezout_coeff_1; | ||
| const t = r.bezout_coeff_2; | ||
| try std.testing.expect(s * a + t * b == g); | ||
| } | ||
| { | ||
| const a = 453973694165307953197296969697410619233826; | ||
| const b = 280571172992510140037611932413038677189525; | ||
| const r = egcd(a, b); | ||
| const g = r.gcd; | ||
| const s = r.bezout_coeff_1; | ||
| const t = r.bezout_coeff_2; | ||
| try std.testing.expect(s * a + t * b == g); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this still a work in progress?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One can avoid integer widening by implementing a custom function for terms of the form (a - b * c) / d. Something like this:
As you can see, this solution is highly inefficient because I am not doing long multiplication / division. If I implement long multiplication / division, this algorithm becomes much more complicated, so I didn't even bother really. Still, I want to encourage looking for something that doesn't require widening the integer type, that's why I left the comment.