public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "ibuclaw at gdcproject dot org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug d/94425] New: [D] Consider always settings ASM_VOLATILE_P on asm statements
Date: Tue, 31 Mar 2020 14:46:04 +0000	[thread overview]
Message-ID: <bug-94425-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94425

            Bug ID: 94425
           Summary: [D] Consider always settings ASM_VOLATILE_P on asm
                    statements
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: d
          Assignee: ibuclaw at gdcproject dot org
          Reporter: ibuclaw at gdcproject dot org
  Target Milestone: ---

Currently, the following have equivalents with the c-family front-ends.

asm      { ""; } -> asm __volatile__ ("");
asm pure { ""; } -> asm ("");

In the case of 'asm pure' the result can be cached, with duplicated statements
being DCE'd.  This happens in the std.math unittests when compiling with -O2.

The example below distils what the test looks like after inlining.  The last
asm statement is removed, and the first result is reused, which produces wrong
run-time results.  The use of 'asm pure' is in line with expectations however,
particularly as the getters are 'pure' in the D sense.

Either will have to say that every asm statement is volatile, or will have to
apply a stricter condition.

In C, asm statements without output operands are automatically considered
volatile, but that won't help us here.

---
import core.stdc.fenv;

enum ROUNDING_MASK = FE_TONEAREST|FE_DOWNWARD|FE_UPWARD|FE_TOWARDZERO;

void main()
{
    uint newMode = FE_DOWNWARD;
    fexcept_t oldState;
    asm pure
    {
        "fstcw %0;" : "=m" (oldState);
    }
    fexcept_t newState = cast(fexcept_t)((oldState & (-1 - ROUNDING_MASK))
                                         | (newMode & ROUNDING_MASK));
    asm
    {
        "fclex; fldcw %0;" : : "m" (newState);
    }
    uint mxcsr;
    asm
    {
        "stmxcsr %0" : "=m" (mxcsr);
    }
    mxcsr &= ~(ROUNDING_MASK << 3);
    mxcsr |= (newState & ROUNDING_MASK) << 3;
    mxcsr &= ~(FE_ALL_EXCEPT << 7);
    mxcsr |= (newState & FE_ALL_EXCEPT) << 7;
    asm
    {
        "ldmxcsr %0" : : "m" (mxcsr);
    }
    fexcept_t contState;
    asm pure
    {
        "fstcw %0;" : "=m" (contState);
    }
    assert(cast(uint)(contState & ROUNDING_MASK) == FE_DOWNWARD);
}

             reply	other threads:[~2020-03-31 14:46 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-31 14:46 ibuclaw at gdcproject dot org [this message]
2020-04-01  6:59 ` [Bug d/94425] " rguenth at gcc dot gnu.org
2020-04-01 17:38 ` ibuclaw at gdcproject dot org
2020-04-06 13:27 ` ibuclaw at gdcproject dot org
2020-04-07  7:44 ` cvs-commit at gcc dot gnu.org
2020-04-07 16:32 ` ibuclaw at gdcproject dot org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-94425-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).