Skip to content

std.math: Move pow function into submodule.#7942

Merged
dlang-bot merged 1 commit intodlang:masterfrom
berni44:math_move_pow
Apr 12, 2021
Merged

std.math: Move pow function into submodule.#7942
dlang-bot merged 1 commit intodlang:masterfrom
berni44:math_move_pow

Conversation

@berni44
Copy link
Contributor

@berni44 berni44 commented Apr 7, 2021

Moving pow to submodule seems to be a little bit more problematic, so I'm doing this in a single step. Let's see if it works...

@berni44 berni44 requested a review from ibuclaw as a code owner April 7, 2021 12:28
@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @berni44! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + phobos#7942"

@berni44
Copy link
Contributor Author

berni44 commented Apr 7, 2021

Some windows computers fail with a unittest in std.internal.math.gammafunction.d. Here the log of the x64 version, which is more verbose:

core.exception.AssertError@std\internal\math\gammafunction.d(1053): unittest failure
----------------
0x00007FF71340BB12 in d_unittestp
0x00007FF7131D508B in std.internal.math.gammafunction.__unittest_L1024_C7 at D:\a\1\phobos\std\internal\math\gammafunction.d(1054)
0x00007FF7131D8F27 in void std.internal.math.gammafunction.__modtest()
0x00007FF71342F139 in int core.runtime.runModuleUnitTests().__foreachbody2(object.ModuleInfo*)
0x00007FF71341B9A7 in int object.ModuleInfo.opApply(scope int delegate(object.ModuleInfo*)).__lambda2(immutable(object.ModuleInfo*))
0x00007FF713432794 in int rt.minfo.moduleinfos_apply(scope int delegate(immutable(object.ModuleInfo*))).__foreachbody2(ref rt.sections_win64.SectionGroup)
0x00007FF7134326FF in int rt.minfo.moduleinfos_apply(scope int delegate(immutable(object.ModuleInfo*)))
0x00007FF71341B967 in int object.ModuleInfo.opApply(scope int delegate(object.ModuleInfo*))
0x00007FF71342EF87 in runModuleUnitTests
0x00007FF713418D25 in void rt.dmain2._d_run_main2(char[][], ulong, extern (C) int function(char[][])*).runAll()
0x00007FF713418C7F in void rt.dmain2._d_run_main2(char[][], ulong, extern (C) int function(char[][])*).tryExec(scope void delegate())
0x00007FF713418A86 in d_run_main2
0x00007FF713404FB3 in d_run_main
0x00007FF7110F9122 in unittest._d_cmain!().main at D:\a\1\phobos\../druntime\import\core\internal\entrypoint.d(30)
0x00007FF7134F1378 in __scrt_common_main_seh at d:\agent\_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl(288)
0x00007FFAD01E84D4 in BaseThreadInitThunk
0x00007FFAD2221821 in RtlUserThreadStart
1/130 modules FAILED unittests
std\internal\math\gammafunction.d(1053): [unittest] unittest failure

Anyone an idea, what's the reason here?

@thewilsonator
Copy link
Contributor

MS win has double == real and the gamma function tests in particular test more precision than they really should.
I take it this PR is a cut and paste job for the most part?

evaluate the failing assertion
feqrel(betaIncompleteInv(5, 10, 0.2000002972865658842L), 0.229121208190918L) >= real.mant_dig - 3 and see how far off it is.

@berni44
Copy link
Contributor Author

berni44 commented Apr 7, 2021

I take it this PR is a cut and paste job for the most part?

Aside from a few needed imports it's cut and paste...

@berni44
Copy link
Contributor Author

berni44 commented Apr 7, 2021

It's only one digit: .0...799 instead of .0...800.

Anyway I wonder, why the calculation changed. It shouldn't.

@berni44
Copy link
Contributor Author

berni44 commented Apr 7, 2021

eBay/tsv-utils fails too, due to the same problems as encountered with the gamma function.

IMHO, we should find out, why dmd produces different code here and fix that...

@RazvanN7
Copy link
Collaborator

RazvanN7 commented Apr 8, 2021

I wonder if might be possible that the compiler searches for pow in both druntime and std.math. Maybe these have different implementations and now since pow is moved from std.math, the compiler picks the one in druntime?

Also, there's the style checker:

