From patchwork Sun Nov 5 15:38:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cassio Neri X-Patchwork-Id: 161649 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:8f47:0:b0:403:3b70:6f57 with SMTP id j7csp2183054vqu; Sun, 5 Nov 2023 07:39:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IE8ofmaAvg9RpTyijjd8jrYIwYb+cXNZ9CauEOfcAAgu1Li+TBT0lTTnVo3sJ3QjOETOX6k X-Received: by 2002:a05:620a:45a3:b0:775:6726:7e77 with SMTP id bp35-20020a05620a45a300b0077567267e77mr32551387qkb.10.1699198744015; Sun, 05 Nov 2023 07:39:04 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1699198744; cv=pass; d=google.com; s=arc-20160816; b=zvfWfnbFB+F5zAh5TsNvYqZvPZxWQJyPUTtwMMQRjg+MD7lmzZVx4MeZtAA5jlyTUc f3hY44R2C+B6oI+aZG+gCf16oPMCNPzzq3lqAlJHSO6XOjRbAuexdNpGUmN9rSLVwPEI r+sbZbpvfETXnwzaDZ968iy5IMR6axdOz6l4FAOjakyvLyq0/JCpCfG+3fyNhSD3ZZ9q 7/LP0I0epOxEOPRuezz0ANw2Cm7Y4j6UakRmljK5EECDyZh2pblGJkIlltTAM101z0T1 IU52W/u0Eew+gq3593UAVm6C0XMCLYxk1ttSbgPpk0yx7FVDYgYsZQTfgRsy8kuGTwaB n7eQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:to:subject:message-id:date:from :mime-version:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=eOq7ppJAGHsL7Jfj0awviU58lkwq5mnl/nuxHnyQTWs=; fh=dtVPMtB1ZqFgz1AUDGKdMG+YGuFxP/9Zc658CfWYEP0=; b=FyZNjpe9OqPg/t5i9HhJg+UAFieLop6sgV6IAUi4X3RuCCM6b4dPeWRvwPdKSDILm6 pLulsmQc1Zumst9XSznKQX6jWppxGGxg31rXu2x64gurmHKaxYDZxf/xy8oCvxPL/Qkw wQWJoz4f1COkYIsgnIpu9ygO3jAPng31ey/lglRj6gwJb7gKSAGe7ld+Q+G3aviIxBvk 6AtnGtKve5fIZ7RBAYNIagq6T6AwpUP9fkKs7HjF7RxOWFHeGYaf0VIZkgxKdxkFM8V3 r9tnQ+N570Swp5cu8Oi18GXWRmiHdEFhPk9zk4/EY9A5AeEEO0DWTM7/3daH9PrmXt4u p9tg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Md5Y7ZBU; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id s10-20020a05620a254a00b0077b94c18fe3si575196qko.625.2023.11.05.07.39.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Nov 2023 07:39:03 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Md5Y7ZBU; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B4E493858434 for ; Sun, 5 Nov 2023 15:39:03 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-yb1-xb2a.google.com (mail-yb1-xb2a.google.com [IPv6:2607:f8b0:4864:20::b2a]) by sourceware.org (Postfix) with ESMTPS id 947993858CDA; Sun, 5 Nov 2023 15:38:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 947993858CDA 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 947993858CDA Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::b2a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699198721; cv=none; b=sBBDOM/l9dzQwPiwcgJ3HPorDooiEOTpEn3Xgv4/3YVBn11p2jzebiKuMjHLqf1Nwv3o77sfVmkGyxCy5gtL8TxLFE6D+/C7NUDyqfbEQs0O6Qr2TEQRV+uYjLcqsmgiTkpUUvDAictrrRU8I/UG9ltkjs03gn/tGAg4iokNG6Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699198721; c=relaxed/simple; bh=6tMG8pd1ZVzj4XHq3OfvRhPzbXTsMg+UVixAxFapCXg=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=cNpBinodFqHwRUWnjmBhwf3zX0/4Vsagifhk5544yoodIw1bHiuDB3maXtgIkL1rz2F5x1a1qtHpDTBRSDU8atElsTT0SwORsVD2Q5urSuSwtKdIZ/7hj+OAVfMEFZA5U97nu0ppdj4eCMy96XYDomHx/6Jj+EVUyyDaL9GLeBw= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yb1-xb2a.google.com with SMTP id 3f1490d57ef6-d9a58f5f33dso3685175276.1; Sun, 05 Nov 2023 07:38:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699198716; x=1699803516; darn=gcc.gnu.org; h=to:subject:message-id:date:from:reply-to:mime-version:from:to:cc :subject:date:message-id:reply-to; bh=eOq7ppJAGHsL7Jfj0awviU58lkwq5mnl/nuxHnyQTWs=; b=Md5Y7ZBU7fYF0OIGD/ONP+22XwBv7SKa0Vg2o8FBl3BBsOQQu6fW1nrw4b1LWLlNZ1 a9Q1E0Ld3MkqPtt4lks3ABkKw9fUHDouGFBfmedVr3IPoPISnO3kbPkiE622gC4ISnMT g7J1Ws9PhH77AEVkEMsc0y2F3LrvRUeuSiiTWZ3hz1vVpBANvHbQa4mmilgoE20+qmGY clKDxIbChXQfxRU2dOzFdSKjpDJVtJFm5GUaR3vRSECpdGUmtveTLFvz/VibchY90VUa Xlqwlj70cnaMaVIbiQXg+FbzGIohXIGp1jQ1UX4aSeaH02beWCr5WlWYkty1jy1+VVWW SYfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699198716; x=1699803516; h=to:subject:message-id:date:from:reply-to:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=eOq7ppJAGHsL7Jfj0awviU58lkwq5mnl/nuxHnyQTWs=; b=koqI4aUJcJQvETqdovaR0hZTlxFl9pdclSzEfhFwNVGx4VjcNfARRt4G9N3kUTN2wO eqX4QIi6Fg6hWRQJ+zsKWXWrzpPoAmufCx8iAO9KY+rGRDN3g3i8WgLgJjNGPswBcBPf Z2gn2U+Nn4mTIon9+Rzjs6RT5IGb3VlpPy1S5QhDfohO8A6XgyUL82hSERtMygsj1twP cWAGXJ/GHoFyiB9KphPNnQIOlU6wO/W7AhycVCZvDOIdANdRt6oG03+dNt5h6o1iFCKH +X/QpZo9DH40kSU8S7usp/2ngx/1UTNfrDp+s6dqJicAXLdRubjAzOWo/cXGSO58qREX HSJQ== X-Gm-Message-State: AOJu0YxK61uqacldLNrap9TZUNhF3bzuKgCVlbVMZ3DSETX9+GnUOmoH 4Dd0r6lOeRWeBMdBg31CObg0Kv2clqrLiGZrFAa+k4xTc0o= X-Received: by 2002:a25:ac07:0:b0:d9b:d0cb:db74 with SMTP id w7-20020a25ac07000000b00d9bd0cbdb74mr25951605ybi.33.1699198716354; Sun, 05 Nov 2023 07:38:36 -0800 (PST) MIME-Version: 1.0 From: Cassio Neri Date: Sun, 5 Nov 2023 15:38:25 +0000 Message-ID: Subject: [PATCH] Remove unnecessary "& 1" in year_month_day_last::day() To: "libstdc++" , Gcc-patches X-Spam-Status: No, score=-8.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, HTML_MESSAGE, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: cassio.neri@gmail.com Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781739021836530054 X-GMAIL-MSGID: 1781739021836530054 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(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