Fix Issue 4125 - std.numeric.gcd can use a binary GCD#4940
Fix Issue 4125 - std.numeric.gcd can use a binary GCD#4940andralex merged 1 commit intodlang:masterfrom
Conversation
|
std/numeric.d
Outdated
| /** | ||
| Computes the greatest common divisor of $(D a) and $(D b) by using | ||
| Euclid's algorithm. | ||
| Euclid's or Stein's algorithm depending on compiler. |
There was a problem hiding this comment.
No need to be this detailed, just write "... using an efficient algorithm such as Euclid or Stein."
There was a problem hiding this comment.
Oh, you may also add Web links to the algorithms: "... using an efficient algorithm such as
There was a problem hiding this comment.
Oh, you may also add Web links to the algorithms:
@Darredevil FYI there's also the LINK2 macro which allows to have clickable/copy-pasteable links in the source code. For more information, see e.g. this discussion.
std/numeric.d
Outdated
| else | ||
| { | ||
| static if (T.min < 0) | ||
| version(DigitalMars) { |
There was a problem hiding this comment.
Since the CircleCI style checker has been re-enabled the failing status of CircleCi refers to detected style violations :)
(or of course to build errors).
@Darredevil there's also a style target in the Makefile, so you can e.g. use make -f posix.mak style to run the linting locally.
std/numeric.d
Outdated
| static if (is(T == const) || is(T == immutable)) | ||
| { | ||
| assert(a >= 0 && b >=0); | ||
| return gcd!(Unqual!T)(a, b); |
There was a problem hiding this comment.
This part is common to both implementations, so pull it before the version test.
std/numeric.d
Outdated
| immutable uint shift = bsf(a | b); | ||
| a >>= a.bsf; | ||
|
|
||
| do { |
There was a problem hiding this comment.
git grep ' do$' **/*.d | wc -l
55
git grep ' do *{' **/*.d | wc -l
8So I guess we want to normalize on { on its own line, too. cc @wilzbach @CyberShadow
There was a problem hiding this comment.
So I guess we want to normalize on { on its own line, too
Sure -> #4942
Also note that our "style" CI is already complaining at the moment :)
std/numeric.d
Outdated
| return gcd!(Unqual!T)(a, b); | ||
| } | ||
| else { | ||
| import std.algorithm: swap; |
There was a problem hiding this comment.
The D style for selective imports is to have a space before and after the colon (:) like import std.range : zip
43899a0 to
840be6a
Compare
std/numeric.d
Outdated
| do | ||
| { | ||
| b >>= b.bsf; | ||
|
|
std/numeric.d
Outdated
| Computes the greatest common divisor of $(D a) and $(D b) by using | ||
| Euclid's algorithm. | ||
| an efficient algorithm such as $(HTTP https://en.wikipedia.org/wiki/Euclidean_algorithm, Euclid's) | ||
| or $(HTTP https://en.wikipedia.org/wiki/Binary_GCD_algorithm, Stein's) algorithm. |
There was a problem hiding this comment.
These links will not work. You either use
There was a problem hiding this comment.
That's exactly why we want to remove the HTTP and HTTPS macros, they cost a lot more than they actually do save!
There was a problem hiding this comment.
edit: didn't see that the change was part of the PR
840be6a to
6b4c258
Compare
|
Auto-merge toggled on |
After conducting some benchmarks we arrived to the conclusion that currently the dmd compiler works best with Euclid's algorithm for GCD, othewise we use Stein's algorithm for ldc or gdc.
https://issues.dlang.org/show_bug.cgi?id=4125