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