Message ID | CAOfgUPhjCZKFuOB+84b-b_0a_uMq=bsT3HUks6Q7eCaJpDrrDQ@mail.gmail.com |
---|---|
State | New |
Headers | show |
Series | Remove unnecessary "& 1" in year_month_day_last::day() | expand |
On Sun, 5 Nov 2023, Cassio Neri wrote: > When year_month_day_last::day() was implemented, Dr. Matthias Kretz realised > that the operation "& 1" wasn't necessary but we did not patch it at that > time. This patch removes the unnecessary operation. Is there an entry in gcc's bugzilla about having the optimizer handle this kind of optimization? unsigned f(unsigned x){ if(x>=32)__builtin_unreachable(); return 30|(x&1); // --> 30|x } (that optimization would come in addition to your patch, doing the optimization by hand is still a good idea) It looks like the criterion would be a|(b&c) when the possible 1 bits of b are included in the certainly 1 bits of a|c.
I could not find any entry in gcc's bugzilla for that. Perhaps my search wasn't good enough. On Sun, 5 Nov 2023 at 15:58, Marc Glisse <marc.glisse@inria.fr> wrote: > On Sun, 5 Nov 2023, Cassio Neri wrote: > > > When year_month_day_last::day() was implemented, Dr. Matthias Kretz > realised > > that the operation "& 1" wasn't necessary but we did not patch it at that > > time. This patch removes the unnecessary operation. > > Is there an entry in gcc's bugzilla about having the optimizer handle this > kind of optimization? > > unsigned f(unsigned x){ > if(x>=32)__builtin_unreachable(); > return 30|(x&1); // --> 30|x > } > > (that optimization would come in addition to your patch, doing the > optimization by hand is still a good idea) > > It looks like the criterion would be a|(b&c) when the possible 1 bits of b > are included in the certainly 1 bits of a|c. > > -- > Marc Glisse >
On Sun, Nov 5, 2023 at 9:13 AM Cassio Neri <cassio.neri@gmail.com> wrote: > > I could not find any entry in gcc's bugzilla for that. Perhaps my search wasn't good enough. I filed https://gcc.gnu.org/PR112395 with a first attempt at the patch (will double check it soon). Thanks, Andrew > > > On Sun, 5 Nov 2023 at 15:58, Marc Glisse <marc.glisse@inria.fr> wrote: >> >> On Sun, 5 Nov 2023, Cassio Neri wrote: >> >> > When year_month_day_last::day() was implemented, Dr. Matthias Kretz realised >> > that the operation "& 1" wasn't necessary but we did not patch it at that >> > time. This patch removes the unnecessary operation. >> >> Is there an entry in gcc's bugzilla about having the optimizer handle this >> kind of optimization? >> >> unsigned f(unsigned x){ >> if(x>=32)__builtin_unreachable(); >> return 30|(x&1); // --> 30|x >> } >> >> (that optimization would come in addition to your patch, doing the >> optimization by hand is still a good idea) >> >> It looks like the criterion would be a|(b&c) when the possible 1 bits of b >> are included in the certainly 1 bits of a|c. >> >> -- >> Marc Glisse
diff --git a/libstdc++-v3/include/std/chrono b/libstdc++-v3/include/std/chrono index 10e868e5a03..c979a5d05dd 100644 --- a/libstdc++-v3/include/std/chrono +++ b/libstdc++-v3/include/std/chrono @@ -1800,8 +1800,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { const auto __m = static_cast<unsigned>(month()); - // Excluding February, the last day of month __m is either 30 or 31 or, - // in another words, it is 30 + b = 30 | b, where b is in {0, 1}. + // Assume 1 <= __m <= 12, otherwise month().ok() == false and the result + // of day() is unspecified. Excluding February, the last day of month __m + // m is either 30 or 31 or, in another words, it is 30 | b, where b is in + // {0, 1}. // If __m in {1, 3, 4, 5, 6, 7}, then b is 1 if, and only if __m is odd. // Hence, b = __m & 1 = (__m ^ 0) & 1. @@ -1812,10 +1814,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // Therefore, b = (__m ^ c) & 1, where c = 0, if __m < 8, or c = 1 if // __m >= 8, that is, c = __m >> 3. + // Since 30 = (11110)_2 and __m <= 31 = (11111)_2, we have: + // 30 | ((__m ^ c) & 1) == 30 | (__m ^ c), that is, the "& 1" is + // unnecessary. + // The above mathematically justifies this implementation whose // performance does not depend on look-up tables being on the L1 cache. - return chrono::day{__m != 2 ? ((__m ^ (__m >> 3)) & 1) | 30 - : _M_y.is_leap() ? 29 : 28}; + return chrono::day{__m != 2 ? (__m ^ (__m >> 3)) | 30 + : _M_y.is_leap() ? 29 : 28}; } constexpr