public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "tkoenig at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug rtl-optimization/97459] __uint128_t remainder for division by 3 Date: Sun, 08 Nov 2020 19:28:04 +0000 [thread overview] Message-ID: <bug-97459-4-tRwmgIJlIT@http.gcc.gnu.org/bugzilla/> (raw) In-Reply-To: <bug-97459-4@http.gcc.gnu.org/bugzilla/> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97459 --- Comment #16 from Thomas Koenig <tkoenig at gcc dot gnu.org> --- (In reply to Jakub Jelinek from comment #15) > I plan to work on this early in stage3. > And we really shouldn't use any tables, GCC should figure it all out. > So, for double-word modulo by constant that would be expanded using a > libcall, go for x from the word bitsize to double-word bitsize and check if > (1max << x) % cst > is 1 It's probably better to search from high to low, to reduce the number of necessary shifts for division by constants like 9 or 13. > (and prefer what we've agreed on for 3), and fall back to > multiplications (see #c8) if there aren't any other options and the costs > don't say it is too costly. I think for variants where the constants aren't power of two, #define ONE ((__uint128_t) 1) #define TWO_64 (ONE << 64) #define MASK60 ((1ul << 60) - 1) void div_rem_13 (mytype n, mytype *div, unsigned int *rem) { const mytype magic = TWO_64 * 14189803133622732012u + 5675921253449092805u * ONE; /* 0xC4EC4EC4EC4EC4EC4EC4EC4EC4EC4EC5 */ __uint64_t a, b, c; unsigned int r; a = n & MASK60; b = (n >> 60); b = b & MASK60; c = (n >> 120); r = (a+b+c) % 13; n = n - r; *div = n * magic; *rem = r; } should be pretty efficient; there is only one shift which spans two words. (The assembly generated from the function looks weird because of quite a few move instructions, but that should not be an issue for code generated inline). Regarding the approach in comment #8, I think I'll run some benchmarks to see how well that works for other constants which don't fit the pattern of being divisors for 2^n-1.
next prev parent reply other threads:[~2020-11-08 19:28 UTC|newest] Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-10-16 13:33 [Bug rtl-optimization/97459] New: " tkoenig at gcc dot gnu.org 2020-10-16 15:22 ` [Bug rtl-optimization/97459] " jakub at gcc dot gnu.org 2020-10-16 15:52 ` jakub at gcc dot gnu.org 2020-10-16 15:57 ` jakub at gcc dot gnu.org 2020-10-17 12:22 ` tkoenig at gcc dot gnu.org 2020-10-18 7:33 ` tkoenig at gcc dot gnu.org 2020-10-18 9:11 ` jakub at gcc dot gnu.org 2020-10-19 11:51 ` jakub at gcc dot gnu.org 2020-10-19 12:05 ` jakub at gcc dot gnu.org 2020-10-20 18:16 ` tkoenig at gcc dot gnu.org 2020-10-23 16:56 ` tkoenig at gcc dot gnu.org 2020-10-24 18:09 ` tkoenig at gcc dot gnu.org 2020-10-24 18:14 ` tkoenig at gcc dot gnu.org 2020-10-26 5:28 ` tkoenig at gcc dot gnu.org 2020-11-08 9:31 ` tkoenig at gcc dot gnu.org 2020-11-08 10:24 ` jakub at gcc dot gnu.org 2020-11-08 19:28 ` tkoenig at gcc dot gnu.org [this message] 2020-11-08 19:33 ` tkoenig at gcc dot gnu.org 2020-11-26 19:12 ` jakub at gcc dot gnu.org 2020-11-27 13:26 ` jakub at gcc dot gnu.org 2020-11-27 15:10 ` jakub at gcc dot gnu.org 2020-11-27 18:34 ` jakub at gcc dot gnu.org 2020-11-30 9:57 ` cvs-commit at gcc dot gnu.org 2020-12-01 15:25 ` cvs-commit at gcc dot gnu.org 2020-12-02 10:41 ` cvs-commit at gcc dot gnu.org 2020-12-02 13:36 ` jakub at gcc dot gnu.org 2020-12-03 19:55 ` tkoenig at gcc dot gnu.org 2021-08-15 11:24 ` pinskia at gcc dot gnu.org
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=bug-97459-4-tRwmgIJlIT@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.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: linkBe 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).