From patchwork Thu Mar 13 14:55:24 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 107834 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C36983858430 for ; Thu, 13 Mar 2025 14:59:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C36983858430 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=GGhKFWpE X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 176403858D39 for ; Thu, 13 Mar 2025 14:57:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 176403858D39 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 176403858D39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1741877834; cv=none; b=xNJNK5bsAYM7nfCrRDer6oZBSy9CueQlNNliZvClNkb4WkDYGPluvCPDFKBnJKzokhs01LOkELl6zpUFEthh6yStr9A5QDBjptgYOSZo2lLtFxqEYcQunhJPygOCVQrHlS/rPe/RfW+TNvrsPUYJ+47TYVybEtZixkvt+H6qGm8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1741877834; c=relaxed/simple; bh=Hy7NcGcWFLaW4qZPU7xxGOux+Y3T+S3WboI0Jlcp+ts=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=c/XQxllkif3YE9Equ08IaIPEV/dNLzYKAn/UHYZMr1Y9zECo4rLYulyq554pJcCGGexQu5H+1h8fQ7tnJUD3YoKtMvBlswYiwEaL9Lkc72MC3bj3FE33skHik/LQivyAsA4lOqcrVg5LGJVMPQUkJvol8Si4N/bvUF8XYV+N9DA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 176403858D39 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1741877833; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mAFxNmzgEdnXFQQeieO7jeb3LdjVlIihUhjGdqe2k8E=; b=GGhKFWpEaTQoNVDltQ7ThIzS7CqtdrCp8UI/6MPaixHrgDNZ1iBSS3GvCXN8ETh1aDK6iL 1HX/2E01uqKMYUPzJriXr2sWa0ekJNyKi7QaBxXmRS0pr7ZquIT34EcD2W2xt5JaNAFZr+ H5hBsdBM2QvtGL6kr81uTh/DccNFudQ= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-59-PSG3Og0pMV6V4A5B2qEhKw-1; Thu, 13 Mar 2025 10:57:11 -0400 X-MC-Unique: PSG3Og0pMV6V4A5B2qEhKw-1 X-Mimecast-MFC-AGG-ID: PSG3Og0pMV6V4A5B2qEhKw_1741877830 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 620F019560B7 for ; Thu, 13 Mar 2025 14:57:10 +0000 (UTC) Received: from localhost (unknown [10.42.28.10]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id E59981800944; Thu, 13 Mar 2025 14:57:09 +0000 (UTC) From: Jonathan Wakely To: gcc-patches@gcc.gnu.org Cc: Patrick Palka Subject: [committed v2 2/2] libstdc++: Implement for C++26 (P3370R1) Date: Thu, 13 Mar 2025 14:55:24 +0000 Message-ID: <20250313145706.1303288-2-jwakely@redhat.com> In-Reply-To: <20250313145706.1303288-1-jwakely@redhat.com> References: <20241203122054.281326-1-jwakely@redhat.com> <20250313145706.1303288-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: D2_bxvdpsLNnRXb47P7YNyg946XfFGzFJcSQsyvFiCM_1741877830 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_HOSTKARMA_W, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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~patchwork=sourceware.org@gcc.gnu.org This is the second part of the P3370R1 proposal just approved by the committee in Wrocław. This adds C++ equivalents of the functions added to C23 by WG14 N2683. These functions are in the global namespace, but to avoid collisions with the same functions defined by other standard library implementations, this change defines them in namespace __gnu_cxx and then adds them to the global namespace. libstdc++-v3/ChangeLog: * include/Makefile.am: Add stdckdint.h. * include/Makefile.in: Regenerate. * src/c++23/std.compat.cc.in: Export functions. * include/c_compatibility/stdckdint.h: New file. * testsuite/26_numerics/stdckdint/1.cc: New test. * testsuite/26_numerics/stdckdint/2_neg.cc: New test. Reviewed-by: Patrick Palka --- Reviewed by Tomasz and Patrick at https://forge.sourceware.org/gcc/gcc-TEST/pulls/28 Tested x86_64-linux. Pushed to trunk. libstdc++-v3/include/Makefile.am | 1 + libstdc++-v3/include/Makefile.in | 1 + .../include/c_compatibility/stdckdint.h | 113 ++++++++++++++++++ libstdc++-v3/src/c++23/std.compat.cc.in | 8 ++ .../testsuite/26_numerics/stdckdint/1.cc | 63 ++++++++++ .../testsuite/26_numerics/stdckdint/2_neg.cc | 39 ++++++ 6 files changed, 225 insertions(+) create mode 100644 libstdc++-v3/include/c_compatibility/stdckdint.h create mode 100644 libstdc++-v3/testsuite/26_numerics/stdckdint/1.cc create mode 100644 libstdc++-v3/testsuite/26_numerics/stdckdint/2_neg.cc diff --git a/libstdc++-v3/include/Makefile.am b/libstdc++-v3/include/Makefile.am index a8ff87fb600..4dc771a540c 100644 --- a/libstdc++-v3/include/Makefile.am +++ b/libstdc++-v3/include/Makefile.am @@ -912,6 +912,7 @@ c_compatibility_headers = \ ${c_compatibility_srcdir}/math.h \ ${c_compatibility_srcdir}/stdatomic.h \ ${c_compatibility_srcdir}/stdbit.h \ + ${c_compatibility_srcdir}/stdckdint.h \ ${c_compatibility_srcdir}/stdlib.h endif diff --git a/libstdc++-v3/include/Makefile.in b/libstdc++-v3/include/Makefile.in index 859cbee53d6..0e3d09b3a75 100644 --- a/libstdc++-v3/include/Makefile.in +++ b/libstdc++-v3/include/Makefile.in @@ -1249,6 +1249,7 @@ c_compatibility_builddir = . @GLIBCXX_C_HEADERS_C_GLOBAL_TRUE@ ${c_compatibility_srcdir}/math.h \ @GLIBCXX_C_HEADERS_C_GLOBAL_TRUE@ ${c_compatibility_srcdir}/stdatomic.h \ @GLIBCXX_C_HEADERS_C_GLOBAL_TRUE@ ${c_compatibility_srcdir}/stdbit.h \ +@GLIBCXX_C_HEADERS_C_GLOBAL_TRUE@ ${c_compatibility_srcdir}/stdckdint.h \ @GLIBCXX_C_HEADERS_C_GLOBAL_TRUE@ ${c_compatibility_srcdir}/stdlib.h @GLIBCXX_C_HEADERS_C_STD_TRUE@c_compatibility_headers = diff --git a/libstdc++-v3/include/c_compatibility/stdckdint.h b/libstdc++-v3/include/c_compatibility/stdckdint.h new file mode 100644 index 00000000000..1de2d18dc1a --- /dev/null +++ b/libstdc++-v3/include/c_compatibility/stdckdint.h @@ -0,0 +1,113 @@ +// C compatibility header -*- C++ -*- + +// Copyright The GNU Toolchain Authors. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// Under Section 7 of GPL version 3, you are granted additional +// permissions described in the GCC Runtime Library Exception, version +// 3.1, as published by the Free Software Foundation. + +// You should have received a copy of the GNU General Public License and +// a copy of the GCC Runtime Library Exception along with this program; +// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +// . + +/** @file include/stdckdint.h + * This is a Standard C++ Library header. + */ + +#ifndef _GLIBCXX_STDCKDINT_H +#define _GLIBCXX_STDCKDINT_H + +#if __cplusplus > 202302L +#include +#include + +#define __STDC_VERSION_STDCKDINT_H__ 202311L + +#ifndef _GLIBCXX_DOXYGEN +// We define these in our own namespace, but let Doxygen think otherwise. +namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) +{ +#endif +/// @cond undocumented +namespace __detail +{ + template + concept __cv_unqual_signed_or_unsigned_integer_type + = std::same_as<_Tp, std::remove_cv_t<_Tp>> + && std::__is_standard_integer<_Tp>::value; +} +/// @endcond + +/** Checked integer arithmetic + * + * Performs arithmetic on `__a` and `__b` and stores the result in `*__result`, + * with overflow detection. + * The arithmetic is performed in infinite signed precision, without overflow, + * then converted to the result type, `_Tp1`. If the converted result is not + * equal to the infinite precision result, the stored result is wrapped to the + * width of `_Tp1` and `true` is returned. Otherwise, the stored result is + * correct and `false` is returned. + * + * @param __result A pointer to a signed or unsigned integer type. + * @param __a A signed or unsigned integer type. + * @param __b A signed or unsigned integer type. + * @return True if overflow occurred, false otherwise. + * @since C++26 + * @{ + */ +template + inline bool + ckd_add(_Tp1* __result, _Tp2 __a, _Tp3 __b) + { + using __gnu_cxx::__detail::__cv_unqual_signed_or_unsigned_integer_type; + static_assert(__cv_unqual_signed_or_unsigned_integer_type<_Tp1>); + static_assert(__cv_unqual_signed_or_unsigned_integer_type<_Tp2>); + static_assert(__cv_unqual_signed_or_unsigned_integer_type<_Tp3>); + return __builtin_add_overflow(__a, __b, __result); + } + +template + inline bool + ckd_sub(_Tp1* __result, _Tp2 __a, _Tp3 __b) + { + using __gnu_cxx::__detail::__cv_unqual_signed_or_unsigned_integer_type; + static_assert(__cv_unqual_signed_or_unsigned_integer_type<_Tp1>); + static_assert(__cv_unqual_signed_or_unsigned_integer_type<_Tp2>); + static_assert(__cv_unqual_signed_or_unsigned_integer_type<_Tp3>); + return __builtin_sub_overflow(__a, __b, __result); + } + +template + inline bool + ckd_mul(_Tp1* __result, _Tp2 __a, _Tp3 __b) + { + using __gnu_cxx::__detail::__cv_unqual_signed_or_unsigned_integer_type; + static_assert(__cv_unqual_signed_or_unsigned_integer_type<_Tp1>); + static_assert(__cv_unqual_signed_or_unsigned_integer_type<_Tp2>); + static_assert(__cv_unqual_signed_or_unsigned_integer_type<_Tp3>); + return __builtin_mul_overflow(__a, __b, __result); + } +/// @} +#ifndef _GLIBCXX_DOXYGEN +} + +using __gnu_cxx::ckd_add; +using __gnu_cxx::ckd_sub; +using __gnu_cxx::ckd_mul; +#endif + +#endif // C++26 + +#endif // _GLIBCXX_STDCKDINT_H diff --git a/libstdc++-v3/src/c++23/std.compat.cc.in b/libstdc++-v3/src/c++23/std.compat.cc.in index 3c25295850f..ba7ed0312ab 100644 --- a/libstdc++-v3/src/c++23/std.compat.cc.in +++ b/libstdc++-v3/src/c++23/std.compat.cc.in @@ -53,6 +53,14 @@ _GLIBCXX_STDBIT_FUNC(stdc_bit_ceil); #undef _GLIBCXX_STDBIT_FUNC } +// +export +{ + using __gnu_cxx::ckd_add; + using __gnu_cxx::ckd_sub; + using __gnu_cxx::ckd_mul; +} + #define STD_COMPAT 1 // C library exports are appended from std-clib.cc.in. diff --git a/libstdc++-v3/testsuite/26_numerics/stdckdint/1.cc b/libstdc++-v3/testsuite/26_numerics/stdckdint/1.cc new file mode 100644 index 00000000000..1402c834a7e --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/stdckdint/1.cc @@ -0,0 +1,63 @@ +// { dg-do run { target c++26 } } + +#include + +#if __STDC_VERSION_STDCKDINT_H__ != 202311L +# error "__STDC_VERSION_STDCKDINT_H__ not defined correctly in " +#endif + +#include +#include + +void +test_add() +{ + int result; + bool overflow; + + overflow = ::ckd_add(&result, (unsigned)INT_MAX, 1LL); + VERIFY( overflow ); + VERIFY( result == INT_MIN ); + + overflow = ::ckd_add(&result, (long long)INT_MIN, -1); + VERIFY( overflow ); + VERIFY( result == INT_MAX ); + + overflow = ::ckd_add(&result, 99u, 100ll); + VERIFY( ! overflow ); + VERIFY( result == 199 ); +} + +void +test_sub() +{ + int result; + bool overflow; + + overflow = ::ckd_sub(&result, -1, -5); + VERIFY( ! overflow ); + VERIFY( result == 4 ); +} + +void +test_mul() +{ + long long result; + bool overflow; + + overflow = ::ckd_mul(&result, INT_MIN, -1); + VERIFY( ! overflow ); + VERIFY( result == -(long long)INT_MIN ); + + unsigned uresult; + overflow = ::ckd_mul(&uresult, INT_MIN, -1); + VERIFY( ! overflow ); + VERIFY( result == (unsigned)INT_MAX + 1u ); +} + +int main() +{ + test_add(); + test_sub(); + test_mul(); +} diff --git a/libstdc++-v3/testsuite/26_numerics/stdckdint/2_neg.cc b/libstdc++-v3/testsuite/26_numerics/stdckdint/2_neg.cc new file mode 100644 index 00000000000..7954da2d0d9 --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/stdckdint/2_neg.cc @@ -0,0 +1,39 @@ +// { dg-do compile { target c++26 } } + +#include + +void +test_add(int i, char c, bool b) +{ + ::ckd_add(&i, c, 1); // { dg-error "here" } + ::ckd_add(&i, 1, c); // { dg-error "here" } + ::ckd_add(&i, b, 2); // { dg-error "here" } + ::ckd_add(&i, 2, b); // { dg-error "here" } + ::ckd_add(&c, 3, 3); // { dg-error "here" } + ::ckd_add((const int*)&i, 4, 4); // { dg-error "here" } +} + +void +test_sub(int i, char c, bool b) +{ + ::ckd_sub(&i, c, 1); // { dg-error "here" } + ::ckd_sub(&i, 1, c); // { dg-error "here" } + ::ckd_sub(&i, b, 2); // { dg-error "here" } + ::ckd_sub(&i, 2, b); // { dg-error "here" } + ::ckd_sub(&c, 3, 3); // { dg-error "here" } + ::ckd_sub((const int*)&i, 4, 4); // { dg-error "here" } +} + +void +test_mul(int i, char c, bool b) +{ + ::ckd_mul(&i, c, 1); // { dg-error "here" } + ::ckd_mul(&i, 1, c); // { dg-error "here" } + ::ckd_mul(&i, b, 2); // { dg-error "here" } + ::ckd_mul(&i, 2, b); // { dg-error "here" } + ::ckd_mul(&c, 3, 3); // { dg-error "here" } + ::ckd_mul((const int*)&i, 4, 4); // { dg-error "here" } +} + +// { dg-prune-output "static assertion failed" } +// { dg-prune-output "pointer to 'const'" }