xref: /netbsd-src/external/gpl3/gcc/dist/libstdc++-v3/include/std/complex (revision d35849d0e1572c7f04ab9d5925eef59d8cba2631)
1// The template and inlines for the -*- C++ -*- complex number classes.
2
3// Copyright (C) 1997-2015 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      _Tp __re_x, __im_x;
506      _CharT __ch;
507      __is >> __ch;
508      if (__ch == '(')
509	{
510	  __is >> __re_x >> __ch;
511	  if (__ch == ',')
512	    {
513	      __is >> __im_x >> __ch;
514	      if (__ch == ')')
515		__x = complex<_Tp>(__re_x, __im_x);
516	      else
517		__is.setstate(ios_base::failbit);
518	    }
519	  else if (__ch == ')')
520	    __x = __re_x;
521	  else
522	    __is.setstate(ios_base::failbit);
523	}
524      else
525	{
526	  __is.putback(__ch);
527	  __is >> __re_x;
528	  __x = __re_x;
529	}
530      return __is;
531    }
532
533  ///  Insertion operator for complex values.
534  template<typename _Tp, typename _CharT, class _Traits>
535    basic_ostream<_CharT, _Traits>&
536    operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
537    {
538      basic_ostringstream<_CharT, _Traits> __s;
539      __s.flags(__os.flags());
540      __s.imbue(__os.getloc());
541      __s.precision(__os.precision());
542      __s << '(' << __x.real() << ',' << __x.imag() << ')';
543      return __os << __s.str();
544    }
545
546  // Values
547#if __cplusplus >= 201103L
548  template<typename _Tp>
549    constexpr _Tp
550    real(const complex<_Tp>& __z)
551    { return __z.real(); }
552
553  template<typename _Tp>
554    constexpr _Tp
555    imag(const complex<_Tp>& __z)
556    { return __z.imag(); }
557#else
558  template<typename _Tp>
559    inline _Tp&
560    real(complex<_Tp>& __z)
561    { return __z.real(); }
562
563  template<typename _Tp>
564    inline const _Tp&
565    real(const complex<_Tp>& __z)
566    { return __z.real(); }
567
568  template<typename _Tp>
569    inline _Tp&
570    imag(complex<_Tp>& __z)
571    { return __z.imag(); }
572
573  template<typename _Tp>
574    inline const _Tp&
575    imag(const complex<_Tp>& __z)
576    { return __z.imag(); }
577#endif
578
579  // 26.2.7/3 abs(__z):  Returns the magnitude of __z.
580  template<typename _Tp>
581    inline _Tp
582    __complex_abs(const complex<_Tp>& __z)
583    {
584      _Tp __x = __z.real();
585      _Tp __y = __z.imag();
586      const _Tp __s = std::max(abs(__x), abs(__y));
587      if (__s == _Tp())  // well ...
588        return __s;
589      __x /= __s;
590      __y /= __s;
591      return __s * sqrt(__x * __x + __y * __y);
592    }
593
594#if _GLIBCXX_USE_C99_COMPLEX
595  // XXX: We can't use __builtin_cabs* because they are broken
596  inline float
597  __complex_abs(__complex__ float __z) { return __c99_cabsf(__z); }
598
599  inline double
600  __complex_abs(__complex__ double __z) { return __c99_cabs(__z); }
601
602  inline long double
603  __complex_abs(const __complex__ long double& __z)
604  { return __c99_cabsl(__z); }
605
606  template<typename _Tp>
607    inline _Tp
608    abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
609#else
610  template<typename _Tp>
611    inline _Tp
612    abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
613#endif
614
615
616  // 26.2.7/4: arg(__z): Returns the phase angle of __z.
617  template<typename _Tp>
618    inline _Tp
619    __complex_arg(const complex<_Tp>& __z)
620    { return  atan2(__z.imag(), __z.real()); }
621
622#if _GLIBCXX_USE_C99_COMPLEX
623  inline float
624  __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
625
626  inline double
627  __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
628
629  inline long double
630  __complex_arg(const __complex__ long double& __z)
631  { return __builtin_cargl(__z); }
632
633  template<typename _Tp>
634    inline _Tp
635    arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
636#else
637  template<typename _Tp>
638    inline _Tp
639    arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
640#endif
641
642  // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
643  //     As defined, norm() is -not- a norm is the common mathematical
644  //     sense used in numerics.  The helper class _Norm_helper<> tries to
645  //     distinguish between builtin floating point and the rest, so as
646  //     to deliver an answer as close as possible to the real value.
647  template<bool>
648    struct _Norm_helper
649    {
650      template<typename _Tp>
651        static inline _Tp _S_do_it(const complex<_Tp>& __z)
652        {
653          const _Tp __x = __z.real();
654          const _Tp __y = __z.imag();
655          return __x * __x + __y * __y;
656        }
657    };
658
659  template<>
660    struct _Norm_helper<true>
661    {
662      template<typename _Tp>
663        static inline _Tp _S_do_it(const complex<_Tp>& __z)
664        {
665          _Tp __res = std::abs(__z);
666          return __res * __res;
667        }
668    };
669
670  template<typename _Tp>
671    inline _Tp
672    norm(const complex<_Tp>& __z)
673    {
674      return _Norm_helper<__is_floating<_Tp>::__value
675	&& !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
676    }
677
678  template<typename _Tp>
679    inline complex<_Tp>
680    polar(const _Tp& __rho, const _Tp& __theta)
681    { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); }
682
683  template<typename _Tp>
684    inline complex<_Tp>
685    conj(const complex<_Tp>& __z)
686    { return complex<_Tp>(__z.real(), -__z.imag()); }
687
688  // Transcendentals
689
690  // 26.2.8/1 cos(__z):  Returns the cosine of __z.
691  template<typename _Tp>
692    inline complex<_Tp>
693    __complex_cos(const complex<_Tp>& __z)
694    {
695      const _Tp __x = __z.real();
696      const _Tp __y = __z.imag();
697      return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
698    }
699
700#if _GLIBCXX_USE_C99_COMPLEX
701  inline __complex__ float
702  __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
703
704  inline __complex__ double
705  __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
706
707  inline __complex__ long double
708  __complex_cos(const __complex__ long double& __z)
709  { return __builtin_ccosl(__z); }
710
711  template<typename _Tp>
712    inline complex<_Tp>
713    cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
714#else
715  template<typename _Tp>
716    inline complex<_Tp>
717    cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
718#endif
719
720  // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
721  template<typename _Tp>
722    inline complex<_Tp>
723    __complex_cosh(const complex<_Tp>& __z)
724    {
725      const _Tp __x = __z.real();
726      const _Tp __y = __z.imag();
727      return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
728    }
729
730#if _GLIBCXX_USE_C99_COMPLEX
731  inline __complex__ float
732  __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
733
734  inline __complex__ double
735  __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
736
737  inline __complex__ long double
738  __complex_cosh(const __complex__ long double& __z)
739  { return __builtin_ccoshl(__z); }
740
741  template<typename _Tp>
742    inline complex<_Tp>
743    cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
744#else
745  template<typename _Tp>
746    inline complex<_Tp>
747    cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
748#endif
749
750  // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
751  template<typename _Tp>
752    inline complex<_Tp>
753    __complex_exp(const complex<_Tp>& __z)
754    { return std::polar<_Tp>(exp(__z.real()), __z.imag()); }
755
756#if _GLIBCXX_USE_C99_COMPLEX
757  inline __complex__ float
758  __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
759
760  inline __complex__ double
761  __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
762
763  inline __complex__ long double
764  __complex_exp(const __complex__ long double& __z)
765  { return __builtin_cexpl(__z); }
766
767  template<typename _Tp>
768    inline complex<_Tp>
769    exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
770#else
771  template<typename _Tp>
772    inline complex<_Tp>
773    exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
774#endif
775
776  // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
777  //                    The branch cut is along the negative axis.
778  template<typename _Tp>
779    inline complex<_Tp>
780    __complex_log(const complex<_Tp>& __z)
781    { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
782
783#if _GLIBCXX_USE_C99_COMPLEX
784  inline __complex__ float
785  __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
786
787  inline __complex__ double
788  __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
789
790  inline __complex__ long double
791  __complex_log(const __complex__ long double& __z)
792  { return __builtin_clogl(__z); }
793
794  template<typename _Tp>
795    inline complex<_Tp>
796    log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
797#else
798  template<typename _Tp>
799    inline complex<_Tp>
800    log(const complex<_Tp>& __z) { return __complex_log(__z); }
801#endif
802
803  template<typename _Tp>
804    inline complex<_Tp>
805    log10(const complex<_Tp>& __z)
806    { return std::log(__z) / log(_Tp(10.0)); }
807
808  // 26.2.8/10 sin(__z): Returns the sine of __z.
809  template<typename _Tp>
810    inline complex<_Tp>
811    __complex_sin(const complex<_Tp>& __z)
812    {
813      const _Tp __x = __z.real();
814      const _Tp __y = __z.imag();
815      return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
816    }
817
818#if _GLIBCXX_USE_C99_COMPLEX
819  inline __complex__ float
820  __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
821
822  inline __complex__ double
823  __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
824
825  inline __complex__ long double
826  __complex_sin(const __complex__ long double& __z)
827  { return __builtin_csinl(__z); }
828
829  template<typename _Tp>
830    inline complex<_Tp>
831    sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
832#else
833  template<typename _Tp>
834    inline complex<_Tp>
835    sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
836#endif
837
838  // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
839  template<typename _Tp>
840    inline complex<_Tp>
841    __complex_sinh(const complex<_Tp>& __z)
842    {
843      const _Tp __x = __z.real();
844      const _Tp  __y = __z.imag();
845      return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
846    }
847
848#if _GLIBCXX_USE_C99_COMPLEX
849  inline __complex__ float
850  __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
851
852  inline __complex__ double
853  __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
854
855  inline __complex__ long double
856  __complex_sinh(const __complex__ long double& __z)
857  { return __builtin_csinhl(__z); }
858
859  template<typename _Tp>
860    inline complex<_Tp>
861    sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
862#else
863  template<typename _Tp>
864    inline complex<_Tp>
865    sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
866#endif
867
868  // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
869  //                     The branch cut is on the negative axis.
870  template<typename _Tp>
871    complex<_Tp>
872    __complex_sqrt(const complex<_Tp>& __z)
873    {
874      _Tp __x = __z.real();
875      _Tp __y = __z.imag();
876
877      if (__x == _Tp())
878        {
879          _Tp __t = sqrt(abs(__y) / 2);
880          return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
881        }
882      else
883        {
884          _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
885          _Tp __u = __t / 2;
886          return __x > _Tp()
887            ? complex<_Tp>(__u, __y / __t)
888            : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
889        }
890    }
891
892#if _GLIBCXX_USE_C99_COMPLEX
893  inline __complex__ float
894  __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
895
896  inline __complex__ double
897  __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
898
899  inline __complex__ long double
900  __complex_sqrt(const __complex__ long double& __z)
901  { return __builtin_csqrtl(__z); }
902
903  template<typename _Tp>
904    inline complex<_Tp>
905    sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
906#else
907  template<typename _Tp>
908    inline complex<_Tp>
909    sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
910#endif
911
912  // 26.2.8/14 tan(__z):  Return the complex tangent of __z.
913
914  template<typename _Tp>
915    inline complex<_Tp>
916    __complex_tan(const complex<_Tp>& __z)
917    { return std::sin(__z) / std::cos(__z); }
918
919#if _GLIBCXX_USE_C99_COMPLEX
920  inline __complex__ float
921  __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
922
923  inline __complex__ double
924  __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
925
926  inline __complex__ long double
927  __complex_tan(const __complex__ long double& __z)
928  { return __builtin_ctanl(__z); }
929
930  template<typename _Tp>
931    inline complex<_Tp>
932    tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
933#else
934  template<typename _Tp>
935    inline complex<_Tp>
936    tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
937#endif
938
939
940  // 26.2.8/15 tanh(__z):  Returns the hyperbolic tangent of __z.
941
942  template<typename _Tp>
943    inline complex<_Tp>
944    __complex_tanh(const complex<_Tp>& __z)
945    { return std::sinh(__z) / std::cosh(__z); }
946
947#if _GLIBCXX_USE_C99_COMPLEX
948  inline __complex__ float
949  __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
950
951  inline __complex__ double
952  __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
953
954  inline __complex__ long double
955  __complex_tanh(const __complex__ long double& __z)
956  { return __builtin_ctanhl(__z); }
957
958  template<typename _Tp>
959    inline complex<_Tp>
960    tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
961#else
962  template<typename _Tp>
963    inline complex<_Tp>
964    tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
965#endif
966
967
968  // 26.2.8/9  pow(__x, __y): Returns the complex power base of __x
969  //                          raised to the __y-th power.  The branch
970  //                          cut is on the negative axis.
971  template<typename _Tp>
972    complex<_Tp>
973    __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
974    {
975      complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
976
977      while (__n >>= 1)
978        {
979          __x *= __x;
980          if (__n % 2)
981            __y *= __x;
982        }
983
984      return __y;
985    }
986
987  // In C++11 mode we used to implement the resolution of
988  // DR 844. complex pow return type is ambiguous.
989  // thus the following overload was disabled in that mode.  However, doing
990  // that causes all sorts of issues, see, for example:
991  //   http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html
992  // and also PR57974.
993  template<typename _Tp>
994    inline complex<_Tp>
995    pow(const complex<_Tp>& __z, int __n)
996    {
997      return __n < 0
998	? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n)
999        : std::__complex_pow_unsigned(__z, __n);
1000    }
1001
1002  template<typename _Tp>
1003    complex<_Tp>
1004    pow(const complex<_Tp>& __x, const _Tp& __y)
1005    {
1006#ifndef _GLIBCXX_USE_C99_COMPLEX
1007      if (__x == _Tp())
1008	return _Tp();
1009#endif
1010      if (__x.imag() == _Tp() && __x.real() > _Tp())
1011        return pow(__x.real(), __y);
1012
1013      complex<_Tp> __t = std::log(__x);
1014      return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag());
1015    }
1016
1017  template<typename _Tp>
1018    inline complex<_Tp>
1019    __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1020    { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
1021
1022#if _GLIBCXX_USE_C99_COMPLEX
1023  inline __complex__ float
1024  __complex_pow(__complex__ float __x, __complex__ float __y)
1025  { return __builtin_cpowf(__x, __y); }
1026
1027  inline __complex__ double
1028  __complex_pow(__complex__ double __x, __complex__ double __y)
1029  { return __builtin_cpow(__x, __y); }
1030
1031  inline __complex__ long double
1032  __complex_pow(const __complex__ long double& __x,
1033		const __complex__ long double& __y)
1034  { return __builtin_cpowl(__x, __y); }
1035
1036  template<typename _Tp>
1037    inline complex<_Tp>
1038    pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1039    { return __complex_pow(__x.__rep(), __y.__rep()); }
1040#else
1041  template<typename _Tp>
1042    inline complex<_Tp>
1043    pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1044    { return __complex_pow(__x, __y); }
1045#endif
1046
1047  template<typename _Tp>
1048    inline complex<_Tp>
1049    pow(const _Tp& __x, const complex<_Tp>& __y)
1050    {
1051      return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()),
1052					   __y.imag() * log(__x))
1053	                 : std::pow(complex<_Tp>(__x), __y);
1054    }
1055
1056  /// 26.2.3  complex specializations
1057  /// complex<float> specialization
1058  template<>
1059    struct complex<float>
1060    {
1061      typedef float value_type;
1062      typedef __complex__ float _ComplexT;
1063
1064      _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1065
1066      _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
1067#if __cplusplus >= 201103L
1068      : _M_value{ __r, __i } { }
1069#else
1070      {
1071	__real__ _M_value = __r;
1072	__imag__ _M_value = __i;
1073      }
1074#endif
1075
1076      explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
1077      explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1078
1079#if __cplusplus >= 201103L
1080      // _GLIBCXX_RESOLVE_LIB_DEFECTS
1081      // DR 387. std::complex over-encapsulated.
1082      __attribute ((__abi_tag__ ("cxx11")))
1083      constexpr float
1084      real() const { return __real__ _M_value; }
1085
1086      __attribute ((__abi_tag__ ("cxx11")))
1087      constexpr float
1088      imag() const { return __imag__ _M_value; }
1089#else
1090      float&
1091      real() { return __real__ _M_value; }
1092
1093      const float&
1094      real() const { return __real__ _M_value; }
1095
1096      float&
1097      imag() { return __imag__ _M_value; }
1098
1099      const float&
1100      imag() const { return __imag__ _M_value; }
1101#endif
1102
1103      // _GLIBCXX_RESOLVE_LIB_DEFECTS
1104      // DR 387. std::complex over-encapsulated.
1105      void
1106      real(float __val) { __real__ _M_value = __val; }
1107
1108      void
1109      imag(float __val) { __imag__ _M_value = __val; }
1110
1111      complex&
1112      operator=(float __f)
1113      {
1114	_M_value = __f;
1115	return *this;
1116      }
1117
1118      complex&
1119      operator+=(float __f)
1120      {
1121	_M_value += __f;
1122	return *this;
1123      }
1124
1125      complex&
1126      operator-=(float __f)
1127      {
1128	_M_value -= __f;
1129	return *this;
1130      }
1131
1132      complex&
1133      operator*=(float __f)
1134      {
1135	_M_value *= __f;
1136	return *this;
1137      }
1138
1139      complex&
1140      operator/=(float __f)
1141      {
1142	_M_value /= __f;
1143	return *this;
1144      }
1145
1146      // Let the compiler synthesize the copy and assignment
1147      // operator.  It always does a pretty good job.
1148      // complex& operator=(const complex&);
1149
1150      template<typename _Tp>
1151        complex&
1152        operator=(const complex<_Tp>&  __z)
1153	{
1154	  __real__ _M_value = __z.real();
1155	  __imag__ _M_value = __z.imag();
1156	  return *this;
1157	}
1158
1159      template<typename _Tp>
1160        complex&
1161        operator+=(const complex<_Tp>& __z)
1162	{
1163	  __real__ _M_value += __z.real();
1164	  __imag__ _M_value += __z.imag();
1165	  return *this;
1166	}
1167
1168      template<class _Tp>
1169        complex&
1170        operator-=(const complex<_Tp>& __z)
1171	{
1172	  __real__ _M_value -= __z.real();
1173	  __imag__ _M_value -= __z.imag();
1174	  return *this;
1175	}
1176
1177      template<class _Tp>
1178        complex&
1179        operator*=(const complex<_Tp>& __z)
1180	{
1181	  _ComplexT __t;
1182	  __real__ __t = __z.real();
1183	  __imag__ __t = __z.imag();
1184	  _M_value *= __t;
1185	  return *this;
1186	}
1187
1188      template<class _Tp>
1189        complex&
1190        operator/=(const complex<_Tp>& __z)
1191	{
1192	  _ComplexT __t;
1193	  __real__ __t = __z.real();
1194	  __imag__ __t = __z.imag();
1195	  _M_value /= __t;
1196	  return *this;
1197	}
1198
1199      _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1200
1201    private:
1202      _ComplexT _M_value;
1203    };
1204
1205  /// 26.2.3  complex specializations
1206  /// complex<double> specialization
1207  template<>
1208    struct complex<double>
1209    {
1210      typedef double value_type;
1211      typedef __complex__ double _ComplexT;
1212
1213      _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1214
1215      _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
1216#if __cplusplus >= 201103L
1217      : _M_value{ __r, __i } { }
1218#else
1219      {
1220	__real__ _M_value = __r;
1221	__imag__ _M_value = __i;
1222      }
1223#endif
1224
1225      _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1226      : _M_value(__z.__rep()) { }
1227
1228      explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1229
1230#if __cplusplus >= 201103L
1231      // _GLIBCXX_RESOLVE_LIB_DEFECTS
1232      // DR 387. std::complex over-encapsulated.
1233      __attribute ((__abi_tag__ ("cxx11")))
1234      constexpr double
1235      real() const { return __real__ _M_value; }
1236
1237      __attribute ((__abi_tag__ ("cxx11")))
1238      constexpr double
1239      imag() const { return __imag__ _M_value; }
1240#else
1241      double&
1242      real() { return __real__ _M_value; }
1243
1244      const double&
1245      real() const { return __real__ _M_value; }
1246
1247      double&
1248      imag() { return __imag__ _M_value; }
1249
1250      const double&
1251      imag() const { return __imag__ _M_value; }
1252#endif
1253
1254      // _GLIBCXX_RESOLVE_LIB_DEFECTS
1255      // DR 387. std::complex over-encapsulated.
1256      void
1257      real(double __val) { __real__ _M_value = __val; }
1258
1259      void
1260      imag(double __val) { __imag__ _M_value = __val; }
1261
1262      complex&
1263      operator=(double __d)
1264      {
1265	_M_value = __d;
1266	return *this;
1267      }
1268
1269      complex&
1270      operator+=(double __d)
1271      {
1272	_M_value += __d;
1273	return *this;
1274      }
1275
1276      complex&
1277      operator-=(double __d)
1278      {
1279	_M_value -= __d;
1280	return *this;
1281      }
1282
1283      complex&
1284      operator*=(double __d)
1285      {
1286	_M_value *= __d;
1287	return *this;
1288      }
1289
1290      complex&
1291      operator/=(double __d)
1292      {
1293	_M_value /= __d;
1294	return *this;
1295      }
1296
1297      // The compiler will synthesize this, efficiently.
1298      // complex& operator=(const complex&);
1299
1300      template<typename _Tp>
1301        complex&
1302        operator=(const complex<_Tp>& __z)
1303	{
1304	  __real__ _M_value = __z.real();
1305	  __imag__ _M_value = __z.imag();
1306	  return *this;
1307	}
1308
1309      template<typename _Tp>
1310        complex&
1311        operator+=(const complex<_Tp>& __z)
1312	{
1313	  __real__ _M_value += __z.real();
1314	  __imag__ _M_value += __z.imag();
1315	  return *this;
1316	}
1317
1318      template<typename _Tp>
1319        complex&
1320        operator-=(const complex<_Tp>& __z)
1321	{
1322	  __real__ _M_value -= __z.real();
1323	  __imag__ _M_value -= __z.imag();
1324	  return *this;
1325	}
1326
1327      template<typename _Tp>
1328        complex&
1329        operator*=(const complex<_Tp>& __z)
1330	{
1331	  _ComplexT __t;
1332	  __real__ __t = __z.real();
1333	  __imag__ __t = __z.imag();
1334	  _M_value *= __t;
1335	  return *this;
1336	}
1337
1338      template<typename _Tp>
1339        complex&
1340        operator/=(const complex<_Tp>& __z)
1341	{
1342	  _ComplexT __t;
1343	  __real__ __t = __z.real();
1344	  __imag__ __t = __z.imag();
1345	  _M_value /= __t;
1346	  return *this;
1347	}
1348
1349      _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1350
1351    private:
1352      _ComplexT _M_value;
1353    };
1354
1355  /// 26.2.3  complex specializations
1356  /// complex<long double> specialization
1357  template<>
1358    struct complex<long double>
1359    {
1360      typedef long double value_type;
1361      typedef __complex__ long double _ComplexT;
1362
1363      _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1364
1365      _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L,
1366				 long double __i = 0.0L)
1367#if __cplusplus >= 201103L
1368      : _M_value{ __r, __i } { }
1369#else
1370      {
1371	__real__ _M_value = __r;
1372	__imag__ _M_value = __i;
1373      }
1374#endif
1375
1376      _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1377      : _M_value(__z.__rep()) { }
1378
1379      _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
1380      : _M_value(__z.__rep()) { }
1381
1382#if __cplusplus >= 201103L
1383      // _GLIBCXX_RESOLVE_LIB_DEFECTS
1384      // DR 387. std::complex over-encapsulated.
1385      __attribute ((__abi_tag__ ("cxx11")))
1386      constexpr long double
1387      real() const { return __real__ _M_value; }
1388
1389      __attribute ((__abi_tag__ ("cxx11")))
1390      constexpr long double
1391      imag() const { return __imag__ _M_value; }
1392#else
1393      long double&
1394      real() { return __real__ _M_value; }
1395
1396      const long double&
1397      real() const { return __real__ _M_value; }
1398
1399      long double&
1400      imag() { return __imag__ _M_value; }
1401
1402      const long double&
1403      imag() const { return __imag__ _M_value; }
1404#endif
1405
1406      // _GLIBCXX_RESOLVE_LIB_DEFECTS
1407      // DR 387. std::complex over-encapsulated.
1408      void
1409      real(long double __val) { __real__ _M_value = __val; }
1410
1411      void
1412      imag(long double __val) { __imag__ _M_value = __val; }
1413
1414      complex&
1415      operator=(long double __r)
1416      {
1417	_M_value = __r;
1418	return *this;
1419      }
1420
1421      complex&
1422      operator+=(long double __r)
1423      {
1424	_M_value += __r;
1425	return *this;
1426      }
1427
1428      complex&
1429      operator-=(long double __r)
1430      {
1431	_M_value -= __r;
1432	return *this;
1433      }
1434
1435      complex&
1436      operator*=(long double __r)
1437      {
1438	_M_value *= __r;
1439	return *this;
1440      }
1441
1442      complex&
1443      operator/=(long double __r)
1444      {
1445	_M_value /= __r;
1446	return *this;
1447      }
1448
1449      // The compiler knows how to do this efficiently
1450      // complex& operator=(const complex&);
1451
1452      template<typename _Tp>
1453        complex&
1454        operator=(const complex<_Tp>& __z)
1455	{
1456	  __real__ _M_value = __z.real();
1457	  __imag__ _M_value = __z.imag();
1458	  return *this;
1459	}
1460
1461      template<typename _Tp>
1462        complex&
1463	operator+=(const complex<_Tp>& __z)
1464	{
1465	  __real__ _M_value += __z.real();
1466	  __imag__ _M_value += __z.imag();
1467	  return *this;
1468	}
1469
1470      template<typename _Tp>
1471        complex&
1472	operator-=(const complex<_Tp>& __z)
1473	{
1474	  __real__ _M_value -= __z.real();
1475	  __imag__ _M_value -= __z.imag();
1476	  return *this;
1477	}
1478
1479      template<typename _Tp>
1480        complex&
1481	operator*=(const complex<_Tp>& __z)
1482	{
1483	  _ComplexT __t;
1484	  __real__ __t = __z.real();
1485	  __imag__ __t = __z.imag();
1486	  _M_value *= __t;
1487	  return *this;
1488	}
1489
1490      template<typename _Tp>
1491        complex&
1492	operator/=(const complex<_Tp>& __z)
1493	{
1494	  _ComplexT __t;
1495	  __real__ __t = __z.real();
1496	  __imag__ __t = __z.imag();
1497	  _M_value /= __t;
1498	  return *this;
1499	}
1500
1501      _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1502
1503    private:
1504      _ComplexT _M_value;
1505    };
1506
1507  // These bits have to be at the end of this file, so that the
1508  // specializations have all been defined.
1509  inline _GLIBCXX_CONSTEXPR
1510  complex<float>::complex(const complex<double>& __z)
1511  : _M_value(__z.__rep()) { }
1512
1513  inline _GLIBCXX_CONSTEXPR
1514  complex<float>::complex(const complex<long double>& __z)
1515  : _M_value(__z.__rep()) { }
1516
1517  inline _GLIBCXX_CONSTEXPR
1518  complex<double>::complex(const complex<long double>& __z)
1519  : _M_value(__z.__rep()) { }
1520
1521  // Inhibit implicit instantiations for required instantiations,
1522  // which are defined via explicit instantiations elsewhere.
1523  // NB:  This syntax is a GNU extension.
1524#if _GLIBCXX_EXTERN_TEMPLATE
1525  extern template istream& operator>>(istream&, complex<float>&);
1526  extern template ostream& operator<<(ostream&, const complex<float>&);
1527  extern template istream& operator>>(istream&, complex<double>&);
1528  extern template ostream& operator<<(ostream&, const complex<double>&);
1529  extern template istream& operator>>(istream&, complex<long double>&);
1530  extern template ostream& operator<<(ostream&, const complex<long double>&);
1531
1532#ifdef _GLIBCXX_USE_WCHAR_T
1533  extern template wistream& operator>>(wistream&, complex<float>&);
1534  extern template wostream& operator<<(wostream&, const complex<float>&);
1535  extern template wistream& operator>>(wistream&, complex<double>&);
1536  extern template wostream& operator<<(wostream&, const complex<double>&);
1537  extern template wistream& operator>>(wistream&, complex<long double>&);
1538  extern template wostream& operator<<(wostream&, const complex<long double>&);
1539#endif
1540#endif
1541
1542  // @} group complex_numbers
1543
1544_GLIBCXX_END_NAMESPACE_VERSION
1545} // namespace
1546
1547namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
1548{
1549_GLIBCXX_BEGIN_NAMESPACE_VERSION
1550
1551  // See ext/type_traits.h for the primary template.
1552  template<typename _Tp, typename _Up>
1553    struct __promote_2<std::complex<_Tp>, _Up>
1554    {
1555    public:
1556      typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1557    };
1558
1559  template<typename _Tp, typename _Up>
1560    struct __promote_2<_Tp, std::complex<_Up> >
1561    {
1562    public:
1563      typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1564    };
1565
1566  template<typename _Tp, typename _Up>
1567    struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
1568    {
1569    public:
1570      typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1571    };
1572
1573_GLIBCXX_END_NAMESPACE_VERSION
1574} // namespace
1575
1576#if __cplusplus >= 201103L
1577
1578namespace std _GLIBCXX_VISIBILITY(default)
1579{
1580_GLIBCXX_BEGIN_NAMESPACE_VERSION
1581
1582  // Forward declarations.
1583  template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
1584  template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
1585  template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
1586
1587  template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
1588  template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
1589  template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
1590  // DR 595.
1591  template<typename _Tp> _Tp               fabs(const std::complex<_Tp>&);
1592
1593  template<typename _Tp>
1594    inline std::complex<_Tp>
1595    __complex_acos(const std::complex<_Tp>& __z)
1596    {
1597      const std::complex<_Tp> __t = std::asin(__z);
1598      const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
1599      return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
1600    }
1601
1602#if _GLIBCXX_USE_C99_COMPLEX_TR1
1603  inline __complex__ float
1604  __complex_acos(__complex__ float __z)
1605  { return __builtin_cacosf(__z); }
1606
1607  inline __complex__ double
1608  __complex_acos(__complex__ double __z)
1609  { return __builtin_cacos(__z); }
1610
1611  inline __complex__ long double
1612  __complex_acos(const __complex__ long double& __z)
1613  { return __builtin_cacosl(__z); }
1614
1615  template<typename _Tp>
1616    inline std::complex<_Tp>
1617    acos(const std::complex<_Tp>& __z)
1618    { return __complex_acos(__z.__rep()); }
1619#else
1620  /// acos(__z) [8.1.2].
1621  //  Effects:  Behaves the same as C99 function cacos, defined
1622  //            in subclause 7.3.5.1.
1623  template<typename _Tp>
1624    inline std::complex<_Tp>
1625    acos(const std::complex<_Tp>& __z)
1626    { return __complex_acos(__z); }
1627#endif
1628
1629  template<typename _Tp>
1630    inline std::complex<_Tp>
1631    __complex_asin(const std::complex<_Tp>& __z)
1632    {
1633      std::complex<_Tp> __t(-__z.imag(), __z.real());
1634      __t = std::asinh(__t);
1635      return std::complex<_Tp>(__t.imag(), -__t.real());
1636    }
1637
1638#if _GLIBCXX_USE_C99_COMPLEX_TR1
1639  inline __complex__ float
1640  __complex_asin(__complex__ float __z)
1641  { return __builtin_casinf(__z); }
1642
1643  inline __complex__ double
1644  __complex_asin(__complex__ double __z)
1645  { return __builtin_casin(__z); }
1646
1647  inline __complex__ long double
1648  __complex_asin(const __complex__ long double& __z)
1649  { return __builtin_casinl(__z); }
1650
1651  template<typename _Tp>
1652    inline std::complex<_Tp>
1653    asin(const std::complex<_Tp>& __z)
1654    { return __complex_asin(__z.__rep()); }
1655#else
1656  /// asin(__z) [8.1.3].
1657  //  Effects:  Behaves the same as C99 function casin, defined
1658  //            in subclause 7.3.5.2.
1659  template<typename _Tp>
1660    inline std::complex<_Tp>
1661    asin(const std::complex<_Tp>& __z)
1662    { return __complex_asin(__z); }
1663#endif
1664
1665  template<typename _Tp>
1666    std::complex<_Tp>
1667    __complex_atan(const std::complex<_Tp>& __z)
1668    {
1669      const _Tp __r2 = __z.real() * __z.real();
1670      const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
1671
1672      _Tp __num = __z.imag() + _Tp(1.0);
1673      _Tp __den = __z.imag() - _Tp(1.0);
1674
1675      __num = __r2 + __num * __num;
1676      __den = __r2 + __den * __den;
1677
1678      return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
1679			       _Tp(0.25) * log(__num / __den));
1680    }
1681
1682#if _GLIBCXX_USE_C99_COMPLEX_TR1
1683  inline __complex__ float
1684  __complex_atan(__complex__ float __z)
1685  { return __builtin_catanf(__z); }
1686
1687  inline __complex__ double
1688  __complex_atan(__complex__ double __z)
1689  { return __builtin_catan(__z); }
1690
1691  inline __complex__ long double
1692  __complex_atan(const __complex__ long double& __z)
1693  { return __builtin_catanl(__z); }
1694
1695  template<typename _Tp>
1696    inline std::complex<_Tp>
1697    atan(const std::complex<_Tp>& __z)
1698    { return __complex_atan(__z.__rep()); }
1699#else
1700  /// atan(__z) [8.1.4].
1701  //  Effects:  Behaves the same as C99 function catan, defined
1702  //            in subclause 7.3.5.3.
1703  template<typename _Tp>
1704    inline std::complex<_Tp>
1705    atan(const std::complex<_Tp>& __z)
1706    { return __complex_atan(__z); }
1707#endif
1708
1709  template<typename _Tp>
1710    std::complex<_Tp>
1711    __complex_acosh(const std::complex<_Tp>& __z)
1712    {
1713      // Kahan's formula.
1714      return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
1715				 + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
1716    }
1717
1718#if _GLIBCXX_USE_C99_COMPLEX_TR1
1719  inline __complex__ float
1720  __complex_acosh(__complex__ float __z)
1721  { return __builtin_cacoshf(__z); }
1722
1723  inline __complex__ double
1724  __complex_acosh(__complex__ double __z)
1725  { return __builtin_cacosh(__z); }
1726
1727  inline __complex__ long double
1728  __complex_acosh(const __complex__ long double& __z)
1729  { return __builtin_cacoshl(__z); }
1730
1731  template<typename _Tp>
1732    inline std::complex<_Tp>
1733    acosh(const std::complex<_Tp>& __z)
1734    { return __complex_acosh(__z.__rep()); }
1735#else
1736  /// acosh(__z) [8.1.5].
1737  //  Effects:  Behaves the same as C99 function cacosh, defined
1738  //            in subclause 7.3.6.1.
1739  template<typename _Tp>
1740    inline std::complex<_Tp>
1741    acosh(const std::complex<_Tp>& __z)
1742    { return __complex_acosh(__z); }
1743#endif
1744
1745  template<typename _Tp>
1746    std::complex<_Tp>
1747    __complex_asinh(const std::complex<_Tp>& __z)
1748    {
1749      std::complex<_Tp> __t((__z.real() - __z.imag())
1750			    * (__z.real() + __z.imag()) + _Tp(1.0),
1751			    _Tp(2.0) * __z.real() * __z.imag());
1752      __t = std::sqrt(__t);
1753
1754      return std::log(__t + __z);
1755    }
1756
1757#if _GLIBCXX_USE_C99_COMPLEX_TR1
1758  inline __complex__ float
1759  __complex_asinh(__complex__ float __z)
1760  { return __builtin_casinhf(__z); }
1761
1762  inline __complex__ double
1763  __complex_asinh(__complex__ double __z)
1764  { return __builtin_casinh(__z); }
1765
1766  inline __complex__ long double
1767  __complex_asinh(const __complex__ long double& __z)
1768  { return __builtin_casinhl(__z); }
1769
1770  template<typename _Tp>
1771    inline std::complex<_Tp>
1772    asinh(const std::complex<_Tp>& __z)
1773    { return __complex_asinh(__z.__rep()); }
1774#else
1775  /// asinh(__z) [8.1.6].
1776  //  Effects:  Behaves the same as C99 function casin, defined
1777  //            in subclause 7.3.6.2.
1778  template<typename _Tp>
1779    inline std::complex<_Tp>
1780    asinh(const std::complex<_Tp>& __z)
1781    { return __complex_asinh(__z); }
1782#endif
1783
1784  template<typename _Tp>
1785    std::complex<_Tp>
1786    __complex_atanh(const std::complex<_Tp>& __z)
1787    {
1788      const _Tp __i2 = __z.imag() * __z.imag();
1789      const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
1790
1791      _Tp __num = _Tp(1.0) + __z.real();
1792      _Tp __den = _Tp(1.0) - __z.real();
1793
1794      __num = __i2 + __num * __num;
1795      __den = __i2 + __den * __den;
1796
1797      return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
1798			       _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
1799    }
1800
1801#if _GLIBCXX_USE_C99_COMPLEX_TR1
1802  inline __complex__ float
1803  __complex_atanh(__complex__ float __z)
1804  { return __builtin_catanhf(__z); }
1805
1806  inline __complex__ double
1807  __complex_atanh(__complex__ double __z)
1808  { return __builtin_catanh(__z); }
1809
1810  inline __complex__ long double
1811  __complex_atanh(const __complex__ long double& __z)
1812  { return __builtin_catanhl(__z); }
1813
1814  template<typename _Tp>
1815    inline std::complex<_Tp>
1816    atanh(const std::complex<_Tp>& __z)
1817    { return __complex_atanh(__z.__rep()); }
1818#else
1819  /// atanh(__z) [8.1.7].
1820  //  Effects:  Behaves the same as C99 function catanh, defined
1821  //            in subclause 7.3.6.3.
1822  template<typename _Tp>
1823    inline std::complex<_Tp>
1824    atanh(const std::complex<_Tp>& __z)
1825    { return __complex_atanh(__z); }
1826#endif
1827
1828  template<typename _Tp>
1829    inline _Tp
1830    /// fabs(__z) [8.1.8].
1831    //  Effects:  Behaves the same as C99 function cabs, defined
1832    //            in subclause 7.3.8.1.
1833    fabs(const std::complex<_Tp>& __z)
1834    { return std::abs(__z); }
1835
1836  /// Additional overloads [8.1.9].
1837  template<typename _Tp>
1838    inline typename __gnu_cxx::__promote<_Tp>::__type
1839    arg(_Tp __x)
1840    {
1841      typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1842#if (_GLIBCXX_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
1843      return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
1844	                       : __type();
1845#else
1846      return std::arg(std::complex<__type>(__x));
1847#endif
1848    }
1849
1850  template<typename _Tp>
1851    inline typename __gnu_cxx::__promote<_Tp>::__type
1852    imag(_Tp)
1853    { return _Tp(); }
1854
1855  template<typename _Tp>
1856    inline typename __gnu_cxx::__promote<_Tp>::__type
1857    norm(_Tp __x)
1858    {
1859      typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
1860      return __type(__x) * __type(__x);
1861    }
1862
1863  template<typename _Tp>
1864    inline typename __gnu_cxx::__promote<_Tp>::__type
1865    real(_Tp __x)
1866    { return __x; }
1867
1868  template<typename _Tp, typename _Up>
1869    inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1870    pow(const std::complex<_Tp>& __x, const _Up& __y)
1871    {
1872      typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1873      return std::pow(std::complex<__type>(__x), __type(__y));
1874    }
1875
1876  template<typename _Tp, typename _Up>
1877    inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1878    pow(const _Tp& __x, const std::complex<_Up>& __y)
1879    {
1880      typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1881      return std::pow(__type(__x), std::complex<__type>(__y));
1882    }
1883
1884  template<typename _Tp, typename _Up>
1885    inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
1886    pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
1887    {
1888      typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
1889      return std::pow(std::complex<__type>(__x),
1890		      std::complex<__type>(__y));
1891    }
1892
1893  // Forward declarations.
1894  // DR 781.
1895  template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&);
1896
1897  template<typename _Tp>
1898    std::complex<_Tp>
1899    __complex_proj(const std::complex<_Tp>& __z)
1900    {
1901      const _Tp __den = (__z.real() * __z.real()
1902			 + __z.imag() * __z.imag() + _Tp(1.0));
1903
1904      return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den,
1905			       (_Tp(2.0) * __z.imag()) / __den);
1906    }
1907
1908#if _GLIBCXX_USE_C99_COMPLEX
1909  inline __complex__ float
1910  __complex_proj(__complex__ float __z)
1911  { return __builtin_cprojf(__z); }
1912
1913  inline __complex__ double
1914  __complex_proj(__complex__ double __z)
1915  { return __builtin_cproj(__z); }
1916
1917  inline __complex__ long double
1918  __complex_proj(const __complex__ long double& __z)
1919  { return __builtin_cprojl(__z); }
1920
1921  template<typename _Tp>
1922    inline std::complex<_Tp>
1923    proj(const std::complex<_Tp>& __z)
1924    { return __complex_proj(__z.__rep()); }
1925#else
1926  template<typename _Tp>
1927    inline std::complex<_Tp>
1928    proj(const std::complex<_Tp>& __z)
1929    { return __complex_proj(__z); }
1930#endif
1931
1932  // DR 1137.
1933  template<typename _Tp>
1934    inline typename __gnu_cxx::__promote<_Tp>::__type
1935    proj(_Tp __x)
1936    { return __x; }
1937
1938  template<typename _Tp>
1939    inline typename __gnu_cxx::__promote<_Tp>::__type
1940    conj(_Tp __x)
1941    { return __x; }
1942
1943#if __cplusplus > 201103L
1944
1945inline namespace literals {
1946inline namespace complex_literals {
1947
1948#define __cpp_lib_complex_udls 201309
1949
1950  constexpr std::complex<float>
1951  operator""if(long double __num)
1952  { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
1953
1954  constexpr std::complex<float>
1955  operator""if(unsigned long long __num)
1956  { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
1957
1958  constexpr std::complex<double>
1959  operator""i(long double __num)
1960  { return std::complex<double>{0.0, static_cast<double>(__num)}; }
1961
1962  constexpr std::complex<double>
1963  operator""i(unsigned long long __num)
1964  { return std::complex<double>{0.0, static_cast<double>(__num)}; }
1965
1966  constexpr std::complex<long double>
1967  operator""il(long double __num)
1968  { return std::complex<long double>{0.0L, __num}; }
1969
1970  constexpr std::complex<long double>
1971  operator""il(unsigned long long __num)
1972  { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; }
1973
1974} // inline namespace complex_literals
1975} // inline namespace literals
1976
1977#endif // C++14
1978
1979_GLIBCXX_END_NAMESPACE_VERSION
1980} // namespace
1981
1982#endif  // C++11
1983
1984#endif  /* _GLIBCXX_COMPLEX */
1985