* [PATCH] Remove unnecessary "& 1" in year_month_day_last::day()
@ 2023-11-05 15:38 Cassio Neri
2023-11-05 15:58 ` Marc Glisse
0 siblings, 1 reply; 4+ messages in thread
From: Cassio Neri @ 2023-11-05 15:38 UTC (permalink / raw)
To: libstdc++, Gcc-patches
[-- Attachment #1: Type: text/plain, Size: 1728 bytes --]
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.
libstdc++-v3/ChangeLog:
* include/std/chrono:
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] Remove unnecessary "& 1" in year_month_day_last::day()
2023-11-05 15:38 [PATCH] Remove unnecessary "& 1" in year_month_day_last::day() Cassio Neri
@ 2023-11-05 15:58 ` Marc Glisse
2023-11-05 16:09 ` Cassio Neri
0 siblings, 1 reply; 4+ messages in thread
From: Marc Glisse @ 2023-11-05 15:58 UTC (permalink / raw)
To: Cassio Neri; +Cc: libstdc++, Gcc-patches
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] Remove unnecessary "& 1" in year_month_day_last::day()
2023-11-05 15:58 ` Marc Glisse
@ 2023-11-05 16:09 ` Cassio Neri
2023-11-05 19:54 ` Andrew Pinski
0 siblings, 1 reply; 4+ messages in thread
From: Cassio Neri @ 2023-11-05 16:09 UTC (permalink / raw)
To: libstdc++; +Cc: Gcc-patches
[-- Attachment #1: Type: text/plain, Size: 900 bytes --]
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
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] Remove unnecessary "& 1" in year_month_day_last::day()
2023-11-05 16:09 ` Cassio Neri
@ 2023-11-05 19:54 ` Andrew Pinski
0 siblings, 0 replies; 4+ messages in thread
From: Andrew Pinski @ 2023-11-05 19:54 UTC (permalink / raw)
To: cassio.neri; +Cc: libstdc++, Gcc-patches
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
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-11-05 19:55 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-05 15:38 [PATCH] Remove unnecessary "& 1" in year_month_day_last::day() Cassio Neri
2023-11-05 15:58 ` Marc Glisse
2023-11-05 16:09 ` Cassio Neri
2023-11-05 19:54 ` Andrew Pinski
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).