public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/113811] New: std::rotate does 64-bit signed division
@ 2024-02-07 16:15 terra at gnome dot org
  2024-02-08  9:13 ` [Bug libstdc++/113811] " rguenth at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: terra at gnome dot org @ 2024-02-07 16:15 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 113811
           Summary: std::rotate does 64-bit signed division
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: terra at gnome dot org
  Target Milestone: ---

In stl_algo.h, function __rotate for RandomAccessIterator lines 1280-1362 for
me, there are two divisions of integers:

__n %= __k;

on lines 1332 and 1356.  They look harmless.

But in the common case on x86_64 where _Distance is, essentially, int64_t this
is a 64-bit signed division which is absurdly slow.

By my reading of https://www.agner.org/optimize/instruction_tables.pdf page
296:

64-bit signed: 57 cycles
64-bit unsigned: 36 cycles
smaller sizes: 10 cycles

(excluding the 64-to-128-bit sign extension needed too)

I believe the numbers involved are all positive, so at the very least the
division could be unsigned.  It might even make sense to check if __n is
smaller than 2^32 and do a 32-bit division instead.

Somewhat related: bug 102580

Note: I do not actually have benchmark results that show this matters in a
practical case.

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

end of thread, other threads:[~2024-02-15 12:45 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-07 16:15 [Bug libstdc++/113811] New: std::rotate does 64-bit signed division terra at gnome dot org
2024-02-08  9:13 ` [Bug libstdc++/113811] " rguenth at gcc dot gnu.org
2024-02-08 10:21 ` redi at gcc dot gnu.org
2024-02-08 21:31 ` redi at gcc dot gnu.org
2024-02-15 11:44 ` cvs-commit at gcc dot gnu.org
2024-02-15 12:45 ` redi 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).