]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.3.3/include/std/complex
Inital import
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.3.3 / include / std / complex
1 // The template and inlines for the -*- C++ -*- complex number classes.
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library.  This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2, or (at your option)
11 // any later version.
12
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this library; see the file COPYING.  If not, write to
20 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301, USA.
22
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction.  Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License.  This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
31
32 /** @file complex
33  *  This is a Standard C++ Library header.
34  */
35
36 //
37 // ISO C++ 14882: 26.2  Complex Numbers
38 // Note: this is not a conforming implementation.
39 // Initially implemented by Ulrich Drepper <drepper@cygnus.com>
40 // Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
41 //
42
43 #ifndef _GLIBCXX_COMPLEX
44 #define _GLIBCXX_COMPLEX 1
45
46 #pragma GCC system_header
47
48 #include <bits/c++config.h>
49 #include <bits/cpp_type_traits.h>
50 #include <ext/type_traits.h>
51 #include <cmath>
52 #include <sstream>
53
54 _GLIBCXX_BEGIN_NAMESPACE(std)
55
56   // Forward declarations.
57   template<typename _Tp> class complex;
58   template<> class complex<float>;
59   template<> class complex<double>;
60   template<> class complex<long double>;
61
62   ///  Return magnitude of @a z.
63   template<typename _Tp> _Tp abs(const complex<_Tp>&);
64   ///  Return phase angle of @a z.
65   template<typename _Tp> _Tp arg(const complex<_Tp>&);
66   ///  Return @a z magnitude squared.
67   template<typename _Tp> _Tp norm(const complex<_Tp>&);
68
69   ///  Return complex conjugate of @a z.
70   template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&);
71   ///  Return complex with magnitude @a rho and angle @a theta.
72   template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
73
74   // Transcendentals:
75   /// Return complex cosine of @a z.
76   template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
77   /// Return complex hyperbolic cosine of @a z.
78   template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
79   /// Return complex base e exponential of @a z.
80   template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
81   /// Return complex natural logarithm of @a z.
82   template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
83   /// Return complex base 10 logarithm of @a z.
84   template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
85   /// Return complex cosine of @a z.
86   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
87   /// Return @a x to the @a y'th power.
88   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
89   /// Return @a x to the @a y'th power.
90   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, 
91                                           const complex<_Tp>&);
92   /// Return @a x to the @a y'th power.
93   template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
94   /// Return complex sine of @a z.
95   template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
96   /// Return complex hyperbolic sine of @a z.
97   template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
98   /// Return complex square root of @a z.
99   template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
100   /// Return complex tangent of @a z.
101   template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
102   /// Return complex hyperbolic tangent of @a z.
103   template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
104   //@}
105     
106     
107   // 26.2.2  Primary template class complex
108   /**
109    *  Template to represent complex numbers.
110    *
111    *  Specializations for float, double, and long double are part of the
112    *  library.  Results with any other type are not guaranteed.
113    *
114    *  @param  Tp  Type of real and imaginary values.
115   */
116   template<typename _Tp>
117     struct complex
118     {
119       /// Value typedef.
120       typedef _Tp value_type;
121       
122       ///  Default constructor.  First parameter is x, second parameter is y.
123       ///  Unspecified parameters default to 0.
124       complex(const _Tp& = _Tp(), const _Tp & = _Tp());
125
126       // Lets the compiler synthesize the copy constructor   
127       // complex (const complex<_Tp>&);
128       ///  Copy constructor.
129       template<typename _Up>
130         complex(const complex<_Up>&);
131
132       ///  Return real part of complex number.
133       _Tp& real(); 
134       ///  Return real part of complex number.
135       const _Tp& real() const;
136       ///  Return imaginary part of complex number.
137       _Tp& imag();
138       ///  Return imaginary part of complex number.
139       const _Tp& imag() const;
140
141       /// Assign this complex number to scalar @a t.
142       complex<_Tp>& operator=(const _Tp&);
143       /// Add @a t to this complex number.
144       complex<_Tp>& operator+=(const _Tp&);
145       /// Subtract @a t from this complex number.
146       complex<_Tp>& operator-=(const _Tp&);
147       /// Multiply this complex number by @a t.
148       complex<_Tp>& operator*=(const _Tp&);
149       /// Divide this complex number by @a t.
150       complex<_Tp>& operator/=(const _Tp&);
151
152       // Lets the compiler synthesize the
153       // copy and assignment operator
154       // complex<_Tp>& operator= (const complex<_Tp>&);
155       /// Assign this complex number to complex @a z.
156       template<typename _Up>
157         complex<_Tp>& operator=(const complex<_Up>&);
158       /// Add @a z to this complex number.
159       template<typename _Up>
160         complex<_Tp>& operator+=(const complex<_Up>&);
161       /// Subtract @a z from this complex number.
162       template<typename _Up>
163         complex<_Tp>& operator-=(const complex<_Up>&);
164       /// Multiply this complex number by @a z.
165       template<typename _Up>
166         complex<_Tp>& operator*=(const complex<_Up>&);
167       /// Divide this complex number by @a z.
168       template<typename _Up>
169         complex<_Tp>& operator/=(const complex<_Up>&);
170
171       const complex& __rep() const;
172
173     private:
174       _Tp _M_real;
175       _Tp _M_imag;
176     };
177
178   template<typename _Tp>
179     inline _Tp&
180     complex<_Tp>::real() { return _M_real; }
181
182   template<typename _Tp>
183     inline const _Tp&
184     complex<_Tp>::real() const { return _M_real; }
185
186   template<typename _Tp>
187     inline _Tp&
188     complex<_Tp>::imag() { return _M_imag; }
189
190   template<typename _Tp>
191     inline const _Tp&
192     complex<_Tp>::imag() const { return _M_imag; }
193
194   template<typename _Tp>
195     inline 
196     complex<_Tp>::complex(const _Tp& __r, const _Tp& __i)
197     : _M_real(__r), _M_imag(__i) { }
198
199   template<typename _Tp>
200     template<typename _Up>
201     inline 
202     complex<_Tp>::complex(const complex<_Up>& __z)
203     : _M_real(__z.real()), _M_imag(__z.imag()) { }
204         
205   template<typename _Tp>
206     complex<_Tp>&
207     complex<_Tp>::operator=(const _Tp& __t)
208     {
209      _M_real = __t;
210      _M_imag = _Tp();
211      return *this;
212     } 
213
214   // 26.2.5/1
215   template<typename _Tp>
216     inline complex<_Tp>&
217     complex<_Tp>::operator+=(const _Tp& __t)
218     {
219       _M_real += __t;
220       return *this;
221     }
222
223   // 26.2.5/3
224   template<typename _Tp>
225     inline complex<_Tp>&
226     complex<_Tp>::operator-=(const _Tp& __t)
227     {
228       _M_real -= __t;
229       return *this;
230     }
231
232   // 26.2.5/5
233   template<typename _Tp>
234     complex<_Tp>&
235     complex<_Tp>::operator*=(const _Tp& __t)
236     {
237       _M_real *= __t;
238       _M_imag *= __t;
239       return *this;
240     }
241
242   // 26.2.5/7
243   template<typename _Tp>
244     complex<_Tp>&
245     complex<_Tp>::operator/=(const _Tp& __t)
246     {
247       _M_real /= __t;
248       _M_imag /= __t;
249       return *this;
250     }
251
252   template<typename _Tp>
253     template<typename _Up>
254     complex<_Tp>&
255     complex<_Tp>::operator=(const complex<_Up>& __z)
256     {
257       _M_real = __z.real();
258       _M_imag = __z.imag();
259       return *this;
260     }
261
262   // 26.2.5/9
263   template<typename _Tp>
264     template<typename _Up>
265     complex<_Tp>&
266     complex<_Tp>::operator+=(const complex<_Up>& __z)
267     {
268       _M_real += __z.real();
269       _M_imag += __z.imag();
270       return *this;
271     }
272
273   // 26.2.5/11
274   template<typename _Tp>
275     template<typename _Up>
276     complex<_Tp>&
277     complex<_Tp>::operator-=(const complex<_Up>& __z)
278     {
279       _M_real -= __z.real();
280       _M_imag -= __z.imag();
281       return *this;
282     }
283
284   // 26.2.5/13
285   // XXX: This is a grammar school implementation.
286   template<typename _Tp>
287     template<typename _Up>
288     complex<_Tp>&
289     complex<_Tp>::operator*=(const complex<_Up>& __z)
290     {
291       const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
292       _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
293       _M_real = __r;
294       return *this;
295     }
296
297   // 26.2.5/15
298   // XXX: This is a grammar school implementation.
299   template<typename _Tp>
300     template<typename _Up>
301     complex<_Tp>&
302     complex<_Tp>::operator/=(const complex<_Up>& __z)
303     {
304       const _Tp __r =  _M_real * __z.real() + _M_imag * __z.imag();
305       const _Tp __n = std::norm(__z);
306       _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
307       _M_real = __r / __n;
308       return *this;
309     }
310
311   template<typename _Tp>
312     inline const complex<_Tp>&
313     complex<_Tp>::__rep() const { return *this; }
314     
315   // Operators:
316   //@{
317   ///  Return new complex value @a x plus @a y.
318   template<typename _Tp>
319     inline complex<_Tp>
320     operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
321     {
322       complex<_Tp> __r = __x;
323       __r += __y;
324       return __r;
325     }
326
327   template<typename _Tp>
328     inline complex<_Tp>
329     operator+(const complex<_Tp>& __x, const _Tp& __y)
330     {
331       complex<_Tp> __r = __x;
332       __r.real() += __y;
333       return __r;
334     }
335
336   template<typename _Tp>
337     inline complex<_Tp>
338     operator+(const _Tp& __x, const complex<_Tp>& __y)
339     {
340       complex<_Tp> __r = __y;
341       __r.real() += __x;
342       return __r;
343     }
344   //@}
345
346   //@{
347   ///  Return new complex value @a x minus @a y.
348   template<typename _Tp>
349     inline complex<_Tp>
350     operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
351     {
352       complex<_Tp> __r = __x;
353       __r -= __y;
354       return __r;
355     }
356     
357   template<typename _Tp>
358     inline complex<_Tp>
359     operator-(const complex<_Tp>& __x, const _Tp& __y)
360     {
361       complex<_Tp> __r = __x;
362       __r.real() -= __y;
363       return __r;
364     }
365
366   template<typename _Tp>
367     inline complex<_Tp>
368     operator-(const _Tp& __x, const complex<_Tp>& __y)
369     {
370       complex<_Tp> __r(__x, -__y.imag());
371       __r.real() -= __y.real();
372       return __r;
373     }
374   //@}
375
376   //@{
377   ///  Return new complex value @a x times @a y.
378   template<typename _Tp>
379     inline complex<_Tp>
380     operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
381     {
382       complex<_Tp> __r = __x;
383       __r *= __y;
384       return __r;
385     }
386
387   template<typename _Tp>
388     inline complex<_Tp>
389     operator*(const complex<_Tp>& __x, const _Tp& __y)
390     {
391       complex<_Tp> __r = __x;
392       __r *= __y;
393       return __r;
394     }
395
396   template<typename _Tp>
397     inline complex<_Tp>
398     operator*(const _Tp& __x, const complex<_Tp>& __y)
399     {
400       complex<_Tp> __r = __y;
401       __r *= __x;
402       return __r;
403     }
404   //@}
405
406   //@{
407   ///  Return new complex value @a x divided by @a y.
408   template<typename _Tp>
409     inline complex<_Tp>
410     operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
411     {
412       complex<_Tp> __r = __x;
413       __r /= __y;
414       return __r;
415     }
416     
417   template<typename _Tp>
418     inline complex<_Tp>
419     operator/(const complex<_Tp>& __x, const _Tp& __y)
420     {
421       complex<_Tp> __r = __x;
422       __r /= __y;
423       return __r;
424     }
425
426   template<typename _Tp>
427     inline complex<_Tp>
428     operator/(const _Tp& __x, const complex<_Tp>& __y)
429     {
430       complex<_Tp> __r = __x;
431       __r /= __y;
432       return __r;
433     }
434   //@}
435
436   ///  Return @a x.
437   template<typename _Tp>
438     inline complex<_Tp>
439     operator+(const complex<_Tp>& __x)
440     { return __x; }
441
442   ///  Return complex negation of @a x.
443   template<typename _Tp>
444     inline complex<_Tp>
445     operator-(const complex<_Tp>& __x)
446     {  return complex<_Tp>(-__x.real(), -__x.imag()); }
447
448   //@{
449   ///  Return true if @a x is equal to @a y.
450   template<typename _Tp>
451     inline bool
452     operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
453     { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
454
455   template<typename _Tp>
456     inline bool
457     operator==(const complex<_Tp>& __x, const _Tp& __y)
458     { return __x.real() == __y && __x.imag() == _Tp(); }
459
460   template<typename _Tp>
461     inline bool
462     operator==(const _Tp& __x, const complex<_Tp>& __y)
463     { return __x == __y.real() && _Tp() == __y.imag(); }
464   //@}
465
466   //@{
467   ///  Return false if @a x is equal to @a y.
468   template<typename _Tp>
469     inline bool
470     operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
471     { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
472
473   template<typename _Tp>
474     inline bool
475     operator!=(const complex<_Tp>& __x, const _Tp& __y)
476     { return __x.real() != __y || __x.imag() != _Tp(); }
477
478   template<typename _Tp>
479     inline bool
480     operator!=(const _Tp& __x, const complex<_Tp>& __y)
481     { return __x != __y.real() || _Tp() != __y.imag(); }
482   //@}
483
484   ///  Extraction operator for complex values.
485   template<typename _Tp, typename _CharT, class _Traits>
486     basic_istream<_CharT, _Traits>&
487     operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
488     {
489       _Tp __re_x, __im_x;
490       _CharT __ch;
491       __is >> __ch;
492       if (__ch == '(') 
493         {
494           __is >> __re_x >> __ch;
495           if (__ch == ',') 
496             {
497               __is >> __im_x >> __ch;
498               if (__ch == ')') 
499                 __x = complex<_Tp>(__re_x, __im_x);
500               else
501                 __is.setstate(ios_base::failbit);
502             }
503           else if (__ch == ')') 
504             __x = __re_x;
505           else
506             __is.setstate(ios_base::failbit);
507         }
508       else 
509         {
510           __is.putback(__ch);
511           __is >> __re_x;
512           __x = __re_x;
513         }
514       return __is;
515     }
516
517   ///  Insertion operator for complex values.
518   template<typename _Tp, typename _CharT, class _Traits>
519     basic_ostream<_CharT, _Traits>&
520     operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
521     {
522       basic_ostringstream<_CharT, _Traits> __s;
523       __s.flags(__os.flags());
524       __s.imbue(__os.getloc());
525       __s.precision(__os.precision());
526       __s << '(' << __x.real() << ',' << __x.imag() << ')';
527       return __os << __s.str();
528     }
529
530   // Values
531   template<typename _Tp>
532     inline _Tp&
533     real(complex<_Tp>& __z)
534     { return __z.real(); }
535     
536   template<typename _Tp>
537     inline const _Tp&
538     real(const complex<_Tp>& __z)
539     { return __z.real(); }
540     
541   template<typename _Tp>
542     inline _Tp&
543     imag(complex<_Tp>& __z)
544     { return __z.imag(); }
545     
546   template<typename _Tp>
547     inline const _Tp&
548     imag(const complex<_Tp>& __z)
549     { return __z.imag(); }
550
551   // 26.2.7/3 abs(__z):  Returns the magnitude of __z.
552   template<typename _Tp>
553     inline _Tp
554     __complex_abs(const complex<_Tp>& __z)
555     {
556       _Tp __x = __z.real();
557       _Tp __y = __z.imag();
558       const _Tp __s = std::max(abs(__x), abs(__y));
559       if (__s == _Tp())  // well ...
560         return __s;
561       __x /= __s; 
562       __y /= __s;
563       return __s * sqrt(__x * __x + __y * __y);
564     }
565
566 #if _GLIBCXX_USE_C99_COMPLEX
567   inline float
568   __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
569
570   inline double
571   __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
572
573   inline long double
574   __complex_abs(const __complex__ long double& __z)
575   { return __builtin_cabsl(__z); }
576
577   template<typename _Tp>
578     inline _Tp
579     abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
580 #else
581   template<typename _Tp>
582     inline _Tp
583     abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
584 #endif  
585
586
587   // 26.2.7/4: arg(__z): Returns the phase angle of __z.
588   template<typename _Tp>
589     inline _Tp
590     __complex_arg(const complex<_Tp>& __z)
591     { return  atan2(__z.imag(), __z.real()); }
592
593 #if _GLIBCXX_USE_C99_COMPLEX
594   inline float
595   __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
596
597   inline double
598   __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
599
600   inline long double
601   __complex_arg(const __complex__ long double& __z)
602   { return __builtin_cargl(__z); }
603
604   template<typename _Tp>
605     inline _Tp
606     arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
607 #else
608   template<typename _Tp>
609     inline _Tp
610     arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
611 #endif
612
613   // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
614   //     As defined, norm() is -not- a norm is the common mathematical
615   //     sens used in numerics.  The helper class _Norm_helper<> tries to
616   //     distinguish between builtin floating point and the rest, so as
617   //     to deliver an answer as close as possible to the real value.
618   template<bool>
619     struct _Norm_helper
620     {
621       template<typename _Tp>
622         static inline _Tp _S_do_it(const complex<_Tp>& __z)
623         {
624           const _Tp __x = __z.real();
625           const _Tp __y = __z.imag();
626           return __x * __x + __y * __y;
627         }
628     };
629
630   template<>
631     struct _Norm_helper<true>
632     {
633       template<typename _Tp>
634         static inline _Tp _S_do_it(const complex<_Tp>& __z)
635         {
636           _Tp __res = std::abs(__z);
637           return __res * __res;
638         }
639     };
640   
641   template<typename _Tp>
642     inline _Tp
643     norm(const complex<_Tp>& __z)
644     {
645       return _Norm_helper<__is_floating<_Tp>::__value 
646         && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
647     }
648
649   template<typename _Tp>
650     inline complex<_Tp>
651     polar(const _Tp& __rho, const _Tp& __theta)
652     { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); }
653
654   template<typename _Tp>
655     inline complex<_Tp>
656     conj(const complex<_Tp>& __z)
657     { return complex<_Tp>(__z.real(), -__z.imag()); }
658   
659   // Transcendentals
660
661   // 26.2.8/1 cos(__z):  Returns the cosine of __z.
662   template<typename _Tp>
663     inline complex<_Tp>
664     __complex_cos(const complex<_Tp>& __z)
665     {
666       const _Tp __x = __z.real();
667       const _Tp __y = __z.imag();
668       return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
669     }
670
671 #if _GLIBCXX_USE_C99_COMPLEX
672   inline __complex__ float
673   __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
674
675   inline __complex__ double
676   __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
677
678   inline __complex__ long double
679   __complex_cos(const __complex__ long double& __z)
680   { return __builtin_ccosl(__z); }
681
682   template<typename _Tp>
683     inline complex<_Tp>
684     cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
685 #else
686   template<typename _Tp>
687     inline complex<_Tp>
688     cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
689 #endif
690
691   // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
692   template<typename _Tp>
693     inline complex<_Tp>
694     __complex_cosh(const complex<_Tp>& __z)
695     {
696       const _Tp __x = __z.real();
697       const _Tp __y = __z.imag();
698       return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
699     }
700
701 #if _GLIBCXX_USE_C99_COMPLEX
702   inline __complex__ float
703   __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
704
705   inline __complex__ double
706   __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
707
708   inline __complex__ long double
709   __complex_cosh(const __complex__ long double& __z)
710   { return __builtin_ccoshl(__z); }
711
712   template<typename _Tp>
713     inline complex<_Tp>
714     cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
715 #else
716   template<typename _Tp>
717     inline complex<_Tp>
718     cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
719 #endif
720
721   // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
722   template<typename _Tp>
723     inline complex<_Tp>
724     __complex_exp(const complex<_Tp>& __z)
725     { return std::polar(exp(__z.real()), __z.imag()); }
726
727 #if _GLIBCXX_USE_C99_COMPLEX
728   inline __complex__ float
729   __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
730
731   inline __complex__ double
732   __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
733
734   inline __complex__ long double
735   __complex_exp(const __complex__ long double& __z)
736   { return __builtin_cexpl(__z); }
737
738   template<typename _Tp>
739     inline complex<_Tp>
740     exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
741 #else
742   template<typename _Tp>
743     inline complex<_Tp>
744     exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
745 #endif
746
747   // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
748   //                    The branch cut is along the negative axis.
749   template<typename _Tp>
750     inline complex<_Tp>
751     __complex_log(const complex<_Tp>& __z)
752     { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
753
754 #if _GLIBCXX_USE_C99_COMPLEX
755   inline __complex__ float
756   __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
757
758   inline __complex__ double
759   __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
760
761   inline __complex__ long double
762   __complex_log(const __complex__ long double& __z)
763   { return __builtin_clogl(__z); }
764
765   template<typename _Tp>
766     inline complex<_Tp>
767     log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
768 #else
769   template<typename _Tp>
770     inline complex<_Tp>
771     log(const complex<_Tp>& __z) { return __complex_log(__z); }
772 #endif
773
774   template<typename _Tp>
775     inline complex<_Tp>
776     log10(const complex<_Tp>& __z)
777     { return std::log(__z) / log(_Tp(10.0)); }
778
779   // 26.2.8/10 sin(__z): Returns the sine of __z.
780   template<typename _Tp>
781     inline complex<_Tp>
782     __complex_sin(const complex<_Tp>& __z)
783     {
784       const _Tp __x = __z.real();
785       const _Tp __y = __z.imag();
786       return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y)); 
787     }
788
789 #if _GLIBCXX_USE_C99_COMPLEX
790   inline __complex__ float
791   __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
792
793   inline __complex__ double
794   __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
795
796   inline __complex__ long double
797   __complex_sin(const __complex__ long double& __z)
798   { return __builtin_csinl(__z); }
799
800   template<typename _Tp>
801     inline complex<_Tp>
802     sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
803 #else
804   template<typename _Tp>
805     inline complex<_Tp>
806     sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
807 #endif
808
809   // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
810   template<typename _Tp>
811     inline complex<_Tp>
812     __complex_sinh(const complex<_Tp>& __z)
813     {
814       const _Tp __x = __z.real();
815       const _Tp  __y = __z.imag();
816       return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
817     }
818
819 #if _GLIBCXX_USE_C99_COMPLEX
820   inline __complex__ float
821   __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }      
822
823   inline __complex__ double
824   __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }      
825
826   inline __complex__ long double
827   __complex_sinh(const __complex__ long double& __z)
828   { return __builtin_csinhl(__z); }      
829
830   template<typename _Tp>
831     inline complex<_Tp>
832     sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
833 #else
834   template<typename _Tp>
835     inline complex<_Tp>
836     sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
837 #endif
838
839   // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
840   //                     The branch cut is on the negative axis.
841   template<typename _Tp>
842     complex<_Tp>
843     __complex_sqrt(const complex<_Tp>& __z)
844     {
845       _Tp __x = __z.real();
846       _Tp __y = __z.imag();
847
848       if (__x == _Tp())
849         {
850           _Tp __t = sqrt(abs(__y) / 2);
851           return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
852         }
853       else
854         {
855           _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
856           _Tp __u = __t / 2;
857           return __x > _Tp()
858             ? complex<_Tp>(__u, __y / __t)
859             : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
860         }
861     }
862
863 #if _GLIBCXX_USE_C99_COMPLEX
864   inline __complex__ float
865   __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
866
867   inline __complex__ double
868   __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
869
870   inline __complex__ long double
871   __complex_sqrt(const __complex__ long double& __z)
872   { return __builtin_csqrtl(__z); }
873
874   template<typename _Tp>
875     inline complex<_Tp>
876     sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
877 #else
878   template<typename _Tp>
879     inline complex<_Tp>
880     sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
881 #endif
882
883   // 26.2.8/14 tan(__z):  Return the complex tangent of __z.
884   
885   template<typename _Tp>
886     inline complex<_Tp>
887     __complex_tan(const complex<_Tp>& __z)
888     { return std::sin(__z) / std::cos(__z); }
889
890 #if _GLIBCXX_USE_C99_COMPLEX
891   inline __complex__ float
892   __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
893
894   inline __complex__ double
895   __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
896
897   inline __complex__ long double
898   __complex_tan(const __complex__ long double& __z)
899   { return __builtin_ctanl(__z); }
900
901   template<typename _Tp>
902     inline complex<_Tp>
903     tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
904 #else
905   template<typename _Tp>
906     inline complex<_Tp>
907     tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
908 #endif
909
910
911   // 26.2.8/15 tanh(__z):  Returns the hyperbolic tangent of __z.
912   
913   template<typename _Tp>
914     inline complex<_Tp>
915     __complex_tanh(const complex<_Tp>& __z)
916     { return std::sinh(__z) / std::cosh(__z); }
917
918 #if _GLIBCXX_USE_C99_COMPLEX
919   inline __complex__ float
920   __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
921
922   inline __complex__ double
923   __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
924
925   inline __complex__ long double
926   __complex_tanh(const __complex__ long double& __z)
927   { return __builtin_ctanhl(__z); }
928
929   template<typename _Tp>
930     inline complex<_Tp>
931     tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
932 #else
933   template<typename _Tp>
934     inline complex<_Tp>
935     tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
936 #endif
937
938
939   // 26.2.8/9  pow(__x, __y): Returns the complex power base of __x
940   //                          raised to the __y-th power.  The branch
941   //                          cut is on the negative axis.
942   template<typename _Tp>
943     inline complex<_Tp>
944     pow(const complex<_Tp>& __z, int __n)
945     { return std::__pow_helper(__z, __n); }
946
947   template<typename _Tp>
948     complex<_Tp>
949     pow(const complex<_Tp>& __x, const _Tp& __y)
950     {
951 #ifndef _GLIBCXX_USE_C99_COMPLEX
952       if (__x == _Tp())
953         return _Tp();
954 #endif
955       if (__x.imag() == _Tp() && __x.real() > _Tp())
956         return pow(__x.real(), __y);
957
958       complex<_Tp> __t = std::log(__x);
959       return std::polar(exp(__y * __t.real()), __y * __t.imag());
960     }
961
962   template<typename _Tp>
963     inline complex<_Tp>
964     __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
965     { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
966
967 #if _GLIBCXX_USE_C99_COMPLEX
968   inline __complex__ float
969   __complex_pow(__complex__ float __x, __complex__ float __y)
970   { return __builtin_cpowf(__x, __y); }
971
972   inline __complex__ double
973   __complex_pow(__complex__ double __x, __complex__ double __y)
974   { return __builtin_cpow(__x, __y); }
975
976   inline __complex__ long double
977   __complex_pow(const __complex__ long double& __x,
978                 const __complex__ long double& __y)
979   { return __builtin_cpowl(__x, __y); }
980
981   template<typename _Tp>
982     inline complex<_Tp>
983     pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
984     { return __complex_pow(__x.__rep(), __y.__rep()); }
985 #else
986   template<typename _Tp>
987     inline complex<_Tp>
988     pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
989     { return __complex_pow(__x, __y); }
990 #endif
991
992   template<typename _Tp>
993     inline complex<_Tp>
994     pow(const _Tp& __x, const complex<_Tp>& __y)
995     {
996       return __x > _Tp() ? std::polar(pow(__x, __y.real()),
997                                       __y.imag() * log(__x))
998                          : std::pow(complex<_Tp>(__x, _Tp()), __y);
999     }
1000
1001   // 26.2.3  complex specializations
1002   // complex<float> specialization
1003   template<>
1004     struct complex<float>
1005     {
1006       typedef float value_type;
1007       typedef __complex__ float _ComplexT;
1008
1009       complex(_ComplexT __z) : _M_value(__z) { }
1010
1011       complex(float = 0.0f, float = 0.0f);
1012
1013       explicit complex(const complex<double>&);
1014       explicit complex(const complex<long double>&);
1015
1016       float& real();
1017       const float& real() const;
1018       float& imag();
1019       const float& imag() const;
1020
1021       complex<float>& operator=(float);
1022       complex<float>& operator+=(float);
1023       complex<float>& operator-=(float);
1024       complex<float>& operator*=(float);
1025       complex<float>& operator/=(float);
1026
1027       // Let the compiler synthesize the copy and assignment
1028       // operator.  It always does a pretty good job.
1029       // complex& operator= (const complex&);
1030       template<typename _Tp>
1031         complex<float>&operator=(const complex<_Tp>&);
1032       template<typename _Tp>
1033         complex<float>& operator+=(const complex<_Tp>&);
1034       template<class _Tp>
1035         complex<float>& operator-=(const complex<_Tp>&);
1036       template<class _Tp>
1037         complex<float>& operator*=(const complex<_Tp>&);
1038       template<class _Tp>
1039         complex<float>&operator/=(const complex<_Tp>&);
1040
1041       const _ComplexT& __rep() const { return _M_value; }
1042
1043     private:
1044       _ComplexT _M_value;
1045     };
1046
1047   inline float&
1048   complex<float>::real()
1049   { return __real__ _M_value; }
1050
1051   inline const float&
1052   complex<float>::real() const
1053   { return __real__ _M_value; }
1054
1055   inline float&
1056   complex<float>::imag()
1057   { return __imag__ _M_value; }
1058
1059   inline const float&
1060   complex<float>::imag() const
1061   { return __imag__ _M_value; }
1062
1063   inline
1064   complex<float>::complex(float __r, float __i)
1065   {
1066     __real__ _M_value = __r;
1067     __imag__ _M_value = __i;
1068   }
1069
1070   inline complex<float>&
1071   complex<float>::operator=(float __f)
1072   {
1073     __real__ _M_value = __f;
1074     __imag__ _M_value = 0.0f;
1075     return *this;
1076   }
1077
1078   inline complex<float>&
1079   complex<float>::operator+=(float __f)
1080   {
1081     __real__ _M_value += __f;
1082     return *this;
1083   }
1084
1085   inline complex<float>&
1086   complex<float>::operator-=(float __f)
1087   {
1088     __real__ _M_value -= __f;
1089     return *this;
1090   }
1091
1092   inline complex<float>&
1093   complex<float>::operator*=(float __f)
1094   {
1095     _M_value *= __f;
1096     return *this;
1097   }
1098
1099   inline complex<float>&
1100   complex<float>::operator/=(float __f)
1101   {
1102     _M_value /= __f;
1103     return *this;
1104   }
1105
1106   template<typename _Tp>
1107   inline complex<float>&
1108   complex<float>::operator=(const complex<_Tp>& __z)
1109   {
1110     __real__ _M_value = __z.real();
1111     __imag__ _M_value = __z.imag();
1112     return *this;
1113   }
1114
1115   template<typename _Tp>
1116   inline complex<float>&
1117   complex<float>::operator+=(const complex<_Tp>& __z)
1118   {
1119     __real__ _M_value += __z.real();
1120     __imag__ _M_value += __z.imag();
1121     return *this;
1122   }
1123     
1124   template<typename _Tp>
1125     inline complex<float>&
1126     complex<float>::operator-=(const complex<_Tp>& __z)
1127     {
1128      __real__ _M_value -= __z.real();
1129      __imag__ _M_value -= __z.imag();
1130      return *this;
1131     } 
1132
1133   template<typename _Tp>
1134     inline complex<float>&
1135     complex<float>::operator*=(const complex<_Tp>& __z)
1136     {
1137       _ComplexT __t;
1138       __real__ __t = __z.real();
1139       __imag__ __t = __z.imag();
1140       _M_value *= __t;
1141       return *this;
1142     }
1143
1144   template<typename _Tp>
1145     inline complex<float>&
1146     complex<float>::operator/=(const complex<_Tp>& __z)
1147     {
1148       _ComplexT __t;
1149       __real__ __t = __z.real();
1150       __imag__ __t = __z.imag();
1151       _M_value /= __t;
1152       return *this;
1153     }
1154
1155   // 26.2.3  complex specializations
1156   // complex<double> specialization
1157   template<>
1158     struct complex<double>
1159     {
1160       typedef double value_type;
1161       typedef __complex__ double _ComplexT;
1162
1163       complex(_ComplexT __z) : _M_value(__z) { }
1164
1165       complex(double = 0.0, double = 0.0);
1166
1167       complex(const complex<float>&);
1168       explicit complex(const complex<long double>&);
1169
1170       double& real();
1171       const double& real() const;
1172       double& imag();
1173       const double& imag() const;
1174
1175       complex<double>& operator=(double);
1176       complex<double>& operator+=(double);
1177       complex<double>& operator-=(double);
1178       complex<double>& operator*=(double);
1179       complex<double>& operator/=(double);
1180
1181       // The compiler will synthesize this, efficiently.
1182       // complex& operator= (const complex&);
1183       template<typename _Tp>
1184         complex<double>& operator=(const complex<_Tp>&);
1185       template<typename _Tp>
1186         complex<double>& operator+=(const complex<_Tp>&);
1187       template<typename _Tp>
1188         complex<double>& operator-=(const complex<_Tp>&);
1189       template<typename _Tp>
1190         complex<double>& operator*=(const complex<_Tp>&);
1191       template<typename _Tp>
1192         complex<double>& operator/=(const complex<_Tp>&);
1193
1194       const _ComplexT& __rep() const { return _M_value; }
1195
1196     private:
1197       _ComplexT _M_value;
1198     };
1199
1200   inline double&
1201   complex<double>::real()
1202   { return __real__ _M_value; }
1203
1204   inline const double&
1205   complex<double>::real() const
1206   { return __real__ _M_value; }
1207
1208   inline double&
1209   complex<double>::imag()
1210   { return __imag__ _M_value; }
1211
1212   inline const double&
1213   complex<double>::imag() const
1214   { return __imag__ _M_value; }
1215
1216   inline
1217   complex<double>::complex(double __r, double __i)
1218   {
1219     __real__ _M_value = __r;
1220     __imag__ _M_value = __i;
1221   }
1222
1223   inline complex<double>&
1224   complex<double>::operator=(double __d)
1225   {
1226     __real__ _M_value = __d;
1227     __imag__ _M_value = 0.0;
1228     return *this;
1229   }
1230
1231   inline complex<double>&
1232   complex<double>::operator+=(double __d)
1233   {
1234     __real__ _M_value += __d;
1235     return *this;
1236   }
1237
1238   inline complex<double>&
1239   complex<double>::operator-=(double __d)
1240   {
1241     __real__ _M_value -= __d;
1242     return *this;
1243   }
1244
1245   inline complex<double>&
1246   complex<double>::operator*=(double __d)
1247   {
1248     _M_value *= __d;
1249     return *this;
1250   }
1251
1252   inline complex<double>&
1253   complex<double>::operator/=(double __d)
1254   {
1255     _M_value /= __d;
1256     return *this;
1257   }
1258
1259   template<typename _Tp>
1260     inline complex<double>&
1261     complex<double>::operator=(const complex<_Tp>& __z)
1262     {
1263       __real__ _M_value = __z.real();
1264       __imag__ _M_value = __z.imag();
1265       return *this;
1266     }
1267     
1268   template<typename _Tp>
1269     inline complex<double>&
1270     complex<double>::operator+=(const complex<_Tp>& __z)
1271     {
1272       __real__ _M_value += __z.real();
1273       __imag__ _M_value += __z.imag();
1274       return *this;
1275     }
1276
1277   template<typename _Tp>
1278     inline complex<double>&
1279     complex<double>::operator-=(const complex<_Tp>& __z)
1280     {
1281       __real__ _M_value -= __z.real();
1282       __imag__ _M_value -= __z.imag();
1283       return *this;
1284     }
1285
1286   template<typename _Tp>
1287     inline complex<double>&
1288     complex<double>::operator*=(const complex<_Tp>& __z)
1289     {
1290       _ComplexT __t;
1291       __real__ __t = __z.real();
1292       __imag__ __t = __z.imag();
1293       _M_value *= __t;
1294       return *this;
1295     }
1296
1297   template<typename _Tp>
1298     inline complex<double>&
1299     complex<double>::operator/=(const complex<_Tp>& __z)
1300     {
1301       _ComplexT __t;
1302       __real__ __t = __z.real();
1303       __imag__ __t = __z.imag();
1304       _M_value /= __t;
1305       return *this;
1306     }
1307
1308   // 26.2.3  complex specializations
1309   // complex<long double> specialization
1310   template<>
1311     struct complex<long double>
1312     {
1313       typedef long double value_type;
1314       typedef __complex__ long double _ComplexT;
1315
1316       complex(_ComplexT __z) : _M_value(__z) { }
1317
1318       complex(long double = 0.0L, long double = 0.0L);
1319
1320       complex(const complex<float>&);
1321       complex(const complex<double>&);
1322
1323       long double& real();
1324       const long double& real() const;
1325       long double& imag();
1326       const long double& imag() const;
1327
1328       complex<long double>& operator= (long double);
1329       complex<long double>& operator+= (long double);
1330       complex<long double>& operator-= (long double);
1331       complex<long double>& operator*= (long double);
1332       complex<long double>& operator/= (long double);
1333
1334       // The compiler knows how to do this efficiently
1335       // complex& operator= (const complex&);
1336       template<typename _Tp>
1337         complex<long double>& operator=(const complex<_Tp>&);
1338       template<typename _Tp>
1339         complex<long double>& operator+=(const complex<_Tp>&);
1340       template<typename _Tp>
1341         complex<long double>& operator-=(const complex<_Tp>&);
1342       template<typename _Tp>
1343         complex<long double>& operator*=(const complex<_Tp>&);
1344       template<typename _Tp>
1345         complex<long double>& operator/=(const complex<_Tp>&);
1346
1347       const _ComplexT& __rep() const { return _M_value; }
1348
1349     private:
1350       _ComplexT _M_value;
1351     };
1352
1353   inline
1354   complex<long double>::complex(long double __r, long double __i)
1355   {
1356     __real__ _M_value = __r;
1357     __imag__ _M_value = __i;
1358   }
1359
1360   inline long double&
1361   complex<long double>::real()
1362   { return __real__ _M_value; }
1363
1364   inline const long double&
1365   complex<long double>::real() const
1366   { return __real__ _M_value; }
1367
1368   inline long double&
1369   complex<long double>::imag()
1370   { return __imag__ _M_value; }
1371
1372   inline const long double&
1373   complex<long double>::imag() const
1374   { return __imag__ _M_value; }
1375
1376   inline complex<long double>&   
1377   complex<long double>::operator=(long double __r)
1378   {
1379     __real__ _M_value = __r;
1380     __imag__ _M_value = 0.0L;
1381     return *this;
1382   }
1383
1384   inline complex<long double>&
1385   complex<long double>::operator+=(long double __r)
1386   {
1387     __real__ _M_value += __r;
1388     return *this;
1389   }
1390
1391   inline complex<long double>&
1392   complex<long double>::operator-=(long double __r)
1393   {
1394     __real__ _M_value -= __r;
1395     return *this;
1396   }
1397
1398   inline complex<long double>&
1399   complex<long double>::operator*=(long double __r)
1400   {
1401     _M_value *= __r;
1402     return *this;
1403   }
1404
1405   inline complex<long double>&
1406   complex<long double>::operator/=(long double __r)
1407   {
1408     _M_value /= __r;
1409     return *this;
1410   }
1411
1412   template<typename _Tp>
1413     inline complex<long double>&
1414     complex<long double>::operator=(const complex<_Tp>& __z)
1415     {
1416       __real__ _M_value = __z.real();
1417       __imag__ _M_value = __z.imag();
1418       return *this;
1419     }
1420
1421   template<typename _Tp>
1422     inline complex<long double>&
1423     complex<long double>::operator+=(const complex<_Tp>& __z)
1424     {
1425       __real__ _M_value += __z.real();
1426       __imag__ _M_value += __z.imag();
1427       return *this;
1428     }
1429
1430   template<typename _Tp>
1431     inline complex<long double>&
1432     complex<long double>::operator-=(const complex<_Tp>& __z)
1433     {
1434       __real__ _M_value -= __z.real();
1435       __imag__ _M_value -= __z.imag();
1436       return *this;
1437     }
1438     
1439   template<typename _Tp>
1440     inline complex<long double>&
1441     complex<long double>::operator*=(const complex<_Tp>& __z)
1442     {
1443       _ComplexT __t;
1444       __real__ __t = __z.real();
1445       __imag__ __t = __z.imag();
1446       _M_value *= __t;
1447       return *this;
1448     }
1449
1450   template<typename _Tp>
1451     inline complex<long double>&
1452     complex<long double>::operator/=(const complex<_Tp>& __z)
1453     {
1454       _ComplexT __t;
1455       __real__ __t = __z.real();
1456       __imag__ __t = __z.imag();
1457       _M_value /= __t;
1458       return *this;
1459     }
1460
1461   // These bits have to be at the end of this file, so that the
1462   // specializations have all been defined.
1463   // ??? No, they have to be there because of compiler limitation at
1464   // inlining.  It suffices that class specializations be defined.
1465   inline
1466   complex<float>::complex(const complex<double>& __z)
1467   : _M_value(__z.__rep()) { }
1468
1469   inline
1470   complex<float>::complex(const complex<long double>& __z)
1471   : _M_value(__z.__rep()) { }
1472
1473   inline
1474   complex<double>::complex(const complex<float>& __z) 
1475   : _M_value(__z.__rep()) { }
1476
1477   inline
1478   complex<double>::complex(const complex<long double>& __z)
1479   : _M_value(__z.__rep()) { }
1480
1481   inline
1482   complex<long double>::complex(const complex<float>& __z)
1483   : _M_value(__z.__rep()) { }
1484
1485   inline
1486   complex<long double>::complex(const complex<double>& __z)
1487   : _M_value(__z.__rep()) { }
1488
1489   // Inhibit implicit instantiations for required instantiations,
1490   // which are defined via explicit instantiations elsewhere.
1491   // NB:  This syntax is a GNU extension.
1492 #if _GLIBCXX_EXTERN_TEMPLATE
1493   extern template istream& operator>>(istream&, complex<float>&);
1494   extern template ostream& operator<<(ostream&, const complex<float>&);
1495   extern template istream& operator>>(istream&, complex<double>&);
1496   extern template ostream& operator<<(ostream&, const complex<double>&);
1497   extern template istream& operator>>(istream&, complex<long double>&);
1498   extern template ostream& operator<<(ostream&, const complex<long double>&);
1499
1500 #ifdef _GLIBCXX_USE_WCHAR_T
1501   extern template wistream& operator>>(wistream&, complex<float>&);
1502   extern template wostream& operator<<(wostream&, const complex<float>&);
1503   extern template wistream& operator>>(wistream&, complex<double>&);
1504   extern template wostream& operator<<(wostream&, const complex<double>&);
1505   extern template wistream& operator>>(wistream&, complex<long double>&);
1506   extern template wostream& operator<<(wostream&, const complex<long double>&);
1507 #endif
1508 #endif
1509
1510 _GLIBCXX_END_NAMESPACE
1511
1512 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
1513
1514   // See ext/type_traits.h for the primary template.
1515   template<typename _Tp, typename _Up>
1516     struct __promote_2<std::complex<_Tp>, _Up>
1517     {
1518     public:
1519       typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1520     };
1521
1522   template<typename _Tp, typename _Up>
1523     struct __promote_2<_Tp, std::complex<_Up> >
1524     {
1525     public:
1526       typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1527     };
1528   
1529   template<typename _Tp, typename _Up>
1530     struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
1531     {
1532     public:
1533       typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
1534     };
1535
1536 _GLIBCXX_END_NAMESPACE
1537
1538 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1539 #  if defined(_GLIBCXX_INCLUDE_AS_TR1)
1540 #    error C++0x header cannot be included from TR1 header
1541 #  endif
1542 #  if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
1543 #    include <tr1_impl/complex>
1544 #  else
1545 #    define _GLIBCXX_INCLUDE_AS_CXX0X
1546 #    define _GLIBCXX_BEGIN_NAMESPACE_TR1
1547 #    define _GLIBCXX_END_NAMESPACE_TR1
1548 #    define _GLIBCXX_TR1
1549 #    include <tr1_impl/complex>
1550 #    undef _GLIBCXX_TR1
1551 #    undef _GLIBCXX_END_NAMESPACE_TR1
1552 #    undef _GLIBCXX_BEGIN_NAMESPACE_TR1
1553 #    undef _GLIBCXX_INCLUDE_AS_CXX0X
1554 #  endif
1555 #endif
1556
1557 #endif  /* _GLIBCXX_COMPLEX */