From patchwork Mon Mar 27 21:23:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Weslley da Silva Pereira X-Patchwork-Id: 75697 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1797948vqo; Mon, 27 Mar 2023 14:24:48 -0700 (PDT) X-Google-Smtp-Source: AKy350b1QQumeOKqg/pkD2ALYcQWbTXPNWElYoLQkrOeQ6hdjiaztwpzE027WBK5y+2+i+sEGBwM X-Received: by 2002:a17:906:e8c:b0:8b2:7564:dfd5 with SMTP id p12-20020a1709060e8c00b008b27564dfd5mr13501084ejf.60.1679952288470; Mon, 27 Mar 2023 14:24:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679952288; cv=none; d=google.com; s=arc-20160816; b=URZO6IFVwDctqmj5TcIHb8CHYKEU0N94BhD1x9nb41r+zTvNh8T/P49gxlB3cL7Lo/ rnyf7P8/9gMBRpu19tGdjh3LSZtjurXOJXh1QTk36GYkdAKSA+PhBtEbWz+qolxxnN1H FY8xidYL/hL7EURfzq6fgG+aX5PUG7XH1QUPFrqODvhRLqRZEumPhKCarzsbnRWyeNVZ VsM1+WMHFL9eZVmYYjAAI5gDcyoTC+S9mItV2POsfX328VXPcvAtLlP6OzY5TPAZLdwY c02pdwR3plYIM4JezfhsVE/+NuZvaC7rAhnRyVUoofWF1jiyqj8iHbPioIk0KTA4YpBd Aedw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:to:subject :message-id:date:mime-version:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=AvvRZSVQKXQP4i1dATsEzfKSwuZA7W8iwADi1QvD10I=; b=aszY7oYVEpOfg0M1H81aAENv9ZK9/YE2RHM1/lZu5VYSNOitNr+9KmYYHdrtG4O4Hz heBCUfNaEszRfdi1jzTMMMO1G0MmLJLhHpsuX2iqf8qZSeIv0r579pjzPeIVUEYeWwNq +S/r1sJiyV2rs42u38lrPeFHkCGptN6gV7tRN7XX4qYz7RTumU12+8xNHAB0/7NCP+NL xnQfxzDf+s1ZHWbHZRVnspxjXW1mRsLR/SsVwn1isds1m1PmZwLClzbsMdih7hADF5OA Jmj26E8PFWXiMTkhqYm01sZjHkGL7udPflctPyxoWr7z5ot1ikpoHzvLkQIXSIu/iRgB zhLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=i62FTb7p; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id o10-20020a170906358a00b0092db84073a0si13775059ejb.440.2023.03.27.14.24.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Mar 2023 14:24:48 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=i62FTb7p; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 64ED13858C2C for ; Mon, 27 Mar 2023 21:24:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 64ED13858C2C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1679952287; bh=AvvRZSVQKXQP4i1dATsEzfKSwuZA7W8iwADi1QvD10I=; h=Date:Subject:To:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=i62FTb7pdGKLWnYzAO0NWPinC9YLDRUKFzRCthYzH+wTg3KsMM22bugEp/qX8EhI3 K21GluZhIaDuIobGNertbsfbC+ZimYivS3oOeY/pHG0eFsyqKTkHA24rXzq2u9zG7h mDOc396ghBg9ig8+YXh6FCA3nYG0k2wAkA2f2nDk= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-vs1-xe30.google.com (mail-vs1-xe30.google.com [IPv6:2607:f8b0:4864:20::e30]) by sourceware.org (Postfix) with ESMTPS id 8D58E3858C54; Mon, 27 Mar 2023 21:24:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8D58E3858C54 Received: by mail-vs1-xe30.google.com with SMTP id z17so2110345vsf.4; Mon, 27 Mar 2023 14:24:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679952242; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Az/LeSzTnRXXwuxKfcCkSC/La0HOdZHk+TXpAw4/0Y8=; b=WiUkdLn+WH1DIpK87MgSkU2SylXp3S6PmRl0AFdqmjb6WVFiK3sKChYHMVA5OIcFk6 dyo+3hLUq19izoNpKpu4NYYY3J++oYkXhewbnfvAT+Dbn7PqotQkIkGfRgCYMjMr6SSb /TpBQYwLOJJyEG8N0QgBqxLtwa/7ngvE3vilv3Y/O3SSzFYcM5W4m7afggsNcG+FFOE0 wNYgiZhqp1zFt1HZwjp6sGK3NaPmMC1rDNUm9ahS96rxvum5kNj9jKW/zqWuJaNUhASZ zTESLkGplmZUl/Z328J6w6Sz+9Zz0hmipgLPWbBVLIvSsCZsdn5MhIROtZURdKQ95irW eFXQ== X-Gm-Message-State: AAQBX9dQ5JMum93wRWU/9eYe4Wnwge0kx3T/6VEurBNh6YpYxCv8I4Vl jAaSa19/axgodoWE/fW+SmDrsWZkvS46UQVRIa+QfK+xG3E= X-Received: by 2002:a67:d709:0:b0:422:194a:8566 with SMTP id p9-20020a67d709000000b00422194a8566mr7007790vsj.5.1679952242410; Mon, 27 Mar 2023 14:24:02 -0700 (PDT) MIME-Version: 1.0 Date: Mon, 27 Mar 2023 15:23:51 -0600 Message-ID: Subject: [PATCH] libstdc++/complex: Remove implicit type casts in complex To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Weslley da Silva Pereira via Gcc-patches From: Weslley da Silva Pereira Reply-To: Weslley da Silva Pereira Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761557650661095143?= X-GMAIL-MSGID: =?utf-8?q?1761557650661095143?= Dear all, Here follows a patch that removes implicit type casts in std::complex. *Description:* The current implementation of `complex<_Tp>` assumes that `int, double, long double` are explicitly convertible to `_Tp`. Moreover, it also assumes that: 1. `int` is implicitly convertible to `_Tp`, e.g., when using `complex<_Tp>(1)`. 2. `long double` can be attributed to a `_Tp` variable, e.g., when using `const _Tp __pi_2 = 1.5707963267948966192313216916397514L`. This patch transforms the implicit casts (1) and (2) into explicit type casts. As a result, `std::complex` is now able to support more types. One example is the type `Eigen::Half` from https://eigen.tuxfamily.org/dox-devel/Half_8h_source.html which does not implement implicit type conversions. *ChangeLog:* libstdc++-v3/ChangeLog: * include/std/complex: *Patch:* fix_complex.diff. (Also at https://github.com/gcc-mirror/gcc/pull/84) *OBS:* I didn't find a good reason for adding new tests or test results here since this is really a small upgrade (in my view) to std::complex. Sincerely, Weslley Signed-off-by: Weslley da Silva Pereira diff --git a/libstdc++-v3/include/std/complex b/libstdc++-v3/include/std/complex index 0f5f14c3ddb..1a4ac8a2a54 100644 --- a/libstdc++-v3/include/std/complex +++ b/libstdc++-v3/include/std/complex @@ -80,7 +80,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template _GLIBCXX20_CONSTEXPR complex<_Tp> conj(const complex<_Tp>&); /// Return complex with magnitude @a rho and angle @a theta. - template complex<_Tp> polar(const _Tp&, const _Tp& = 0); + template complex<_Tp> polar(const _Tp&, const _Tp& = _Tp(0)); // Transcendentals: /// Return complex cosine of @a z. @@ -961,7 +961,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline complex<_Tp> polar(const _Tp& __rho, const _Tp& __theta) { - __glibcxx_assert( __rho >= 0 ); + __glibcxx_assert( __rho >= _Tp(0) ); return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); } @@ -1161,13 +1161,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION if (__x == _Tp()) { - _Tp __t = sqrt(abs(__y) / 2); + _Tp __t = sqrt(abs(__y) / _Tp(2)); return complex<_Tp>(__t, __y < _Tp() ? -__t : __t); } else { - _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x))); - _Tp __u = __t / 2; + _Tp __t = sqrt(_Tp(2) * (std::abs(__z) + abs(__x))); + _Tp __u = __t / _Tp(2); return __x > _Tp() ? complex<_Tp>(__u, __y / __t) : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u); @@ -1257,7 +1257,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION complex<_Tp> __complex_pow_unsigned(complex<_Tp> __x, unsigned __n) { - complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1); + complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(_Tp(1)); while (__n >>= 1) { @@ -1280,7 +1280,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION pow(const complex<_Tp>& __z, int __n) { return __n < 0 - ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n) + ? complex<_Tp>(_Tp(1)) / std::__complex_pow_unsigned(__z, -(unsigned)__n) : std::__complex_pow_unsigned(__z, __n); } @@ -2017,7 +2017,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __complex_acos(const std::complex<_Tp>& __z) { const std::complex<_Tp> __t = std::asin(__z); - const _Tp __pi_2 = 1.5707963267948966192313216916397514L; + const _Tp __pi_2 = _Tp(1.5707963267948966192313216916397514L); return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag()); }