public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/112566] New: Some ctz/popcount/parity/ffs optimizations
@ 2023-11-16 12:26 jakub at gcc dot gnu.org
  2023-11-16 12:27 ` [Bug tree-optimization/112566] " jakub at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2023-11-16 12:26 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 112566
           Summary: Some ctz/popcount/parity/ffs optimizations
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jakub at gcc dot gnu.org
  Target Milestone: ---

I believe
ctz(ext(x)) == ctz(x) if UB on zero,
popcount(zext(x)) == popcount(x),
parity(zext(x)) == parity(x),
parity(sext(x)) == parity(x) if the extension is by even number of bits and
ffs(ext(x)) == ffs(x).

So, e.g. with x86 -O2 -m32 -mbmi2 -mlzcnt -mpopcnt
int foo (unsigned int x) { return __builtin_ctzll (x); }
int bar (unsigned int x) { return __builtin_popcountll (x); }
int baz (unsigned int x) { return __builtin_parityll (x); }
int qux (int x) { return __builtin_ffsll (x); }
int corge (int x) { return __builtin_ctzll (x); }
int garply (int x) { return __builtin_parityll (x); }
int fred (unsigned int x) { return __builtin_ffsll (x); }
shouldn't use any double-word bit query, or similarly
int foo (unsigned _BitInt(256) x) { return __builtin_ctzg ((unsigned
_BitInt(512)) x); }
int bar (unsigned _BitInt(256) x) { return __builtin_popcountg ((unsigned
_BitInt(512)) x); }
int baz (unsigned _BitInt(256) x) { return __builtin_parityg ((unsigned
_BitInt(512)) x); }
int qux (_BitInt(256) x) { return __builtin_ffsg ((_BitInt(512)) x); }
int corge (_BitInt(256) x) { return __builtin_ctzg ((unsigned _BitInt(512)) x);
}
int garply (_BitInt(256) x) { return __builtin_parityg ((unsigned _BitInt(512))
x); }
int fred (unsigned _BitInt(256) x) { return __builtin_ffsg ((_BitInt(512)) x);
}
Of course, we shouldn't do this if we deoptimize some supported precision into
an unsupported narrower one.

For clz(zext(x)) = clz(x)+difference_in_precision, but at that point it might
not be a win.

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

end of thread, other threads:[~2023-11-17 14:11 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-16 12:26 [Bug tree-optimization/112566] New: Some ctz/popcount/parity/ffs optimizations jakub at gcc dot gnu.org
2023-11-16 12:27 ` [Bug tree-optimization/112566] " jakub at gcc dot gnu.org
2023-11-16 12:52 ` jakub at gcc dot gnu.org
2023-11-16 17:28 ` pinskia at gcc dot gnu.org
2023-11-16 17:53 ` joseph at codesourcery dot com
2023-11-17 14:11 ` cvs-commit 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).