Remove m_nloops field from loop_versioning
[official-gcc.git] / libstdc++-v3 / include / std / complex
blob415c735665c3eb8ab001b586e5331ecdd9e1b5f4
1 // The template and inlines for the -*- C++ -*- complex number classes.
3 // Copyright (C) 1997-2024 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.
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.
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.
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/>.
25 /** @file include/complex
26  *  This is a Standard C++ Library header.
27  */
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>
36 #ifndef _GLIBCXX_COMPLEX
37 #define _GLIBCXX_COMPLEX 1
39 #pragma GCC system_header
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>
47 // Get rid of a macro possibly defined in <complex.h>
48 #undef complex
50 #ifdef __clang__
51 #pragma clang diagnostic push
52 #pragma clang diagnostic ignored "-Wc99-extensions"
53 #endif
55 #define __glibcxx_want_constexpr_complex
56 #define __glibcxx_want_complex_udls
57 #include <bits/version.h>
59 namespace std _GLIBCXX_VISIBILITY(default)
61 _GLIBCXX_BEGIN_NAMESPACE_VERSION
63   /**
64    * @defgroup complex_numbers Complex Numbers
65    * @ingroup numerics
66    *
67    * Classes and functions for complex numbers.
68    * @{
69    */
71   // Forward declarations.
72   template<typename _Tp> class complex;
73   template<> class complex<float>;
74   template<> class complex<double>;
75   template<> class complex<long double>;
77   ///  Return magnitude of @a z.
78   template<typename _Tp> _Tp abs(const complex<_Tp>&);
79   ///  Return phase angle of @a z.
80   template<typename _Tp> _Tp arg(const complex<_Tp>&);
81   ///  Return @a z magnitude squared.
82   template<typename _Tp> _Tp _GLIBCXX20_CONSTEXPR norm(const complex<_Tp>&);
84   ///  Return complex conjugate of @a z.
85   template<typename _Tp>
86     _GLIBCXX20_CONSTEXPR complex<_Tp> conj(const complex<_Tp>&);
87   ///  Return complex with magnitude @a rho and angle @a theta.
88   template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
90   // Transcendentals:
91   /// Return complex cosine of @a z.
92   template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
93   /// Return complex hyperbolic cosine of @a z.
94   template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
95   /// Return complex base e exponential of @a z.
96   template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
97   /// Return complex natural logarithm of @a z.
98   template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
99   /// Return complex base 10 logarithm of @a z.
100   template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
101   /// Return @a x to the @a y'th power.
102   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
103   /// Return @a x to the @a y'th power.
104   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
105   /// Return @a x to the @a y'th power.
106   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
107                                           const complex<_Tp>&);
108   /// Return @a x to the @a y'th power.
109   template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
110   /// Return complex sine of @a z.
111   template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
112   /// Return complex hyperbolic sine of @a z.
113   template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
114   /// Return complex square root of @a z.
115   template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
116   /// Return complex tangent of @a z.
117   template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
118   /// Return complex hyperbolic tangent of @a z.
119   template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
122   // 26.2.2  Primary template class complex
123   /**
124    *  Template to represent complex numbers.
125    *
126    *  Specializations for float, double, and long double are part of the
127    *  library.  Results with any other type are not guaranteed.
128    *
129    *  @param  Tp  Type of real and imaginary values.
130   */
131   template<typename _Tp>
132     class complex
133     {
134     public:
135       /// Value typedef.
136       typedef _Tp value_type;
138       ///  Default constructor.  First parameter is x, second parameter is y.
139       ///  Unspecified parameters default to 0.
140       _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
141       : _M_real(__r), _M_imag(__i) { }
143       // Let the compiler synthesize the copy constructor
144 #if __cplusplus >= 201103L
145       constexpr complex(const complex&) = default;
146 #endif
148       ///  Converting constructor.
149       template<typename _Up>
150 #if __cplusplus > 202002L
151         explicit(!requires(_Up __u) { _Tp{__u}; })
152 #endif
153         _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z)
154         : _M_real(_Tp(__z.real())), _M_imag(_Tp(__z.imag())) { }
156 #if __cplusplus >= 201103L
157       // _GLIBCXX_RESOLVE_LIB_DEFECTS
158       // DR 387. std::complex over-encapsulated.
159       _GLIBCXX_ABI_TAG_CXX11
160       constexpr _Tp
161       real() const { return _M_real; }
163       _GLIBCXX_ABI_TAG_CXX11
164       constexpr _Tp
165       imag() const { return _M_imag; }
166 #else
167       ///  Return real part of complex number.
168       _Tp&
169       real() { return _M_real; }
171       ///  Return real part of complex number.
172       const _Tp&
173       real() const { return _M_real; }
175       ///  Return imaginary part of complex number.
176       _Tp&
177       imag() { return _M_imag; }
179       ///  Return imaginary part of complex number.
180       const _Tp&
181       imag() const { return _M_imag; }
182 #endif
184       // _GLIBCXX_RESOLVE_LIB_DEFECTS
185       // DR 387. std::complex over-encapsulated.
186       _GLIBCXX20_CONSTEXPR void
187       real(_Tp __val) { _M_real = __val; }
189       _GLIBCXX20_CONSTEXPR void
190       imag(_Tp __val) { _M_imag = __val; }
192       /// Assign a scalar to this complex number.
193       _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const _Tp&);
195       /// Add a scalar to this complex number.
196       // 26.2.5/1
197       _GLIBCXX20_CONSTEXPR complex<_Tp>&
198       operator+=(const _Tp& __t)
199       {
200         _M_real += __t;
201         return *this;
202       }
204       /// Subtract a scalar from this complex number.
205       // 26.2.5/3
206       _GLIBCXX20_CONSTEXPR complex<_Tp>&
207       operator-=(const _Tp& __t)
208       {
209         _M_real -= __t;
210         return *this;
211       }
213       /// Multiply this complex number by a scalar.
214       _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const _Tp&);
215       /// Divide this complex number by a scalar.
216       _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const _Tp&);
218       // Let the compiler synthesize the copy assignment operator
219 #if __cplusplus >= 201103L
220       _GLIBCXX20_CONSTEXPR complex& operator=(const complex&) = default;
221 #endif
223       /// Assign another complex number to this one.
224       template<typename _Up>
225         _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const complex<_Up>&);
226       /// Add another complex number to this one.
227       template<typename _Up>
228         _GLIBCXX20_CONSTEXPR complex<_Tp>& operator+=(const complex<_Up>&);
229       /// Subtract another complex number from this one.
230       template<typename _Up>
231         _GLIBCXX20_CONSTEXPR complex<_Tp>& operator-=(const complex<_Up>&);
232       /// Multiply this complex number by another.
233       template<typename _Up>
234         _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const complex<_Up>&);
235       /// Divide this complex number by another.
236       template<typename _Up>
237         _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const complex<_Up>&);
239       _GLIBCXX_CONSTEXPR complex __rep() const
240       { return *this; }
242     private:
243       _Tp _M_real;
244       _Tp _M_imag;
245     };
247   template<typename _Tp>
248     _GLIBCXX20_CONSTEXPR complex<_Tp>&
249     complex<_Tp>::operator=(const _Tp& __t)
250     {
251      _M_real = __t;
252      _M_imag = _Tp();
253      return *this;
254     }
256   // 26.2.5/5
257   template<typename _Tp>
258     _GLIBCXX20_CONSTEXPR complex<_Tp>&
259     complex<_Tp>::operator*=(const _Tp& __t)
260     {
261       _M_real *= __t;
262       _M_imag *= __t;
263       return *this;
264     }
266   // 26.2.5/7
267   template<typename _Tp>
268     _GLIBCXX20_CONSTEXPR complex<_Tp>&
269     complex<_Tp>::operator/=(const _Tp& __t)
270     {
271       _M_real /= __t;
272       _M_imag /= __t;
273       return *this;
274     }
276   template<typename _Tp>
277     template<typename _Up>
278     _GLIBCXX20_CONSTEXPR complex<_Tp>&
279     complex<_Tp>::operator=(const complex<_Up>& __z)
280     {
281       _M_real = __z.real();
282       _M_imag = __z.imag();
283       return *this;
284     }
286   // 26.2.5/9
287   template<typename _Tp>
288     template<typename _Up>
289     _GLIBCXX20_CONSTEXPR complex<_Tp>&
290     complex<_Tp>::operator+=(const complex<_Up>& __z)
291     {
292       _M_real += __z.real();
293       _M_imag += __z.imag();
294       return *this;
295     }
297   // 26.2.5/11
298   template<typename _Tp>
299     template<typename _Up>
300     _GLIBCXX20_CONSTEXPR complex<_Tp>&
301     complex<_Tp>::operator-=(const complex<_Up>& __z)
302     {
303       _M_real -= __z.real();
304       _M_imag -= __z.imag();
305       return *this;
306     }
308   // 26.2.5/13
309   // XXX: This is a grammar school implementation.
310   template<typename _Tp>
311     template<typename _Up>
312     _GLIBCXX20_CONSTEXPR complex<_Tp>&
313     complex<_Tp>::operator*=(const complex<_Up>& __z)
314     {
315       const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
316       _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
317       _M_real = __r;
318       return *this;
319     }
321   // 26.2.5/15
322   // XXX: This is a grammar school implementation.
323   template<typename _Tp>
324     template<typename _Up>
325     _GLIBCXX20_CONSTEXPR complex<_Tp>&
326     complex<_Tp>::operator/=(const complex<_Up>& __z)
327     {
328       const _Tp __r =  _M_real * __z.real() + _M_imag * __z.imag();
329       const _Tp __n = std::norm(__z);
330       _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
331       _M_real = __r / __n;
332       return *this;
333     }
335   // Operators:
336   ///@{
337   ///  Return new complex value @a x plus @a y.
338   template<typename _Tp>
339     inline _GLIBCXX20_CONSTEXPR complex<_Tp>
340     operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
341     {
342       complex<_Tp> __r = __x;
343       __r += __y;
344       return __r;
345     }
347   template<typename _Tp>
348     inline _GLIBCXX20_CONSTEXPR complex<_Tp>
349     operator+(const complex<_Tp>& __x, const _Tp& __y)
350     {
351       complex<_Tp> __r = __x;
352       __r += __y;
353       return __r;
354     }
356   template<typename _Tp>
357     inline _GLIBCXX20_CONSTEXPR complex<_Tp>
358     operator+(const _Tp& __x, const complex<_Tp>& __y)
359     {
360       complex<_Tp> __r = __y;
361       __r += __x;
362       return __r;
363     }
364   ///@}
366   ///@{
367   ///  Return new complex value @a x minus @a y.
368   template<typename _Tp>
369     inline _GLIBCXX20_CONSTEXPR complex<_Tp>
370     operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
371     {
372       complex<_Tp> __r = __x;
373       __r -= __y;
374       return __r;
375     }
377   template<typename _Tp>
378     inline _GLIBCXX20_CONSTEXPR complex<_Tp>
379     operator-(const complex<_Tp>& __x, const _Tp& __y)
380     {
381       complex<_Tp> __r = __x;
382       __r -= __y;
383       return __r;
384     }
386   template<typename _Tp>
387     inline _GLIBCXX20_CONSTEXPR complex<_Tp>
388     operator-(const _Tp& __x, const complex<_Tp>& __y)
389     {
390       complex<_Tp> __r = -__y;
391       __r += __x;
392       return __r;
393     }
394   ///@}
396   ///@{
397   ///  Return new complex value @a x times @a y.
398   template<typename _Tp>
399     inline _GLIBCXX20_CONSTEXPR complex<_Tp>
400     operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
401     {
402       complex<_Tp> __r = __x;
403       __r *= __y;
404       return __r;
405     }
407   template<typename _Tp>
408     inline _GLIBCXX20_CONSTEXPR complex<_Tp>
409     operator*(const complex<_Tp>& __x, const _Tp& __y)
410     {
411       complex<_Tp> __r = __x;
412       __r *= __y;
413       return __r;
414     }
416   template<typename _Tp>
417     inline _GLIBCXX20_CONSTEXPR complex<_Tp>
418     operator*(const _Tp& __x, const complex<_Tp>& __y)
419     {
420       complex<_Tp> __r = __y;
421       __r *= __x;
422       return __r;
423     }
424   ///@}
426   ///@{
427   ///  Return new complex value @a x divided by @a y.
428   template<typename _Tp>
429     inline _GLIBCXX20_CONSTEXPR complex<_Tp>
430     operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
431     {
432       complex<_Tp> __r = __x;
433       __r /= __y;
434       return __r;
435     }
437   template<typename _Tp>
438     inline _GLIBCXX20_CONSTEXPR complex<_Tp>
439     operator/(const complex<_Tp>& __x, const _Tp& __y)
440     {
441       complex<_Tp> __r = __x;
442       __r /= __y;
443       return __r;
444     }
446   template<typename _Tp>
447     inline _GLIBCXX20_CONSTEXPR complex<_Tp>
448     operator/(const _Tp& __x, const complex<_Tp>& __y)
449     {
450       complex<_Tp> __r = __x;
451       __r /= __y;
452       return __r;
453     }
454   ///@}
456   ///  Return @a x.
457   template<typename _Tp>
458     inline _GLIBCXX20_CONSTEXPR complex<_Tp>
459     operator+(const complex<_Tp>& __x)
460     { return __x; }
462   ///  Return complex negation of @a x.
463   template<typename _Tp>
464     inline _GLIBCXX20_CONSTEXPR complex<_Tp>
465     operator-(const complex<_Tp>& __x)
466     { return complex<_Tp>(-__x.real(), -__x.imag()); }
468   ///@{
469   ///  Return true if @a x is equal to @a y.
470   template<typename _Tp>
471     inline _GLIBCXX_CONSTEXPR bool
472     operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
473     { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
475   template<typename _Tp>
476     inline _GLIBCXX_CONSTEXPR bool
477     operator==(const complex<_Tp>& __x, const _Tp& __y)
478     { return __x.real() == __y && __x.imag() == _Tp(); }
480 #if !(__cpp_impl_three_way_comparison >= 201907L)
481   template<typename _Tp>
482     inline _GLIBCXX_CONSTEXPR bool
483     operator==(const _Tp& __x, const complex<_Tp>& __y)
484     { return __x == __y.real() && _Tp() == __y.imag(); }
485   ///@}
487   ///@{
488   ///  Return false if @a x is equal to @a y.
489   template<typename _Tp>
490     inline _GLIBCXX_CONSTEXPR bool
491     operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
492     { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
494   template<typename _Tp>
495     inline _GLIBCXX_CONSTEXPR bool
496     operator!=(const complex<_Tp>& __x, const _Tp& __y)
497     { return __x.real() != __y || __x.imag() != _Tp(); }
499   template<typename _Tp>
500     inline _GLIBCXX_CONSTEXPR bool
501     operator!=(const _Tp& __x, const complex<_Tp>& __y)
502     { return __x != __y.real() || _Tp() != __y.imag(); }
503 #endif
504   ///@}
506   ///  Extraction operator for complex values.
507   template<typename _Tp, typename _CharT, class _Traits>
508     basic_istream<_CharT, _Traits>&
509     operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
510     {
511       bool __fail = true;
512       _CharT __ch;
513       if (__is >> __ch)
514         {
515           if (_Traits::eq(__ch, __is.widen('(')))
516             {
517               _Tp __u;
518               if (__is >> __u >> __ch)
519                 {
520                   const _CharT __rparen = __is.widen(')');
521                   if (_Traits::eq(__ch, __rparen))
522                     {
523                       __x = __u;
524                       __fail = false;
525                     }
526                   else if (_Traits::eq(__ch, __is.widen(',')))
527                     {
528                       _Tp __v;
529                       if (__is >> __v >> __ch)
530                         {
531                           if (_Traits::eq(__ch, __rparen))
532                             {
533                               __x = complex<_Tp>(__u, __v);
534                               __fail = false;
535                             }
536                           else
537                             __is.putback(__ch);
538                         }
539                     }
540                   else
541                     __is.putback(__ch);
542                 }
543             }
544           else
545             {
546               __is.putback(__ch);
547               _Tp __u;
548               if (__is >> __u)
549                 {
550                   __x = __u;
551                   __fail = false;
552                 }
553             }
554         }
555       if (__fail)
556         __is.setstate(ios_base::failbit);
557       return __is;
558     }
560   ///  Insertion operator for complex values.
561   template<typename _Tp, typename _CharT, class _Traits>
562     basic_ostream<_CharT, _Traits>&
563     operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
564     {
565       basic_ostringstream<_CharT, _Traits> __s;
566       __s.flags(__os.flags());
567       __s.imbue(__os.getloc());
568       __s.precision(__os.precision());
569       __s << '(' << __x.real() << ',' << __x.imag() << ')';
570       return __os << __s.str();
571     }
573   // Values
574 #if __cplusplus >= 201103L
575   template<typename _Tp>
576     constexpr _Tp
577     real(const complex<_Tp>& __z)
578     { return __z.real(); }
580   template<typename _Tp>
581     constexpr _Tp
582     imag(const complex<_Tp>& __z)
583     { return __z.imag(); }
584 #else
585   template<typename _Tp>
586     inline _Tp&
587     real(complex<_Tp>& __z)
588     { return __z.real(); }
590   template<typename _Tp>
591     inline const _Tp&
592     real(const complex<_Tp>& __z)
593     { return __z.real(); }
595   template<typename _Tp>
596     inline _Tp&
597     imag(complex<_Tp>& __z)
598     { return __z.imag(); }
600   template<typename _Tp>
601     inline const _Tp&
602     imag(const complex<_Tp>& __z)
603     { return __z.imag(); }
604 #endif
606 #if _GLIBCXX_USE_C99_COMPLEX
607 #if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
608   inline _Float16
609   __complex_abs(__complex__ _Float16 __z)
610   { return _Float16(__builtin_cabsf(__z)); }
612   inline _Float16
613   __complex_arg(__complex__ _Float16 __z)
614   { return _Float16(__builtin_cargf(__z)); }
616   inline __complex__ _Float16
617   __complex_cos(__complex__ _Float16 __z)
618   { return static_cast<__complex__ _Float16>(__builtin_ccosf(__z)); }
620   inline __complex__ _Float16
621   __complex_cosh(__complex__ _Float16 __z)
622   { return static_cast<__complex__ _Float16>(__builtin_ccoshf(__z)); }
624   inline __complex__ _Float16
625   __complex_exp(__complex__ _Float16 __z)
626   { return static_cast<__complex__ _Float16>(__builtin_cexpf(__z)); }
628   inline __complex__ _Float16
629   __complex_log(__complex__ _Float16 __z)
630   { return static_cast<__complex__ _Float16>(__builtin_clogf(__z)); }
632   inline __complex__ _Float16
633   __complex_sin(__complex__ _Float16 __z)
634   { return static_cast<__complex__ _Float16>(__builtin_csinf(__z)); }
636   inline __complex__ _Float16
637   __complex_sinh(__complex__ _Float16 __z)
638   { return static_cast<__complex__ _Float16>(__builtin_csinhf(__z)); }
640   inline __complex__ _Float16
641   __complex_sqrt(__complex__ _Float16 __z)
642   { return static_cast<__complex__ _Float16>(__builtin_csqrtf(__z)); }
644   inline __complex__ _Float16
645   __complex_tan(__complex__ _Float16 __z)
646   { return static_cast<__complex__ _Float16>(__builtin_ctanf(__z)); }
648   inline __complex__ _Float16
649   __complex_tanh(__complex__ _Float16 __z)
650   { return static_cast<__complex__ _Float16>(__builtin_ctanhf(__z)); }
652   inline __complex__ _Float16
653   __complex_pow(__complex__ _Float16 __x, __complex__ _Float16 __y)
654   { return static_cast<__complex__ _Float16>(__builtin_cpowf(__x, __y)); }
655 #endif
657 #if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
658   inline _Float32
659   __complex_abs(__complex__ _Float32 __z) { return __builtin_cabsf(__z); }
661   inline _Float32
662   __complex_arg(__complex__ _Float32 __z) { return __builtin_cargf(__z); }
664   inline __complex__ _Float32
665   __complex_cos(__complex__ _Float32 __z) { return __builtin_ccosf(__z); }
667   inline __complex__ _Float32
668   __complex_cosh(__complex__ _Float32 __z) { return __builtin_ccoshf(__z); }
670   inline __complex__ _Float32
671   __complex_exp(__complex__ _Float32 __z) { return __builtin_cexpf(__z); }
673   inline __complex__ _Float32
674   __complex_log(__complex__ _Float32 __z) { return __builtin_clogf(__z); }
676   inline __complex__ _Float32
677   __complex_sin(__complex__ _Float32 __z) { return __builtin_csinf(__z); }
679   inline __complex__ _Float32
680   __complex_sinh(__complex__ _Float32 __z) { return __builtin_csinhf(__z); }
682   inline __complex__ _Float32
683   __complex_sqrt(__complex__ _Float32 __z) { return __builtin_csqrtf(__z); }
685   inline __complex__ _Float32
686   __complex_tan(__complex__ _Float32 __z) { return __builtin_ctanf(__z); }
688   inline __complex__ _Float32
689   __complex_tanh(__complex__ _Float32 __z) { return __builtin_ctanhf(__z); }
691   inline __complex__ _Float32
692   __complex_pow(__complex__ _Float32 __x, __complex__ _Float32 __y)
693   { return __builtin_cpowf(__x, __y); }
694 #endif
696 #if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
697   inline _Float64
698   __complex_abs(__complex__ _Float64 __z) { return __builtin_cabs(__z); }
700   inline _Float64
701   __complex_arg(__complex__ _Float64 __z) { return __builtin_carg(__z); }
703   inline __complex__ _Float64
704   __complex_cos(__complex__ _Float64 __z) { return __builtin_ccos(__z); }
706   inline __complex__ _Float64
707   __complex_cosh(__complex__ _Float64 __z) { return __builtin_ccosh(__z); }
709   inline __complex__ _Float64
710   __complex_exp(__complex__ _Float64 __z) { return __builtin_cexp(__z); }
712   inline __complex__ _Float64
713   __complex_log(__complex__ _Float64 __z) { return __builtin_clog(__z); }
715   inline __complex__ _Float64
716   __complex_sin(__complex__ _Float64 __z) { return __builtin_csin(__z); }
718   inline __complex__ _Float64
719   __complex_sinh(__complex__ _Float64 __z) { return __builtin_csinh(__z); }
721   inline __complex__ _Float64
722   __complex_sqrt(__complex__ _Float64 __z) { return __builtin_csqrt(__z); }
724   inline __complex__ _Float64
725   __complex_tan(__complex__ _Float64 __z) { return __builtin_ctan(__z); }
727   inline __complex__ _Float64
728   __complex_tanh(__complex__ _Float64 __z) { return __builtin_ctanh(__z); }
730   inline __complex__ _Float64
731   __complex_pow(__complex__ _Float64 __x, __complex__ _Float64 __y)
732   { return __builtin_cpow(__x, __y); }
733 #endif
735 #if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
736   inline _Float128
737   __complex_abs(__complex__ _Float128 __z) { return __builtin_cabsl(__z); }
739   inline _Float128
740   __complex_arg(__complex__ _Float128 __z) { return __builtin_cargl(__z); }
742   inline __complex__ _Float128
743   __complex_cos(__complex__ _Float128 __z) { return __builtin_ccosl(__z); }
745   inline __complex__ _Float128
746   __complex_cosh(__complex__ _Float128 __z) { return __builtin_ccoshl(__z); }
748   inline __complex__ _Float128
749   __complex_exp(__complex__ _Float128 __z) { return __builtin_cexpl(__z); }
751   inline __complex__ _Float128
752   __complex_log(__complex__ _Float128 __z) { return __builtin_clogl(__z); }
754   inline __complex__ _Float128
755   __complex_sin(__complex__ _Float128 __z) { return __builtin_csinl(__z); }
757   inline __complex__ _Float128
758   __complex_sinh(__complex__ _Float128 __z) { return __builtin_csinhl(__z); }
760   inline __complex__ _Float128
761   __complex_sqrt(__complex__ _Float128 __z) { return __builtin_csqrtl(__z); }
763   inline __complex__ _Float128
764   __complex_tan(__complex__ _Float128 __z) { return __builtin_ctanl(__z); }
766   inline __complex__ _Float128
767   __complex_tanh(__complex__ _Float128 __z) { return __builtin_ctanhl(__z); }
769   inline __complex__ _Float128
770   __complex_pow(__complex__ _Float128 __x, __complex__ _Float128 __y)
771   { return __builtin_cpowl(__x, __y); }
772 #elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
773   inline _Float128
774   __complex_abs(__complex__ _Float128 __z) { return __builtin_cabsf128(__z); }
776   inline _Float128
777   __complex_arg(__complex__ _Float128 __z) { return __builtin_cargf128(__z); }
779   inline __complex__ _Float128
780   __complex_cos(__complex__ _Float128 __z) { return __builtin_ccosf128(__z); }
782   inline __complex__ _Float128
783   __complex_cosh(__complex__ _Float128 __z) { return __builtin_ccoshf128(__z); }
785   inline __complex__ _Float128
786   __complex_exp(__complex__ _Float128 __z) { return __builtin_cexpf128(__z); }
788   inline __complex__ _Float128
789   __complex_log(__complex__ _Float128 __z) { return __builtin_clogf128(__z); }
791   inline __complex__ _Float128
792   __complex_sin(__complex__ _Float128 __z) { return __builtin_csinf128(__z); }
794   inline __complex__ _Float128
795   __complex_sinh(__complex__ _Float128 __z) { return __builtin_csinhf128(__z); }
797   inline __complex__ _Float128
798   __complex_sqrt(__complex__ _Float128 __z) { return __builtin_csqrtf128(__z); }
800   inline __complex__ _Float128
801   __complex_tan(__complex__ _Float128 __z) { return __builtin_ctanf128(__z); }
803   inline __complex__ _Float128
804   __complex_tanh(__complex__ _Float128 __z) { return __builtin_ctanhf128(__z); }
806   inline __complex__ _Float128
807   __complex_pow(__complex__ _Float128 __x, __complex__ _Float128 __y)
808   { return __builtin_cpowf128(__x, __y); }
809 #endif
811 #if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
812   inline __gnu_cxx::__bfloat16_t
813   __complex_abs(__complex__ decltype(0.0bf16) __z)
814   { return __gnu_cxx::__bfloat16_t(__builtin_cabsf(__z)); }
816   inline __gnu_cxx::__bfloat16_t
817   __complex_arg(__complex__ decltype(0.0bf16) __z)
818   { return __gnu_cxx::__bfloat16_t(__builtin_cargf(__z)); }
820   inline __complex__ decltype(0.0bf16)
821   __complex_cos(__complex__ decltype(0.0bf16) __z)
822   { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ccosf(__z)); }
824   inline __complex__ decltype(0.0bf16)
825   __complex_cosh(__complex__ decltype(0.0bf16) __z)
826   { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ccoshf(__z)); }
828   inline __complex__ decltype(0.0bf16)
829   __complex_exp(__complex__ decltype(0.0bf16) __z)
830   { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cexpf(__z)); }
832   inline __complex__ decltype(0.0bf16)
833   __complex_log(__complex__ decltype(0.0bf16) __z)
834   { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_clogf(__z)); }
836   inline __complex__ decltype(0.0bf16)
837   __complex_sin(__complex__ decltype(0.0bf16) __z)
838   { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csinf(__z)); }
840   inline __complex__ decltype(0.0bf16)
841   __complex_sinh(__complex__ decltype(0.0bf16) __z)
842   { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csinhf(__z)); }
844   inline __complex__ decltype(0.0bf16)
845   __complex_sqrt(__complex__ decltype(0.0bf16) __z)
846   { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csqrtf(__z)); }
848   inline __complex__ decltype(0.0bf16)
849   __complex_tan(__complex__ decltype(0.0bf16) __z)
850   { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ctanf(__z)); }
852   inline __complex__ decltype(0.0bf16)
853   __complex_tanh(__complex__ decltype(0.0bf16) __z)
854   { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ctanhf(__z)); }
856   inline __complex__ decltype(0.0bf16)
857   __complex_pow(__complex__ decltype(0.0bf16) __x,
858                 __complex__ decltype(0.0bf16) __y)
859   { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cpowf(__x,
860                                                                       __y)); }
861 #endif
862 #endif
864   // 26.2.7/3 abs(__z):  Returns the magnitude of __z.
865   template<typename _Tp>
866     inline _Tp
867     __complex_abs(const complex<_Tp>& __z)
868     {
869       _Tp __x = __z.real();
870       _Tp __y = __z.imag();
871       const _Tp __s = std::max(abs(__x), abs(__y));
872       if (__s == _Tp())  // well ...
873         return __s;
874       __x /= __s;
875       __y /= __s;
876       return __s * sqrt(__x * __x + __y * __y);
877     }
879 #if _GLIBCXX_USE_C99_COMPLEX
880   inline float
881   __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
883   inline double
884   __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
886   inline long double
887   __complex_abs(const __complex__ long double& __z)
888   { return __builtin_cabsl(__z); }
890   template<typename _Tp>
891     inline _Tp
892     abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
893 #else
894   template<typename _Tp>
895     inline _Tp
896     abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
897 #endif
900   // 26.2.7/4: arg(__z): Returns the phase angle of __z.
901   template<typename _Tp>
902     inline _Tp
903     __complex_arg(const complex<_Tp>& __z)
904     { return  atan2(__z.imag(), __z.real()); }
906 #if _GLIBCXX_USE_C99_COMPLEX
907   inline float
908   __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
910   inline double
911   __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
913   inline long double
914   __complex_arg(const __complex__ long double& __z)
915   { return __builtin_cargl(__z); }
917   template<typename _Tp>
918     inline _Tp
919     arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
920 #else
921   template<typename _Tp>
922     inline _Tp
923     arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
924 #endif
926   // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
927   //     As defined, norm() is -not- a norm is the common mathematical
928   //     sense used in numerics.  The helper class _Norm_helper<> tries to
929   //     distinguish between builtin floating point and the rest, so as
930   //     to deliver an answer as close as possible to the real value.
931   template<bool>
932     struct _Norm_helper
933     {
934       template<typename _Tp>
935         static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
936         {
937           const _Tp __x = __z.real();
938           const _Tp __y = __z.imag();
939           return __x * __x + __y * __y;
940         }
941     };
943   template<>
944     struct _Norm_helper<true>
945     {
946       template<typename _Tp>
947         static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
948         {
949           //_Tp __res = std::abs(__z);
950           //return __res * __res;
951           const _Tp __x = __z.real();
952           const _Tp __y = __z.imag();
953           return __x * __x + __y * __y;
954         }
955     };
957   template<typename _Tp>
958     inline _GLIBCXX20_CONSTEXPR _Tp
959     norm(const complex<_Tp>& __z)
960     {
961       return _Norm_helper<__is_floating<_Tp>::__value
962         && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
963     }
965   template<typename _Tp>
966     inline complex<_Tp>
967     polar(const _Tp& __rho, const _Tp& __theta)
968     {
969       __glibcxx_assert( __rho >= 0 );
970       return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta));
971     }
973   template<typename _Tp>
974     inline _GLIBCXX20_CONSTEXPR complex<_Tp>
975     conj(const complex<_Tp>& __z)
976     { return complex<_Tp>(__z.real(), -__z.imag()); }
978   // Transcendentals
980   // 26.2.8/1 cos(__z):  Returns the cosine of __z.
981   template<typename _Tp>
982     inline complex<_Tp>
983     __complex_cos(const complex<_Tp>& __z)
984     {
985       const _Tp __x = __z.real();
986       const _Tp __y = __z.imag();
987       return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
988     }
990 #if _GLIBCXX_USE_C99_COMPLEX
991   inline __complex__ float
992   __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
994   inline __complex__ double
995   __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
997   inline __complex__ long double
998   __complex_cos(const __complex__ long double& __z)
999   { return __builtin_ccosl(__z); }
1001   template<typename _Tp>
1002     inline complex<_Tp>
1003     cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
1004 #else
1005   template<typename _Tp>
1006     inline complex<_Tp>
1007     cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
1008 #endif
1010   // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
1011   template<typename _Tp>
1012     inline complex<_Tp>
1013     __complex_cosh(const complex<_Tp>& __z)
1014     {
1015       const _Tp __x = __z.real();
1016       const _Tp __y = __z.imag();
1017       return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
1018     }
1020 #if _GLIBCXX_USE_C99_COMPLEX
1021   inline __complex__ float
1022   __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
1024   inline __complex__ double
1025   __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
1027   inline __complex__ long double
1028   __complex_cosh(const __complex__ long double& __z)
1029   { return __builtin_ccoshl(__z); }
1031   template<typename _Tp>
1032     inline complex<_Tp>
1033     cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
1034 #else
1035   template<typename _Tp>
1036     inline complex<_Tp>
1037     cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
1038 #endif
1040   // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
1041   template<typename _Tp>
1042     inline complex<_Tp>
1043     __complex_exp(const complex<_Tp>& __z)
1044     { return std::polar<_Tp>(exp(__z.real()), __z.imag()); }
1046 #if _GLIBCXX_USE_C99_COMPLEX
1047   inline __complex__ float
1048   __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
1050   inline __complex__ double
1051   __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
1053   inline __complex__ long double
1054   __complex_exp(const __complex__ long double& __z)
1055   { return __builtin_cexpl(__z); }
1057   template<typename _Tp>
1058     inline complex<_Tp>
1059     exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
1060 #else
1061   template<typename _Tp>
1062     inline complex<_Tp>
1063     exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
1064 #endif
1066   // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
1067   //                    The branch cut is along the negative axis.
1068   template<typename _Tp>
1069     inline complex<_Tp>
1070     __complex_log(const complex<_Tp>& __z)
1071     { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
1073 #if _GLIBCXX_USE_C99_COMPLEX
1074   inline __complex__ float
1075   __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
1077   inline __complex__ double
1078   __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
1080   inline __complex__ long double
1081   __complex_log(const __complex__ long double& __z)
1082   { return __builtin_clogl(__z); }
1084   template<typename _Tp>
1085     inline complex<_Tp>
1086     log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
1087 #else
1088   template<typename _Tp>
1089     inline complex<_Tp>
1090     log(const complex<_Tp>& __z) { return __complex_log(__z); }
1091 #endif
1093   template<typename _Tp>
1094     inline complex<_Tp>
1095     log10(const complex<_Tp>& __z)
1096     { return std::log(__z) / log(_Tp(10.0)); }
1098   // 26.2.8/10 sin(__z): Returns the sine of __z.
1099   template<typename _Tp>
1100     inline complex<_Tp>
1101     __complex_sin(const complex<_Tp>& __z)
1102     {
1103       const _Tp __x = __z.real();
1104       const _Tp __y = __z.imag();
1105       return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
1106     }
1108 #if _GLIBCXX_USE_C99_COMPLEX
1109   inline __complex__ float
1110   __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
1112   inline __complex__ double
1113   __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
1115   inline __complex__ long double
1116   __complex_sin(const __complex__ long double& __z)
1117   { return __builtin_csinl(__z); }
1119   template<typename _Tp>
1120     inline complex<_Tp>
1121     sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
1122 #else
1123   template<typename _Tp>
1124     inline complex<_Tp>
1125     sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
1126 #endif
1128   // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
1129   template<typename _Tp>
1130     inline complex<_Tp>
1131     __complex_sinh(const complex<_Tp>& __z)
1132     {
1133       const _Tp __x = __z.real();
1134       const _Tp  __y = __z.imag();
1135       return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
1136     }
1138 #if _GLIBCXX_USE_C99_COMPLEX
1139   inline __complex__ float
1140   __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
1142   inline __complex__ double
1143   __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
1145   inline __complex__ long double
1146   __complex_sinh(const __complex__ long double& __z)
1147   { return __builtin_csinhl(__z); }
1149   template<typename _Tp>
1150     inline complex<_Tp>
1151     sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
1152 #else
1153   template<typename _Tp>
1154     inline complex<_Tp>
1155     sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
1156 #endif
1158   // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
1159   //                     The branch cut is on the negative axis.
1160   template<typename _Tp>
1161     complex<_Tp>
1162     __complex_sqrt(const complex<_Tp>& __z)
1163     {
1164       _Tp __x = __z.real();
1165       _Tp __y = __z.imag();
1167       if (__x == _Tp())
1168         {
1169           _Tp __t = sqrt(abs(__y) / 2);
1170           return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
1171         }
1172       else
1173         {
1174           _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
1175           _Tp __u = __t / 2;
1176           return __x > _Tp()
1177             ? complex<_Tp>(__u, __y / __t)
1178             : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
1179         }
1180     }
1182 #if _GLIBCXX_USE_C99_COMPLEX
1183   inline __complex__ float
1184   __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
1186   inline __complex__ double
1187   __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
1189   inline __complex__ long double
1190   __complex_sqrt(const __complex__ long double& __z)
1191   { return __builtin_csqrtl(__z); }
1193   template<typename _Tp>
1194     inline complex<_Tp>
1195     sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
1196 #else
1197   template<typename _Tp>
1198     inline complex<_Tp>
1199     sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
1200 #endif
1202   // 26.2.8/14 tan(__z):  Return the complex tangent of __z.
1204   template<typename _Tp>
1205     inline complex<_Tp>
1206     __complex_tan(const complex<_Tp>& __z)
1207     { return std::sin(__z) / std::cos(__z); }
1209 #if _GLIBCXX_USE_C99_COMPLEX
1210   inline __complex__ float
1211   __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
1213   inline __complex__ double
1214   __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
1216   inline __complex__ long double
1217   __complex_tan(const __complex__ long double& __z)
1218   { return __builtin_ctanl(__z); }
1220   template<typename _Tp>
1221     inline complex<_Tp>
1222     tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
1223 #else
1224   template<typename _Tp>
1225     inline complex<_Tp>
1226     tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
1227 #endif
1230   // 26.2.8/15 tanh(__z):  Returns the hyperbolic tangent of __z.
1232   template<typename _Tp>
1233     inline complex<_Tp>
1234     __complex_tanh(const complex<_Tp>& __z)
1235     { return std::sinh(__z) / std::cosh(__z); }
1237 #if _GLIBCXX_USE_C99_COMPLEX
1238   inline __complex__ float
1239   __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
1241   inline __complex__ double
1242   __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
1244   inline __complex__ long double
1245   __complex_tanh(const __complex__ long double& __z)
1246   { return __builtin_ctanhl(__z); }
1248   template<typename _Tp>
1249     inline complex<_Tp>
1250     tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
1251 #else
1252   template<typename _Tp>
1253     inline complex<_Tp>
1254     tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
1255 #endif
1258   // 26.2.8/9  pow(__x, __y): Returns the complex power base of __x
1259   //                          raised to the __y-th power.  The branch
1260   //                          cut is on the negative axis.
1261   template<typename _Tp>
1262     complex<_Tp>
1263     __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
1264     {
1265       complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
1267       while (__n >>= 1)
1268         {
1269           __x *= __x;
1270           if (__n % 2)
1271             __y *= __x;
1272         }
1274       return __y;
1275     }
1277   // In C++11 mode we used to implement the resolution of
1278   // DR 844. complex pow return type is ambiguous.
1279   // thus the following overload was disabled in that mode.  However, doing
1280   // that causes all sorts of issues, see, for example:
1281   //   http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html
1282   // and also PR57974.
1283   template<typename _Tp>
1284     inline complex<_Tp>
1285     pow(const complex<_Tp>& __z, int __n)
1286     {
1287       return __n < 0
1288         ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n)
1289         : std::__complex_pow_unsigned(__z, __n);
1290     }
1292   template<typename _Tp>
1293     complex<_Tp>
1294     pow(const complex<_Tp>& __x, const _Tp& __y)
1295     {
1296 #if ! _GLIBCXX_USE_C99_COMPLEX
1297       if (__x == _Tp())
1298         return _Tp();
1299 #endif
1300       if (__x.imag() == _Tp() && __x.real() > _Tp())
1301         return pow(__x.real(), __y);
1303       complex<_Tp> __t = std::log(__x);
1304       return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag());
1305     }
1307   template<typename _Tp>
1308     inline complex<_Tp>
1309     __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1310     { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
1312 #if _GLIBCXX_USE_C99_COMPLEX
1313   inline __complex__ float
1314   __complex_pow(__complex__ float __x, __complex__ float __y)
1315   { return __builtin_cpowf(__x, __y); }
1317   inline __complex__ double
1318   __complex_pow(__complex__ double __x, __complex__ double __y)
1319   { return __builtin_cpow(__x, __y); }
1321   inline __complex__ long double
1322   __complex_pow(const __complex__ long double& __x,
1323                 const __complex__ long double& __y)
1324   { return __builtin_cpowl(__x, __y); }
1326   template<typename _Tp>
1327     inline complex<_Tp>
1328     pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1329     { return __complex_pow(__x.__rep(), __y.__rep()); }
1330 #else
1331   template<typename _Tp>
1332     inline complex<_Tp>
1333     pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1334     { return __complex_pow(__x, __y); }
1335 #endif
1337   template<typename _Tp>
1338     inline complex<_Tp>
1339     pow(const _Tp& __x, const complex<_Tp>& __y)
1340     {
1341       return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()),
1342                                            __y.imag() * log(__x))
1343                          : std::pow(complex<_Tp>(__x), __y);
1344     }
1346   /// 26.2.3  complex specializations
1347   /// complex<float> specialization
1348   template<>
1349     class complex<float>
1350     {
1351     public:
1352       typedef float value_type;
1353       typedef __complex__ float _ComplexT;
1355       _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1357       _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
1358 #if __cplusplus >= 201103L
1359       : _M_value{ __r, __i } { }
1360 #else
1361       {
1362         __real__ _M_value = __r;
1363         __imag__ _M_value = __i;
1364       }
1365 #endif
1367 #if __cplusplus >= 201103L
1368       _GLIBCXX14_CONSTEXPR complex(const complex&) = default;
1369 #endif
1371 #if __cplusplus > 202002L
1372       template<typename _Up>
1373         explicit(!requires(_Up __u) { value_type{__u}; })
1374         constexpr complex(const complex<_Up>& __z)
1375         : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1376 #else
1377       explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
1378       explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1379 #endif
1381 #if __cplusplus >= 201103L
1382       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1383       // DR 387. std::complex over-encapsulated.
1384       __attribute ((__abi_tag__ ("cxx11")))
1385       constexpr float
1386       real() const { return __real__ _M_value; }
1388       __attribute ((__abi_tag__ ("cxx11")))
1389       constexpr float
1390       imag() const { return __imag__ _M_value; }
1391 #else
1392       float&
1393       real() { return __real__ _M_value; }
1395       const float&
1396       real() const { return __real__ _M_value; }
1398       float&
1399       imag() { return __imag__ _M_value; }
1401       const float&
1402       imag() const { return __imag__ _M_value; }
1403 #endif
1405       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1406       // DR 387. std::complex over-encapsulated.
1407       _GLIBCXX20_CONSTEXPR void
1408       real(float __val) { __real__ _M_value = __val; }
1410       _GLIBCXX20_CONSTEXPR void
1411       imag(float __val) { __imag__ _M_value = __val; }
1413       _GLIBCXX20_CONSTEXPR complex&
1414       operator=(float __f)
1415       {
1416         _M_value = __f;
1417         return *this;
1418       }
1420       _GLIBCXX20_CONSTEXPR complex&
1421       operator+=(float __f)
1422       {
1423         _M_value += __f;
1424         return *this;
1425       }
1427       _GLIBCXX20_CONSTEXPR complex&
1428       operator-=(float __f)
1429       {
1430         _M_value -= __f;
1431         return *this;
1432       }
1434       _GLIBCXX20_CONSTEXPR complex&
1435       operator*=(float __f)
1436       {
1437         _M_value *= __f;
1438         return *this;
1439       }
1441       _GLIBCXX20_CONSTEXPR complex&
1442       operator/=(float __f)
1443       {
1444         _M_value /= __f;
1445         return *this;
1446       }
1448       // Let the compiler synthesize the copy and assignment
1449       // operator.  It always does a pretty good job.
1450 #if __cplusplus >= 201103L
1451       _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1452 #endif
1454       template<typename _Tp>
1455         _GLIBCXX20_CONSTEXPR complex&
1456         operator=(const complex<_Tp>&  __z)
1457         {
1458           __real__ _M_value = __z.real();
1459           __imag__ _M_value = __z.imag();
1460           return *this;
1461         }
1463       template<typename _Tp>
1464         _GLIBCXX20_CONSTEXPR complex&
1465         operator+=(const complex<_Tp>& __z)
1466         {
1467           _M_value += __z.__rep();
1468           return *this;
1469         }
1471       template<class _Tp>
1472         _GLIBCXX20_CONSTEXPR complex&
1473         operator-=(const complex<_Tp>& __z)
1474         {
1475           _M_value -= __z.__rep();
1476           return *this;
1477         }
1479       template<class _Tp>
1480         _GLIBCXX20_CONSTEXPR complex&
1481         operator*=(const complex<_Tp>& __z)
1482         {
1483           const _ComplexT __t = __z.__rep();
1484           _M_value *= __t;
1485           return *this;
1486         }
1488       template<class _Tp>
1489         _GLIBCXX20_CONSTEXPR complex&
1490         operator/=(const complex<_Tp>& __z)
1491         {
1492           const _ComplexT __t = __z.__rep();
1493           _M_value /= __t;
1494           return *this;
1495         }
1497       _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1499     private:
1500       _ComplexT _M_value;
1501     };
1503   /// 26.2.3  complex specializations
1504   /// complex<double> specialization
1505   template<>
1506     class complex<double>
1507     {
1508     public:
1509       typedef double value_type;
1510       typedef __complex__ double _ComplexT;
1512       _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1514       _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
1515 #if __cplusplus >= 201103L
1516       : _M_value{ __r, __i } { }
1517 #else
1518       {
1519         __real__ _M_value = __r;
1520         __imag__ _M_value = __i;
1521       }
1522 #endif
1524 #if __cplusplus >= 201103L
1525       _GLIBCXX14_CONSTEXPR complex(const complex&) = default;
1526 #endif
1528 #if __cplusplus > 202002L
1529       template<typename _Up>
1530         explicit(!requires(_Up __u) { value_type{__u}; })
1531         constexpr complex(const complex<_Up>& __z)
1532         : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1533 #else
1534       _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1535       : _M_value(__z.__rep()) { }
1537       explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
1538 #endif
1540 #if __cplusplus >= 201103L
1541       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1542       // DR 387. std::complex over-encapsulated.
1543       __attribute ((__abi_tag__ ("cxx11")))
1544       constexpr double
1545       real() const { return __real__ _M_value; }
1547       __attribute ((__abi_tag__ ("cxx11")))
1548       constexpr double
1549       imag() const { return __imag__ _M_value; }
1550 #else
1551       double&
1552       real() { return __real__ _M_value; }
1554       const double&
1555       real() const { return __real__ _M_value; }
1557       double&
1558       imag() { return __imag__ _M_value; }
1560       const double&
1561       imag() const { return __imag__ _M_value; }
1562 #endif
1564       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1565       // DR 387. std::complex over-encapsulated.
1566       _GLIBCXX20_CONSTEXPR void
1567       real(double __val) { __real__ _M_value = __val; }
1569       _GLIBCXX20_CONSTEXPR void
1570       imag(double __val) { __imag__ _M_value = __val; }
1572       _GLIBCXX20_CONSTEXPR complex&
1573       operator=(double __d)
1574       {
1575         _M_value = __d;
1576         return *this;
1577       }
1579       _GLIBCXX20_CONSTEXPR complex&
1580       operator+=(double __d)
1581       {
1582         _M_value += __d;
1583         return *this;
1584       }
1586       _GLIBCXX20_CONSTEXPR complex&
1587       operator-=(double __d)
1588       {
1589         _M_value -= __d;
1590         return *this;
1591       }
1593       _GLIBCXX20_CONSTEXPR complex&
1594       operator*=(double __d)
1595       {
1596         _M_value *= __d;
1597         return *this;
1598       }
1600       _GLIBCXX20_CONSTEXPR complex&
1601       operator/=(double __d)
1602       {
1603         _M_value /= __d;
1604         return *this;
1605       }
1607       // The compiler will synthesize this, efficiently.
1608 #if __cplusplus >= 201103L
1609       _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1610 #endif
1612       template<typename _Tp>
1613         _GLIBCXX20_CONSTEXPR complex&
1614         operator=(const complex<_Tp>& __z)
1615         {
1616           _M_value = __z.__rep();
1617           return *this;
1618         }
1620       template<typename _Tp>
1621         _GLIBCXX20_CONSTEXPR complex&
1622         operator+=(const complex<_Tp>& __z)
1623         {
1624           _M_value += __z.__rep();
1625           return *this;
1626         }
1628       template<typename _Tp>
1629         _GLIBCXX20_CONSTEXPR complex&
1630         operator-=(const complex<_Tp>& __z)
1631         {
1632           _M_value -= __z.__rep();
1633           return *this;
1634         }
1636       template<typename _Tp>
1637         _GLIBCXX20_CONSTEXPR complex&
1638         operator*=(const complex<_Tp>& __z)
1639         {
1640           const _ComplexT __t = __z.__rep();
1641           _M_value *= __t;
1642           return *this;
1643         }
1645       template<typename _Tp>
1646         _GLIBCXX20_CONSTEXPR complex&
1647         operator/=(const complex<_Tp>& __z)
1648         {
1649           const _ComplexT __t = __z.__rep();
1650           _M_value /= __t;
1651           return *this;
1652         }
1654       _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1656     private:
1657       _ComplexT _M_value;
1658     };
1660   /// 26.2.3  complex specializations
1661   /// complex<long double> specialization
1662   template<>
1663     class complex<long double>
1664     {
1665     public:
1666       typedef long double value_type;
1667       typedef __complex__ long double _ComplexT;
1669       _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
1671       _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L,
1672                                  long double __i = 0.0L)
1673 #if __cplusplus >= 201103L
1674       : _M_value{ __r, __i } { }
1675 #else
1676       {
1677         __real__ _M_value = __r;
1678         __imag__ _M_value = __i;
1679       }
1680 #endif
1682 #if __cplusplus >= 201103L
1683       _GLIBCXX14_CONSTEXPR complex(const complex&) = default;
1684 #endif
1686 #if __cplusplus > 202002L
1687       template<typename _Up>
1688         explicit(!requires(_Up __u) { value_type{__u}; })
1689         constexpr complex(const complex<_Up>& __z)
1690         : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1691 #else
1692       _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
1693       : _M_value(__z.__rep()) { }
1695       _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
1696       : _M_value(__z.__rep()) { }
1697 #endif
1699 #if __cplusplus >= 201103L
1700       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1701       // DR 387. std::complex over-encapsulated.
1702       __attribute ((__abi_tag__ ("cxx11")))
1703       constexpr long double
1704       real() const { return __real__ _M_value; }
1706       __attribute ((__abi_tag__ ("cxx11")))
1707       constexpr long double
1708       imag() const { return __imag__ _M_value; }
1709 #else
1710       long double&
1711       real() { return __real__ _M_value; }
1713       const long double&
1714       real() const { return __real__ _M_value; }
1716       long double&
1717       imag() { return __imag__ _M_value; }
1719       const long double&
1720       imag() const { return __imag__ _M_value; }
1721 #endif
1723       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1724       // DR 387. std::complex over-encapsulated.
1725       _GLIBCXX20_CONSTEXPR void
1726       real(long double __val) { __real__ _M_value = __val; }
1728       _GLIBCXX20_CONSTEXPR void
1729       imag(long double __val) { __imag__ _M_value = __val; }
1731       _GLIBCXX20_CONSTEXPR complex&
1732       operator=(long double __r)
1733       {
1734         _M_value = __r;
1735         return *this;
1736       }
1738       _GLIBCXX20_CONSTEXPR complex&
1739       operator+=(long double __r)
1740       {
1741         _M_value += __r;
1742         return *this;
1743       }
1745       _GLIBCXX20_CONSTEXPR complex&
1746       operator-=(long double __r)
1747       {
1748         _M_value -= __r;
1749         return *this;
1750       }
1752       _GLIBCXX20_CONSTEXPR complex&
1753       operator*=(long double __r)
1754       {
1755         _M_value *= __r;
1756         return *this;
1757       }
1759       _GLIBCXX20_CONSTEXPR complex&
1760       operator/=(long double __r)
1761       {
1762         _M_value /= __r;
1763         return *this;
1764       }
1766       // The compiler knows how to do this efficiently
1767 #if __cplusplus >= 201103L
1768       _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
1769 #endif
1771       template<typename _Tp>
1772         _GLIBCXX20_CONSTEXPR complex&
1773         operator=(const complex<_Tp>& __z)
1774         {
1775           _M_value = __z.__rep();
1776           return *this;
1777         }
1779       template<typename _Tp>
1780         _GLIBCXX20_CONSTEXPR complex&
1781         operator+=(const complex<_Tp>& __z)
1782         {
1783           _M_value += __z.__rep();
1784           return *this;
1785         }
1787       template<typename _Tp>
1788         _GLIBCXX20_CONSTEXPR complex&
1789         operator-=(const complex<_Tp>& __z)
1790         {
1791           _M_value -= __z.__rep();
1792           return *this;
1793         }
1795       template<typename _Tp>
1796         _GLIBCXX20_CONSTEXPR complex&
1797         operator*=(const complex<_Tp>& __z)
1798         {
1799           const _ComplexT __t = __z.__rep();
1800           _M_value *= __t;
1801           return *this;
1802         }
1804       template<typename _Tp>
1805         _GLIBCXX20_CONSTEXPR complex&
1806         operator/=(const complex<_Tp>& __z)
1807         {
1808           const _ComplexT __t = __z.__rep();
1809           _M_value /= __t;
1810           return *this;
1811         }
1813       _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
1815     private:
1816       _ComplexT _M_value;
1817     };
1819 #if __cplusplus > 202002L
1820   template<typename _Tp>
1821     struct __complex_type
1822     { };
1824 #ifdef __STDCPP_FLOAT16_T__
1825   template<>
1826     struct __complex_type<_Float16>
1827     { typedef __complex__ _Float16 type; };
1828 #endif
1830 #ifdef __STDCPP_FLOAT32_T__
1831   template<>
1832     struct __complex_type<_Float32>
1833     { typedef __complex__ _Float32 type; };
1834 #endif
1836 #ifdef __STDCPP_FLOAT64_T__
1837   template<>
1838     struct __complex_type<_Float64>
1839     { typedef __complex__ _Float64 type; };
1840 #endif
1842 #ifdef __STDCPP_FLOAT128_T__
1843   template<>
1844     struct __complex_type<_Float128>
1845     { typedef __complex__ _Float128 type; };
1846 #endif
1848 #ifdef __STDCPP_BFLOAT16_T__
1849   template<>
1850     struct __complex_type<__gnu_cxx::__bfloat16_t>
1851     { typedef __complex__ decltype(0.0bf16) type; };
1852 #endif
1854   template<typename _Tp>
1855     requires requires { typename __complex_type<_Tp>::type; }
1856     class complex<_Tp>
1857     {
1858     public:
1859       typedef _Tp value_type;
1860       typedef typename std::__complex_type<_Tp>::type _ComplexT;
1862       constexpr complex(_ComplexT __z) : _M_value(__z) { }
1864       constexpr complex(_Tp __r = _Tp(), _Tp __i = _Tp())
1865       : _M_value{ __r, __i } { }
1867       template<typename _Up>
1868         explicit(!requires(_Up __u) { value_type{__u}; })
1869         constexpr complex(const complex<_Up>& __z)
1870         : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
1872       constexpr _Tp
1873       real() const { return __real__ _M_value; }
1875       constexpr _Tp
1876       imag() const { return __imag__ _M_value; }
1878       constexpr void
1879       real(_Tp __val) { __real__ _M_value = __val; }
1881       constexpr void
1882       imag(_Tp __val) { __imag__ _M_value = __val; }
1884       constexpr complex&
1885       operator=(_Tp __f)
1886       {
1887         _M_value = __f;
1888         return *this;
1889       }
1891       constexpr complex&
1892       operator+=(_Tp __f)
1893       {
1894         _M_value += __f;
1895         return *this;
1896       }
1898       constexpr complex&
1899       operator-=(_Tp __f)
1900       {
1901         _M_value -= __f;
1902         return *this;
1903       }
1905       constexpr complex&
1906       operator*=(_Tp __f)
1907       {
1908         _M_value *= __f;
1909         return *this;
1910       }
1912       constexpr complex&
1913       operator/=(_Tp __f)
1914       {
1915         _M_value /= __f;
1916         return *this;
1917       }
1919       // Let the compiler synthesize the copy and assignment
1920       // operator.  It always does a pretty good job.
1921       constexpr complex(const complex&) = default;
1922       constexpr complex& operator=(const complex&) = default;
1924       template<typename _Up>
1925         constexpr complex&
1926         operator=(const complex<_Up>&  __z)
1927         {
1928           __real__ _M_value = __z.real();
1929           __imag__ _M_value = __z.imag();
1930           return *this;
1931         }
1933       template<typename _Up>
1934         constexpr complex&
1935         operator+=(const complex<_Up>& __z)
1936         {
1937           _M_value += __z.__rep();
1938           return *this;
1939         }
1941       template<class _Up>
1942         constexpr complex&
1943         operator-=(const complex<_Up>& __z)
1944         {
1945           _M_value -= __z.__rep();
1946           return *this;
1947         }
1949       template<class _Up>
1950         constexpr complex&
1951         operator*=(const complex<_Up>& __z)
1952         {
1953           const _ComplexT __t = __z.__rep();
1954           _M_value *= __t;
1955           return *this;
1956         }
1958       template<class _Up>
1959         constexpr complex&
1960         operator/=(const complex<_Up>& __z)
1961         {
1962           const _ComplexT __t = __z.__rep();
1963           _M_value /= __t;
1964           return *this;
1965         }
1967       constexpr _ComplexT __rep() const { return _M_value; }
1969     private:
1970       _ComplexT _M_value;
1971     };
1972 #endif
1974 #if __cplusplus <= 202002L
1975   // These bits have to be at the end of this file, so that the
1976   // specializations have all been defined.
1977   inline _GLIBCXX_CONSTEXPR
1978   complex<float>::complex(const complex<double>& __z)
1979   : _M_value(__z.__rep()) { }
1981   inline _GLIBCXX_CONSTEXPR
1982   complex<float>::complex(const complex<long double>& __z)
1983   : _M_value(__z.__rep()) { }
1985   inline _GLIBCXX_CONSTEXPR
1986   complex<double>::complex(const complex<long double>& __z)
1987   : _M_value(__z.__rep()) { }
1988 #endif
1990   // Inhibit implicit instantiations for required instantiations,
1991   // which are defined via explicit instantiations elsewhere.
1992   // NB:  This syntax is a GNU extension.
1993 #if _GLIBCXX_EXTERN_TEMPLATE
1994   extern template istream& operator>>(istream&, complex<float>&);
1995   extern template ostream& operator<<(ostream&, const complex<float>&);
1996   extern template istream& operator>>(istream&, complex<double>&);
1997   extern template ostream& operator<<(ostream&, const complex<double>&);
1998   extern template istream& operator>>(istream&, complex<long double>&);
1999   extern template ostream& operator<<(ostream&, const complex<long double>&);
2001 #ifdef _GLIBCXX_USE_WCHAR_T
2002   extern template wistream& operator>>(wistream&, complex<float>&);
2003   extern template wostream& operator<<(wostream&, const complex<float>&);
2004   extern template wistream& operator>>(wistream&, complex<double>&);
2005   extern template wostream& operator<<(wostream&, const complex<double>&);
2006   extern template wistream& operator>>(wistream&, complex<long double>&);
2007   extern template wostream& operator<<(wostream&, const complex<long double>&);
2008 #endif
2009 #endif
2011   /// @} group complex_numbers
2013 _GLIBCXX_END_NAMESPACE_VERSION
2014 } // namespace
2016 #if __cplusplus >= 201103L
2018 namespace std _GLIBCXX_VISIBILITY(default)
2020 _GLIBCXX_BEGIN_NAMESPACE_VERSION
2022   // Forward declarations.
2023   template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
2024   template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
2025   template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
2027   template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
2028   template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
2029   template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
2030   // DR 595.
2031   template<typename _Tp> _Tp               fabs(const std::complex<_Tp>&);
2033   template<typename _Tp>
2034     inline std::complex<_Tp>
2035     __complex_acos(const std::complex<_Tp>& __z)
2036     {
2037       const std::complex<_Tp> __t = std::asin(__z);
2038       const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
2039       return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
2040     }
2042 #if _GLIBCXX_USE_C99_COMPLEX_ARC
2043 #if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2044   inline __complex__ _Float16
2045   __complex_acos(__complex__ _Float16 __z)
2046   { return static_cast<__complex__ _Float16>(__builtin_cacosf(__z)); }
2048   inline __complex__ _Float16
2049   __complex_asin(__complex__ _Float16 __z)
2050   { return static_cast<__complex__ _Float16>(__builtin_casinf(__z)); }
2052   inline __complex__ _Float16
2053   __complex_atan(__complex__ _Float16 __z)
2054   { return static_cast<__complex__ _Float16>(__builtin_catanf(__z)); }
2056   inline __complex__ _Float16
2057   __complex_acosh(__complex__ _Float16 __z)
2058   { return static_cast<__complex__ _Float16>(__builtin_cacoshf(__z)); }
2060   inline __complex__ _Float16
2061   __complex_asinh(__complex__ _Float16 __z)
2062   { return static_cast<__complex__ _Float16>(__builtin_casinhf(__z)); }
2064   inline __complex__ _Float16
2065   __complex_atanh(__complex__ _Float16 __z)
2066   { return static_cast<__complex__ _Float16>(__builtin_catanhf(__z)); }
2067 #endif
2069 #if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2070   inline __complex__ _Float32
2071   __complex_acos(__complex__ _Float32 __z)
2072   { return __builtin_cacosf(__z); }
2074   inline __complex__ _Float32
2075   __complex_asin(__complex__ _Float32 __z)
2076   { return __builtin_casinf(__z); }
2078   inline __complex__ _Float32
2079   __complex_atan(__complex__ _Float32 __z)
2080   { return __builtin_catanf(__z); }
2082   inline __complex__ _Float32
2083   __complex_acosh(__complex__ _Float32 __z)
2084   { return __builtin_cacoshf(__z); }
2086   inline __complex__ _Float32
2087   __complex_asinh(__complex__ _Float32 __z)
2088   { return __builtin_casinhf(__z); }
2090   inline __complex__ _Float32
2091   __complex_atanh(__complex__ _Float32 __z)
2092   { return __builtin_catanhf(__z); }
2093 #endif
2095 #if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
2096   inline __complex__ _Float64
2097   __complex_acos(__complex__ _Float64 __z)
2098   { return __builtin_cacos(__z); }
2100   inline __complex__ _Float64
2101   __complex_asin(__complex__ _Float64 __z)
2102   { return __builtin_casin(__z); }
2104   inline __complex__ _Float64
2105   __complex_atan(__complex__ _Float64 __z)
2106   { return __builtin_catan(__z); }
2108   inline __complex__ _Float64
2109   __complex_acosh(__complex__ _Float64 __z)
2110   { return __builtin_cacosh(__z); }
2112   inline __complex__ _Float64
2113   __complex_asinh(__complex__ _Float64 __z)
2114   { return __builtin_casinh(__z); }
2116   inline __complex__ _Float64
2117   __complex_atanh(__complex__ _Float64 __z)
2118   { return __builtin_catanh(__z); }
2119 #endif
2121 #if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
2122   inline __complex__ _Float128
2123   __complex_acos(__complex__ _Float128 __z)
2124   { return __builtin_cacosl(__z); }
2126   inline __complex__ _Float128
2127   __complex_asin(__complex__ _Float128 __z)
2128   { return __builtin_casinl(__z); }
2130   inline __complex__ _Float128
2131   __complex_atan(__complex__ _Float128 __z)
2132   { return __builtin_catanl(__z); }
2134   inline __complex__ _Float128
2135   __complex_acosh(__complex__ _Float128 __z)
2136   { return __builtin_cacoshl(__z); }
2138   inline __complex__ _Float128
2139   __complex_asinh(__complex__ _Float128 __z)
2140   { return __builtin_casinhl(__z); }
2142   inline __complex__ _Float128
2143   __complex_atanh(__complex__ _Float128 __z)
2144   { return __builtin_catanhl(__z); }
2145 #elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
2146   inline __complex__ _Float128
2147   __complex_acos(__complex__ _Float128 __z)
2148   { return __builtin_cacosf128(__z); }
2150   inline __complex__ _Float128
2151   __complex_asin(__complex__ _Float128 __z)
2152   { return __builtin_casinf128(__z); }
2154   inline __complex__ _Float128
2155   __complex_atan(__complex__ _Float128 __z)
2156   { return __builtin_catanf128(__z); }
2158   inline __complex__ _Float128
2159   __complex_acosh(__complex__ _Float128 __z)
2160   { return __builtin_cacoshf128(__z); }
2162   inline __complex__ _Float128
2163   __complex_asinh(__complex__ _Float128 __z)
2164   { return __builtin_casinhf128(__z); }
2166   inline __complex__ _Float128
2167   __complex_atanh(__complex__ _Float128 __z)
2168   { return __builtin_catanhf128(__z); }
2169 #endif
2171 #if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2172   inline __complex__ decltype(0.0bf16)
2173   __complex_acos(__complex__ decltype(0.0bf16) __z)
2174   { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cacosf(__z)); }
2176   inline __complex__ decltype(0.0bf16)
2177   __complex_asin(__complex__ decltype(0.0bf16) __z)
2178   { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_casinf(__z)); }
2180   inline __complex__ decltype(0.0bf16)
2181   __complex_atan(__complex__ decltype(0.0bf16) __z)
2182   { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_catanf(__z)); }
2184   inline __complex__ decltype(0.0bf16)
2185   __complex_acosh(__complex__ decltype(0.0bf16) __z)
2186   { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cacoshf(__z)); }
2188   inline __complex__ decltype(0.0bf16)
2189   __complex_asinh(__complex__ decltype(0.0bf16) __z)
2190   { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_casinhf(__z)); }
2192   inline __complex__ decltype(0.0bf16)
2193   __complex_atanh(__complex__ decltype(0.0bf16) __z)
2194   { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_catanhf(__z)); }
2195 #endif
2196 #endif
2198 #if _GLIBCXX_USE_C99_COMPLEX_ARC
2199   inline __complex__ float
2200   __complex_acos(__complex__ float __z)
2201   { return __builtin_cacosf(__z); }
2203   inline __complex__ double
2204   __complex_acos(__complex__ double __z)
2205   { return __builtin_cacos(__z); }
2207   inline __complex__ long double
2208   __complex_acos(const __complex__ long double& __z)
2209   { return __builtin_cacosl(__z); }
2211   template<typename _Tp>
2212     inline std::complex<_Tp>
2213     acos(const std::complex<_Tp>& __z)
2214     { return __complex_acos(__z.__rep()); }
2215 #else
2216   /// acos(__z) [8.1.2].
2217   //  Effects:  Behaves the same as C99 function cacos, defined
2218   //            in subclause 7.3.5.1.
2219   template<typename _Tp>
2220     inline std::complex<_Tp>
2221     acos(const std::complex<_Tp>& __z)
2222     { return __complex_acos(__z); }
2223 #endif
2225   template<typename _Tp>
2226     inline std::complex<_Tp>
2227     __complex_asin(const std::complex<_Tp>& __z)
2228     {
2229       std::complex<_Tp> __t(-__z.imag(), __z.real());
2230       __t = std::asinh(__t);
2231       return std::complex<_Tp>(__t.imag(), -__t.real());
2232     }
2234 #if _GLIBCXX_USE_C99_COMPLEX_ARC
2235   inline __complex__ float
2236   __complex_asin(__complex__ float __z)
2237   { return __builtin_casinf(__z); }
2239   inline __complex__ double
2240   __complex_asin(__complex__ double __z)
2241   { return __builtin_casin(__z); }
2243   inline __complex__ long double
2244   __complex_asin(const __complex__ long double& __z)
2245   { return __builtin_casinl(__z); }
2247   template<typename _Tp>
2248     inline std::complex<_Tp>
2249     asin(const std::complex<_Tp>& __z)
2250     { return __complex_asin(__z.__rep()); }
2251 #else
2252   /// asin(__z) [8.1.3].
2253   //  Effects:  Behaves the same as C99 function casin, defined
2254   //            in subclause 7.3.5.2.
2255   template<typename _Tp>
2256     inline std::complex<_Tp>
2257     asin(const std::complex<_Tp>& __z)
2258     { return __complex_asin(__z); }
2259 #endif
2261   template<typename _Tp>
2262     std::complex<_Tp>
2263     __complex_atan(const std::complex<_Tp>& __z)
2264     {
2265       const _Tp __r2 = __z.real() * __z.real();
2266       const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
2268       _Tp __num = __z.imag() + _Tp(1.0);
2269       _Tp __den = __z.imag() - _Tp(1.0);
2271       __num = __r2 + __num * __num;
2272       __den = __r2 + __den * __den;
2274       return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
2275                                _Tp(0.25) * log(__num / __den));
2276     }
2278 #if _GLIBCXX_USE_C99_COMPLEX_ARC
2279   inline __complex__ float
2280   __complex_atan(__complex__ float __z)
2281   { return __builtin_catanf(__z); }
2283   inline __complex__ double
2284   __complex_atan(__complex__ double __z)
2285   { return __builtin_catan(__z); }
2287   inline __complex__ long double
2288   __complex_atan(const __complex__ long double& __z)
2289   { return __builtin_catanl(__z); }
2291   template<typename _Tp>
2292     inline std::complex<_Tp>
2293     atan(const std::complex<_Tp>& __z)
2294     { return __complex_atan(__z.__rep()); }
2295 #else
2296   /// atan(__z) [8.1.4].
2297   //  Effects:  Behaves the same as C99 function catan, defined
2298   //            in subclause 7.3.5.3.
2299   template<typename _Tp>
2300     inline std::complex<_Tp>
2301     atan(const std::complex<_Tp>& __z)
2302     { return __complex_atan(__z); }
2303 #endif
2305   template<typename _Tp>
2306     std::complex<_Tp>
2307     __complex_acosh(const std::complex<_Tp>& __z)
2308     {
2309       // Kahan's formula.
2310       return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
2311                                  + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
2312     }
2314 #if _GLIBCXX_USE_C99_COMPLEX_ARC
2315   inline __complex__ float
2316   __complex_acosh(__complex__ float __z)
2317   { return __builtin_cacoshf(__z); }
2319   inline __complex__ double
2320   __complex_acosh(__complex__ double __z)
2321   { return __builtin_cacosh(__z); }
2323   inline __complex__ long double
2324   __complex_acosh(const __complex__ long double& __z)
2325   { return __builtin_cacoshl(__z); }
2327   template<typename _Tp>
2328     inline std::complex<_Tp>
2329     acosh(const std::complex<_Tp>& __z)
2330     { return __complex_acosh(__z.__rep()); }
2331 #else
2332   /// acosh(__z) [8.1.5].
2333   //  Effects:  Behaves the same as C99 function cacosh, defined
2334   //            in subclause 7.3.6.1.
2335   template<typename _Tp>
2336     inline std::complex<_Tp>
2337     acosh(const std::complex<_Tp>& __z)
2338     { return __complex_acosh(__z); }
2339 #endif
2341   template<typename _Tp>
2342     std::complex<_Tp>
2343     __complex_asinh(const std::complex<_Tp>& __z)
2344     {
2345       std::complex<_Tp> __t((__z.real() - __z.imag())
2346                             * (__z.real() + __z.imag()) + _Tp(1.0),
2347                             _Tp(2.0) * __z.real() * __z.imag());
2348       __t = std::sqrt(__t);
2350       return std::log(__t + __z);
2351     }
2353 #if _GLIBCXX_USE_C99_COMPLEX_ARC
2354   inline __complex__ float
2355   __complex_asinh(__complex__ float __z)
2356   { return __builtin_casinhf(__z); }
2358   inline __complex__ double
2359   __complex_asinh(__complex__ double __z)
2360   { return __builtin_casinh(__z); }
2362   inline __complex__ long double
2363   __complex_asinh(const __complex__ long double& __z)
2364   { return __builtin_casinhl(__z); }
2366   template<typename _Tp>
2367     inline std::complex<_Tp>
2368     asinh(const std::complex<_Tp>& __z)
2369     { return __complex_asinh(__z.__rep()); }
2370 #else
2371   /// asinh(__z) [8.1.6].
2372   //  Effects:  Behaves the same as C99 function casin, defined
2373   //            in subclause 7.3.6.2.
2374   template<typename _Tp>
2375     inline std::complex<_Tp>
2376     asinh(const std::complex<_Tp>& __z)
2377     { return __complex_asinh(__z); }
2378 #endif
2380   template<typename _Tp>
2381     std::complex<_Tp>
2382     __complex_atanh(const std::complex<_Tp>& __z)
2383     {
2384       const _Tp __i2 = __z.imag() * __z.imag();
2385       const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
2387       _Tp __num = _Tp(1.0) + __z.real();
2388       _Tp __den = _Tp(1.0) - __z.real();
2390       __num = __i2 + __num * __num;
2391       __den = __i2 + __den * __den;
2393       return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
2394                                _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
2395     }
2397 #if _GLIBCXX_USE_C99_COMPLEX_ARC
2398   inline __complex__ float
2399   __complex_atanh(__complex__ float __z)
2400   { return __builtin_catanhf(__z); }
2402   inline __complex__ double
2403   __complex_atanh(__complex__ double __z)
2404   { return __builtin_catanh(__z); }
2406   inline __complex__ long double
2407   __complex_atanh(const __complex__ long double& __z)
2408   { return __builtin_catanhl(__z); }
2410   template<typename _Tp>
2411     inline std::complex<_Tp>
2412     atanh(const std::complex<_Tp>& __z)
2413     { return __complex_atanh(__z.__rep()); }
2414 #else
2415   /// atanh(__z) [8.1.7].
2416   //  Effects:  Behaves the same as C99 function catanh, defined
2417   //            in subclause 7.3.6.3.
2418   template<typename _Tp>
2419     inline std::complex<_Tp>
2420     atanh(const std::complex<_Tp>& __z)
2421     { return __complex_atanh(__z); }
2422 #endif
2424   template<typename _Tp>
2425     inline _Tp
2426     /// fabs(__z) [8.1.8].
2427     //  Effects:  Behaves the same as C99 function cabs, defined
2428     //            in subclause 7.3.8.1.
2429     fabs(const std::complex<_Tp>& __z)
2430     { return std::abs(__z); }
2432   /// Additional overloads [8.1.9].
2433   template<typename _Tp>
2434     inline typename __gnu_cxx::__promote<_Tp>::__type
2435     arg(_Tp __x)
2436     {
2437       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2438 #if (_GLIBCXX11_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
2439       return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
2440                                : __type();
2441 #else
2442       return std::arg(std::complex<__type>(__x));
2443 #endif
2444     }
2446   template<typename _Tp>
2447     _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
2448     imag(_Tp)
2449     { return _Tp(); }
2451   template<typename _Tp>
2452     _GLIBCXX20_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
2453     norm(_Tp __x)
2454     {
2455       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2456       return __type(__x) * __type(__x);
2457     }
2459   template<typename _Tp>
2460     _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
2461     real(_Tp __x)
2462     { return __x; }
2464   template<typename _Tp, typename _Up>
2465     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
2466     pow(const std::complex<_Tp>& __x, const _Up& __y)
2467     {
2468       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
2469       return std::pow(std::complex<__type>(__x), __type(__y));
2470     }
2472   template<typename _Tp, typename _Up>
2473     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
2474     pow(const _Tp& __x, const std::complex<_Up>& __y)
2475     {
2476       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
2477       return std::pow(__type(__x), std::complex<__type>(__y));
2478     }
2480   template<typename _Tp, typename _Up>
2481     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
2482     pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
2483     {
2484       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
2485       return std::pow(std::complex<__type>(__x),
2486                       std::complex<__type>(__y));
2487     }
2489   // Forward declarations.
2490   // DR 781.
2491   template<typename _Tp>
2492     std::complex<_Tp> proj(const std::complex<_Tp>&);
2494   // Generic implementation of std::proj, does not work for infinities.
2495   template<typename _Tp>
2496     inline std::complex<_Tp>
2497     __complex_proj(const std::complex<_Tp>& __z)
2498     { return __z; }
2500 #if _GLIBCXX_USE_C99_COMPLEX
2501   inline complex<float>
2502   __complex_proj(const complex<float>& __z)
2503   { return __builtin_cprojf(__z.__rep()); }
2505   inline complex<double>
2506   __complex_proj(const complex<double>& __z)
2507   { return __builtin_cproj(__z.__rep()); }
2509   inline complex<long double>
2510   __complex_proj(const complex<long double>& __z)
2511   { return __builtin_cprojl(__z.__rep()); }
2513 #if __cplusplus > 202002L
2514 #if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2515   inline __complex__ _Float16
2516   __complex_proj(__complex__ _Float16 __z)
2517   { return static_cast<__complex__ _Float16>(__builtin_cprojf(__z)); }
2518 #endif
2520 #if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2521   inline __complex__ _Float32
2522   __complex_proj(__complex__ _Float32 __z)
2523   { return __builtin_cprojf(__z); }
2524 #endif
2526 #if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
2527   inline __complex__ _Float64
2528   __complex_proj(__complex__ _Float64 __z)
2529   { return __builtin_cproj(__z); }
2530 #endif
2532 #if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
2533   inline __complex__ _Float128
2534   __complex_proj(__complex__ _Float128 __z)
2535   { return __builtin_cprojl(__z); }
2536 #elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
2537   inline __complex__ _Float128
2538   __complex_proj(__complex__ _Float128 __z)
2539   { return __builtin_cprojf128(__z); }
2540 #endif
2542 #if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
2543   inline __complex__ decltype(0.0bf16)
2544   __complex_proj(__complex__ decltype(0.0bf16) __z)
2545   { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cprojf(__z)); }
2546 #endif
2548   template<typename _Tp>
2549     requires requires { typename __complex_type<_Tp>::type; }
2550     inline complex<_Tp>
2551     __complex_proj(const complex<_Tp>& __z)
2552     { return __complex_proj(__z.__rep()); }
2553 #endif
2555 #elif defined _GLIBCXX_USE_C99_MATH_FUNCS
2556   inline complex<float>
2557   __complex_proj(const complex<float>& __z)
2558   {
2559     if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
2560       return complex<float>(__builtin_inff(),
2561                             __builtin_copysignf(0.0f, __z.imag()));
2562     return __z;
2563   }
2565   inline complex<double>
2566   __complex_proj(const complex<double>& __z)
2567   {
2568     if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
2569       return complex<double>(__builtin_inf(),
2570                              __builtin_copysign(0.0, __z.imag()));
2571     return __z;
2572   }
2574   inline complex<long double>
2575   __complex_proj(const complex<long double>& __z)
2576   {
2577     if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
2578       return complex<long double>(__builtin_infl(),
2579                                   __builtin_copysignl(0.0l, __z.imag()));
2580     return __z;
2581   }
2582 #endif
2584   template<typename _Tp>
2585     inline std::complex<_Tp>
2586     proj(const std::complex<_Tp>& __z)
2587     { return __complex_proj(__z); }
2589   // Overload for scalars
2590   template<typename _Tp>
2591     inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
2592     proj(_Tp __x)
2593     {
2594       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2595       return std::proj(std::complex<__type>(__x));
2596     }
2598   template<typename _Tp>
2599     inline _GLIBCXX20_CONSTEXPR
2600         std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
2601     conj(_Tp __x)
2602     {
2603       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
2604       return std::complex<__type>(__x, -__type());
2605     }
2607 #ifdef __cpp_lib_complex_udls // C++ >= 14
2609 inline namespace literals {
2610 inline namespace complex_literals {
2611 #pragma GCC diagnostic push
2612 #pragma GCC diagnostic ignored "-Wliteral-suffix"
2614   constexpr std::complex<float>
2615   operator""if(long double __num)
2616   { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
2618   constexpr std::complex<float>
2619   operator""if(unsigned long long __num)
2620   { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
2622   constexpr std::complex<double>
2623   operator""i(long double __num)
2624   { return std::complex<double>{0.0, static_cast<double>(__num)}; }
2626   constexpr std::complex<double>
2627   operator""i(unsigned long long __num)
2628   { return std::complex<double>{0.0, static_cast<double>(__num)}; }
2630   constexpr std::complex<long double>
2631   operator""il(long double __num)
2632   { return std::complex<long double>{0.0L, __num}; }
2634   constexpr std::complex<long double>
2635   operator""il(unsigned long long __num)
2636   { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; }
2638 #pragma GCC diagnostic pop
2639 } // inline namespace complex_literals
2640 } // inline namespace literals
2642 #endif // __cpp_lib_complex_udls
2644 _GLIBCXX_END_NAMESPACE_VERSION
2645 } // namespace
2647 #endif  // C++11
2649 #ifdef __clang__
2650 #pragma clang diagnostic pop
2651 #endif
2653 #endif  /* _GLIBCXX_COMPLEX */