diff mbox series

libstdc++: Improve operator-(weekday x, weekday y).

Message ID 20231114002739.14809-1-cassio.neri@gmail.com
State New
Headers show
Series libstdc++: Improve operator-(weekday x, weekday y). | expand

Commit Message

Cassio Neri Nov. 14, 2023, 12:27 a.m. UTC
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:
---

OK for trunk?

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

--
2.41.0

Comments

Jonathan Wakely Nov. 14, 2023, 10:48 p.m. UTC | #1
On Tue, 14 Nov 2023 at 00:27, Cassio Neri <cassio.neri@gmail.com> wrote:
>
> 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:
> ---
>
> OK for trunk?

Pushed to trunk, thanks!


>
>  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 10e868e5a03..6131e7e97b3 100644
> --- a/libstdc++-v3/include/std/chrono
> +++ b/libstdc++-v3/include/std/chrono
> @@ -1036,8 +1036,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};
>        }
>      };
>
> --
> 2.41.0
>
diff mbox series

Patch

diff --git a/libstdc++-v3/include/std/chrono b/libstdc++-v3/include/std/chrono
index 10e868e5a03..6131e7e97b3 100644
--- a/libstdc++-v3/include/std/chrono
+++ b/libstdc++-v3/include/std/chrono
@@ -1036,8 +1036,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};
       }
     };