From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id CECFC3858CD1 for ; Tue, 14 Nov 2023 22:47:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CECFC3858CD1 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CECFC3858CD1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700002067; cv=none; b=b9+rbKOW+JqOhircRyx2K2EiM9kqs1EtuDq6XM8jf80ybmpfnIxSso6Olb2/z+xIBmBB9fmnijfLvjTfIio2eoVNWvLkEsmc6jVIvofDOM/rvYEPIXtIYAOladQKs/6SIkyq+VC7qM1dj8daJ9dgnJCGnJCSVoGcp6oCrapKW2U= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700002067; c=relaxed/simple; bh=T+ISa+E92Sq5Jaenfs6/nuJHvGJHjPbqjPmtZbeDetM=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=gQf6Sdy+u50toMeMinir8gvi32eyTWDqcWYIq/hlfJXDQX99KI/ljVL4A7Szglff244QrxQhz9dWaC3yQEuusDhabFEkix6BUQ3hWfY3chxZWwEIYFodfivsEZs8KOtKRsV6zSD1jykL9YrmsEC7HwaDMU8KUkdFa7kHGcJsTT8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700002064; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=PMhwTS6g1c37i/tgmthreVb7G1js8IgRUQnGatEcFtE=; b=ZMC7Bdkv4P35d24/kgz1WR/E81n0y3jQA7YSYeGP17Lnfjid2Ou6Kwfu5ifkGGPMNQsblX CGYjdjYbNOanBomLxIDRWkqolAJiBCjf8Vu5zgnrL9WtE7vlAPhd4Km6bFlMl0qrebT+P/ g1d1y1TC98OZ7cipbbV8yHSSP5DIYis= Received: from mail-yb1-f198.google.com (mail-yb1-f198.google.com [209.85.219.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-320-ds2T4c73MYmlG1BElcDV_A-1; Tue, 14 Nov 2023 17:47:42 -0500 X-MC-Unique: ds2T4c73MYmlG1BElcDV_A-1 Received: by mail-yb1-f198.google.com with SMTP id 3f1490d57ef6-da03390793fso7577277276.3 for ; Tue, 14 Nov 2023 14:47:42 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700002062; x=1700606862; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=PMhwTS6g1c37i/tgmthreVb7G1js8IgRUQnGatEcFtE=; b=hUipce0Bt8hIsaWvwPAecPzgQcEOAp26QvqrEjyoRgPpmLdcGNyylDvx9Jmjwc7BCJ n2vFB4NWvzLrF8fDbnwud8u/3IyTgGPithdpkpldAD1NdRpvpl5pkSjL7YZOs1Kk4Mbu HhVPH5pppHQbJotX7c+PLywKhYdI3aVoU7AaneEz8uWeauSbro2Yh9LJ5P32RU9D86cB TPYF28TSccVTOe/owpWNfTd6JqCBQmWgBpTwoe4Aw1utmpMPm9lBRmslTniW+z6jK4SQ 9Bx8IE4On0mSpISebLUdm7rRqDiTJyxbyyibon75+pA5McbFiXUx5umdlNtZdf6mQTXW Qqhg== X-Gm-Message-State: AOJu0YzRInrfFIbU/y2KSHjZdB7nXewOTa7inhsimy/A1CKtn24/mlze 2lGaABlbj8633zCNQKnUS1kMZx6p18DQ1S2U9mYEJ9ewGwlvorGcqafK7D914ZwNG1yeKr5xAq7 /x+OL9izK4/c90bL1EgznPaCHWKTX2fFMVw== X-Received: by 2002:a25:cec5:0:b0:daf:56fe:41f8 with SMTP id x188-20020a25cec5000000b00daf56fe41f8mr10455562ybe.11.1700002062472; Tue, 14 Nov 2023 14:47:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IEJbDgYlthEiIIZGw7Xx2uqE2Vd3Awd2kI8Fiy3zbGLGnOp8wBvrm3pTTxHU4vcdYBx+9V/cs9zRFHRw2P0g4U= X-Received: by 2002:a25:cec5:0:b0:daf:56fe:41f8 with SMTP id x188-20020a25cec5000000b00daf56fe41f8mr10455545ybe.11.1700002062200; Tue, 14 Nov 2023 14:47:42 -0800 (PST) MIME-Version: 1.0 References: <20231111164458.46230-1-cassio.neri@gmail.com> In-Reply-To: <20231111164458.46230-1-cassio.neri@gmail.com> From: Jonathan Wakely Date: Tue, 14 Nov 2023 22:47:31 +0000 Message-ID: Subject: Re: [PATCH v4] libstdc++: Remove unnecessary "& 1" from year_month_day_last::day(). To: Cassio Neri Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,RCVD_IN_SORBS_WEB,SPF_HELO_NONE,SPF_NONE,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: On Sat, 11 Nov 2023 at 16:46, Cassio Neri wrote: > > 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? Yes, thanks - pushed to 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 >