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