public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/113353] New: Wrong rounding in std::nearbyint when vectorized with -funsafe-math-optimizations on PPC
@ 2024-01-12 12:02 alexander.grund@tu-dresden.de
  2024-01-13  2:07 ` [Bug target/113353] " pinskia at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: alexander.grund@tu-dresden.de @ 2024-01-12 12:02 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 113353
           Summary: Wrong rounding in std::nearbyint when vectorized with
                    -funsafe-math-optimizations on PPC
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: alexander.grund@tu-dresden.de
  Target Milestone: ---

According to the standard `std::nearbyint` and `std::rint` are supposed to give
the same results. However the former does not tie to even in halfway cases when
used in a tight loop.

The following code basically rounding "-8.5" produces `-9` when compiled with
`-funsafe-math-optimizations` but (as actually correct) `-8` when compiled
without it or when using `std::rint` instead. (Additional flags: `-O3
-mcpu=power9`)

This is at least unexpected given that
> The only difference between std::nearbyint and std::rint is that std::nearbyint never raises FE_INEXACT. 

which in this case does not hold.

#include <iostream>
#include <cmath>
#include <vector>

extern "C" void kernel(const double* in_ptr,
                       double* out_ptr,
                       const long ks)
{
    for(long i=0; i<ks; i+=1)
        out_ptr[i] = std::nearbyint(in_ptr[i]);
}


int main()
{
    std::vector<double> in(16, -8.5);
    decltype(in) out(in.size());
    kernel(in.data(), out.data(), in.size());
    for(const auto t: out)
        std::cout << t << ", ";
    std::cout << '\n';
}

Is this an intended effect of -funsafe-math-optimizations? I'd expect to at
least be able to workaround this with -frounding-math or so.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug target/113353] Wrong rounding in std::nearbyint when vectorized with -funsafe-math-optimizations on PPC
  2024-01-12 12:02 [Bug target/113353] New: Wrong rounding in std::nearbyint when vectorized with -funsafe-math-optimizations on PPC alexander.grund@tu-dresden.de
@ 2024-01-13  2:07 ` pinskia at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-01-13  2:07 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
https://gcc.gnu.org/pipermail/gcc-patches/2009-July/266131.html

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-01-13  2:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-12 12:02 [Bug target/113353] New: Wrong rounding in std::nearbyint when vectorized with -funsafe-math-optimizations on PPC alexander.grund@tu-dresden.de
2024-01-13  2:07 ` [Bug target/113353] " pinskia at gcc dot gnu.org

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).