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