From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 1AE4D385829C; Wed, 13 Mar 2024 09:52:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1AE4D385829C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1710323574; bh=cHKuzAC03iEuSWKlmrWoGjGeOhtqy4IfDpj2pmeaYdM=; h=From:To:Subject:Date:From; b=xl2xE0n2c+loEddG6v73Ci50tsgwycH6/VAlxfaH+NyOIrgQipMaTMPeI4FhIEEzp +228d6nMK/R3fS6pMfQ2z3HeZe133fUZWGQLVs3ew899cpu3qiMuPnuO9AdKXXXAJg EWhE9/ARAR6fZIEzs1d8ltWS3iguCI3e4mqJZCG4= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-10209] libstdc++: Remove unnecessary "& 1" from year_month_day_last::day() X-Act-Checkin: gcc X-Git-Author: Cassio Neri X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 2d5eee092aa3cbfb50496ed4b224600de2e7fb08 X-Git-Newrev: d472bd346ddd1d679ecd3ff392fda98343ed7a36 Message-Id: <20240313095254.1AE4D385829C@sourceware.org> Date: Wed, 13 Mar 2024 09:52:54 +0000 (GMT) List-Id: https://gcc.gnu.org/g:d472bd346ddd1d679ecd3ff392fda98343ed7a36 commit r12-10209-gd472bd346ddd1d679ecd3ff392fda98343ed7a36 Author: Cassio Neri Date: Sat Nov 11 16:44:58 2023 +0000 libstdc++: Remove unnecessary "& 1" from year_month_day_last::day() 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 (year_month_day_last::day): Remove &1. (cherry picked from commit b011535456396a6846ff24fb5b1baea8fe0a33b1) Diff: --- libstdc++-v3/include/std/chrono | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/libstdc++-v3/include/std/chrono b/libstdc++-v3/include/std/chrono index f5a88331b1b..5c1b7a8daf4 100644 --- a/libstdc++-v3/include/std/chrono +++ b/libstdc++-v3/include/std/chrono @@ -1468,22 +1468,26 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { const auto __m = static_cast(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}. + // The result is unspecified if __m < 1 or __m > 12. Hence, assume + // 1 <= __m <= 12. For __m != 2, day() == 30 or day() == 31 or, in + // other words, day () == 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. + // 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. - // If __m in {8, 9, 10, 11, 12}, then b is 1 if, and only if __m is even. - // Hence, b = (__m ^ 1) & 1. + // If __m in {8, 9, 10, 11, 12}, then b is 1 if, and only if, __m is + // even. Hence, b = (__m ^ 1) & 1. // Therefore, b = (__m ^ c) & 1, where c = 0, if __m < 8, or c = 1 if // __m >= 8, that is, c = __m >> 3. - // 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}; + // Since 30 = (11110)_2 and __m <= 31 = (11111)_2, the "& 1" in b's + // calculation is unnecessary. + + // The performance of this implementation does not depend on look-up + // tables being on the L1 cache. + return chrono::day{__m != 2 ? (__m ^ (__m >> 3)) | 30 + : _M_y.is_leap() ? 29 : 28}; } constexpr