This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Patch] Second half of tr1/complex


Hi,

the below would be the remaining part of tr1/complex, the additional
overloads.

Actually, these are the most thorny and debated bits (same for the real
counterparts, of course) but I believe we are closely following the
letter and the spirit of the actual TR1. From the latter point of view,
there is a problem with the existing std::pow(const complex<>&, int)
overload which prevails over the new pow overloads and makes impossible
the effective promotion to complex<double> for a pow(complex<float>,
int) call. Well, some may consider this behavior even a virtue ;)
Otherwise, as far as I can see, we gonna need a mechanism to change,
upon user request of strict conformance to TR1, the behavior of existing
std facilities... worth some long thoughts...

Tested x86-linux.

Paolo.

////////////////////
2006-01-13  Paolo Carlini  <pcarlini@suse.de>
	    Howard Hinnant  <hhinnant@apple.com>

	* include/tr1/complex (arg, conj, imag, norm, polar, pow, real):
	Add, implementing TR1, 8.1.9.
	(__promote_2): New.
	* include/tr1/common.h: New, provides __promote, __promote_2.
	* include/Makefile.am: Add.
	* include/Makefile.in: Regenerate.
	* testsuite/testsuite_tr1.h (check_ret_type): New.
	* testsuite/tr1/8_c_compatibility/complex/overloads_int.cc: New.
	* testsuite/tr1/8_c_compatibility/complex/overloads_float.cc: New.
Index: include/tr1/complex
===================================================================
--- include/tr1/complex	(revision 109550)
+++ include/tr1/complex	(working copy)
@@ -35,6 +35,7 @@
 #define _TR1_COMPLEX 1
 
 #include "../complex"
+#include <tr1/common.h>
 
 // namespace std::tr1
 namespace std
@@ -51,7 +52,7 @@
   template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
   template<typename _Tp> std::complex<_Tp> fabs(const std::complex<_Tp>&);
 
-  /// @brief acos(__z) [8.1.2]
+  /// @brief acos(__z) [8.1.2].
   //  Effects:  Behaves the same as C99 function cacos, defined
   //            in subclause 7.3.5.1.
   template<typename _Tp>
@@ -87,7 +88,7 @@
     { return __complex_acos(__z); }
 #endif
 
-  /// @brief asin(__z) [8.1.3]
+  /// @brief asin(__z) [8.1.3].
   //  Effects:  Behaves the same as C99 function casin, defined
   //            in subclause 7.3.5.2.
   template<typename _Tp>
@@ -123,7 +124,7 @@
     { return __complex_asin(__z); }
 #endif
   
-  /// @brief atan(__z) [8.1.4]
+  /// @brief atan(__z) [8.1.4].
   //  Effects:  Behaves the same as C99 function catan, defined
   //            in subclause 7.3.5.3.
   template<typename _Tp>
@@ -167,7 +168,7 @@
     { return __complex_atan(__z); }
 #endif
 
-  /// @brief acosh(__z) [8.1.5]
+  /// @brief acosh(__z) [8.1.5].
   //  Effects:  Behaves the same as C99 function cacosh, defined
   //            in subclause 7.3.6.1.
   template<typename _Tp>
@@ -206,7 +207,7 @@
     { return __complex_acosh(__z); }
 #endif
 
-  /// @brief asinh(__z) [8.1.6]
+  /// @brief asinh(__z) [8.1.6].
   //  Effects:  Behaves the same as C99 function casin, defined
   //            in subclause 7.3.6.2.
   template<typename _Tp>
@@ -245,7 +246,7 @@
     { return __complex_asinh(__z); }
 #endif
 
-  /// @brief atanh(__z) [8.1.7]
+  /// @brief atanh(__z) [8.1.7].
   //  Effects:  Behaves the same as C99 function catanh, defined
   //            in subclause 7.3.6.3.
   template<typename _Tp>
@@ -289,7 +290,7 @@
     { return __complex_atanh(__z); }
 #endif
 
-  /// @brief fabs(__z) [8.1.8]
+  /// @brief fabs(__z) [8.1.8].
   //  Effects:  Behaves the same as C99 function cabs, defined
   //            in subclause 7.3.8.1.
   template<typename _Tp>
@@ -297,6 +298,97 @@
     fabs(const std::complex<_Tp>& __z)
     { return std::abs(__z); }
 