std/internal/math/gammafunction.d(26:1)[warn]: A unittest should be annotated with at least @safe or @system
`

@berni44
Copy link
Contributor Author

berni44 commented Apr 8, 2021

Also, there's the style checker:

I'm aware of that, but I see no merit in starting the testers again, when the main issue is not fixed... ;-) (Ideally, the gammafunction should not be touched at all.)

@RazvanN7
Copy link
Collaborator

RazvanN7 commented Apr 8, 2021

Ok, I was just pointing that out :-"

@ibuclaw
Copy link
Member

ibuclaw commented Apr 9, 2021

The compiler lowers ^^ operator to

static import std.math;
std.math.pow(x, y);

Hazarding a guess, but you might need to force that the std version takes precedent with:

alias pow = std.math.exponential.pow;

In the package module.

Ideally pow should really be in druntime though because it is doubles as a compiler-runtime interface, however the non-trivial template implementation makes it awkward to move without also privately copying a good chunk of meta and traits along with it.

@berni44
Copy link
Contributor Author

berni44 commented Apr 9, 2021

Hazarding a guess, but you might need to force that the std version takes precedent with:

alias pow = std.math.exponential.pow;

In the package module.

For round and trunc this didn't work. But maybe here it does. Let's try...

@berni44
Copy link
Contributor Author

berni44 commented Apr 9, 2021

For round and trunc this didn't work. But maybe here it does. Let's try...

No, unfortunately not...

@ibuclaw
Copy link
Member

ibuclaw commented Apr 10, 2021

For round and trunc this didn't work. But maybe here it does. Let's try...

No, unfortunately not...

Another shot in the dark. Remove the public import of core.math from the std.math package.

In its place, use public selective imports in the appropriate submodules.

// e.g in std.math.exponential
public import core.math : pow;

An intentional side effect being future core.math intrinsics won't be implicitly pulled into std.math.

@berni44
Copy link
Contributor Author

berni44 commented Apr 10, 2021

Another shot in the dark. Remove the public import of core.math from the std.math package.

core.math isn't publically imported, only static. I moved it into the functions anyway to see, if this has any effect...

@ibuclaw
Copy link
Member

ibuclaw commented Apr 10, 2021

Another shot in the dark. Remove the public import of core.math from the std.math package.

core.math isn't publically imported, only static. I moved it into the functions anyway to see, if this has any effect...

Oh, then I suppose it'll likely have no effect at all them, except if there're bugs in the import visibility system.

@ibuclaw
Copy link
Member

ibuclaw commented Apr 10, 2021

core.math isn't publically imported, only static. I moved it into the functions anyway to see, if this has any effect...

Oh, then I suppose it'll likely have no effect at all them, except if there're bugs in the import visibility system.

Saying that though, it all seems to be green?

@berni44
Copy link
Contributor Author

berni44 commented Apr 10, 2021

Saying that though, it all seems to be green?

Unfortunately not. Still the same problem.

@ibuclaw
Copy link
Member

ibuclaw commented Apr 10, 2021

@berni44 - I've managed to find some time to check this proper.

Compiling gammafunction.d on x86_64-linux using -vcg-ast and diff'd the generated dump before and after this patch. This difference in codegen has no relation to a change in this PR.

@@ -5473,7 +5474,8 @@ pow!(real, real)
 				}
 				x = -x;
 			}
-			return sign * exp2(yl2x(x, y));
+			real w = exp2(y * log2(x));
+			return sign * w;
 		}
 		return impl(x, y);
 	}

Looks like the culprit is that you forgot to define version = INLINE_YL2X; for DigitalMars platforms.

@ibuclaw
Copy link
Member

ibuclaw commented Apr 10, 2021

Just realised I can commit the suggestions myself.

@ibuclaw
Copy link
Member

ibuclaw commented Apr 10, 2021

New error:

D:\a\1\dmd\test\..\..\phobos\std\math\exponential.d(667): Error: `yl2x` cannot be interpreted at compile time, because it has no available source code
compilable\test5227.d(120): Error: cannot interpret `<error>` at compile time
compilable\test5227.d(120):        while evaluating `pragma(msg, powf)`

Which is correct, when LDC/GDC is building DMD, the resultant compiler can not interpret yl2x at compile-time.

@ibuclaw
Copy link
Member

ibuclaw commented Apr 10, 2021

So this is blocked by the compiler now.
https://github.com/dlang/dmd/blob/e658df1cc3a1622dea8e3614a5c3d94742ebbf52/src/dmd/builtin.d#L89-L93

It looks only for module names ending with math (i.e: core.math, std.math), so it returns BUILTIN.unimpl so tries to interpret the body.

@berni44
Copy link
Contributor Author

berni44 commented Apr 10, 2021

So this is blocked by the compiler now.
https://github.com/dlang/dmd/blob/e658df1cc3a1622dea8e3614a5c3d94742ebbf52/src/dmd/builtin.d#L89-L93

It looks only for module names ending with math (i.e: core.math, std.math), so it returns BUILTIN.unimpl so tries to interpret the body.

Wilsonator pointed me to the same line in PR #7943.

@ibuclaw
Copy link
Member

ibuclaw commented Apr 10, 2021

So this is blocked by the compiler now.
https://github.com/dlang/dmd/blob/e658df1cc3a1622dea8e3614a5c3d94742ebbf52/src/dmd/builtin.d#L89-L93
It looks only for module names ending with math (i.e: core.math, std.math), so it returns BUILTIN.unimpl so tries to interpret the body.

Wilsonator pointed me to the same line in PR #7943.

Validating a fix just now with compilable/test5227.d, it looks like fabs stopped being matched in #7941, but this wasn't noticed because the body just forwards to core.math.

@ibuclaw
Copy link
Member

ibuclaw commented Apr 11, 2021

No longer blocked by compiler built-ins support. Looks like one remaining failure is due to missing symbols.

Copy link
Member

@ibuclaw ibuclaw left a comment

Choose a reason for hiding this comment

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

One last nit, and this is good to go.

@berni44
Copy link
Contributor Author

berni44 commented Apr 11, 2021

Many thanks for your help!

@dlang-bot dlang-bot merged commit 59f78a2 into dlang:master Apr 12, 2021
@berni44 berni44 deleted the math_move_pow branch April 28, 2021 17:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants