xref: /netbsd-src/external/gpl3/gcc.old/dist/libstdc++-v3/include/std/complex (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1// The template and inlines for the -*- C++ -*- complex number classes.
2
3// Copyright (C) 1997-2018 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library.  This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file include/complex
26 *  This is a Standard C++ Library header.
27 */
28
29//
30// ISO C++ 14882: 26.2  Complex Numbers
31// Note: this is not a conforming implementation.
32// Initially implemented by Ulrich Drepper <drepper@cygnus.com>
33// Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
34//
35
36#ifndef _GLIBCXX_COMPLEX
37#define _GLIBCXX_COMPLEX 1
38
39#pragma GCC system_header
40
41#include <bits/c++config.h>
42#include <bits/cpp_type_traits.h>
43#include <ext/type_traits.h>
44#include <cmath>
45#include <sstream>
46
47#if _GLIBCXX_USE_C99_COMPLEX
48// This is disgusting; we can't include ccomplex because that requires c++11
49// and we can't use the builtins because those point to the wrong
50// ABI-wise cabs/cabsf so we manually declare those here and use
51// them directly.
52extern "C" float __c99_cabsf(_Complex float);
53extern "C" double __c99_cabs(_Complex double);
54extern "C" long double __c99_cabsl(_Complex long double);
55#endif
56
57// Get rid of a macro possibly defined in <complex.h>
58#undef complex
59
60namespace std _GLIBCXX_VISIBILITY(default)
61{
62_GLIBCXX_BEGIN_NAMESPACE_VERSION
63
64  /**
65   * @defgroup complex_numbers Complex Numbers
66   * @ingroup numerics
67   *
68   * Classes and functions for complex numbers.
69   * @{
70   */
71
72  // Forward declarations.
73  template<typename _Tp> class complex;
74  template<> class complex<float>;
75  template<> class complex<double>;
76  template<> class complex<long double>;
77
78  ///  Return magnitude of @a z.
79  template<typename _Tp> _Tp abs(const complex<_Tp>&);
80  ///  Return phase angle of @a z.
81  template<typename _Tp> _Tp arg(const complex<_Tp>&);
82  ///  Return @a z magnitude squared.
83  template<typename _Tp> _Tp norm(const complex<_Tp>&);
84
85  ///  Return complex conjugate of @a z.
86  template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&);
87  ///  Return complex with magnitude @a rho and angle @a theta.
88  template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
89
90  // Transcendentals:
91  /// Return complex cosine of @a z.
92  template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
93  /// Return complex hyperbolic cosine of @a z.
94  template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
95  /// Return complex base e exponential of @a z.
96  template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
97  /// Return complex natural logarithm of @a z.
98  template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
99  /// Return complex base 10 logarithm of @a z.
100  template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
101  /// Return @a x to the @a y'th power.
102  template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
103  /// Return @a x to the @a y'th power.
104  template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
105  /// Return @a x to the @a y'th power.
106  template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
107                                          const complex<_Tp>&);
108  /// Return @a x to the @a y'th power.
109  template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
110  /// Return complex sine of @a z.
111  template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
112  /// Return complex hyperbolic sine of @a z.
113  template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
114  /// Return complex square root of @a z.
115  template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
116  /// Return complex tangent of @a z.
117  template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
118  /// Return complex hyperbolic tangent of @a z.
119  template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
120
121
122  // 26.2.2  Primary template class complex
123  /**
124   *  Template to represent complex numbers.
125   *
126   *  Specializations for float, double, and long double are part of the
127   *  library.  Results with any other type are not guaranteed.
128   *
129   *  @param  Tp  Type of real and imaginary values.
130  */
131  template<typename _Tp>
132    struct complex
133    {
134      /// Value typedef.
135      typedef _Tp value_type;
136
137      ///  Default constructor.  First parameter is x, second parameter is y.
138      ///  Unspecified parameters default to 0.
139      _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
140      : _M_real(__r), _M_imag(__i) { }
141
142      // Let the compiler synthesize the copy constructor
143#if __cplusplus >= 201103L
144      constexpr complex(const complex&) = default;
145#endif
146
147      ///  Converting constructor.
148      template<typename _Up>
149        _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z)
150	: _M_real(__z.real()), _M_imag(__z.imag()) { }
151
152#if __cplusplus >= 201103L
153      // _GLIBCXX_RESOLVE_LIB_DEFECTS
154      // DR 387. std::complex over-encapsulated.
155      _GLIBCXX_ABI_TAG_CXX11
156      constexpr _Tp
157      real() const { return _M_real; }
158
159      _GLIBCXX_ABI_TAG_CXX11
160      constexpr _Tp
161      imag() const { return _M_imag; }
162#else
163      ///  Return real part of complex number.
164      _Tp&
165      real() { return _M_real; }
166
167      ///  Return real part of complex number.
168      const _Tp&
169      real() const { return _M_real; }
170
171      ///  Return imaginary part of complex number.
172      _Tp&
173      imag() { return _M_imag; }
174
175      ///  Return imaginary part of complex number.
176      const _Tp&
177      imag() const { return _M_imag; }
178#endif
179
180      // _GLIBCXX_RESOLVE_LIB_DEFECTS
181      // DR 387. std::complex over-encapsulated.
182      void
183      real(_Tp __val) { _M_real = __val; }
184
185      void
186      imag(_Tp __val) { _M_imag = __val; }
187
188      /// Assign a scalar to this complex number.
189      complex<_Tp>& operator=(const _Tp&);
190
191      /// Add a scalar to this complex number.
192      // 26.2.5/1
193      complex<_Tp>&
194      operator+=(const _Tp& __t)
195      {
196	_M_real += __t;
197	return *this;
198      }
199
200      /// Subtract a scalar from this complex number.
201      // 26.2.5/3
202      complex<_Tp>&
203      operator-=(const _Tp& __t)
204      {
205	_M_real -= __t;
206	return *this;
207      }
208
209      /// Multiply this complex number by a scalar.
210      complex<_Tp>& operator*=(const _Tp&);
211      /// Divide this complex number by a scalar.
212      complex<_Tp>& operator/=(const _Tp&);
213
214      // Let the compiler synthesize the copy assignment operator
215#if __cplusplus >= 201103L
216      complex& operator=(const complex&) = default;
217#endif
218
219      /// Assign another complex number to this one.
220      template<typename _Up>
221        complex<_Tp>& operator=(const complex<_Up>&);
222      /// Add another complex number to this one.
223      template<typename _Up>
224        complex<_Tp>& operator+=(const complex<_Up>&);
225      /// Subtract another complex number from this one.
226      template<typename _Up>
227        complex<_Tp>& operator-=(const complex<_Up>&);
228      /// Multiply this complex number by another.
229      template<typename _Up>
230        complex<_Tp>& operator*=(const complex<_Up>&);
231      /// Divide this complex number by another.
232      template<typename _Up>
233        complex<_Tp>& operator/=(const complex<_Up>&);
234
235      _GLIBCXX_CONSTEXPR complex __rep() const
236      { return *this; }
237
238    private:
239      _Tp _M_real;
240      _Tp _M_imag;
241    };
242
243  template<typename _Tp>
244    complex<_Tp>&
245    complex<_Tp>::operator=(const _Tp& __t)
246    {
247     _M_real = __t;
248     _M_imag = _Tp();
249     return *this;
250    }
251
252  // 26.2.5/5
253  template<typename _Tp>
254    complex<_Tp>&
255    complex<_Tp>::operator*=(const _Tp& __t)
256    {
257      _M_real *= __t;
258      _M_imag *= __t;
259      return *this;
260    }
261
262  // 26.2.5/7
263  template<typename _Tp>
264    complex<_Tp>&
265    complex<_Tp>::operator/=(const _Tp& __t)
266    {
267      _M_real /= __t;
268      _M_imag /= __t;
269      return *this;
270    }
271
272  template<typename _Tp>
273    template<typename _Up>
274    complex<_Tp>&
275    complex<_Tp>::operator=(const complex<_Up>& __z)
276    {
277      _M_real = __z.real();
278      _M_imag = __z.imag();
279      return *this;
280    }
281
282  // 26.2.5/9
283  template<typename _Tp>
284    template<typename _Up>
285    complex<_Tp>&
286    complex<_Tp>::operator+=(const complex<_Up>& __z)
287    {
288      _M_real += __z.real();
289      _M_imag += __z.imag();
290      return *this;
291    }
292
293  // 26.2.5/11
294  template<typename _Tp>
295    template<typename _Up>
296    complex<_Tp>&
297    complex<_Tp>::operator-=(const complex<_Up>& __z)
298    {
299      _M_real -= __z.real();
300      _M_imag -= __z.imag();
301      return *this;
302    }
303
304  // 26.2.5/13
305  // XXX: This is a grammar school implementation.
306  template<typename _Tp>
307    template<typename _Up>
308    complex<_Tp>&
309    complex<_Tp>::operator*=(const complex<_Up>& __z)
310    {
311      const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
312      _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
313      _M_real = __r;
314      return *this;
315    }
316
317  // 26.2.5/15
318  // XXX: This is a grammar school implementation.
319  template<typename _Tp>
320    template<typename _Up>
321    complex<_Tp>&
322    complex<_Tp>::operator/=(const complex<_Up>& __z)
323    {
324      const _Tp __r =  _M_real * __z.real() + _M_imag * __z.imag();
325      const _Tp __n = std::norm(__z);
326      _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
327      _M_real = __r / __n;
328      return *this;
329    }
330
331  // Operators:
332  //@{
333  ///  Return new complex value @a x plus @a y.
334  template<typename _Tp>
335    inline complex<_Tp>
336    operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
337    {
338      complex<_Tp> __r = __x;
339      __r += __y;
340      return __r;
341    }
342
343  template<typename _Tp>
344    inline complex<_Tp>
345    operator+(const complex<_Tp>& __x, const _Tp& __y)
346    {
347      complex<_Tp> __r = __x;
348      __r += __y;
349      return __r;
350    }
351
352  template<typename _Tp>
353    inline complex<_Tp>
354    operator+(const _Tp& __x, const complex<_Tp>& __y)
355    {
356      complex<_Tp> __r = __y;
357      __r += __x;
358      return __r;
359    }
360  //@}
361
362  //@{
363  ///  Return new complex value @a x minus @a y.
364  template<typename _Tp>
365    inline complex<_Tp>
366    operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
367    {
368      complex<_Tp> __r = __x;
369      __r -= __y;
370      return __r;
371    }
372
373  template<typename _Tp>
374    inline complex<_Tp>
375    operator-(const complex<_Tp>& __x, const _Tp& __y)
376    {
377      complex<_Tp> __r = __x;
378      __r -= __y;
379      return __r;
380    }
381
382  template<typename _Tp>
383    inline complex<_Tp>
384    operator-(const _Tp& __x, const complex<_Tp>& __y)
385    {
386      complex<_Tp> __r(__x, -__y.imag());
387      __r -= __y.real();
388      return __r;
389    }
390  //@}
391
392  //@{
393  ///  Return new complex value @a x times @a y.
394  template<typename _Tp>
395    inline complex<_Tp>
396    operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
397    {
398      complex<_Tp> __r = __x;
399      __r *= __y;
400      return __r;
401    }
402
403  template<typename _Tp>
404    inline complex<_Tp>
405    operator*(const complex<_Tp>& __x, const _Tp& __y)
406    {
407      complex<_Tp> __r = __x;
408      __r *= __y;
409      return __r;
410    }
411
412  template<typename _Tp>
413    inline complex<_Tp>
414    operator*(const _Tp& __x, const complex<_Tp>& __y)
415    {
416      complex<_Tp> __r = __y;
417      __r *= __x;
418      return __r;
419    }
420  //@}
421
422  //@{
423  ///  Return new complex value @a x divided by @a y.
424  template<typename _Tp>
425    inline complex<_Tp>
426    operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
427    {
428      complex<_Tp> __r = __x;
429      __r /= __y;
430      return __r;
431    }
432
433  template<typename _Tp>
434    inline complex<_Tp>
435    operator/(const complex<_Tp>& __x, const _Tp& __y)
436    {
437      complex<_Tp> __r = __x;
438      __r /= __y;
439      return __r;
440    }
441
442  template<typename _Tp>
443    inline complex<_Tp>
444    operator/(const _Tp& __x, const complex<_Tp>& __y)
445    {
446      complex<_Tp> __r = __x;
447      __r /= __y;
448      return __r;
449    }
450  //@}
451
452  ///  Return @a x.
453  template<typename _Tp>
454    inline complex<_Tp>
455    operator+(const complex<_Tp>& __x)
456    { return __x; }
457
458  ///  Return complex negation of @a x.
459  template<typename _Tp>
460    inline complex<_Tp>
461    operator-(const complex<_Tp>& __x)
462    {  return complex<_Tp>(-__x.real(), -__x.imag()); }
463
464  //@{
465  ///  Return true if @a x is equal to @a y.
466  template<typename _Tp>
467    inline _GLIBCXX_CONSTEXPR bool
468    operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
469    { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
470
471  template<typename _Tp>
472    inline _GLIBCXX_CONSTEXPR bool
473    operator==(const complex<_Tp>& __x, const _Tp& __y)
474    { return __x.real() == __y && __x.imag() == _Tp(); }
475
476  template<typename _Tp>
477    inline _GLIBCXX_CONSTEXPR bool
478    operator==(const _Tp& __x, const complex<_Tp>& __y)
479    { return __x == __y.real() && _Tp() == __y.imag(); }
480  //@}
481
482  //@{
483  ///  Return false if @a x is equal to @a y.
484  template<typename _Tp>
485    inline _GLIBCXX_CONSTEXPR bool
486    operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
487    { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
488
489  template<typename _Tp>
490    inline _GLIBCXX_CONSTEXPR bool
491    operator!=(const complex<_Tp>& __x, const _Tp& __y)
492    { return __x.real() != __y || __x.imag() != _Tp(); }
493
494  template<typename _Tp>
495    inline _GLIBCXX_CONSTEXPR bool
496    operator!=(const _Tp& __x, const complex<_Tp>& __y)
497    { return __x != __y.real() || _Tp() != __y.imag(); }
498  //@}
499
500  ///  Extraction operator for complex values.
501  template<typename _Tp, typename _CharT, class _Traits>
502    basic_istream<_CharT, _Traits>&
503    operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
504    {
505      bool __fail = true;
506      _CharT __ch;
507      if (__is >> __ch)
508	{
509	  if (_Traits::eq(__ch, __is.widen('(')))
510	    {
511	      _Tp __u;
512	      if (__is >> __u >> __ch)
513		{
514		  const _CharT __rparen = __is.widen(')');
515		  if (_Traits::eq(__ch, __rparen))
516		    {
517		      __x = __u;
518		      __fail = false;
519		    }
520		  else if (_Traits::eq(__ch, __is.widen(',')))
521		    {
522		      _Tp __v;
523		      if (__is >> __v >> __ch)
524			{
525			  if (_Traits::eq(__ch, __rparen))
526			    {
527			      __x = complex<_Tp>(__u, __v);
528			      __fail = false;
529			    }
530			  else
531			    __is.putback(__ch);
532			}
533		    }
534		  else
535		    __is.putback(__ch);
536		}
537	    }
538	  else
539	    {
540	      __is.putback(__ch);
541	      _Tp __u;
542	      if (__is >> __u)
543		{
544		  __x = __u;
545		  __fail = false;
546		}
547	    }
548	}
549      if (__fail)
550	__is.setstate(ios_base::failbit);
551      return __is;
552    }
553
554  ///  Insertion operator for complex values.
555  template<typename _Tp, typename _CharT, class _Traits>
556    basic_ostream<_CharT, _Traits>&
557    operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
558    {
559      basic_ostringstream<_CharT, _Traits> __s;
560      __s.flags(__os.flags());
561      __s.imbue(__os.getloc());
562      __s.precision(__os.precision());
563      __s << '(' << __x.real() << ',' << __x.imag() << ')';
564      return __os << __s.str();
565    }
566
567  // Values
568#if __cplusplus >= 201103L
569  template<typename _Tp>
570    constexpr _Tp
571    real(const complex<_Tp>& __z)
572    { return __z.real(); }
573
574  template<typename _Tp>
575    constexpr _Tp
576    imag(const complex<_Tp>& __z)
577    { return __z.imag(); }
578#else
579  template<typename _Tp>
580    inline _Tp&
581    real(complex<_Tp>& __z)
582    { return __z.real(); }
583
584  template<typename _Tp>
585    inline const _Tp&
586    real(const complex<_Tp>& __z)
587    { return __z.real(); }
588
589  template<typename _Tp>
590    inline _Tp&
591    imag(complex<_Tp>& __z)
592    { return __z.imag(); }
593
594  template<typename _Tp>
595    inline const _Tp&
596    imag(const complex<_Tp>& __z)
597    { return __z.imag(); }
598#endif
599
600  // 26.2.7/3 abs(__z):  Returns the magnitude of __z.
601  template<typename _Tp>
602    inline _Tp
603    __complex_abs(const complex<_Tp>& __z)
604    {
605      _Tp __x = __z.real();
606      _Tp __y = __z.imag();
607      const _Tp __s = std::max(abs(__x), abs(__y));
608      if (__s == _Tp())  // well ...
609        return __s;
610      __x /= __s;
611      __y /= __s;
612      return __s * sqrt(__x * __x + __y * __y);
613    }
614
615#if _GLIBCXX_USE_C99_COMPLEX
616  // XXX: We can't use __builtin_cabs* because they are broken
617  inline float
618  __complex_abs(__complex__ float __z) { return __c99_cabsf(__z); }
619
620  inline double
621  __complex_abs(__complex__ double __z) { return __c99_cabs(__z); }
622
623  inline long double
624  __complex_abs(const __complex__ long double& __z)
625  { return __c99_cabsl(__z); }
626
627  template<typename _Tp>
628    inline _Tp
629    abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
630#else
631  template<typename _Tp>
632    inline _Tp
633    abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
634#endif
635
636
637  // 26.2.7/4: arg(__z): Returns the phase angle of __z.
638  template<typename _Tp>
639    inline _Tp
640    __complex_arg(const complex<_Tp>& __z)
641    { return  atan2(__z.imag(), __z.real()); }
642
643#if _GLIBCXX_USE_C99_COMPLEX
644  inline float
645  __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
646
647  inline double
648  __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
649
650  inline long double
651  __complex_arg(const __complex__ long double& __z)
652  { return __builtin_cargl(__z); }
653
654  template<typename _Tp>
655    inline _Tp
656    arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
657#else
658  template<typename _Tp>
659    inline _Tp
660    arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
661#endif
662
663  // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
664  //     As defined, norm() is -not- a norm is the common mathematical
665  //     sense used in numerics.  The helper class _Norm_helper<> tries to
666  //     distinguish between builtin floating point and the rest, so as
667  //     to deliver an answer as close as possible to the real value.
668  template<bool>
669    struct _Norm_helper
670    {
671      template<typename _Tp>
672        static inline _Tp _S_do_it(const complex<_Tp>& __z)
673        {
674          const _Tp __x = __z.real();
675          const _Tp __y = __z.imag();
676          return __x * __x + __y * __y;
677        }
678    };
679
680  template<>
681    struct _Norm_helper<true>
682    {
683      template<typename _Tp>
684        static inline _Tp _S_do_it(const complex<_Tp>& __z)
685        {
686          _Tp __res = std::abs(__z);
687          return __res * __res;
688        }
689    };
690
691  template<typename _Tp>
692    inline _Tp
693    norm(const complex<_Tp>& __z)
694    {
695      return _Norm_helper<__is_floating<_Tp>::__value
696	&& !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
697    }
698
699  template<typename _Tp>
700    inline complex<_Tp>
701    polar(const _Tp& __rho, const _Tp& __theta)
702    {
703      __glibcxx_assert( __rho >= 0 );
704      return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta));
705    }
706
707  template<typename _Tp>
708    inline complex<_Tp>
709    conj(const complex<_Tp>& __z)
710    { return complex<_Tp>(__z.real(), -__z.imag()); }
711
712  // Transcendentals
713
714  // 26.2.8/1 cos(__z):  Returns the cosine of __z.
715  template<typename _Tp>
716    inline complex<_Tp>
717    __complex_cos(const complex<_Tp>& __z)
718    {
719      const _Tp __x = __z.real();
720      const _Tp __y = __z.imag();
721      return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
722    }
723
724#if _GLIBCXX_USE_C99_COMPLEX
725  inline __complex__ float
726  __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
727
728  inline __complex__ double
729  __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
730
731  inline __complex__ long double
732  __complex_cos(const __complex__ long double& __z)
733  { return __builtin_ccosl(__z); }
734
735  template<typename _Tp>
736    inline complex<_Tp>
737    cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
738#else
739  template<typename _Tp>
740    inline complex<_Tp>
741    cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
742#endif
743
744  // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
745  template<typename _Tp>
746    inline complex<_Tp>
747    __complex_cosh(const complex<_Tp>& __z)
748    {
749      const _Tp __x = __z.real();
750      const _Tp __y = __z.imag();
751      return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
752    }
753
754#if _GLIBCXX_USE_C99_COMPLEX
755  inline __complex__ float
756  __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
757
758  inline __complex__ double
759  __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
760
761  inline __complex__ long double
762  __complex_cosh(const __complex__ long double& __z)
763  { return __builtin_ccoshl(__z); }
764
765  template<typename _Tp>
766    inline complex<_Tp>
767    cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
768#else
769  template<typename _Tp>
770    inline complex<_Tp>
771    cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
772#endif
773
774  // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
775  template<typename _Tp>
776    inline complex<_Tp>
777    __complex_exp(const complex<_Tp>& __z)
778    { return std::polar<_Tp>(exp(__z.real()), __z.imag()); }
779
780#if _GLIBCXX_USE_C99_COMPLEX
781  inline __complex__ float
782  __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
783
784  inline __complex__ double
785  __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
786
787  inline __complex__ long double
788  __complex_exp(const __complex__ long double& __z)
789  { return __builtin_cexpl(__z); }
790
791  template<typename _Tp>
792    inline complex<_Tp>
793    exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
794#else
795  template<typename _Tp>
796    inline complex<_Tp>
797    exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
798#endif
799
800  // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
801  //                    The branch cut is along the negative axis.
802  template<typename _Tp>
803    inline complex<_Tp>
804    __complex_log(const complex<_Tp>& __z)
805    { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
806
807#if _GLIBCXX_USE_C99_COMPLEX
808  inline __complex__ float
809  __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
810
811  inline __complex__ double
812  __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
813
814  inline __complex__ long double
815  __complex_log(const __complex__ long double& __z)
816  { return __builtin_clogl(__z); }
817
818  template<typename _Tp>
819    inline complex<_Tp>
820    log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
821#else
822  template<typename _Tp>
823    inline complex<_Tp>
824    log(const complex<_Tp>& __z) { return __complex_log(__z); }
825#endif
826
827  template<typename _Tp>
828    inline complex<_Tp>
829    log10(const complex<_Tp>& __z)
830    { return std::log(__z) / log(_Tp(10.0)); }
831
832  // 26.2.8/10 sin(__z): Returns the sine of __z.
833  template<typename _Tp>
834    inline complex<_Tp>
835    __complex_sin(const complex<_Tp>& __z)
836    {
837      const _Tp __x = __z.real();
838      const _Tp __y = __z.imag();
839      return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
840    }
841
842#if _GLIBCXX_USE_C99_COMPLEX
843  inline __complex__ float
844  __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
845
846  inline __complex__ double
847  __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
848
849  inline __complex__ long double
850  __complex_sin(const __complex__ long double& __z)
851  { return __builtin_csinl(__z); }
852
853  template<typename _Tp>
854    inline complex<_Tp>
855    sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
856#else
857  template<typename _Tp>
858    inline complex<_Tp>
859    sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
860#endif
861
862  // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
863  template<typename _Tp>
864    inline complex<_Tp>
865    __complex_sinh(const complex<_Tp>& __z)
866    {
867      const _Tp __x = __z.real();
868      const _Tp  __y = __z.imag();
869      return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
870    }
871
872#if _GLIBCXX_USE_C99_COMPLEX
873  inline __complex__ float
874  __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
875
876  inline __complex__ double
877  __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
878
879  inline __complex__ long double
880  __complex_sinh(const __complex__ long double& __z)
881  { return __builtin_csinhl(__z); }
882
883  template<typename _Tp>
884    inline complex<_Tp>
885    sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
886#else
887  template<typename _Tp>
888    inline complex<_Tp>
889    sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
890#endif
891
892  // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
893  //                     The branch cut is on the negative axis.
894  template<typename _Tp>
895    complex<_Tp>
896    __complex_sqrt(const complex<_Tp>& __z)
897    {
898      _Tp __x = __z.real();
899      _Tp __y = __z.imag();
900
901      if (__x == _Tp())
902        {
903          _Tp __t = sqrt(abs(__y) / 2);
904          return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
905        }
906      else
907        {
908          _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
909          _Tp __u = __t / 2;
910          return __x > _Tp()
911            ? complex<_Tp>(__u, __y / __t)
912            : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
913        }
914    }
915
916#if _GLIBCXX_USE_C99_COMPLEX
917  inline __complex__ float
918  __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
919
920  inline __complex__ double
921  __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
922
923  inline __complex__ long double
924  __complex_sqrt(const __complex__ long double& __z)
925  { return __builtin_csqrtl(__z); }
926
927  template<typename _Tp>
928    inline complex<_Tp>
929    sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
930#else
931  template<typename _Tp>
932    inline complex<_Tp>
933    sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
934#endif
935
936  // 26.2.8/14 tan(__z):  Return the complex tangent of __z.
937
938  template<typename _Tp>
939    inline complex<_Tp>
940    __complex_tan(const complex<_Tp>& __z)
941    { return std::sin(__z) / std::cos(__z); }
942
943#if _GLIBCXX_USE_C99_COMPLEX
944  inline __complex__ float
945  __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
946
947  inline __complex__ double
948  __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
949
950  inline __complex__ long double
951  __complex_tan(const __complex__ long double& __z)
952  { return __builtin_ctanl(__z); }
953
954  template<typename _Tp>
955    inline complex<_Tp>
956    tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
957#else
958  template<typename _Tp>
959    inline complex<_Tp>
960    tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
961#endif
962
963
964  // 26.2.8/15 tanh(__z):  Returns the hyperbolic tangent of __z.
965
966  template<typename _Tp>
967    inline complex<_Tp>
968    __complex_tanh(const complex<_Tp>& __z)
969    { return std::sinh(__z) / std::cosh(__z); }
970
971#if _GLIBCXX_USE_C99_COMPLEX
972  inline __complex__ float
973  __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
974
975  inline __complex__ double
976  __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
977
978  inline __complex__ long double
979  __complex_tanh(const __complex__ long double& __z)
980  { return __builtin_ctanhl(__z); }
981
982  template<typename _Tp>
983    inline complex<_Tp>
984    tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
985#else
986  template<typename _Tp>
987    inline complex<_Tp>
988    tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
989#endif
990
991
992  // 26.2.8/9  pow(__x, __y): Returns the complex power base of __x
993  //                          raised to the __y-th power.  The branch
994  //                          cut is on the negative axis.
995  template<typename _Tp>
996    complex<_Tp>
997    __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
998    {
999      complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
1000
1001      while (__n >>= 1)
1002        {
1003          __x *= __x;
1004          if (__n % 2)
1005            __y *= __x;
1006        }
1007
1008      return __y;
1009    }
1010
1011  // In C++11 mode we used to implement the resolution of
1012  // DR 844. complex pow return type is ambiguous.
1013  // thus the following overload was disabled in that mode.  However, doing
1014  // that causes all sorts of issues, see, for example:
1015  //   http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html
1016  // and also PR57974.
1017  template<typename _Tp>
1018    inline complex<_Tp>
1019    pow(const complex<_Tp>& __z, int __n)
1020    {
1021      return __n < 0
1022	? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n)
1023        : std::__complex_pow_unsigned(__z, __n);
1024    }
1025
1026  template<typename _Tp>
1027    complex<_Tp>
1028    pow(const complex<_Tp>& __x, const _Tp& __y)
1029    {
1030#if ! _GLIBCXX_USE_C99_COMPLEX
1031      if (__x == _Tp())
1032	return _Tp();
1033#endif
1034      if (__x.imag() == _Tp() && __x.real() > _Tp())
1035        return pow(__x.real(), __y);
1036
1037      complex<_Tp> __t = std::log(__x);
1038      return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag());
1039    }
1040
1041  template<typename _Tp>
1042    inline complex<_Tp>
1043    __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1044    { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
1045
1046#if _GLIBCXX_USE_C99_COMPLEX
1047  inline __complex__ float
1048  __complex_pow(__complex__ float __x, __complex__ float __y)
1049  { return __builtin_cpowf(__x, __y); }
1050
1051  inline __complex__ double
1052  __complex_pow(__complex__ double __x, __complex__ double __y)
1053  { return __builtin_cpow(__x, __y); }
1054
1055  inline __complex__ long double
1056  __complex_pow(const __complex__ long double& __x,
1057		const __complex__ long double& __y)
1058  { return __builtin_cpowl(__x, __y); }
1059
1060  template<typename _Tp>
1061    inline complex<_Tp>
1062    pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1063    { return __complex_pow(__x.__rep(), __y.__rep()); }
1064#else
1065  template<typename _Tp>
1066    inline complex<_Tp>
1067    pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1068    { return __complex_pow(__x, __y); }
1069#endif
1070
1071  template<typename _Tp>
1072    inline complex<_Tp>
1073    pow(const _Tp& __x, const complex<_Tp>& __y)
1074    {
1075      return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()),
1076					   __y.imag() * log(__x))
1077	                 : std::pow(complex<_Tp>(__x), __y);
1078    }
1079
1080  /// 26.2.3  complex specializations
1081  /// complex<float> specialization
1082  template<>
1083    struct complex<float>
1084    {
1085      typedef float value_type;
1086      typedef __complex__ float _ComplexT;
1087
1088      _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1089
1090      _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
1091#if __cplusplus >= 201103L
1092      : _M_value{ __r, __i } { }
1093#else
1094      {
1095	__real__ _M_value = __r;
1096	__imag__ _M_value = __i;
1097      }
1098#endif
1099
1100      explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
1101      explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1102
1103#if __cplusplus >= 201103L
1104      // _GLIBCXX_RESOLVE_LIB_DEFECTS
1105      // DR 387. std::complex over-encapsulated.
1106      __attribute ((__abi_tag__ ("cxx11")))
1107      constexpr float
1108      real() const { return __real__ _M_value; }
1109
1110      __attribute ((__abi_tag__ ("cxx11")))
1111      constexpr float
1112      imag() const { return __imag__ _M_value; }
1113#else
1114      float&
1115      real() { return __real__ _M_value; }
1116
1117      const float&
1118      real() const { return __real__ _M_value; }
1119
1120      float&
1121      imag() { return __imag__ _M_value; }
1122
1123      const float&
1124      imag() const { return __imag__ _M_value; }
1125#endif
1126
1127      // _GLIBCXX_RESOLVE_LIB_DEFECTS
1128      // DR 387. std::complex over-encapsulated.
1129      void
1130      real(float __val) { __real__ _M_value = __val; }
1131
1132      void
1133      imag(float __val) { __imag__ _M_value = __val; }
1134
1135      complex&
1136      operator=(float __f)
1137      {
1138	_M_value = __f;
1139	return *this;
1140      }
1141
1142      complex&
1143      operator+=(float __f)
1144      {
1145	_M_value += __f;
1146	return *this;
1147      }
1148
1149      complex&
1150      operator-=(float __f)
1151      {
1152	_M_value -= __f;
1153	return *this;
1154      }
1155
1156      complex&
1157      operator*=(float __f)
1158      {
1159	_M_value *= __f;
1160	return *this;
1161      }
1162
1163      complex&
1164      operator/=(float __f)
1165      {
1166	_M_value /= __f;
1167	return *this;
1168      }
1169
1170      // Let the compiler synthesize the copy and assignment
1171      // operator.  It always does a pretty good job.
1172      // complex& operator=(const complex&);
1173
1174      template<typename _Tp>
1175        complex&
1176        operator=(const complex<_Tp>&  __z)
1177	{
1178	  __real__ _M_value = __z.real();
1179	  __imag__ _M_value = __z.imag();
1180	  return *this;
1181	}
1182
1183      template<typename _Tp>
1184        complex&
1185        operator+=(const complex<_Tp>& __z)
1186	{
1187	  __real__ _M_value += __z.real();
1188	  __imag__ _M_value += __z.imag();
1189	  return *this;
1190	}
1191
1192      template<class _Tp>
1193        complex&
1194        operator-=(const complex<_Tp>& __z)
1195	{
1196	  __real__ _M_value -= __z.real();
1197	  __imag__ _M_value -= __z.imag();
1198	  return *this;
1199	}
1200
1201      template<class _Tp>
1202        complex&
1203        operator*=(const complex<_Tp>& __z)
1204	{
1205	  _ComplexT __t;
1206	  __real__ __t = __z.real();
1207	  __imag__ __t = __z.imag();
1208	  _M_value *= __t;
1209	  return *this;
1210	}
1211
1212      template<class _Tp>
1213        complex&
1214        operator/=(const complex<_Tp>& __z)
1215	{
1216	  _ComplexT __t;
1217	  __real__ __t = __z.real();
1218	  __imag__ __t = __z.imag();
1219	  _M_value /= __t;
1220	  return *this;
1221	}
1222
1223      _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1224
1225    private:
1226      _ComplexT _M_value;
1227    };
1228
1229  /// 26.2.3  complex specializations
1230  /// complex<double> specialization
1231  template<>
1232    struct complex<double>
1233    {
1234      typedef double value_type;
1235      typedef __complex__ double _ComplexT;
1236
1237      _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1238
1239      _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
1240#if __cplusplus >= 201103L
1241      : _M_value{ __r, __i } { }
1242#else
1243      {
1244	__real__ _M_value = __r;
1245	__imag__ _M_value = __i;
1246      }
1247#endif
1248
1249      _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1250      : _M_value(__z.__rep()) { }
1251
1252      explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1253
1254#if __cplusplus >= 201103L
1255      // _GLIBCXX_RESOLVE_LIB_DEFECTS
1256      // DR 387. std::complex over-encapsulated.
1257      __attribute ((__abi_tag__ ("cxx11")))
1258      constexpr double
1259      real() const { return __real__ _M_value; }
1260
1261      __attribute ((__abi_tag__ ("cxx11")))
1262      constexpr double
1263      imag() const { return __imag__ _M_value; }
1264#else
1265      double&
1266      real() { return __real__ _M_value; }
1267
1268      const double&
1269      real() const { return __real__ _M_value; }
1270
1271      double&
1272      imag() { return __imag__ _M_value; }
1273
1274      const double&
1275      imag() const { return __imag__ _M_value; }
1276#endif
1277
1278      // _GLIBCXX_RESOLVE_LIB_DEFECTS
1279      // DR 387. std::complex over-encapsulated.
1280      void
1281      real(double __val) { __real__ _M_value = __val; }
1282
1283      void
1284      imag(double __val) { __imag__ _M_value = __val; }
1285
1286      complex&
1287      operator=(double __d)
1288      {
1289	_M_value = __d;
1290	return *this;
1291      }
1292
1293      complex&
1294      operator+=(double __d)
1295      {
1296	_M_value += __d;
1297	return *this;
1298      }
1299
1300      complex&
1301      operator-=(double __d)
1302      {
1303	_M_value -= __d;
1304	return *this;
1305      }
1306
1307      complex&
1308      operator*=(double __d)
1309      {
1310	_M_value *= __d;
1311	return *this;
1312      }
1313
1314      complex&
1315      operator/=(double __d)
1316      {
1317	_M_value /= __d;
1318	return *this;
1319      }
1320
1321      // The compiler will synthesize this, efficiently.
1322      // complex& operator=(const complex&);
1323
1324      template<typename _Tp>
1325        complex&
1326        operator=(const complex<_Tp>& __z)
1327	{
1328	  __real__ _M_value = __z.real();
1329	  __imag__ _M_value = __z.imag();
1330	  return *this;
1331	}
1332
1333      template<typename _Tp>
1334        complex&
1335        operator+=(const complex<_Tp>& __z)
1336	{
1337	  __real__ _M_value += __z.real();
1338	  __imag__ _M_value += __z.imag();
1339	  return *this;
1340	}
1341
1342      template<typename _Tp>
1343        complex&
1344        operator-=(const complex<_Tp>& __z)
1345	{
1346	  __real__ _M_value -= __z.real();
1347	  __imag__ _M_value -= __z.imag();
1348	  return *this;
1349	}
1350
1351      template<typename _Tp>
1352        complex&
1353        operator*=(const complex<_Tp>& __z)
1354	{
1355	  _ComplexT __t;
1356	  __real__ __t = __z.real();
1357	  __imag__ __t = __z.imag();
1358	  _M_value *= __t;
1359	  return *this;
1360	}
1361
1362      template<typename _Tp>
1363        complex&
1364        operator/=(const complex<_Tp>& __z)
1365	{
1366	  _ComplexT __t;
1367	  __real__ __t = __z.real();
1368	  __imag__ __t = __z.imag();
1369	  _M_value /= __t;
1370	  return *this;
1371	}
1372
1373      _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1374
1375    private:
1376      _ComplexT _M_value;
1377    };
1378
1379  /// 26.2.3  complex specializations
1380  /// complex<long double> specialization
1381  template<>
1382    struct complex<long double>
1383    {
1384      typedef long double value_type;
1385      typedef __complex__ long double _ComplexT;
1386
1387      _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1388
1389      _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L,
1390				 long double __i = 0.0L)
1391#if __cplusplus >= 201103L
1392      : _M_value{ __r, __i } { }
1393#else
1394      {
1395	__real__ _M_value = __r;
1396	__imag__ _M_value = __i;
1397      }
1398#endif
1399
1400      _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1401      : _M_value(__z.__rep()) { }
1402
1403      _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
1404      : _M_value(__z.__rep()) { }
1405
1406#if __cplusplus >= 201103L
1407      // _GLIBCXX_RESOLVE_LIB_DEFECTS
1408      // DR 387. std::complex over-encapsulated.
1409      __attribute ((__abi_tag__ ("cxx11")))
1410      constexpr long double
1411      real() const { return __real__ _M_value; }
1412
1413      __attribute ((__abi_tag__ ("cxx11")))
1414      constexpr long double
1415      imag() const { return __imag__ _M_value; }
1416#else
1417      long double&
1418      real() { return __real__ _M_value; }
1419
1420      const long double&
1421      real() const { return __real__ _M_value; }
1422
1423      long double&
1424      imag() { return __imag__ _M_value; }
1425
1426      const long double&
1427      imag() const { return __imag__ _M_value; }
1428#endif
1429
1430      // _GLIBCXX_RESOLVE_LIB_DEFECTS
1431      // DR 387. std::complex over-encapsulated.
1432      void
1433      real(long double __val) { __real__ _M_value = __val; }
1434
1435      void
1436      imag(long double __val) { __imag__ _M_value = __val; }
1437
1438      complex&
1439      operator=(long double __r)
1440      {
1441	_M_value = __r;
1442	return *this;
1443      }
1444
1445      complex&
1446      operator+=(long double __r)
1447      {
1448	_M_value += __r;
1449	return *this;
1450      }
1451
1452      complex&
1453      operator-=(long double __r)
1454      {
1455	_M_value -= __r;
1456	return *this;
1457      }
1458
1459      complex&
1460      operator*=(long double __r)
1461      {
1462	_M_value *= __r;
1463	return *this;
1464      }
1465
1466      complex&
1467      operator/=(long double __r)
1468      {
1469	_M_value /= __r;
1470	return *this;
1471      }
1472
1473      // The compiler knows how to do this efficiently
1474      // complex& operator=(const complex&);
1475
1476      template<typename _Tp>
1477        complex&
1478        operator=(const complex<_Tp>& __z)
1479	{
1480	  __real__ _M_value = __z.real();
1481	  __imag__ _M_value = __z.imag();
1482	  return *this;
1483	}
1484
1485      template<typename _Tp>
1486        complex&
1487	operator+=(const complex<_Tp>& __z)
1488	{
1489	  __real__ _M_value += __z.real();
1490	  __imag__ _M_value += __z.imag();
1491	  return *this;
1492	}
1493
1494      template<typename _Tp>
1495        complex&
1496	operator-=(const complex<_Tp>& __z)
1497	{
1498	  __real__ _M_value -= __z.real();
1499	  __imag__ _M_value -= __z.imag();
1500	  return *this;
1501	}
1502
1503      template<typename _Tp>
1504        complex&
1505	operator*=(const complex<_Tp>& __z)
1506	{
1507	  _ComplexT __t;
1508	  __real__ __t = __z.real();
1509	  __imag__ __t = __z.imag();
1510	  _M_value *= __t;
1511	  return *this;
1512	}
1513
1514      template<typename _Tp>
1515        complex&
1516	operator/=(const complex<_Tp>& __z)
1517	{
1518	  _ComplexT __t;
1519	  __real__ __t = __z.real();
1520	  __imag__ __t = __z.imag();
1521	  _M_value /= __t;
1522	  return *this;
1523	}
1524
1525      _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1526
1527    private:
1528      _ComplexT _M_value;
1529    };
1530
1531  // These bits have to be at the end of this file, so that the
1532  // specializations have all been defined.
1533  inline _GLIBCXX_CONSTEXPR
1534  complex<float>::complex(const complex<double>& __z)
1535  : _M_value(__z.__rep()) { }
1536
1537  inline _GLIBCXX_CONSTEXPR
1538  complex<float>::complex(const complex<long double>& __z)
1539  : _M_value(__z.__rep()) { }
1540
1541  inline _GLIBCXX_CONSTEXPR
1542  complex<double>::complex(const complex<long double>& __z)
1543  : _M_value(__z.__rep()) { }
1544
1545  // Inhibit implicit instantiations for required instantiations,
1546  // which are defined via explicit instantiations elsewhere.
1547  // NB:  This syntax is a GNU extension.
1548#if _GLIBCXX_EXTERN_TEMPLATE
1549  extern template istream& operator>>(istream&, complex<float>&);
1550  extern template ostream& operator<<(ostream&, const complex<float>&);
1551  extern template istream& operator>>(istream&, complex<double>&);
1552  extern template ostream& operator<<(ostream&, const complex<double>&);
1553  extern template istream& operator>>(istream&, complex<long double>&);
1554  extern template ostream& operator<<(ostream&, const complex<long double>&);
1555
1556#ifdef _GLIBCXX_USE_WCHAR_T
1557  extern template wistream& operator>>(wistream&, complex<float>&);
1558  extern template wostream& operator<<(wostream&, const complex<float>&);
1559  extern template wistream& operator>>(wistream&, complex<double>&);
1560  extern template wostream& operator<<(wostream&, const complex<double>&);
1561  extern template wistream& operator>>(wistream&, complex<long double>&);
1562  extern template wostream& operator<<(wostream&, const complex<long double>&);
1563#endif
1564#endif
1565
1566  // @} group complex_numbers
1567
1568_GLIBCXX_END_NAMESPACE_VERSION
1569} // namespace
1570
1571namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
1572{
1573_GLIBCXX_BEGIN_NAMESPACE_VERSION
1574
1575  // See ext/type_traits.h for the primary template.
1576  template<typename _Tp, typename _Up>
1577    struct __promote_2<std::complex<_Tp>, _Up>
1578    {
1579    public:
1580      typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1581    };
1582
1583  template<typename _Tp, typename _Up>
1584    struct __promote_2<_Tp, std::complex<_Up> >
1585    {
1586    public:
1587      typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1588    };
1589
1590  template<typename _Tp, typename _Up>
1591    struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
1592    {
1593    public:
1594      typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1595    };
1596
1597_GLIBCXX_END_NAMESPACE_VERSION
1598} // namespace
1599
1600#if __cplusplus >= 201103L
1601
1602namespace std _GLIBCXX_VISIBILITY(default)
1603{
1604_GLIBCXX_BEGIN_NAMESPACE_VERSION
1605
1606  // Forward declarations.
1607  template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
1608  template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
1609  template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
1610
1611  template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
1612  template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
1613  template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
1614  // DR 595.
1615  template<typename _Tp> _Tp               fabs(const std::complex<_Tp>&);
1616
1617  template<typename _Tp>
1618    inline std::complex<_Tp>
1619    __complex_acos(const std::complex<_Tp>& __z)
1620    {
1621      const std::complex<_Tp> __t = std::asin(__z);
1622      const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
1623      return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
1624    }
1625
1626#if _GLIBCXX_USE_C99_COMPLEX_TR1
1627  inline __complex__ float
1628  __complex_acos(__complex__ float __z)
1629  { return __builtin_cacosf(__z); }
1630
1631  inline __complex__ double
1632  __complex_acos(__complex__ double __z)
1633  { return __builtin_cacos(__z); }
1634
1635  inline __complex__ long double
1636  __complex_acos(const __complex__ long double& __z)
1637  { return __builtin_cacosl(__z); }
1638
1639  template<typename _Tp>
1640    inline std::complex<_Tp>
1641    acos(const std::complex<_Tp>& __z)
1642    { return __complex_acos(__z.__rep()); }
1643#else
1644  /// acos(__z) [8.1.2].
1645  //  Effects:  Behaves the same as C99 function cacos, defined
1646  //            in subclause 7.3.5.1.
1647  template<typename _Tp>
1648    inline std::complex<_Tp>
1649    acos(const std::complex<_Tp>& __z)
1650    { return __complex_acos(__z); }
1651#endif
1652
1653  template<typename _Tp>
1654    inline std::complex<_Tp>
1655    __complex_asin(const std::complex<_Tp>& __z)
1656    {
1657      std::complex<_Tp> __t(-__z.imag(), __z.real());
1658      __t = std::asinh(__t);
1659      return std::complex<_Tp>(__t.imag(), -__t.real());
1660    }
1661
1662#if _GLIBCXX_USE_C99_COMPLEX_TR1
1663  inline __complex__ float
1664  __complex_asin(__complex__ float __z)
1665  { return __builtin_casinf(__z); }
1666
1667  inline __complex__ double
1668  __complex_asin(__complex__ double __z)
1669  { return __builtin_casin(__z); }
1670
1671  inline __complex__ long double
1672  __complex_asin(const __complex__ long double& __z)
1673  { return __builtin_casinl(__z); }
1674
1675  template<typename _Tp>
1676    inline std::complex<_Tp>
1677    asin(const std::complex<_Tp>& __z)
1678    { return __complex_asin(__z.__rep()); }
1679#else
1680  /// asin(__z) [8.1.3].
1681  //  Effects:  Behaves the same as C99 function casin, defined
1682  //            in subclause 7.3.5.2.
1683  template<typename _Tp>
1684    inline std::complex<_Tp>
1685    asin(const std::complex<_Tp>& __z)
1686    { return __complex_asin(__z); }
1687#endif
1688
1689  template<typename _Tp>
1690    std::complex<_Tp>
1691    __complex_atan(const std::complex<_Tp>& __z)
1692    {
1693      const _Tp __r2 = __z.real() * __z.real();
1694      const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
1695
1696      _Tp __num = __z.imag() + _Tp(1.0);
1697      _Tp __den = __z.imag() - _Tp(1.0);
1698
1699      __num = __r2 + __num * __num;
1700      __den = __r2 + __den * __den;
1701
1702      return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
1703			       _Tp(0.25) * log(__num / __den));
1704    }
1705
1706#if _GLIBCXX_USE_C99_COMPLEX_TR1
1707  inline __complex__ float
1708  __complex_atan(__complex__ float __z)
1709  { return __builtin_catanf(__z); }
1710
1711  inline __complex__ double
1712  __complex_atan(__complex__ double __z)
1713  { return __builtin_catan(__z); }
1714
1715  inline __complex__ long double
1716  __complex_atan(const __complex__ long double& __z)
1717  { return __builtin_catanl(__z); }
1718
1719  template<typename _Tp>
1720    inline std::complex<_Tp>
1721    atan(const std::complex<_Tp>& __z)
1722    { return __complex_atan(__z.__rep()); }
1723#else
1724  /// atan(__z) [8.1.4].
1725  //  Effects:  Behaves the same as C99 function catan, defined
1726  //            in subclause 7.3.5.3.
1727  template<typename _Tp>
1728    inline std::complex<_Tp>
1729    atan(const std::complex<_Tp>& __z)
1730    { return __complex_atan(__z); }
1731#endif
1732
1733  template<typename _Tp>
1734    std::complex<_Tp>
1735    __complex_acosh(const std::complex<_Tp>& __z)
1736    {
1737      // Kahan's formula.
1738      return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
1739				 + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
1740    }
1741
1742#if _GLIBCXX_USE_C99_COMPLEX_TR1
1743  inline __complex__ float
1744  __complex_acosh(__complex__ float __z)
1745  { return __builtin_cacoshf(__z); }
1746
1747  inline __complex__ double
1748  __complex_acosh(__complex__ double __z)
1749  { return __builtin_cacosh(__z); }
1750
1751  inline __complex__ long double
1752  __complex_acosh(const __complex__ long double& __z)
1753  { return __builtin_cacoshl(__z); }
1754
1755  template<typename _Tp>
1756    inline std::complex<_Tp>
1757    acosh(const std::complex<_Tp>& __z)
1758    { return __complex_acosh(__z.__rep()); }
1759#else
1760  /// acosh(__z) [8.1.5].
1761  //  Effects:  Behaves the same as C99 function cacosh, defined
1762  //            in subclause 7.3.6.1.
1763  template<typename _Tp>
1764    inline std::complex<_Tp>
1765    acosh(const std::complex<_Tp>& __z)
1766    { return __complex_acosh(__z); }
1767#endif
1768
1769  template<typename _Tp>
1770    std::complex<_Tp>
1771    __complex_asinh(const std::complex<_Tp>& __z)
1772    {
1773      std::complex<_Tp> __t((__z.real() - __z.imag())
1774			    * (__z.real() + __z.imag()) + _Tp(1.0),
1775			    _Tp(2.0) * __z.real() * __z.imag());
1776      __t = std::sqrt(__t);
1777
1778      return std::log(__t + __z);
1779    }
1780
1781#if _GLIBCXX_USE_C99_COMPLEX_TR1
1782  inline __complex__ float
1783  __complex_asinh(__complex__ float __z)
1784  { return __builtin_casinhf(__z); }
1785
1786  inline __complex__ double
1787  __complex_asinh(__complex__ double __z)
1788  { return __builtin_casinh(__z); }
1789
1790  inline __complex__ long double
1791  __complex_asinh(const __complex__ long double& __z)
1792  { return __builtin_casinhl(__z); }
1793
1794  template<typename _Tp>
1795    inline std::complex<_Tp>
1796    asinh(const std::complex<_Tp>& __z)
1797    { return __complex_asinh(__z.__rep()); }
1798#else
1799  /// asinh(__z) [8.1.6].
1800  //  Effects:  Behaves the same as C99 function casin, defined
1801  //            in subclause 7.3.6.2.
1802  template<typename _Tp>
1803    inline std::complex<_Tp>
1804    asinh(const std::complex<_Tp>& __z)
1805    { return __complex_asinh(__z); }
1806#endif
1807
1808  template<typename _Tp>
1809    std::complex<_Tp>
1810    __complex_atanh(const std::complex<_Tp>& __z)
1811    {
1812      const _Tp __i2 = __z.imag() * __z.imag();
1813      const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
1814
1815      _Tp __num = _Tp(1.0) + __z.real();
1816      _Tp __den = _Tp(1.0) - __z.real();
1817
1818      __num = __i2 + __num * __num;
1819      __den = __i2 + __den * __den;
1820
1821      return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
1822			       _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
1823    }
1824
1825#if _GLIBCXX_USE_C99_COMPLEX_TR1
1826  inline __complex__ float
1827  __complex_atanh(__complex__ float __z)
1828  { return __builtin_catanhf(__z); }
1829
1830  inline __complex__ double
1831  __complex_atanh(__complex__ double __z)
1832  { return __builtin_catanh(__z); }
1833
1834  inline __complex__ long double
1835  __complex_atanh(const __complex__ long double& __z)
1836  { return __builtin_catanhl(__z); }
1837
1838  template<typename _Tp>
1839    inline std::complex<_Tp>
1840    atanh(const std::complex<_Tp>& __z)
1841    { return __complex_atanh(__z.__rep()); }
1842#else
1843  /// atanh(__z) [8.1.7].
1844  //  Effects:  Behaves the same as C99 function catanh, defined
1845  //            in subclause 7.3.6.3.
1846  template<typename _Tp>
1847    inline std::complex<_Tp>
1848    atanh(const std::complex<_Tp>& __z)
1849    { return __complex_atanh(__z); }
1850#endif
1851
1852  template<typename _Tp>
1853    inline _Tp
1854    /// fabs(__z) [8.1.8].
1855    //  Effects:  Behaves the same as C99 function cabs, defined
1856    //            in subclause 7.3.8.1.
1857    fabs(const std::complex<_Tp>& __z)
1858    { return std::abs(__z); }
1859
1860  /// Additional overloads [8.1.9].
1861  template<typename _Tp>
1862    inline typename __gnu_cxx::__promote<_Tp>::__type
1863    arg(_Tp __x)
1864    {
1865      typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1866#if (_GLIBCXX11_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
1867      return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
1868	                       : __type();
1869#else
1870      return std::arg(std::complex<__type>(__x));
1871#endif
1872    }
1873
1874  template<typename _Tp>
1875    _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
1876    imag(_Tp)
1877    { return _Tp(); }
1878
1879  template<typename _Tp>
1880    inline typename __gnu_cxx::__promote<_Tp>::__type
1881    norm(_Tp __x)
1882    {
1883      typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1884      return __type(__x) * __type(__x);
1885    }
1886
1887  template<typename _Tp>
1888    _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
1889    real(_Tp __x)
1890    { return __x; }
1891
1892  template<typename _Tp, typename _Up>
1893    inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1894    pow(const std::complex<_Tp>& __x, const _Up& __y)
1895    {
1896      typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1897      return std::pow(std::complex<__type>(__x), __type(__y));
1898    }
1899
1900  template<typename _Tp, typename _Up>
1901    inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1902    pow(const _Tp& __x, const std::complex<_Up>& __y)
1903    {
1904      typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1905      return std::pow(__type(__x), std::complex<__type>(__y));
1906    }
1907
1908  template<typename _Tp, typename _Up>
1909    inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1910    pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
1911    {
1912      typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1913      return std::pow(std::complex<__type>(__x),
1914		      std::complex<__type>(__y));
1915    }
1916
1917  // Forward declarations.
1918  // DR 781.
1919  template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&);
1920
1921  template<typename _Tp>
1922    std::complex<_Tp>
1923    __complex_proj(const std::complex<_Tp>& __z)
1924    {
1925      const _Tp __den = (__z.real() * __z.real()
1926			 + __z.imag() * __z.imag() + _Tp(1.0));
1927
1928      return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den,
1929			       (_Tp(2.0) * __z.imag()) / __den);
1930    }
1931
1932#if _GLIBCXX_USE_C99_COMPLEX
1933  inline __complex__ float
1934  __complex_proj(__complex__ float __z)
1935  { return __builtin_cprojf(__z); }
1936
1937  inline __complex__ double
1938  __complex_proj(__complex__ double __z)
1939  { return __builtin_cproj(__z); }
1940
1941  inline __complex__ long double
1942  __complex_proj(const __complex__ long double& __z)
1943  { return __builtin_cprojl(__z); }
1944
1945  template<typename _Tp>
1946    inline std::complex<_Tp>
1947    proj(const std::complex<_Tp>& __z)
1948    { return __complex_proj(__z.__rep()); }
1949#else
1950  template<typename _Tp>
1951    inline std::complex<_Tp>
1952    proj(const std::complex<_Tp>& __z)
1953    { return __complex_proj(__z); }
1954#endif
1955
1956  template<typename _Tp>
1957    inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
1958    proj(_Tp __x)
1959    {
1960      typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1961      return std::proj(std::complex<__type>(__x));
1962    }
1963
1964  template<typename _Tp>
1965    inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
1966    conj(_Tp __x)
1967    {
1968      typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1969      return std::complex<__type>(__x, -__type());
1970    }
1971
1972#if __cplusplus > 201103L
1973
1974inline namespace literals {
1975inline namespace complex_literals {
1976#pragma GCC diagnostic push
1977#pragma GCC diagnostic ignored "-Wliteral-suffix"
1978#define __cpp_lib_complex_udls 201309
1979
1980  constexpr std::complex<float>
1981  operator""if(long double __num)
1982  { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
1983
1984  constexpr std::complex<float>
1985  operator""if(unsigned long long __num)
1986  { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
1987
1988  constexpr std::complex<double>
1989  operator""i(long double __num)
1990  { return std::complex<double>{0.0, static_cast<double>(__num)}; }
1991
1992  constexpr std::complex<double>
1993  operator""i(unsigned long long __num)
1994  { return std::complex<double>{0.0, static_cast<double>(__num)}; }
1995
1996  constexpr std::complex<long double>
1997  operator""il(long double __num)
1998  { return std::complex<long double>{0.0L, __num}; }
1999
2000  constexpr std::complex<long double>
2001  operator""il(unsigned long long __num)
2002  { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; }
2003
2004#pragma GCC diagnostic pop
2005} // inline namespace complex_literals
2006} // inline namespace literals
2007
2008#endif // C++14
2009
2010_GLIBCXX_END_NAMESPACE_VERSION
2011} // namespace
2012
2013#endif  // C++11
2014
2015#endif  /* _GLIBCXX_COMPLEX */
2016