public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r12-10211] libstdc++: Improve operator-(weekday x, weekday y)
Date: Wed, 13 Mar 2024 09:53:04 +0000 (GMT)	[thread overview]
Message-ID: <20240313095304.5FF273857C65@sourceware.org> (raw)

https://gcc.gnu.org/g:a491dd0eac360758dd20b29bef78b27d005547a1

commit r12-10211-ga491dd0eac360758dd20b29bef78b27d005547a1
Author: Cassio Neri <cassio.neri@gmail.com>
Date:   Tue Nov 14 00:27:39 2023 +0000

    libstdc++: Improve operator-(weekday x, weekday y)
    
    The current implementation calls __detail::__modulo which is relatively
    expensive.
    
    A better implementation is possible if we assume that x.ok() && y.ok() == true,
    so that n = x.c_encoding() - y.c_encoding() is in [-6, 6]. In this case, it
    suffices to return n >= 0 ? n : n + 7.
    
    The above is allowed by [time.cal.wd.nonmembers]/5: the returned value is
    unspecified when x.ok() || y.ok() == false.
    
    The assembly emitted for x86-64 and ARM can be seen in:
    https://godbolt.org/z/nMdc5vv9n.
    
    libstdc++-v3/ChangeLog:
    
            * include/std/chrono (operator-(const weekday&, const weekday&)):
            Optimize.
    
    (cherry picked from commit f71352c71d78ac977ea0e71a6900699a8cf09219)

Diff:
---
 libstdc++-v3/include/std/chrono | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libstdc++-v3/include/std/chrono b/libstdc++-v3/include/std/chrono
index 0d3c0819439..3c5b425c7fe 100644
--- a/libstdc++-v3/include/std/chrono
+++ b/libstdc++-v3/include/std/chrono
@@ -686,8 +686,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       friend constexpr days
       operator-(const weekday& __x, const weekday& __y) noexcept
       {
-	auto __n = static_cast<long long>(__x._M_wd) - __y._M_wd;
-	return days{__detail::__modulo(__n, 7)};
+	const auto __n = __x.c_encoding() - __y.c_encoding();
+	return static_cast<int>(__n) >= 0 ? days{__n} : days{__n + 7};
       }
 
       // TODO: operator<<, from_stream.

                 reply	other threads:[~2024-03-13  9:53 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240313095304.5FF273857C65@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).