libstdc++: More efficient last day of month
commit8265ab07f3bbeb672488fdfc6418e0bce89dff9c
authorCassio Neri <cassio.neri@gmail.com>
Wed, 24 Feb 2021 18:12:47 +0000 (24 18:12 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Wed, 24 Feb 2021 18:25:35 +0000 (24 18:25 +0000)
tree210908aa2f048f84d549e864883cecc7387620d4
parent126793971bee0e92bea237823bdc51a594951faa
libstdc++: More efficient last day of month

This patch reimplements std::chrono::year_month_day_last:day() which yields the
last day of a particular month.  The current implementation uses a look-up table
implemented as an unsigned[12] array.  The new implementation instead
is based on
the fact that a month m in [1, 12], except for m == 2 (February), is
either 31 or
30 days long and m's length depends on two things: m's parity and whether m >= 8
or not. These two conditions are determined by the 0th and 3th bit of m and,
therefore, cheap and straightforward bit-twiddling can provide the right result.

Measurements in x86_64 [1] suggest a 10% performance boost.  Although this does
not seem to be huge, notice that measurements are done in hot L1 cache
conditions which might not be very representative of production runs. Also
freeing L1 cache from holding the look-up table might allow performance
improvements elsewhere.

References:
[1] https://github.com/cassioneri/calendar

libstdc++-v3/ChangeLog:

* include/std/chrono (year_month_day_last:day): New
implementation.
libstdc++-v3/include/std/chrono