2006-01-13 Paolo Carlini <pcarlini@suse.de>
authorpaolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 13 Jan 2006 09:45:57 +0000 (09:45 +0000)
committerpaolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 13 Jan 2006 09:45:57 +0000 (09:45 +0000)
    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.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@109663 138bc75d-0d04-0410-961f-82ee72b054a4

libstdc++-v3/ChangeLog
libstdc++-v3/include/Makefile.am
libstdc++-v3/include/Makefile.in
libstdc++-v3/include/tr1/common.h [new file with mode: 0644]
libstdc++-v3/include/tr1/complex
libstdc++-v3/testsuite/testsuite_tr1.h
libstdc++-v3/testsuite/tr1/8_c_compatibility/complex/overloads_float.cc [new file with mode: 0644]
libstdc++-v3/testsuite/tr1/8_c_compatibility/complex/overloads_int.cc [new file with mode: 0644]

index 995e262..ca1b988 100644 (file)
@@ -1,3 +1,16 @@
+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.
+
 2006-01-12  Benjamin Kosnik  <bkoz@redhat.com>
 
        * acinclude.m4(GLIBCXX_CHECK_LINKER_FEATURES): Enable
index 68b1d81..faa2349 100644 (file)
@@ -483,6 +483,7 @@ tr1_headers = \
        ${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 9b9c822..2425898 100644 (file)
@@ -701,6 +701,7 @@ tr1_headers = \
        ${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 \
diff --git a/libstdc++-v3/include/tr1/common.h b/libstdc++-v3/include/tr1/common.h
new file mode 100644 (file)
index 0000000..ca9559e
--- /dev/null
@@ -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 5df17bb..39205af 100644 (file)
@@ -35,6 +35,7 @@
 #define _TR1_COMPLEX 1
 
 #include "../complex"
+#include <tr1/common.h>
 
 // namespace std::tr1
 namespace std
@@ -51,7 +52,7 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
   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 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
     { 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 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
     { 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 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
     { 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 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
     { 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 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
     { 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 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
     { 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 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
     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<typename _Tp, typename _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<typename _Tp, typename _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<typename _Tp, typename _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<typename _Tp, typename _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 e925c84..8fddc88 100644 (file)
@@ -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 @@ namespace __gnu_test
     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
diff --git a/libstdc++-v3/testsuite/tr1/8_c_compatibility/complex/overloads_float.cc b/libstdc++-v3/testsuite/tr1/8_c_compatibility/complex/overloads_float.cc
new file mode 100644 (file)
index 0000000..d03075a
--- /dev/null
@@ -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;
+}
diff --git a/libstdc++-v3/testsuite/tr1/8_c_compatibility/complex/overloads_int.cc b/libstdc++-v3/testsuite/tr1/8_c_compatibility/complex/overloads_int.cc
new file mode 100644 (file)
index 0000000..97b4cd2
--- /dev/null
@@ -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;
+}