From patchwork Tue Nov 14 00:27:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cassio Neri X-Patchwork-Id: 1863418 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=EpOiQcTs; 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 4STnFc0lm2z1yRV for ; Tue, 14 Nov 2023 11:28:02 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D9BA2383F18A for ; Tue, 14 Nov 2023 00:27:59 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by sourceware.org (Postfix) with ESMTPS id 8D8423858D35; Tue, 14 Nov 2023 00:27:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8D8423858D35 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 8D8423858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::32d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699921667; cv=none; b=XMH6EPwu3Vd8C4Q7tqQt8ZIp9OkAHumBJ9IZ7aZwPGgNjo4zvHaefjhy+r4VE4JtsbohYtCPeiAFp+8G5SoGYrz6LKvdnPv8YofoI2nALP7i42JJbLmf6OTCfg+EIgjUlDZvd7g1nqn5a+6Qfg7NykvEYodYScuTNVsg3TKx1wk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699921667; c=relaxed/simple; bh=LeLieHtLrB2EDITjIfurg0Pa5q9rhNyYKRNfdA+WtoA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=GrRtTdUHWSf6XG9q/2OYtn8Qd9/4+R2mOoVFJ3WXZ71bnTnBMyF4z/2ZRkHwUzGDIPq2O7nPOtGnCmU68zRvpq60jY4YziTPTdFuR74CYEWMgbHBYhk6JL7D6MkeuJrD6XX1xfaPhf2atHrQVX/8Iq7BtHwHLjQpiR9IrnZWfKQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-x32d.google.com with SMTP id 5b1f17b1804b1-40859dee28cso41946225e9.0; Mon, 13 Nov 2023 16:27:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699921664; x=1700526464; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=/QlH1dSWsrfxinAVkPHLtsuu/4+LprvKPkWjTs6/5vA=; b=EpOiQcTsLefsPwNjHLbbbhxN/69FQsVcpJI9DQ9MgSRTyXuw2dBDHepmnMDgQDpfI2 Mzx9S4Io2Gb6xIvpq6SwMzwL0aU0iJQ9oi7Im8LZ1xSlTzMH3mQ6VRg87Hf+ixdRWONe uxfcPnVo6WhU4uFfGmz1yuBbtojO11apJXQ2j/gZ7Ahl8L3hx2E9TycDWE+IHQbF58dA eqwzzQSY2mJqUhAYMTr5nRV0eeuEw/z00gz7fu7HZhQnqyzSFmV2YHeuC3VjWvWMzy1o 3Xi2RdV+v/ychTNty7wVbgk4g5bUh5OmXNRShFVRqSRKBSWO4qkJCwRc/SR54+HS+qpn yJjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699921664; x=1700526464; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=/QlH1dSWsrfxinAVkPHLtsuu/4+LprvKPkWjTs6/5vA=; b=mJNg5oGoD+d7rIfYyOE37mkQDQ5fSQICaLzCdHuJwTdA744lCw8fzqKNxXa9/e/rBD NG75W++SvHAte9WGZhd0X9aEgwl86B+31ExUxQYvwG8VQL0JlXHm9+azSc9CR57okaJu yF1lm0ltQ4CyQqnfeuAGLUoMEdHKpZfGKSLVorguXOEFQXPiWd2pt+LIqzIKQdWrGBk+ mH3Jd7P02Q/vfpSXJeRA8LiWM23lZNToKeLoJHoxf/c+mbEUk/VKkcdErUCEQ5irpgch RMDmfqOy25FnG3zU+kCKN2VtifW04L0fzGeC5M4jEC51dWPsWit3PtBZGkUh6OGzBdLG pzPA== X-Gm-Message-State: AOJu0YwhM+N2Zdo/DWzxGvTSapbfO5AtJWFqsqv9WEMrnYM6M1cYH9Lt s4txH9yXk+1vWQjgGnVb8rJC4jGXwio= X-Google-Smtp-Source: AGHT+IEhAkpU7ZOOz1CPWLrIHdTNv/XKfS2eaRwX7ZK5wG1IE3nLnVVWdsxltFVWuBOWS/clFPLNzA== X-Received: by 2002:a05:6000:2aa:b0:32d:a68d:3fb6 with SMTP id l10-20020a05600002aa00b0032da68d3fb6mr6403062wry.50.1699921663680; Mon, 13 Nov 2023 16:27:43 -0800 (PST) Received: from othello.cust.communityfibre.co.uk ([2a02:6b64:8086:0:f9dd:28dd:c9b9:d8f4]) by smtp.gmail.com with ESMTPSA id j12-20020adfea4c000000b0031f82743e25sm6416454wrn.67.2023.11.13.16.27.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Nov 2023 16:27:42 -0800 (PST) From: Cassio Neri To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Cassio Neri Subject: [PATCH] libstdc++: Improve operator-(weekday x, weekday y). Date: Tue, 14 Nov 2023 00:27:39 +0000 Message-ID: <20231114002739.14809-1-cassio.neri@gmail.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, 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: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org 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 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(__x._M_wd) - __y._M_wd; - return days{__detail::__modulo(__n, 7)}; + const auto __n = __x.c_encoding() - __y.c_encoding(); + return static_cast(__n) >= 0 ? days{__n} : days{__n + 7}; } };