public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: Carlos O'Donell <carlos@redhat.com>
Cc: Adhemerval Zanella <adhemerval.zanella@linaro.org>,
	libc-alpha@sourceware.org
Subject: Re: [PATCH v2 6/7] alpha: Fix fesetexceptflag (BZ 30998)
Date: Mon, 06 Nov 2023 18:36:09 +0100	[thread overview]
Message-ID: <6460552.QZNE9M9tJY@nimes> (raw)
In-Reply-To: <cc72427f-7a66-3c3c-2d62-7893aded8655@redhat.com>

Carlos O'Donell wrote:
> I would expect (taken from hppa code I wrote for that port):
> 
> /* Clear all status bits we care about.  */
> tmp = tmp & ~(excepts & SWCR_STATUS_MASK);
> /* Install the new ones.  */
> tmp |= *flagp & excepts & SWCR_STATUS_MASK;

This code is correct. And, as I wrote in
https://sourceware.org/bugzilla/show_bug.cgi?id=30998#c2
the code with the double-xor is faster, because it uses one less
instruction.

Basically the code you proposed masks the bits to clear
and the bits to set separately. Whereas the code with the double-xor
masks the bits to *change* - a single AND instead of two.

> >    /* Set all the bits that were called for.  */
> > -  tmp = (tmp & ~SWCR_STATUS_MASK) | (*flagp & excepts & SWCR_STATUS_MASK);
> > +  tmp ^= (tmp ^ *flagp) & excepts & SWCR_STATUS_MASK;
> 
> Does this actually work?

Yes: It computes the bits to *change*, then applies the mask, then applies
the changes.

For bits in the mask, it does
        tmp.bit[i] ^= tmp.bit[i] ^ (*flagp).bit[i];
which simplifies to
        tmp.bit[i] = (*flagp).bit[i];

For bits outside the mask, it does
        tmp.bit[i] ^= 0;
i.e. it leaves the bit unchanged.

I'm using this same idiom also in gnulib
https://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob;f=lib/fenv-except-state-set.c;h=2035f61e2714126e432c0c1f29450f18f58d81f1;hb=HEAD#l182
and I have verified that it works.

> Assume excepts is FE_INVALID, and *flagp bit 17 is 0.
> Assume currently bit 17 is 1.
> 
> tmp ^ *flagp       => bit 17 is still 1, even though bit 17 in flagp is 0.
> & excepts          => bit 17 is still 1.
> & SWCR_STATUS_MASK => bit 17 is still 1.
> ^=                 => bit 17 is still 1.

In the last step: 1 xor 1 is 0.

Bruno




  reply	other threads:[~2023-11-06 17:36 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-06 13:27 [PATCH v2 0/7] Multiple floating-point environment fixes Adhemerval Zanella
2023-11-06 13:27 ` [PATCH v2 1/7] powerpc: Do not raise exception traps for fesetexcept/fesetexceptflag (BZ 30988) Adhemerval Zanella
2023-11-06 16:08   ` Carlos O'Donell
2023-11-06 16:50     ` Adhemerval Zanella Netto
2023-11-06 17:02       ` Carlos O'Donell
2023-11-06 17:11         ` Adhemerval Zanella Netto
2023-11-06 17:37           ` Adhemerval Zanella Netto
2023-11-06 17:38           ` Carlos O'Donell
2023-11-06 17:56             ` Adhemerval Zanella Netto
2023-11-06 20:46               ` Adhemerval Zanella Netto
2023-11-23 21:47                 ` Carlos O'Donell
2023-11-24 12:28                   ` Adhemerval Zanella Netto
2023-11-24 12:37                     ` Adhemerval Zanella Netto
2023-11-24 16:22                     ` Carlos O'Donell
2023-11-24 17:53                       ` Adhemerval Zanella Netto
2023-11-24 18:15                         ` Carlos O'Donell
2023-11-24 18:46                           ` Adhemerval Zanella Netto
2023-11-27 13:46                             ` Adhemerval Zanella Netto
2023-12-19 14:57                               ` Carlos O'Donell
2023-11-06 13:27 ` [PATCH v2 2/7] i686: Do not raise exception traps on fesetexcept (BZ 30989) Adhemerval Zanella
2023-11-06 16:14   ` Carlos O'Donell
2023-11-06 13:27 ` [PATCH v2 3/7] x86: Do not raises floating-point exception traps on fesetexceptflag (BZ 30990) Adhemerval Zanella
2023-11-06 16:16   ` Carlos O'Donell
2023-11-06 13:27 ` [PATCH v2 4/7] manual: Clarify undefined behavior of feenableexcept (BZ 31019) Adhemerval Zanella
2023-11-06 16:17   ` Carlos O'Donell
2023-11-06 13:27 ` [PATCH v2 5/7] riscv: Fix feenvupdate with FE_DFL_ENV (BZ 31022) Adhemerval Zanella
2023-11-06 16:19   ` Carlos O'Donell
2023-11-06 13:27 ` [PATCH v2 6/7] alpha: Fix fesetexceptflag (BZ 30998) Adhemerval Zanella
2023-11-06 16:54   ` Carlos O'Donell
2023-11-06 17:36     ` Bruno Haible [this message]
2023-11-06 18:15       ` Carlos O'Donell
2023-11-06 13:27 ` [PATCH v2 7/7] hppa: Fix undefined behaviour in feclearexcept (BZ 30983) Adhemerval Zanella
2023-11-06 16:57   ` Carlos O'Donell

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=6460552.QZNE9M9tJY@nimes \
    --to=bruno@clisp.org \
    --cc=adhemerval.zanella@linaro.org \
    --cc=carlos@redhat.com \
    --cc=libc-alpha@sourceware.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).