+
+  /// @brief Additional overloads [8.1.9].
+  //
+
+  // See common.h for the primary template.
+  template<typename _Tp, typename _Up>
+    struct __promote_2<std::complex<_Tp>, _Up>
+    {
+    public:
+      typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
+    };
+
+  template<typename _Tp, typename _Up>
+    struct __promote_2<_Tp, std::complex<_Up> >
+    {
+    public:
+      typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
+    };
+  
+  template<typename _Tp, typename _Up>
+    struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
+    {
+    public:
+      typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
+    };
+
+
+  template<typename _Tp>
+    inline typename __promote<_Tp>::__type
+    arg(_Tp __x)
+    {
+      typedef typename __promote<_Tp>::__type __type;
+      return std::arg(std::complex<__type>(__x));
+    }
+
+  template<typename _Tp>
+    inline std::complex<typename __promote<_Tp>::__type>
+    conj(_Tp __x)
+    { return __x; }
+
+  template<typename _Tp>
+    inline typename __promote<_Tp>::__type
+    imag(_Tp)
+    { return _Tp(); }
+
+  template<typename _Tp>
+    inline typename __promote<_Tp>::__type
+    norm(_Tp __x)
+    {
+      typedef typename __promote<_Tp>::__type __type;
+      return __type(__x) * __type(__x);
+    }
+
+  template<class _Tp, class _Up>
+    inline std::complex<typename __promote_2<_Tp, _Up>::__type>
+    polar(const _Tp& __rho, const _Up& __theta)
+    {
+      typedef typename __promote_2<_Tp, _Up>::__type __type;
+      return std::polar(__type(__rho), __type(__theta));
+    }
+  
+  template<class _Tp, class _Up>
+    inline std::complex<typename __promote_2<_Tp, _Up>::__type>
+    pow(const std::complex<_Tp>& __x, const _Up& __y)
+    {
+      typedef typename __promote_2<_Tp, _Up>::__type __type;
+      return std::pow(std::complex<__type>(__x), __type(__y));
+    }
+
+  template<class _Tp, class _Up>
+    inline std::complex<typename __promote_2<_Tp, _Up>::__type>
+    pow(const _Tp& __x, const std::complex<_Up>& __y)
+    {
+      typedef typename __promote_2<_Tp, _Up>::__type __type;
+      return std::pow(__type(__x), std::complex<__type>(__y));
+    }
+
+  template<class _Tp, class _Up>
+    inline std::complex<typename __promote_2<_Tp, _Up>::__type>
+    pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
+    {
+      typedef typename __promote_2<_Tp, _Up>::__type __type;
+      return std::pow(std::complex<__type>(__x),
+		      std::complex<__type>(__y));
+    }
+
+  template<typename _Tp>
+    inline typename __promote<_Tp>::__type
+    real(_Tp __x)
+    { return __x; }
+
 _GLIBCXX_END_NAMESPACE
 }
 
Index: include/tr1/common.h
===================================================================
--- include/tr1/common.h	(revision 0)
+++ include/tr1/common.h	(revision 0)
@@ -0,0 +1,66 @@
+// Internal header for TR1 complex -*- C++ -*-
+
+// Copyright (C) 2006 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 2, 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 COPYING.  If not, write to the Free
+// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+// USA.
+
+// As a special exception, you may use this file as part of a free software
+// library without restriction.  Specifically, if other files instantiate
+// templates or use macros or inline functions from this file, or you compile
+// this file and link it with other files to produce an executable, this
+// file does not by itself cause the resulting executable to be covered by
+// the GNU General Public License.  This exception does not however
+// invalidate any other reasons why the executable file might be covered by
+// the GNU General Public License.
+
+/** @file 
+ *  This is a TR1 C++ Library header. 
+ */
+
+#ifndef _TR1_COMMON_H
+#define _TR1_COMMON_H 1
+
+#include <tr1/type_traits>
+
+// namespace std::tr1
+namespace std
+{
+_GLIBCXX_BEGIN_NAMESPACE(tr1)
+
+  template<typename _Tp, bool = is_integral<_Tp>::value>
+    struct __promote
+    { typedef double __type; };
+
+  template<typename _Tp>
+    struct __promote<_Tp, false>
+    { typedef _Tp __type; };
+
+  template<typename _Tp, typename _Up> 
+    struct __promote_2
+    {
+    private:
+      typedef typename __promote<_Tp>::__type __type1;
+      typedef typename __promote<_Up>::__type __type2;    
+      
+    public:
+      typedef __typeof__(__type1() + __type2()) __type; 
+    };
+
+_GLIBCXX_END_NAMESPACE
+} // namespace std
+
+#endif
Index: include/Makefile.am
===================================================================
--- include/Makefile.am	(revision 109550)
+++ include/Makefile.am	(working copy)
@@ -483,6 +483,7 @@
 	${tr1_srcdir}/bind_repeat.h \
 	${tr1_srcdir}/bind_iterate.h \
 	${tr1_srcdir}/boost_shared_ptr.h \
+	${tr1_srcdir}/common.h \
 	${tr1_srcdir}/complex \
 	${tr1_srcdir}/functional \
 	${tr1_srcdir}/functional_iterate.h \
