-
-
Notifications
You must be signed in to change notification settings - Fork 740
fix Issue 12412. CTFE-able math #2521
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
Conversation
Please rebase to remove the merge commit. I see the function return value changed. This is technically a breaking change, however |
@CyberShadow, rebased. Previously this functions returned either |
LGTM. |
Stupid question: Why can't the CTFE versions be used at runtime as well, instead of the bit testing? This would simplify the code greatly. Would it have bad performance? |
@schuetzm Bad performance would be with |
None of these functions need an It also reduces the amount of diff we need to review, or the amount of code blame that gets assigned to you. |
@monarchdodra PR rebased |
foreach(T; TypeTuple!(float, double, real)) | ||
{ | ||
// CTFE-able tests | ||
assert(isFinite(T(1.23))); |
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.
How are these "CTFE-able" tests? Shouldn't those be static assert
s?
BTW, you might be interested in assertCTFEable(alias dg)()
in std.exception
. This will run your entire "dg", both in runtime and ctfe time.
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.
Thanks, I will use it.
@monarchdodra I will add CTFE signbit, frexp, isIdentical. |
…unittests minor update remove else after ctfe update unittests remove line remove line
Rebased |
:-( I have no idea how make signbit, frexp, isIdentical CTFE-able . |
Welcome to the club. :-P I've tried this before. I think the only solution is to use bit manipulation on the IEEE representation, but unfortunately that's not yet possible in CTFE. Basically, we need to implement support for type reinterpretation via unions in CTFE; once that is done, then actually most of the |
@quickfur I have tried: int signbit(X)(X x) @nogc @trusted pure nothrow
{
alias F = floatTraits!(X);
static union Rep
{
X x;
ubyte[X.sizeof] b;
}
Rep rep = Rep(x);
return (rep.b[F.SIGNPOS_BYTE] & 0x80) != 0;
} ... without any hope, |
Yes, because CTFE doesn't support that yet. The first thing to do would be to implement support in dmd, then we can proceed. Unfortunately, CTFE is one of the places I'm completely unfamiliar with, so I've no idea where to even begin. |
Blocked on dmd limitation. |
Well, partially, right? What's currently in the pull works. |
True. I suppose we could merge this first, and then do a followup PR once the dmd limitation is addressed. |
Yes, the easy part done. |
Well, I don't see anything wrong with this pull, but I'm no numerics expert. |
@@ -4525,76 +4533,24 @@ bool isInfinity(X)(X x) @nogc @trusted pure nothrow | |||
} | |||
else | |||
{ | |||
return (x - 1) == x; | |||
return x == x && x - x; |
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.
Just trying to understand the change here: what was wrong with (x - 1) == x
?
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.
enum M = 2.0L^^(real.max_exp-1) ;
static assert(M - 1 == M);//OK
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.
Ah, I see.
Also, is there a performance hit associated with writing x == x && x - x != 0.0
? I find that a little clearer, as it isn't obvious that x-x
here is being implicitly cast to bool unless you're reading the code carefully.
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.
I think performance is same.
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.
sgn
function for example uses implicite cast to bool. It is faster to understand the line of code. When I see f == 0.0
this really mean
f == 0.0 || f == -0.0
so I need to think about that.
LGTM with the same notes as @monarchdodra I'm no numerics guy. |
assert(!isNaN(-f)); | ||
f = cast(T)53.6; | ||
assert(!isNaN(f)); | ||
assert(!isNaN(-f)); |
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.
I'd rather that you don't remove the runtime tests. These were put here because the compiler - at least gdc - was able to const-fold the tests using literals above.
PR Closed.
|
Has been on my todo list, but keeps being pushed back. ;) |
Can some of these can have their current implementation replaced with the ctfe version without performance loss? I'd like to see all the bitmasking replaced eventually... |
Need tests with LDC and GDC like foreach(double a; ar)
{
if(a.isNaN && some_int==other_int)
{
...
}
else
{
...
}
} |
Issue:
https://issues.dlang.org/show_bug.cgi?id=12412
Make isNaN, isNormal, isSubnormal, isFinite, isInfinity CTFE-able.