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: 1859478 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=Md5Y7ZBU; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SNdtk68Tqz1yQ5 for ; Mon, 6 Nov 2023 02:38:53 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5C8E03858C54 for ; Sun, 5 Nov 2023 15:38:51 +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-Google-Smtp-Source: AGHT+IG1dWX5pi/v3w+AXwMP/3P2GGE3B9TT6Dm93r4Lwufb4OrpfoDDbR2bFq/iiuEFQW7yluvFemmsLECxmF/GhfU= 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+incoming=patchwork.ozlabs.org@gcc.gnu.org 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