From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by sourceware.org (Postfix) with ESMTPS id BC4203858D28; Sat, 11 Nov 2023 16:46:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BC4203858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BC4203858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::329 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699721171; cv=none; b=t5SjpUCw8zeCHd1L/XGc3m1VbhzjfmyC3Hf5501GnsAK8Wd3du5XDDNfL2tqLN2QGFnhvC6aHY90+iOPCjeKsB1pJEt5UPH4YVUs0mspIsqfcvBQABgtcznk/GrEK7hB8JeVmAjNIIEmKX5LvWT6BLT3igx4dMr4jxElgNXXgC8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699721171; c=relaxed/simple; bh=rwY2+IBhUiIJd3VUu+eDT3b9JOMjas2yHO5WntzdCA8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ZoqTHq6AdjBZAaKXx68rTp5eU35bNP+VsnrTtceMpEcYn9dKhENXAC9xcCjc56NV5av3MGNi1CffgqGOsekVD3hxIOS2kYq1c3cl1c44IoNbaBNCRVNb+ShzLSU055ehdhMXOFozSQ+MJr+zqU2pyPNuGpSyZ75934c4ksz9Jpg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-40859dee28cso24206835e9.0; Sat, 11 Nov 2023 08:46:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699721168; x=1700325968; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=0bWqD7ZZ9Y0ytZHzHX6SO5AVbY9sIk/R0KoVtd5nN+E=; b=U2y68lTpQF7oF0aj4cSa5Hmpf53VUYf4Pp29vNriIZXPHNMjkZnG9BrYpFK3TWPGK2 SA0LrkOwcXrdCAitceOA4pA7wTfZQzi/X6VIuZlYfvjlFtLCNiWRbeLOZk0eq+pBUO7D 11yI/oxniW7G5dfh35GVFghotNSWryVm01Op9A1p/3k+LREuVrwHeUsmYQ5harTOvz4o 2wfBnadY9DlHBFJ7oP8aYvZseASJ4QIzlNuc3x1/6BxBWI5dfKBWpvRQAaqcrr7Qjkc9 b53jnF5KSvISwDXnqVy6lg6SyglJxctI7e3uhgEIZV5/mE/fJR1M5aSGsbdQ4Zap1f8F u7vA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699721168; x=1700325968; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=0bWqD7ZZ9Y0ytZHzHX6SO5AVbY9sIk/R0KoVtd5nN+E=; b=NcbF/DryrqGU3+OQyEcQElGYr/RM25n/AOGOB6DR+QgeGjhl8GQLZwMLcxPcue1/+d ZgsHDZIxjcIfZJVy/9kWVxHumpB2lnqkB2ali7t3kqKXPX8q76BmFOKohdSf7KOU8rWY fA9l2hnGNSspOzofp8UjeSf6kfRDvXPXJH79ZsTtNXQMV+PMNfakFTarraK1mDEfERv3 jJPYDo5wj/5uRvVMmSXJ6ILYgpOHdB783WogYcEqZ7BIOqfL5LZEr2B5ZU9fDo0FJox/ 0SjjekDzvb+LROqN54onBc7OfNwXw8xGdhGofjC69VwdJ8RV9wKOZ5RP73aFLdrp009N kAhQ== X-Gm-Message-State: AOJu0YxWpnVte8NJu8vXlKaiG+QSZA6gwyDB+UQvw9htgLneKQfppYfO dDwkaEqB0TSfc5FA7YP2jVD4wPPKeVY= X-Google-Smtp-Source: AGHT+IF3kHVlQYVVEnxLvUrSuzVsjJrlIvxTl5d0Td6OpK9DLAcGkjX0JD9yO6oH3y776B/WS5Mi8g== X-Received: by 2002:a05:600c:4755:b0:406:84a0:bc87 with SMTP id w21-20020a05600c475500b0040684a0bc87mr1712107wmo.15.1699721167952; Sat, 11 Nov 2023 08:46:07 -0800 (PST) Received: from othello.cust.communityfibre.co.uk ([2a02:6b64:8086:0:f9dd:28dd:c9b9:d8f4]) by smtp.gmail.com with ESMTPSA id c20-20020a05600c0ad400b0040588d85b3asm8238935wmr.15.2023.11.11.08.46.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Nov 2023 08:46:07 -0800 (PST) From: Cassio Neri To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Cassio Neri Subject: [PATCH v4] libstdc++: Remove unnecessary "& 1" from year_month_day_last::day(). Date: Sat, 11 Nov 2023 16:44:58 +0000 Message-ID: <20231111164458.46230-1-cassio.neri@gmail.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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: Remove &1 from year_month_day_last::day(). --- Previous versions of this patch failed to apply. I hope it works this time. OK for trunk? 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 10e868e5a03..a826982803b 100644 --- a/libstdc++-v3/include/std/chrono +++ b/libstdc++-v3/include/std/chrono @@ -1800,22 +1800,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 -- 2.41.0