Index: testsuite/testsuite_tr1.h
===================================================================
--- testsuite/testsuite_tr1.h	(revision 109548)
+++ testsuite/testsuite_tr1.h	(working copy)
@@ -1,7 +1,7 @@
 // -*- C++ -*-
 // Testing utilities for the tr1 testsuite.
 //
-// Copyright (C) 2004, 2005 Free Software Foundation, Inc.
+// Copyright (C) 2004, 2005, 2006 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
@@ -164,6 +164,13 @@
     int foo_v()  volatile       { return 3; }
     int foo_cv() const volatile { return 4; }
   };
+
+  // For use in 8_c_compatibility.
+  template<typename R, typename T>
+    typename std::__enable_if<bool, std::tr1::is_same<R, T>::value>::__type
+    check_ret_type(T)
+    { return true; }
+
 } // namespace __gnu_test
 
 #endif // _GLIBCXX_TESTSUITE_TR1_H
Index: testsuite/tr1/8_c_compatibility/complex/overloads_float.cc
===================================================================
--- testsuite/tr1/8_c_compatibility/complex/overloads_float.cc	(revision 0)
+++ testsuite/tr1/8_c_compatibility/complex/overloads_float.cc	(revision 0)
@@ -0,0 +1,109 @@
+// 2006-01-12  Paolo Carlini  <pcarlini@suse.de>
+//
+// Copyright (C) 2006 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 2, 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 COPYING.  If not, write to the Free
+// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+// USA.
+
+// 8.1 Additions to header <complex>
+
+#include <tr1/complex>
+#include <testsuite_hooks.h>
+#include <testsuite_tr1.h>
+
+void test01()
+{
+  using namespace std::tr1;
+  using namespace __gnu_test;
+
+  typedef std::complex<float>       cmplx_f_type;
+  typedef std::complex<double>      cmplx_d_type;
+  typedef std::complex<long double> cmplx_ld_type;
+
+  const float        f1 = 1.0f;
+  const double       d1 = 1.0;
+  const long double ld1 = 1.0l;
+
+  const cmplx_f_type  c_f1(f1, f1);
+  const cmplx_d_type  c_d1(d1, d1);
+  const cmplx_ld_type c_ld1(ld1, ld1);
+
+  check_ret_type<float>(arg(f1));
+  check_ret_type<double>(arg(d1));
+  check_ret_type<long double>(arg(ld1));
+
+  check_ret_type<cmplx_f_type>(conj(f1));
+  check_ret_type<cmplx_d_type>(conj(d1));
+  check_ret_type<cmplx_ld_type>(conj(ld1));
+  
+  check_ret_type<float>(imag(f1));
+  check_ret_type<double>(imag(d1));
+  check_ret_type<long double>(imag(ld1));
+  
+  check_ret_type<float>(norm(f1));
+  check_ret_type<double>(norm(d1));
+  check_ret_type<long double>(norm(ld1));
+
+  check_ret_type<cmplx_f_type>(polar(f1, f1));
+  check_ret_type<cmplx_d_type>(polar(d1, f1));
+  check_ret_type<cmplx_d_type>(polar(f1, d1));
+  check_ret_type<cmplx_d_type>(polar(d1, d1));
+  check_ret_type<cmplx_ld_type>(polar(ld1, d1));
+  check_ret_type<cmplx_ld_type>(polar(d1, ld1));
+  check_ret_type<cmplx_ld_type>(polar(ld1, f1));
+  check_ret_type<cmplx_ld_type>(polar(f1, ld1));
+  check_ret_type<cmplx_ld_type>(polar(ld1, ld1));
+
+  check_ret_type<cmplx_f_type>(pow(c_f1, f1));
+  check_ret_type<cmplx_d_type>(pow(c_d1, f1));
+  check_ret_type<cmplx_d_type>(pow(c_f1, d1));
+  check_ret_type<cmplx_d_type>(pow(c_d1, d1));
+  check_ret_type<cmplx_ld_type>(pow(c_ld1, d1));
+  check_ret_type<cmplx_ld_type>(pow(c_d1, ld1));
+  check_ret_type<cmplx_ld_type>(pow(c_ld1, f1));
+  check_ret_type<cmplx_ld_type>(pow(c_f1, ld1));
+  check_ret_type<cmplx_ld_type>(pow(c_ld1, ld1));
+
+  check_ret_type<cmplx_f_type>(pow(f1, c_f1));
+  check_ret_type<cmplx_d_type>(pow(d1, c_f1));
+  check_ret_type<cmplx_d_type>(pow(f1, c_d1));
+  check_ret_type<cmplx_d_type>(pow(d1, c_d1));
+  check_ret_type<cmplx_ld_type>(pow(ld1, c_d1));
+  check_ret_type<cmplx_ld_type>(pow(d1, c_ld1));
+  check_ret_type<cmplx_ld_type>(pow(ld1, c_f1));
+  check_ret_type<cmplx_ld_type>(pow(f1, c_ld1));
+  check_ret_type<cmplx_ld_type>(pow(ld1, c_ld1));
+
+  check_ret_type<cmplx_f_type>(pow(c_f1, c_f1));
+  check_ret_type<cmplx_d_type>(pow(c_d1, c_f1));
+  check_ret_type<cmplx_d_type>(pow(c_f1, c_d1));
+  check_ret_type<cmplx_d_type>(pow(c_d1, c_d1));
+  check_ret_type<cmplx_ld_type>(pow(c_ld1, c_d1));
+  check_ret_type<cmplx_ld_type>(pow(c_d1, c_ld1));
+  check_ret_type<cmplx_ld_type>(pow(c_ld1, c_f1));
+  check_ret_type<cmplx_ld_type>(pow(c_f1, c_ld1));
+  check_ret_type<cmplx_ld_type>(pow(c_ld1, c_ld1));
+
+  check_ret_type<float>(real(f1));
+  check_ret_type<double>(real(d1));
+  check_ret_type<long double>(real(ld1));
+}
+    
+int main()
+{
+  test01();
+  return 0;
+}
Index: testsuite/tr1/8_c_compatibility/complex/overloads_int.cc
===================================================================
--- testsuite/tr1/8_c_compatibility/complex/overloads_int.cc	(revision 0)
+++ testsuite/tr1/8_c_compatibility/complex/overloads_int.cc	(revision 0)
@@ -0,0 +1,87 @@
+// 2006-01-12  Paolo Carlini  <pcarlini@suse.de>
+//
+// Copyright (C) 2006 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 2, 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 COPYING.  If not, write to the Free
+// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+// USA.
+
+// 8.1 Additions to header <complex>
+
+#include <tr1/complex>
+#include <testsuite_hooks.h>
+#include <testsuite_tr1.h>
+
+void test01()
+{
+  bool test __attribute__((unused)) = true;
+  using namespace std::tr1;
+  using namespace __gnu_test;
+
+  typedef std::complex<float>  cmplx_f_type;
+  typedef std::complex<double> cmplx_d_type;
+
+  const int       i1 = 1;
+  const unsigned  u1 = 1;
+  const long      l1 = 1;
+  const double    f1 = 1.0f;
+  const double    d0 = 0.0;
+  const double    d1 = 1.0;
+    
+  check_ret_type<double>(arg(i1));
+  VERIFY( arg(i1) == arg(d1) );
+  VERIFY( arg(i1) == arg(cmplx_d_type(d1, d0)) );
+
+  check_ret_type<cmplx_d_type>(conj(i1));
+  VERIFY( conj(i1) == conj(d1) );
+  VERIFY( conj(i1) == conj(cmplx_d_type(d1, d0)) );
+
+  check_ret_type<double>(imag(i1));
+  VERIFY( imag(i1) == imag(d1) );
+  VERIFY( imag(i1) == imag(cmplx_d_type(d1, d0)) );
+
+  check_ret_type<double>(norm(i1));
+  VERIFY( norm(i1) == norm(d1) );
+  VERIFY( norm(i1) == norm(cmplx_d_type(d1, d0)) );
+
+  check_ret_type<cmplx_d_type>(polar(i1, i1));
+  VERIFY( polar(i1, i1) == polar(d1, d1) );
+
+  // NB: According to the letter of 8.1.9/3 the return type
+  //     should be a cmplx_d_type, but the existing overload
+  //     std::pow(const complex<>&, int) wins.
+  check_ret_type<cmplx_f_type>(pow(cmplx_f_type(f1, f1), i1));
+
+  check_ret_type<cmplx_d_type>(pow(cmplx_f_type(f1, f1), u1));
+  check_ret_type<cmplx_d_type>(pow(cmplx_f_type(f1, f1), l1));
+  check_ret_type<cmplx_d_type>(pow(cmplx_d_type(d1, d1), i1));
+  VERIFY( pow(cmplx_d_type(d1, d1), i1) == pow(cmplx_d_type(d1, d1), d1) );
+
+  check_ret_type<cmplx_d_type>(pow(i1, cmplx_f_type(f1, f1)));
+  check_ret_type<cmplx_d_type>(pow(u1, cmplx_f_type(f1, f1)));
+  check_ret_type<cmplx_d_type>(pow(l1, cmplx_f_type(f1, f1)));
+  check_ret_type<cmplx_d_type>(pow(i1, cmplx_d_type(d1, d1)));
+  VERIFY( pow(i1, cmplx_d_type(d1, d1)) == pow(d1, cmplx_d_type(d1, d1)) );
+
+  check_ret_type<double>(real(i1));
+  VERIFY( real(i1) == real(d1) );
+  VERIFY( real(i1) == real(cmplx_d_type(d1, d1)) );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]