Commit e987fb1e by Edward Smith-Rowland Committed by Edward Smith-Rowland

Implement P0415 More constexpr for std::complex.


2018-11-23  Edward Smith-Rowland  <3dw4rd@verizon.net>

	Implement P0415 More constexpr for std::complex.
	* include/std/complex (conj(complex<Tp>), norm(complex<Tp>)): Constexpr;
	(real(Tp), imag(Tp)): Constexpr;
	(operator@=(Tp), operator@=(complex<Tp>)): Constexpr;
	(operator@(Tp,complex<Tp>), operator@(complex<Tp>,Tp)
	operator@(complex<Tp>,complex<Tp>)): Constexpr.
	* testsuite/26_numerics/complex/comparison_operators/
	more_constexpr.cc: New test.
	* testsuite/26_numerics/complex/operators/more_constexpr.cc: New test.
	* testsuite/26_numerics/complex/requirements/
	more_constexpr.cc: New test.
	* testsuite/26_numerics/complex/value_operations/
	more_constexpr.cc: New test.
	* testsuite/26_numerics/headers/complex/synopsis.cc:
	Add _GLIBCXX20_CONSTEXPR to applicable operators; Add missing proj().
	* testsuite/26_numerics/headers/complex/synopsis.cc:
	Add _GLIBCXX20_CONSTEXPR to relevant decls.

