public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug rtl-optimization/97738] New: Optimizing division by value & - value for HAKMEM 175
@ 2020-11-06  8:23 tkoenig at gcc dot gnu.org
  2020-11-06  8:45 ` [Bug middle-end/97738] " rguenth at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: tkoenig at gcc dot gnu.org @ 2020-11-06  8:23 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 97738
           Summary: Optimizing division by value & - value for HAKMEM 175
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tkoenig at gcc dot gnu.org
  Target Milestone: ---

A straightforward implementation of HAKMEM 175 (returning
the next number with the same number of bits) is

unsigned int
next_same_bit (unsigned int value)
{
  unsigned int lowest_bit;
  unsigned int left_bits;
  unsigned int changed_bits;
  unsigned int right_bits;

  lowest_bit = value & - value;
  left_bits = value + lowest_bit;
  changed_bits = value ^ left_bits;
  right_bits = (changed_bits / lowest_bit) >> 2;
  return left_bits | right_bits;
}

In two's complement, this can be replaced by

unsigned int
next_s_bit (unsigned int value)
{
  unsigned int lowest_bit;
  unsigned int ctz;
  unsigned int left_bits;
  unsigned int changed_bits;
  unsigned int right_bits;

  ctz = __builtin_ctz (value);
  lowest_bit = 1u << ctz;
  left_bits = value + lowest_bit;
  changed_bits = value ^ left_bits;
  right_bits = changed_bits >> (ctz + 2);
  return left_bits | right_bits;
}

to replace the expensive division by what is known to be a
power of two by a shift.

That transformation is counter-productive (and might be done
the other way) if there is no division by lowest_bit.

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

end of thread, other threads:[~2021-09-26  8:24 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-06  8:23 [Bug rtl-optimization/97738] New: Optimizing division by value & - value for HAKMEM 175 tkoenig at gcc dot gnu.org
2020-11-06  8:45 ` [Bug middle-end/97738] " rguenth at gcc dot gnu.org
2020-11-06 14:21 ` tkoenig at gcc dot gnu.org
2020-11-06 17:52 ` tkoenig at gcc dot gnu.org
2020-11-06 18:27 ` jakub at gcc dot gnu.org
2020-11-07 10:21 ` tkoenig at gcc dot gnu.org
2021-09-26  8:23 ` pinskia at gcc dot gnu.org
2021-09-26  8:24 ` 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).