From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-il1-x134.google.com (mail-il1-x134.google.com [IPv6:2607:f8b0:4864:20::134]) by sourceware.org (Postfix) with ESMTPS id DFE28385DC37; Thu, 25 Feb 2021 13:46:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org DFE28385DC37 Received: by mail-il1-x134.google.com with SMTP id q9so4967350ilo.1; Thu, 25 Feb 2021 05:46:15 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:reply-to :from:date:message-id:subject:to:cc; bh=BvyUFFOcRkagzmrv7VBb2NUxG3nD5ptKchzshWRiS4E=; b=p2UJGUCOzXuFWG6fHzV+AlQLIS9XYUF+Sk/+UXOlYBNQ7Y9qf8ZPUIhKPwPqifzYtV VvlZC3byQx/nM62Px7l80KFR1/+fEmtURoC5xPk76YczDxSRd5iTZRRuLkHuaJSbNjtg uYli/oZAx600OlYtPhBvVy1Hch7fgBDI0b8A9nGPsOMeD6DOllCZcOqMKhQ9aEeI2Zkd 8tgduXdrGSJ2wMSEbmiG+Fjfee/8LoH7i5hE12qeGsLf/4fsjidrANQjIz0xbIHpuoXM BBMIOPY1S/csIRn7P2zFHLSlKcrvqVvmTdw8itpeIj6k7sHKEPYb2Tl2/lJETwbvp7pV iQAQ== X-Gm-Message-State: AOAM530yoVBi/dTljgNcE1WZ/Od8VqRXA+EWCsFdCa6FEUB2b1Z0kA6T xU5rLhUqgCHVf2PhUNnBmAuzmRW+ErHzQuUCisE= X-Google-Smtp-Source: ABdhPJwNcGFaz87IR9DNwjTvv+wBPs16kEJueAxi9EtJRBOBvd5G2n/5s0avuaNEVfxXcrxxG9YwAsChhIVRVucoals= X-Received: by 2002:a05:6e02:152e:: with SMTP id i14mr2476021ilu.247.1614260775394; Thu, 25 Feb 2021 05:46:15 -0800 (PST) MIME-Version: 1.0 References: <20210224172826.GZ3008@redhat.com> <20210225115609.GE3008@redhat.com> In-Reply-To: <20210225115609.GE3008@redhat.com> Reply-To: cassio.neri@gmail.com From: Cassio Neri Date: Thu, 25 Feb 2021 13:46:04 +0000 Message-ID: Subject: Re: [PATCH 1/4] libstdc++: More efficient date from days. To: Jonathan Wakely Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 25 Feb 2021 13:46:17 -0000 Hi Jonathan, The issue is that I didn't cast __dp.count() to uint32_t: - const auto __r0 = __dp.count() + __r2_e3; + const auto __r0 = static_cast(__dp.count()) + __r2_e3; The above would be a better fix. Indeed, __r0 belongs to [0, 2^32[ which allows all arithmetics that follow to be performed on uint32_t values. For performance this is better than using signed integers. I wrongly assumed __dp.count() was int32_t which would make __r0 also uint32_t. It turns out, it is int64_t so are __r0 and other expressions including __y1 + __z2. Hence, we're losing a bit of performance. I'm glad the compiler warned us. (Although I don't understand why I didn't get the warning.) Thanks, Cassio. On Thu, Feb 25, 2021 at 11:56 AM Jonathan Wakely wrote: > > On 24/02/21 17:28 +0000, Jonathan Wakely wrote: > >On 23/02/21 13:24 +0000, Cassio Neri via Libstdc++ wrote: > >>This patch reimplements std::chrono::year_month_day::_S_from_days() which > >>retrieves a date from the number of elapsed days since 1970/01/01. The new > >>implementation is based on Proposition 6.3 of Neri and Schneider, "Euclidean > >>Affine Functions and Applications to Calendar Algorithms" available at > >>https://arxiv.org/abs/2102.06959. > >> > >>The aforementioned paper benchmarks the implementation against several > >>counterparts, including libc++'s (which is identical to the current > >>implementation). The results, shown in Figure 4, indicate the new algorithm is > >>2.2 times faster than the current one. > >> > >>The patch adds a test which loops through all integers in [-12687428, 11248737], > >>and for each of them, gets the corresponding date and compares the result > >>against its expected value. The latter is calculated using a much simpler and > >>easy to understand algorithm but which is also much slower. > >> > >>The interval used in the test covers the full range of values for which a > >>roundtrip must work [time.cal.ymd.members]. Despite its completeness the test > >>runs in a matter of seconds. > >> > >>libstdc++-v3/ChangeLog: > >> > >> * include/std/chrono: > >> * testsuite/std/time/year_month_day/3.cc: New test. > > > >Thanks! I'm committing this to trunk (it only changes new C++20 > >material so OK during stage 4 ... and anyway it's both faster and > >better tested than the old code). > > > >I've tweaked it slightly to keep some lines below 80 columns, but no > >changes except whitespace. > > I've made the attached tweak, to avoid a narrowing conversion from > long to int. Tested x86_64-linux, committed to trunk. >