From-SVN: r266416
parent e02669db
2018-11-23 Edward Smith-Rowland <3dw4rd@verizon.net>
Implement P0415 More constexpr for std::complex.
* include/std/complex (conj(complex<Tp>), norm(complex<Tp>)): Constexpr;
(real(Tp), imag(Tp)): Constexpr;
(operator@=(Tp), operator@=(complex<Tp>)): Constexpr;
(operator@(Tp,complex<Tp>), operator@(complex<Tp>,Tp)
operator@(complex<Tp>,complex<Tp>)): Constexpr.
* testsuite/26_numerics/complex/comparison_operators/
more_constexpr.cc: New test.
* testsuite/26_numerics/complex/operators/more_constexpr.cc: New test.
* testsuite/26_numerics/complex/requirements/
more_constexpr.cc: New test.
* testsuite/26_numerics/complex/value_operations/
more_constexpr.cc: New test.
* testsuite/26_numerics/headers/complex/synopsis.cc:
Add _GLIBCXX20_CONSTEXPR to applicable operators; Add missing proj().
* testsuite/26_numerics/headers/complex/synopsis.cc:
Add _GLIBCXX20_CONSTEXPR to relevant decls.
2018-11-23 Martin Sebor <msebor@redhat.com>
Jonathan Wakely <jwakely@redhat.com>
......
// { dg-options "-std=gnu++2a" }
// { dg-do compile { target c++2a } }
// Copyright (C) 2018 Free Software Foundation, Inc.
//
// 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.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include <complex>
#include <testsuite_common_types.h>
template<bool ok>
struct thing
{ };
//
template<typename _Tp>
void
test_comparison()
{
constexpr std::complex<_Tp> a{1.1, 2.2};
constexpr std::complex<_Tp> b{3.3, 4.4};
if constexpr (a == b)
auto c [[maybe_unused]] = a + b;
if constexpr (a != b)
auto c [[maybe_unused]] = a - b;
thing<a == b> thing1 [[maybe_unused]];
thing<a != b> thing2 [[maybe_unused]];
}
int
main()
{
test_comparison<float>();
test_comparison<double>();
test_comparison<long double>();
return 0;
}
// { dg-options "-std=gnu++2a" }
// { dg-do compile { target c++2a } }
// Copyright (C) 2018 Free Software Foundation, Inc.
//
// 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.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include <complex>
#include <testsuite_common_types.h>
namespace __gnu_test
{
// Test constexpr symmetric complex @ real, real @ complex, complex @ complex.
template<typename _Tp>
void
test_operators()
{
constexpr std::complex<_Tp> a{1, 2};
constexpr std::complex<_Tp> b{3, 4};
constexpr _Tp c = 5;
constexpr auto w [[maybe_unused]] = +a;
constexpr auto z [[maybe_unused]] = -a;
constexpr auto apc [[maybe_unused]] = a + c;
constexpr auto amc [[maybe_unused]] = a - c;
constexpr auto atc [[maybe_unused]] = a * c;
constexpr auto adc [[maybe_unused]] = a / c;
constexpr auto cpa [[maybe_unused]] = c + a;
constexpr auto cma [[maybe_unused]] = c - a;
constexpr auto cta [[maybe_unused]] = c * a;
constexpr auto cda [[maybe_unused]] = c / a;
constexpr auto apb [[maybe_unused]] = a + b;
constexpr auto amb [[maybe_unused]] = a - b;
constexpr auto atb [[maybe_unused]] = a * b;
constexpr auto adb [[maybe_unused]] = a / b;
}
}
int main()
{
__gnu_test::test_operators<float>();
__gnu_test::test_operators<double>();
__gnu_test::test_operators<long double>();
return 0;
}
// { dg-options "-std=gnu++2a" }
// { dg-do compile { target c++2a } }
// Copyright (C) 2018 Free Software Foundation, Inc.
//
// 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.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include <complex>
#include <testsuite_common_types.h>
namespace __gnu_test
{
// Test constexpr real(val) imag(val).
template<typename _Tp, const int _Val = 42>
inline void
set_real(std::complex<_Tp>& a)
{ a.real(_Val); }
template<typename _Tp, const int _Val = 42>
inline void
set_imag(std::complex<_Tp>& a)
{ a.imag(_Val); }
template<typename _Tp>
void
test_members()
{
constexpr std::complex<_Tp> a{1.1, 2.2};
std::complex<_Tp> z = a;
set_real<_Tp, 33>(z);
set_imag<_Tp, 44>(z);
}
// Test operators @=complex and @=real.
template<typename _Tp, typename _Up>
constexpr std::complex<_Tp>
sum(const std::complex<_Tp>& z, const std::complex<_Up>& w)
{
std::complex<_Tp> x = z;
x += w;
return x;
}
template<typename _Tp, typename _Up>
constexpr std::complex<_Tp>
sum(const std::complex<_Tp>& z, _Up w)
{
std::complex<_Tp> x = z;
x += w;
return x;
}
template<typename _Tp, typename _Up>
constexpr std::complex<_Tp>
dif(const std::complex<_Tp>& z, const std::complex<_Up>& w)
{
std::complex<_Tp> x = z;
x -= w;
return x;
}
template<typename _Tp, typename _Up>
constexpr std::complex<_Tp>
dif(const std::complex<_Tp>& z, _Up w)
{
std::complex<_Tp> x = z;
x -= w;
return x;
}
template<typename _Tp, typename _Up>
constexpr std::complex<_Tp>
prod(const std::complex<_Tp>& z, const std::complex<_Up>& w)
{
std::complex<_Tp> x = z;
x *= w;
return x;
}
template<typename _Tp, typename _Up>
constexpr std::complex<_Tp>
prod(const std::complex<_Tp>& z, _Up w)
{
std::complex<_Tp> x = z;
x *= w;
return x;
}
template<typename _Tp, typename _Up>
constexpr std::complex<_Tp>
quot(const std::complex<_Tp>& z, const std::complex<_Up>& w)
{
std::complex<_Tp> x = z;
x /= w;
return x;
}
template<typename _Tp, typename _Up>
constexpr std::complex<_Tp>
quot(const std::complex<_Tp>& z, _Up w)
{
std::complex<_Tp> x = z;
x /= w;
return x;
}
template<typename _Tp, typename _Up>
void
test_operator_members()
{
constexpr std::complex<_Tp> a{10, 20};
constexpr std::complex<_Up> b{6, 8};
constexpr _Up c{10};
constexpr auto apc = sum(a, c);
static_assert(apc == std::complex<_Tp>{20, 20});
constexpr auto amc = dif(a, c);
static_assert(amc == std::complex<_Tp>{0, 20});
constexpr auto atc = prod(a, c);
static_assert(atc == std::complex<_Tp>{100, 200});
constexpr auto adc = quot(a, c);
static_assert(adc == std::complex<_Tp>{1, 2});
constexpr auto apb = sum(a, b);
static_assert(apb == std::complex<_Tp>{16, 28});
constexpr auto amb = dif(a, b);
static_assert(amb == std::complex<_Tp>{4, 12});
constexpr auto atb = prod(a, b);
static_assert(atb == std::complex<_Tp>{-100, 200});
constexpr auto adb = quot(a, b);
static_assert(adb == std::complex<_Tp>{11/_Tp{5}, 2/_Tp{5}});
}
}
int main()
{
__gnu_test::test_members<float>();
__gnu_test::test_members<double>();
__gnu_test::test_members<long double>();
__gnu_test::test_operator_members<float, float>();
__gnu_test::test_operator_members<float, double>();
__gnu_test::test_operator_members<float, long double>();
__gnu_test::test_operator_members<double, float>();
__gnu_test::test_operator_members<double, double>();
__gnu_test::test_operator_members<double, long double>();
__gnu_test::test_operator_members<long double, float>();
__gnu_test::test_operator_members<long double, double>();
__gnu_test::test_operator_members<long double, long double>();
// Test primary template.
__gnu_test::test_operator_members<__float128, __float128>();
return 0;
}
// { dg-options "-std=gnu++2a" }
// { dg-do compile { target c++2a } }
// Copyright (C) 2018 Free Software Foundation, Inc.
//
// 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.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include <complex>
#include <testsuite_common_types.h>
namespace __gnu_test
{
struct constexpr_functions
{
template<typename _Ttesttype>
void
operator()()
{
struct _Concept
{
void __constraint()
{
typedef typename _Ttesttype::_ComplexT _ComplexT;
constexpr _ComplexT cc = { 1.1 };
constexpr _Ttesttype a(cc);
constexpr auto v1 [[maybe_unused]] = norm(a);
constexpr auto v2 [[maybe_unused]] = conj(a);
}
};
_Concept c;
c.__constraint();
}
};
}
int main()
{
__gnu_test::constexpr_functions test;
test.operator()<std::complex<float>>();
test.operator()<std::complex<double>>();
test.operator()<std::complex<long double>>();
return 0;
}
......@@ -28,51 +28,71 @@ namespace std {
// 26.2.6 operators:
template<class T>
complex<T> operator+(const complex<T>&, const complex<T>&);
template<class T> complex<T> operator+(const complex<T>&, const T&);
template<class T> complex<T> operator+(const T&, const complex<T>&);
template<class T> complex<T> operator-
(const complex<T>&, const complex<T>&);
template<class T> complex<T> operator-(const complex<T>&, const T&);
template<class T> complex<T> operator-(const T&, const complex<T>&);
template<class T> complex<T> operator*
(const complex<T>&, const complex<T>&);
template<class T> complex<T> operator*(const complex<T>&, const T&);
template<class T> complex<T> operator*(const T&, const complex<T>&);
template<class T> complex<T> operator/
(const complex<T>&, const complex<T>&);
template<class T> complex<T> operator/(const complex<T>&, const T&);
template<class T> complex<T> operator/(const T&, const complex<T>&);
template<class T> complex<T> operator+(const complex<T>&);
template<class T> complex<T> operator-(const complex<T>&);
template<class T> _GLIBCXX_CONSTEXPR bool operator==
(const complex<T>&, const complex<T>&);
template<class T> _GLIBCXX_CONSTEXPR bool operator==
(const complex<T>&, const T&);
template<class T> _GLIBCXX_CONSTEXPR bool operator==
(const T&, const complex<T>&);
_GLIBCXX20_CONSTEXPR complex<T>
operator+(const complex<T>&, const complex<T>&);
template<class T>
_GLIBCXX20_CONSTEXPR complex<T> operator+(const complex<T>&, const T&);
template<class T>
_GLIBCXX20_CONSTEXPR complex<T> operator+(const T&, const complex<T>&);
template<class T>
_GLIBCXX20_CONSTEXPR complex<T>
operator-(const complex<T>&, const complex<T>&);
template<class T>
_GLIBCXX20_CONSTEXPR complex<T> operator-(const complex<T>&, const T&);
template<class T>
_GLIBCXX20_CONSTEXPR complex<T> operator-(const T&, const complex<T>&);
template<class T> _GLIBCXX_CONSTEXPR bool operator!=
template<class T>
_GLIBCXX20_CONSTEXPR complex<T> operator*
(const complex<T>&, const complex<T>&);
template<class T> _GLIBCXX_CONSTEXPR bool operator!=
(const complex<T>&, const T&);
template<class T> _GLIBCXX_CONSTEXPR bool operator!=
(const T&, const complex<T>&);
template<class T>
_GLIBCXX20_CONSTEXPR complex<T> operator*(const complex<T>&, const T&);
template<class T>
_GLIBCXX20_CONSTEXPR complex<T> operator*(const T&, const complex<T>&);
template<class T>
_GLIBCXX20_CONSTEXPR complex<T>
operator/(const complex<T>&, const complex<T>&);
template<class T>
_GLIBCXX20_CONSTEXPR complex<T> operator/(const complex<T>&, const T&);
template<class T>
_GLIBCXX20_CONSTEXPR complex<T> operator/(const T&, const complex<T>&);
template<class T>
_GLIBCXX20_CONSTEXPR complex<T> operator+(const complex<T>&);
template<class T>
_GLIBCXX20_CONSTEXPR complex<T> operator-(const complex<T>&);
template<class T>
_GLIBCXX_CONSTEXPR bool operator==(const complex<T>&, const complex<T>&);
template<class T>
_GLIBCXX_CONSTEXPR bool operator==(const complex<T>&, const T&);
template<class T>
_GLIBCXX_CONSTEXPR bool operator==(const T&, const complex<T>&);
template<class T>
_GLIBCXX_CONSTEXPR bool operator!=(const complex<T>&, const complex<T>&);
template<class T>
_GLIBCXX_CONSTEXPR bool operator!=(const complex<T>&, const T&);
template<class T>
_GLIBCXX_CONSTEXPR bool operator!=(const T&, const complex<T>&);
template<class T, class charT, class traits>
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>&, complex<T>&);
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>&, complex<T>&);
template<class T, class charT, class traits>
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>&, const complex<T>&);
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>&, const complex<T>&);
// 26.2.7 values:
template<class T> _GLIBCXX_CONSTEXPR T real(const complex<T>&);
template<class T> _GLIBCXX_CONSTEXPR T imag(const complex<T>&);
template<class T> T abs(const complex<T>&);
template<class T> T arg(const complex<T>&);
template<class T> T norm(const complex<T>&);
template<class T> complex<T> conj(const complex<T>&);
template<class T> _GLIBCXX20_CONSTEXPR T arg(const complex<T>&);
template<class T> _GLIBCXX20_CONSTEXPR T norm(const complex<T>&);
template<class T> _GLIBCXX20_CONSTEXPR complex<T> conj(const complex<T>&);
template<class T> _GLIBCXX20_CONSTEXPR complex<T> proj(const complex<T>&);
template<class T> complex<T> polar(const T& rho, const T& theta);
// 26.2.8 transcendentals:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment