Skip to content

Fix Issue 4125 - std.numeric.gcd can use a binary GCD#4940

Merged
andralex merged 1 commit intodlang:masterfrom
Darredevil:issue-4125
Dec 12, 2016
Merged

Fix Issue 4125 - std.numeric.gcd can use a binary GCD#4940
andralex merged 1 commit intodlang:masterfrom
Darredevil:issue-4125

Conversation

@Darredevil
Copy link
Contributor

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

@dlang-bot
Copy link
Contributor

Fix Bugzilla Description
4125 std.numeric.gcd can use a binary GCD

Copy link
Member

@andralex andralex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

few nits

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.
Copy link
Member

@andralex andralex Dec 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to be this detailed, just write "... using an efficient algorithm such as Euclid or Stein."

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you may also add Web links to the algorithms: "... using an efficient algorithm such as $(HTTP xyz.com/page.html, Euclid) or $(HTTP xyz.com/page.html, Stein)."

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

brace on its own line

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

git grep ' do$' **/*.d | wc -l
55
git grep ' do *{' **/*.d | wc -l
8

So I guess we want to normalize on { on its own line, too. cc @wilzbach @CyberShadow

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The D style for selective imports is to have a space before and after the colon (:) like import std.range : zip

@Darredevil Darredevil force-pushed the issue-4125 branch 4 times, most recently from 43899a0 to 840be6a Compare December 10, 2016 21:42
@Darredevil
Copy link
Contributor Author

@andralex @wilzbach think I addressed everything you mentioned, let me know if there is anything else, thx!

std/numeric.d Outdated
do
{
b >>= b.bsf;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant whitespace

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These links will not work. You either use $(HTTP no.protocol.here) or $(LINK2 protocol://site.domain). Right now the link formed will be http://https://en.wikipedia.org/wiki/Euclidean_algorithm, which obviously won't work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's exactly why we want to remove the HTTP and HTTPS macros, they cost a lot more than they actually do save!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love them

Copy link
Contributor

@wilzbach wilzbach Dec 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

edit: didn't see that the change was part of the PR

@andralex
Copy link
Member

Auto-merge toggled on

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants