[Issue 11320] std.math.fmod, round, trunc are not yet pure

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Oct 26 10:24:15 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=11320

--- Comment #3 from Simon Naarmann <eiderdaus at gmail.com> ---
I see no problems to making fmod() pure.

But with round(), here's the Phobos code:

    auto round(real x) @trusted nothrow @nogc
    {
        version (CRuntime_Microsoft)
        {
            auto old = FloatingPointControl.getControlState();
            FloatingPointControl.setControlState(
                (old & (-1 - FloatingPointControl.roundingMask))
                | FloatingPointControl.roundToZero
            );
            x = rint((x >= 0) ? x + 0.5 : x - 0.5);
            FloatingPointControl.setControlState(old);
            return x;
        }
        else
            return core.stdc.math.roundl(x);
    }

Can the CRuntime_Microsoft version ever be pure? It backs up global state of
the CPU's floating point processing, then restores it:

    static void setControlState(ControlState newState) @trusted
    {
        version (InlineAsm_X86_Any)
        {
            asm nothrow @nogc
            {
                fclex;
                fldcw newState;
            }
            // Also update MXCSR, SSE's control register.
            // ...
                asm nothrow @nogc { ldmxcsr mxcsr; }
        // ...

How should this be handled w.r.t. purity? I haven't looked at all into how such
CPU state behaves with multithreaded code. For now, I'd have to leave round()
as impure across all platforms.

--


More information about the Digitalmars-d-bugs mailing list