xref: /freebsd/contrib/llvm-project/libcxx/include/complex (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
10b57cec5SDimitry Andric// -*- C++ -*-
20b57cec5SDimitry Andric//===--------------------------- complex ----------------------------------===//
30b57cec5SDimitry Andric//
40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70b57cec5SDimitry Andric//
80b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric
100b57cec5SDimitry Andric#ifndef _LIBCPP_COMPLEX
110b57cec5SDimitry Andric#define _LIBCPP_COMPLEX
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric/*
140b57cec5SDimitry Andric    complex synopsis
150b57cec5SDimitry Andric
160b57cec5SDimitry Andricnamespace std
170b57cec5SDimitry Andric{
180b57cec5SDimitry Andric
190b57cec5SDimitry Andrictemplate<class T>
200b57cec5SDimitry Andricclass complex
210b57cec5SDimitry Andric{
220b57cec5SDimitry Andricpublic:
230b57cec5SDimitry Andric    typedef T value_type;
240b57cec5SDimitry Andric
250b57cec5SDimitry Andric    complex(const T& re = T(), const T& im = T()); // constexpr in C++14
260b57cec5SDimitry Andric    complex(const complex&);  // constexpr in C++14
270b57cec5SDimitry Andric    template<class X> complex(const complex<X>&);  // constexpr in C++14
280b57cec5SDimitry Andric
290b57cec5SDimitry Andric    T real() const; // constexpr in C++14
300b57cec5SDimitry Andric    T imag() const; // constexpr in C++14
310b57cec5SDimitry Andric
320b57cec5SDimitry Andric    void real(T);
330b57cec5SDimitry Andric    void imag(T);
340b57cec5SDimitry Andric
350b57cec5SDimitry Andric    complex<T>& operator= (const T&);
360b57cec5SDimitry Andric    complex<T>& operator+=(const T&);
370b57cec5SDimitry Andric    complex<T>& operator-=(const T&);
380b57cec5SDimitry Andric    complex<T>& operator*=(const T&);
390b57cec5SDimitry Andric    complex<T>& operator/=(const T&);
400b57cec5SDimitry Andric
410b57cec5SDimitry Andric    complex& operator=(const complex&);
420b57cec5SDimitry Andric    template<class X> complex<T>& operator= (const complex<X>&);
430b57cec5SDimitry Andric    template<class X> complex<T>& operator+=(const complex<X>&);
440b57cec5SDimitry Andric    template<class X> complex<T>& operator-=(const complex<X>&);
450b57cec5SDimitry Andric    template<class X> complex<T>& operator*=(const complex<X>&);
460b57cec5SDimitry Andric    template<class X> complex<T>& operator/=(const complex<X>&);
470b57cec5SDimitry Andric};
480b57cec5SDimitry Andric
490b57cec5SDimitry Andrictemplate<>
500b57cec5SDimitry Andricclass complex<float>
510b57cec5SDimitry Andric{
520b57cec5SDimitry Andricpublic:
530b57cec5SDimitry Andric    typedef float value_type;
540b57cec5SDimitry Andric
550b57cec5SDimitry Andric    constexpr complex(float re = 0.0f, float im = 0.0f);
560b57cec5SDimitry Andric    explicit constexpr complex(const complex<double>&);
570b57cec5SDimitry Andric    explicit constexpr complex(const complex<long double>&);
580b57cec5SDimitry Andric
590b57cec5SDimitry Andric    constexpr float real() const;
600b57cec5SDimitry Andric    void real(float);
610b57cec5SDimitry Andric    constexpr float imag() const;
620b57cec5SDimitry Andric    void imag(float);
630b57cec5SDimitry Andric
640b57cec5SDimitry Andric    complex<float>& operator= (float);
650b57cec5SDimitry Andric    complex<float>& operator+=(float);
660b57cec5SDimitry Andric    complex<float>& operator-=(float);
670b57cec5SDimitry Andric    complex<float>& operator*=(float);
680b57cec5SDimitry Andric    complex<float>& operator/=(float);
690b57cec5SDimitry Andric
700b57cec5SDimitry Andric    complex<float>& operator=(const complex<float>&);
710b57cec5SDimitry Andric    template<class X> complex<float>& operator= (const complex<X>&);
720b57cec5SDimitry Andric    template<class X> complex<float>& operator+=(const complex<X>&);
730b57cec5SDimitry Andric    template<class X> complex<float>& operator-=(const complex<X>&);
740b57cec5SDimitry Andric    template<class X> complex<float>& operator*=(const complex<X>&);
750b57cec5SDimitry Andric    template<class X> complex<float>& operator/=(const complex<X>&);
760b57cec5SDimitry Andric};
770b57cec5SDimitry Andric
780b57cec5SDimitry Andrictemplate<>
790b57cec5SDimitry Andricclass complex<double>
800b57cec5SDimitry Andric{
810b57cec5SDimitry Andricpublic:
820b57cec5SDimitry Andric    typedef double value_type;
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric    constexpr complex(double re = 0.0, double im = 0.0);
850b57cec5SDimitry Andric    constexpr complex(const complex<float>&);
860b57cec5SDimitry Andric    explicit constexpr complex(const complex<long double>&);
870b57cec5SDimitry Andric
880b57cec5SDimitry Andric    constexpr double real() const;
890b57cec5SDimitry Andric    void real(double);
900b57cec5SDimitry Andric    constexpr double imag() const;
910b57cec5SDimitry Andric    void imag(double);
920b57cec5SDimitry Andric
930b57cec5SDimitry Andric    complex<double>& operator= (double);
940b57cec5SDimitry Andric    complex<double>& operator+=(double);
950b57cec5SDimitry Andric    complex<double>& operator-=(double);
960b57cec5SDimitry Andric    complex<double>& operator*=(double);
970b57cec5SDimitry Andric    complex<double>& operator/=(double);
980b57cec5SDimitry Andric    complex<double>& operator=(const complex<double>&);
990b57cec5SDimitry Andric
1000b57cec5SDimitry Andric    template<class X> complex<double>& operator= (const complex<X>&);
1010b57cec5SDimitry Andric    template<class X> complex<double>& operator+=(const complex<X>&);
1020b57cec5SDimitry Andric    template<class X> complex<double>& operator-=(const complex<X>&);
1030b57cec5SDimitry Andric    template<class X> complex<double>& operator*=(const complex<X>&);
1040b57cec5SDimitry Andric    template<class X> complex<double>& operator/=(const complex<X>&);
1050b57cec5SDimitry Andric};
1060b57cec5SDimitry Andric
1070b57cec5SDimitry Andrictemplate<>
1080b57cec5SDimitry Andricclass complex<long double>
1090b57cec5SDimitry Andric{
1100b57cec5SDimitry Andricpublic:
1110b57cec5SDimitry Andric    typedef long double value_type;
1120b57cec5SDimitry Andric
1130b57cec5SDimitry Andric    constexpr complex(long double re = 0.0L, long double im = 0.0L);
1140b57cec5SDimitry Andric    constexpr complex(const complex<float>&);
1150b57cec5SDimitry Andric    constexpr complex(const complex<double>&);
1160b57cec5SDimitry Andric
1170b57cec5SDimitry Andric    constexpr long double real() const;
1180b57cec5SDimitry Andric    void real(long double);
1190b57cec5SDimitry Andric    constexpr long double imag() const;
1200b57cec5SDimitry Andric    void imag(long double);
1210b57cec5SDimitry Andric
1220b57cec5SDimitry Andric    complex<long double>& operator=(const complex<long double>&);
1230b57cec5SDimitry Andric    complex<long double>& operator= (long double);
1240b57cec5SDimitry Andric    complex<long double>& operator+=(long double);
1250b57cec5SDimitry Andric    complex<long double>& operator-=(long double);
1260b57cec5SDimitry Andric    complex<long double>& operator*=(long double);
1270b57cec5SDimitry Andric    complex<long double>& operator/=(long double);
1280b57cec5SDimitry Andric
1290b57cec5SDimitry Andric    template<class X> complex<long double>& operator= (const complex<X>&);
1300b57cec5SDimitry Andric    template<class X> complex<long double>& operator+=(const complex<X>&);
1310b57cec5SDimitry Andric    template<class X> complex<long double>& operator-=(const complex<X>&);
1320b57cec5SDimitry Andric    template<class X> complex<long double>& operator*=(const complex<X>&);
1330b57cec5SDimitry Andric    template<class X> complex<long double>& operator/=(const complex<X>&);
1340b57cec5SDimitry Andric};
1350b57cec5SDimitry Andric
1360b57cec5SDimitry Andric// 26.3.6 operators:
1370b57cec5SDimitry Andrictemplate<class T> complex<T> operator+(const complex<T>&, const complex<T>&);
1380b57cec5SDimitry Andrictemplate<class T> complex<T> operator+(const complex<T>&, const T&);
1390b57cec5SDimitry Andrictemplate<class T> complex<T> operator+(const T&, const complex<T>&);
1400b57cec5SDimitry Andrictemplate<class T> complex<T> operator-(const complex<T>&, const complex<T>&);
1410b57cec5SDimitry Andrictemplate<class T> complex<T> operator-(const complex<T>&, const T&);
1420b57cec5SDimitry Andrictemplate<class T> complex<T> operator-(const T&, const complex<T>&);
1430b57cec5SDimitry Andrictemplate<class T> complex<T> operator*(const complex<T>&, const complex<T>&);
1440b57cec5SDimitry Andrictemplate<class T> complex<T> operator*(const complex<T>&, const T&);
1450b57cec5SDimitry Andrictemplate<class T> complex<T> operator*(const T&, const complex<T>&);
1460b57cec5SDimitry Andrictemplate<class T> complex<T> operator/(const complex<T>&, const complex<T>&);
1470b57cec5SDimitry Andrictemplate<class T> complex<T> operator/(const complex<T>&, const T&);
1480b57cec5SDimitry Andrictemplate<class T> complex<T> operator/(const T&, const complex<T>&);
1490b57cec5SDimitry Andrictemplate<class T> complex<T> operator+(const complex<T>&);
1500b57cec5SDimitry Andrictemplate<class T> complex<T> operator-(const complex<T>&);
1510b57cec5SDimitry Andrictemplate<class T> bool operator==(const complex<T>&, const complex<T>&); // constexpr in C++14
1520b57cec5SDimitry Andrictemplate<class T> bool operator==(const complex<T>&, const T&); // constexpr in C++14
1530b57cec5SDimitry Andrictemplate<class T> bool operator==(const T&, const complex<T>&); // constexpr in C++14
1540b57cec5SDimitry Andrictemplate<class T> bool operator!=(const complex<T>&, const complex<T>&); // constexpr in C++14
1550b57cec5SDimitry Andrictemplate<class T> bool operator!=(const complex<T>&, const T&); // constexpr in C++14
1560b57cec5SDimitry Andrictemplate<class T> bool operator!=(const T&, const complex<T>&); // constexpr in C++14
1570b57cec5SDimitry Andric
1580b57cec5SDimitry Andrictemplate<class T, class charT, class traits>
1590b57cec5SDimitry Andric  basic_istream<charT, traits>&
1600b57cec5SDimitry Andric  operator>>(basic_istream<charT, traits>&, complex<T>&);
1610b57cec5SDimitry Andrictemplate<class T, class charT, class traits>
1620b57cec5SDimitry Andric  basic_ostream<charT, traits>&
1630b57cec5SDimitry Andric  operator<<(basic_ostream<charT, traits>&, const complex<T>&);
1640b57cec5SDimitry Andric
1650b57cec5SDimitry Andric// 26.3.7 values:
1660b57cec5SDimitry Andric
1670b57cec5SDimitry Andrictemplate<class T>              T real(const complex<T>&); // constexpr in C++14
1680b57cec5SDimitry Andric                     long double real(long double);       // constexpr in C++14
1690b57cec5SDimitry Andric                          double real(double);            // constexpr in C++14
1700b57cec5SDimitry Andrictemplate<Integral T>      double real(T);                 // constexpr in C++14
1710b57cec5SDimitry Andric                          float  real(float);             // constexpr in C++14
1720b57cec5SDimitry Andric
1730b57cec5SDimitry Andrictemplate<class T>              T imag(const complex<T>&); // constexpr in C++14
1740b57cec5SDimitry Andric                     long double imag(long double);       // constexpr in C++14
1750b57cec5SDimitry Andric                          double imag(double);            // constexpr in C++14
1760b57cec5SDimitry Andrictemplate<Integral T>      double imag(T);                 // constexpr in C++14
1770b57cec5SDimitry Andric                          float  imag(float);             // constexpr in C++14
1780b57cec5SDimitry Andric
1790b57cec5SDimitry Andrictemplate<class T> T abs(const complex<T>&);
1800b57cec5SDimitry Andric
1810b57cec5SDimitry Andrictemplate<class T>              T arg(const complex<T>&);
1820b57cec5SDimitry Andric                     long double arg(long double);
1830b57cec5SDimitry Andric                          double arg(double);
1840b57cec5SDimitry Andrictemplate<Integral T>      double arg(T);
1850b57cec5SDimitry Andric                          float  arg(float);
1860b57cec5SDimitry Andric
1870b57cec5SDimitry Andrictemplate<class T>              T norm(const complex<T>&);
1880b57cec5SDimitry Andric                     long double norm(long double);
1890b57cec5SDimitry Andric                          double norm(double);
1900b57cec5SDimitry Andrictemplate<Integral T>      double norm(T);
1910b57cec5SDimitry Andric                          float  norm(float);
1920b57cec5SDimitry Andric
1930b57cec5SDimitry Andrictemplate<class T>      complex<T>           conj(const complex<T>&);
1940b57cec5SDimitry Andric                       complex<long double> conj(long double);
1950b57cec5SDimitry Andric                       complex<double>      conj(double);
1960b57cec5SDimitry Andrictemplate<Integral T>   complex<double>      conj(T);
1970b57cec5SDimitry Andric                       complex<float>       conj(float);
1980b57cec5SDimitry Andric
1990b57cec5SDimitry Andrictemplate<class T>    complex<T>           proj(const complex<T>&);
2000b57cec5SDimitry Andric                     complex<long double> proj(long double);
2010b57cec5SDimitry Andric                     complex<double>      proj(double);
2020b57cec5SDimitry Andrictemplate<Integral T> complex<double>      proj(T);
2030b57cec5SDimitry Andric                     complex<float>       proj(float);
2040b57cec5SDimitry Andric
2050b57cec5SDimitry Andrictemplate<class T> complex<T> polar(const T&, const T& = T());
2060b57cec5SDimitry Andric
2070b57cec5SDimitry Andric// 26.3.8 transcendentals:
2080b57cec5SDimitry Andrictemplate<class T> complex<T> acos(const complex<T>&);
2090b57cec5SDimitry Andrictemplate<class T> complex<T> asin(const complex<T>&);
2100b57cec5SDimitry Andrictemplate<class T> complex<T> atan(const complex<T>&);
2110b57cec5SDimitry Andrictemplate<class T> complex<T> acosh(const complex<T>&);
2120b57cec5SDimitry Andrictemplate<class T> complex<T> asinh(const complex<T>&);
2130b57cec5SDimitry Andrictemplate<class T> complex<T> atanh(const complex<T>&);
2140b57cec5SDimitry Andrictemplate<class T> complex<T> cos (const complex<T>&);
2150b57cec5SDimitry Andrictemplate<class T> complex<T> cosh (const complex<T>&);
2160b57cec5SDimitry Andrictemplate<class T> complex<T> exp (const complex<T>&);
2170b57cec5SDimitry Andrictemplate<class T> complex<T> log (const complex<T>&);
2180b57cec5SDimitry Andrictemplate<class T> complex<T> log10(const complex<T>&);
2190b57cec5SDimitry Andric
2200b57cec5SDimitry Andrictemplate<class T> complex<T> pow(const complex<T>&, const T&);
2210b57cec5SDimitry Andrictemplate<class T> complex<T> pow(const complex<T>&, const complex<T>&);
2220b57cec5SDimitry Andrictemplate<class T> complex<T> pow(const T&, const complex<T>&);
2230b57cec5SDimitry Andric
2240b57cec5SDimitry Andrictemplate<class T> complex<T> sin (const complex<T>&);
2250b57cec5SDimitry Andrictemplate<class T> complex<T> sinh (const complex<T>&);
2260b57cec5SDimitry Andrictemplate<class T> complex<T> sqrt (const complex<T>&);
2270b57cec5SDimitry Andrictemplate<class T> complex<T> tan (const complex<T>&);
2280b57cec5SDimitry Andrictemplate<class T> complex<T> tanh (const complex<T>&);
2290b57cec5SDimitry Andric
2300b57cec5SDimitry Andric}  // std
2310b57cec5SDimitry Andric
2320b57cec5SDimitry Andric*/
2330b57cec5SDimitry Andric
2340b57cec5SDimitry Andric#include <__config>
2350b57cec5SDimitry Andric#include <type_traits>
2360b57cec5SDimitry Andric#include <stdexcept>
2370b57cec5SDimitry Andric#include <cmath>
2385ffd83dbSDimitry Andric#include <iosfwd>
2390b57cec5SDimitry Andric#include <version>
2400b57cec5SDimitry Andric
241*e8d8bef9SDimitry Andric#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
242*e8d8bef9SDimitry Andric#   include <sstream> // for std::basic_ostringstream
243*e8d8bef9SDimitry Andric#endif
244*e8d8bef9SDimitry Andric
2450b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2460b57cec5SDimitry Andric#pragma GCC system_header
2470b57cec5SDimitry Andric#endif
2480b57cec5SDimitry Andric
2490b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
2500b57cec5SDimitry Andric
2510b57cec5SDimitry Andrictemplate<class _Tp> class _LIBCPP_TEMPLATE_VIS complex;
2520b57cec5SDimitry Andric
2530b57cec5SDimitry Andrictemplate<class _Tp> complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
2540b57cec5SDimitry Andrictemplate<class _Tp> complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
2550b57cec5SDimitry Andric
2560b57cec5SDimitry Andrictemplate<class _Tp>
2570b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS complex
2580b57cec5SDimitry Andric{
2590b57cec5SDimitry Andricpublic:
2600b57cec5SDimitry Andric    typedef _Tp value_type;
2610b57cec5SDimitry Andricprivate:
2620b57cec5SDimitry Andric    value_type __re_;
2630b57cec5SDimitry Andric    value_type __im_;
2640b57cec5SDimitry Andricpublic:
2650b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2660b57cec5SDimitry Andric    complex(const value_type& __re = value_type(), const value_type& __im = value_type())
2670b57cec5SDimitry Andric        : __re_(__re), __im_(__im) {}
2680b57cec5SDimitry Andric    template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2690b57cec5SDimitry Andric    complex(const complex<_Xp>& __c)
2700b57cec5SDimitry Andric        : __re_(__c.real()), __im_(__c.imag()) {}
2710b57cec5SDimitry Andric
2720b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type real() const {return __re_;}
2730b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type imag() const {return __im_;}
2740b57cec5SDimitry Andric
2750b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
2760b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
2770b57cec5SDimitry Andric
2780b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY complex& operator= (const value_type& __re)
2790b57cec5SDimitry Andric        {__re_ = __re; __im_ = value_type(); return *this;}
2800b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY complex& operator+=(const value_type& __re) {__re_ += __re; return *this;}
2810b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY complex& operator-=(const value_type& __re) {__re_ -= __re; return *this;}
2820b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY complex& operator*=(const value_type& __re) {__re_ *= __re; __im_ *= __re; return *this;}
2830b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY complex& operator/=(const value_type& __re) {__re_ /= __re; __im_ /= __re; return *this;}
2840b57cec5SDimitry Andric
2850b57cec5SDimitry Andric    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
2860b57cec5SDimitry Andric        {
2870b57cec5SDimitry Andric            __re_ = __c.real();
2880b57cec5SDimitry Andric            __im_ = __c.imag();
2890b57cec5SDimitry Andric            return *this;
2900b57cec5SDimitry Andric        }
2910b57cec5SDimitry Andric    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
2920b57cec5SDimitry Andric        {
2930b57cec5SDimitry Andric            __re_ += __c.real();
2940b57cec5SDimitry Andric            __im_ += __c.imag();
2950b57cec5SDimitry Andric            return *this;
2960b57cec5SDimitry Andric        }
2970b57cec5SDimitry Andric    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
2980b57cec5SDimitry Andric        {
2990b57cec5SDimitry Andric            __re_ -= __c.real();
3000b57cec5SDimitry Andric            __im_ -= __c.imag();
3010b57cec5SDimitry Andric            return *this;
3020b57cec5SDimitry Andric        }
3030b57cec5SDimitry Andric    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
3040b57cec5SDimitry Andric        {
3050b57cec5SDimitry Andric            *this = *this * complex(__c.real(), __c.imag());
3060b57cec5SDimitry Andric            return *this;
3070b57cec5SDimitry Andric        }
3080b57cec5SDimitry Andric    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
3090b57cec5SDimitry Andric        {
3100b57cec5SDimitry Andric            *this = *this / complex(__c.real(), __c.imag());
3110b57cec5SDimitry Andric            return *this;
3120b57cec5SDimitry Andric        }
3130b57cec5SDimitry Andric};
3140b57cec5SDimitry Andric
3150b57cec5SDimitry Andrictemplate<> class _LIBCPP_TEMPLATE_VIS complex<double>;
3160b57cec5SDimitry Andrictemplate<> class _LIBCPP_TEMPLATE_VIS complex<long double>;
3170b57cec5SDimitry Andric
3180b57cec5SDimitry Andrictemplate<>
3190b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS complex<float>
3200b57cec5SDimitry Andric{
3210b57cec5SDimitry Andric    float __re_;
3220b57cec5SDimitry Andric    float __im_;
3230b57cec5SDimitry Andricpublic:
3240b57cec5SDimitry Andric    typedef float value_type;
3250b57cec5SDimitry Andric
3260b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(float __re = 0.0f, float __im = 0.0f)
3270b57cec5SDimitry Andric        : __re_(__re), __im_(__im) {}
3280b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3290b57cec5SDimitry Andric    explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
3300b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3310b57cec5SDimitry Andric    explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
3320b57cec5SDimitry Andric
3330b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float real() const {return __re_;}
3340b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float imag() const {return __im_;}
3350b57cec5SDimitry Andric
3360b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
3370b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
3380b57cec5SDimitry Andric
3390b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY complex& operator= (float __re)
3400b57cec5SDimitry Andric        {__re_ = __re; __im_ = value_type(); return *this;}
3410b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY complex& operator+=(float __re) {__re_ += __re; return *this;}
3420b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY complex& operator-=(float __re) {__re_ -= __re; return *this;}
3430b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY complex& operator*=(float __re) {__re_ *= __re; __im_ *= __re; return *this;}
3440b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY complex& operator/=(float __re) {__re_ /= __re; __im_ /= __re; return *this;}
3450b57cec5SDimitry Andric
3460b57cec5SDimitry Andric    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
3470b57cec5SDimitry Andric        {
3480b57cec5SDimitry Andric            __re_ = __c.real();
3490b57cec5SDimitry Andric            __im_ = __c.imag();
3500b57cec5SDimitry Andric            return *this;
3510b57cec5SDimitry Andric        }
3520b57cec5SDimitry Andric    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
3530b57cec5SDimitry Andric        {
3540b57cec5SDimitry Andric            __re_ += __c.real();
3550b57cec5SDimitry Andric            __im_ += __c.imag();
3560b57cec5SDimitry Andric            return *this;
3570b57cec5SDimitry Andric        }
3580b57cec5SDimitry Andric    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
3590b57cec5SDimitry Andric        {
3600b57cec5SDimitry Andric            __re_ -= __c.real();
3610b57cec5SDimitry Andric            __im_ -= __c.imag();
3620b57cec5SDimitry Andric            return *this;
3630b57cec5SDimitry Andric        }
3640b57cec5SDimitry Andric    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
3650b57cec5SDimitry Andric        {
3660b57cec5SDimitry Andric            *this = *this * complex(__c.real(), __c.imag());
3670b57cec5SDimitry Andric            return *this;
3680b57cec5SDimitry Andric        }
3690b57cec5SDimitry Andric    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
3700b57cec5SDimitry Andric        {
3710b57cec5SDimitry Andric            *this = *this / complex(__c.real(), __c.imag());
3720b57cec5SDimitry Andric            return *this;
3730b57cec5SDimitry Andric        }
3740b57cec5SDimitry Andric};
3750b57cec5SDimitry Andric
3760b57cec5SDimitry Andrictemplate<>
3770b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS complex<double>
3780b57cec5SDimitry Andric{
3790b57cec5SDimitry Andric    double __re_;
3800b57cec5SDimitry Andric    double __im_;
3810b57cec5SDimitry Andricpublic:
3820b57cec5SDimitry Andric    typedef double value_type;
3830b57cec5SDimitry Andric
3840b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0)
3850b57cec5SDimitry Andric        : __re_(__re), __im_(__im) {}
3860b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3870b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
3880b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
3890b57cec5SDimitry Andric    explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
3900b57cec5SDimitry Andric
3910b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double real() const {return __re_;}
3920b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double imag() const {return __im_;}
3930b57cec5SDimitry Andric
3940b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
3950b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
3960b57cec5SDimitry Andric
3970b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY complex& operator= (double __re)
3980b57cec5SDimitry Andric        {__re_ = __re; __im_ = value_type(); return *this;}
3990b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY complex& operator+=(double __re) {__re_ += __re; return *this;}
4000b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY complex& operator-=(double __re) {__re_ -= __re; return *this;}
4010b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;}
4020b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;}
4030b57cec5SDimitry Andric
4040b57cec5SDimitry Andric    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
4050b57cec5SDimitry Andric        {
4060b57cec5SDimitry Andric            __re_ = __c.real();
4070b57cec5SDimitry Andric            __im_ = __c.imag();
4080b57cec5SDimitry Andric            return *this;
4090b57cec5SDimitry Andric        }
4100b57cec5SDimitry Andric    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
4110b57cec5SDimitry Andric        {
4120b57cec5SDimitry Andric            __re_ += __c.real();
4130b57cec5SDimitry Andric            __im_ += __c.imag();
4140b57cec5SDimitry Andric            return *this;
4150b57cec5SDimitry Andric        }
4160b57cec5SDimitry Andric    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
4170b57cec5SDimitry Andric        {
4180b57cec5SDimitry Andric            __re_ -= __c.real();
4190b57cec5SDimitry Andric            __im_ -= __c.imag();
4200b57cec5SDimitry Andric            return *this;
4210b57cec5SDimitry Andric        }
4220b57cec5SDimitry Andric    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
4230b57cec5SDimitry Andric        {
4240b57cec5SDimitry Andric            *this = *this * complex(__c.real(), __c.imag());
4250b57cec5SDimitry Andric            return *this;
4260b57cec5SDimitry Andric        }
4270b57cec5SDimitry Andric    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
4280b57cec5SDimitry Andric        {
4290b57cec5SDimitry Andric            *this = *this / complex(__c.real(), __c.imag());
4300b57cec5SDimitry Andric            return *this;
4310b57cec5SDimitry Andric        }
4320b57cec5SDimitry Andric};
4330b57cec5SDimitry Andric
4340b57cec5SDimitry Andrictemplate<>
4350b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS complex<long double>
4360b57cec5SDimitry Andric{
4370b57cec5SDimitry Andric    long double __re_;
4380b57cec5SDimitry Andric    long double __im_;
4390b57cec5SDimitry Andricpublic:
4400b57cec5SDimitry Andric    typedef long double value_type;
4410b57cec5SDimitry Andric
4420b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L)
4430b57cec5SDimitry Andric        : __re_(__re), __im_(__im) {}
4440b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
4450b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
4460b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
4470b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
4480b57cec5SDimitry Andric
4490b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double real() const {return __re_;}
4500b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double imag() const {return __im_;}
4510b57cec5SDimitry Andric
4520b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
4530b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
4540b57cec5SDimitry Andric
4550b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY complex& operator= (long double __re)
4560b57cec5SDimitry Andric        {__re_ = __re; __im_ = value_type(); return *this;}
4570b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY complex& operator+=(long double __re) {__re_ += __re; return *this;}
4580b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY complex& operator-=(long double __re) {__re_ -= __re; return *this;}
4590b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY complex& operator*=(long double __re) {__re_ *= __re; __im_ *= __re; return *this;}
4600b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY complex& operator/=(long double __re) {__re_ /= __re; __im_ /= __re; return *this;}
4610b57cec5SDimitry Andric
4620b57cec5SDimitry Andric    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
4630b57cec5SDimitry Andric        {
4640b57cec5SDimitry Andric            __re_ = __c.real();
4650b57cec5SDimitry Andric            __im_ = __c.imag();
4660b57cec5SDimitry Andric            return *this;
4670b57cec5SDimitry Andric        }
4680b57cec5SDimitry Andric    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
4690b57cec5SDimitry Andric        {
4700b57cec5SDimitry Andric            __re_ += __c.real();
4710b57cec5SDimitry Andric            __im_ += __c.imag();
4720b57cec5SDimitry Andric            return *this;
4730b57cec5SDimitry Andric        }
4740b57cec5SDimitry Andric    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
4750b57cec5SDimitry Andric        {
4760b57cec5SDimitry Andric            __re_ -= __c.real();
4770b57cec5SDimitry Andric            __im_ -= __c.imag();
4780b57cec5SDimitry Andric            return *this;
4790b57cec5SDimitry Andric        }
4800b57cec5SDimitry Andric    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
4810b57cec5SDimitry Andric        {
4820b57cec5SDimitry Andric            *this = *this * complex(__c.real(), __c.imag());
4830b57cec5SDimitry Andric            return *this;
4840b57cec5SDimitry Andric        }
4850b57cec5SDimitry Andric    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
4860b57cec5SDimitry Andric        {
4870b57cec5SDimitry Andric            *this = *this / complex(__c.real(), __c.imag());
4880b57cec5SDimitry Andric            return *this;
4890b57cec5SDimitry Andric        }
4900b57cec5SDimitry Andric};
4910b57cec5SDimitry Andric
4920b57cec5SDimitry Andricinline
4930b57cec5SDimitry Andric_LIBCPP_CONSTEXPR
4940b57cec5SDimitry Andriccomplex<float>::complex(const complex<double>& __c)
4950b57cec5SDimitry Andric    : __re_(__c.real()), __im_(__c.imag()) {}
4960b57cec5SDimitry Andric
4970b57cec5SDimitry Andricinline
4980b57cec5SDimitry Andric_LIBCPP_CONSTEXPR
4990b57cec5SDimitry Andriccomplex<float>::complex(const complex<long double>& __c)
5000b57cec5SDimitry Andric    : __re_(__c.real()), __im_(__c.imag()) {}
5010b57cec5SDimitry Andric
5020b57cec5SDimitry Andricinline
5030b57cec5SDimitry Andric_LIBCPP_CONSTEXPR
5040b57cec5SDimitry Andriccomplex<double>::complex(const complex<float>& __c)
5050b57cec5SDimitry Andric    : __re_(__c.real()), __im_(__c.imag()) {}
5060b57cec5SDimitry Andric
5070b57cec5SDimitry Andricinline
5080b57cec5SDimitry Andric_LIBCPP_CONSTEXPR
5090b57cec5SDimitry Andriccomplex<double>::complex(const complex<long double>& __c)
5100b57cec5SDimitry Andric    : __re_(__c.real()), __im_(__c.imag()) {}
5110b57cec5SDimitry Andric
5120b57cec5SDimitry Andricinline
5130b57cec5SDimitry Andric_LIBCPP_CONSTEXPR
5140b57cec5SDimitry Andriccomplex<long double>::complex(const complex<float>& __c)
5150b57cec5SDimitry Andric    : __re_(__c.real()), __im_(__c.imag()) {}
5160b57cec5SDimitry Andric
5170b57cec5SDimitry Andricinline
5180b57cec5SDimitry Andric_LIBCPP_CONSTEXPR
5190b57cec5SDimitry Andriccomplex<long double>::complex(const complex<double>& __c)
5200b57cec5SDimitry Andric    : __re_(__c.real()), __im_(__c.imag()) {}
5210b57cec5SDimitry Andric
5220b57cec5SDimitry Andric// 26.3.6 operators:
5230b57cec5SDimitry Andric
5240b57cec5SDimitry Andrictemplate<class _Tp>
5250b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
5260b57cec5SDimitry Andriccomplex<_Tp>
5270b57cec5SDimitry Andricoperator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
5280b57cec5SDimitry Andric{
5290b57cec5SDimitry Andric    complex<_Tp> __t(__x);
5300b57cec5SDimitry Andric    __t += __y;
5310b57cec5SDimitry Andric    return __t;
5320b57cec5SDimitry Andric}
5330b57cec5SDimitry Andric
5340b57cec5SDimitry Andrictemplate<class _Tp>
5350b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
5360b57cec5SDimitry Andriccomplex<_Tp>
5370b57cec5SDimitry Andricoperator+(const complex<_Tp>& __x, const _Tp& __y)
5380b57cec5SDimitry Andric{
5390b57cec5SDimitry Andric    complex<_Tp> __t(__x);
5400b57cec5SDimitry Andric    __t += __y;
5410b57cec5SDimitry Andric    return __t;
5420b57cec5SDimitry Andric}
5430b57cec5SDimitry Andric
5440b57cec5SDimitry Andrictemplate<class _Tp>
5450b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
5460b57cec5SDimitry Andriccomplex<_Tp>
5470b57cec5SDimitry Andricoperator+(const _Tp& __x, const complex<_Tp>& __y)
5480b57cec5SDimitry Andric{
5490b57cec5SDimitry Andric    complex<_Tp> __t(__y);
5500b57cec5SDimitry Andric    __t += __x;
5510b57cec5SDimitry Andric    return __t;
5520b57cec5SDimitry Andric}
5530b57cec5SDimitry Andric
5540b57cec5SDimitry Andrictemplate<class _Tp>
5550b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
5560b57cec5SDimitry Andriccomplex<_Tp>
5570b57cec5SDimitry Andricoperator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
5580b57cec5SDimitry Andric{
5590b57cec5SDimitry Andric    complex<_Tp> __t(__x);
5600b57cec5SDimitry Andric    __t -= __y;
5610b57cec5SDimitry Andric    return __t;
5620b57cec5SDimitry Andric}
5630b57cec5SDimitry Andric
5640b57cec5SDimitry Andrictemplate<class _Tp>
5650b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
5660b57cec5SDimitry Andriccomplex<_Tp>
5670b57cec5SDimitry Andricoperator-(const complex<_Tp>& __x, const _Tp& __y)
5680b57cec5SDimitry Andric{
5690b57cec5SDimitry Andric    complex<_Tp> __t(__x);
5700b57cec5SDimitry Andric    __t -= __y;
5710b57cec5SDimitry Andric    return __t;
5720b57cec5SDimitry Andric}
5730b57cec5SDimitry Andric
5740b57cec5SDimitry Andrictemplate<class _Tp>
5750b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
5760b57cec5SDimitry Andriccomplex<_Tp>
5770b57cec5SDimitry Andricoperator-(const _Tp& __x, const complex<_Tp>& __y)
5780b57cec5SDimitry Andric{
5790b57cec5SDimitry Andric    complex<_Tp> __t(-__y);
5800b57cec5SDimitry Andric    __t += __x;
5810b57cec5SDimitry Andric    return __t;
5820b57cec5SDimitry Andric}
5830b57cec5SDimitry Andric
5840b57cec5SDimitry Andrictemplate<class _Tp>
5850b57cec5SDimitry Andriccomplex<_Tp>
5860b57cec5SDimitry Andricoperator*(const complex<_Tp>& __z, const complex<_Tp>& __w)
5870b57cec5SDimitry Andric{
5880b57cec5SDimitry Andric    _Tp __a = __z.real();
5890b57cec5SDimitry Andric    _Tp __b = __z.imag();
5900b57cec5SDimitry Andric    _Tp __c = __w.real();
5910b57cec5SDimitry Andric    _Tp __d = __w.imag();
5920b57cec5SDimitry Andric    _Tp __ac = __a * __c;
5930b57cec5SDimitry Andric    _Tp __bd = __b * __d;
5940b57cec5SDimitry Andric    _Tp __ad = __a * __d;
5950b57cec5SDimitry Andric    _Tp __bc = __b * __c;
5960b57cec5SDimitry Andric    _Tp __x = __ac - __bd;
5970b57cec5SDimitry Andric    _Tp __y = __ad + __bc;
5980b57cec5SDimitry Andric    if (__libcpp_isnan_or_builtin(__x) && __libcpp_isnan_or_builtin(__y))
5990b57cec5SDimitry Andric    {
6000b57cec5SDimitry Andric        bool __recalc = false;
6010b57cec5SDimitry Andric        if (__libcpp_isinf_or_builtin(__a) || __libcpp_isinf_or_builtin(__b))
6020b57cec5SDimitry Andric        {
6030b57cec5SDimitry Andric            __a = copysign(__libcpp_isinf_or_builtin(__a) ? _Tp(1) : _Tp(0), __a);
6040b57cec5SDimitry Andric            __b = copysign(__libcpp_isinf_or_builtin(__b) ? _Tp(1) : _Tp(0), __b);
6050b57cec5SDimitry Andric            if (__libcpp_isnan_or_builtin(__c))
6060b57cec5SDimitry Andric                __c = copysign(_Tp(0), __c);
6070b57cec5SDimitry Andric            if (__libcpp_isnan_or_builtin(__d))
6080b57cec5SDimitry Andric                __d = copysign(_Tp(0), __d);
6090b57cec5SDimitry Andric            __recalc = true;
6100b57cec5SDimitry Andric        }
6110b57cec5SDimitry Andric        if (__libcpp_isinf_or_builtin(__c) || __libcpp_isinf_or_builtin(__d))
6120b57cec5SDimitry Andric        {
6130b57cec5SDimitry Andric            __c = copysign(__libcpp_isinf_or_builtin(__c) ? _Tp(1) : _Tp(0), __c);
6140b57cec5SDimitry Andric            __d = copysign(__libcpp_isinf_or_builtin(__d) ? _Tp(1) : _Tp(0), __d);
6150b57cec5SDimitry Andric            if (__libcpp_isnan_or_builtin(__a))
6160b57cec5SDimitry Andric                __a = copysign(_Tp(0), __a);
6170b57cec5SDimitry Andric            if (__libcpp_isnan_or_builtin(__b))
6180b57cec5SDimitry Andric                __b = copysign(_Tp(0), __b);
6190b57cec5SDimitry Andric            __recalc = true;
6200b57cec5SDimitry Andric        }
6210b57cec5SDimitry Andric        if (!__recalc && (__libcpp_isinf_or_builtin(__ac) || __libcpp_isinf_or_builtin(__bd) ||
6220b57cec5SDimitry Andric                          __libcpp_isinf_or_builtin(__ad) || __libcpp_isinf_or_builtin(__bc)))
6230b57cec5SDimitry Andric        {
6240b57cec5SDimitry Andric            if (__libcpp_isnan_or_builtin(__a))
6250b57cec5SDimitry Andric                __a = copysign(_Tp(0), __a);
6260b57cec5SDimitry Andric            if (__libcpp_isnan_or_builtin(__b))
6270b57cec5SDimitry Andric                __b = copysign(_Tp(0), __b);
6280b57cec5SDimitry Andric            if (__libcpp_isnan_or_builtin(__c))
6290b57cec5SDimitry Andric                __c = copysign(_Tp(0), __c);
6300b57cec5SDimitry Andric            if (__libcpp_isnan_or_builtin(__d))
6310b57cec5SDimitry Andric                __d = copysign(_Tp(0), __d);
6320b57cec5SDimitry Andric            __recalc = true;
6330b57cec5SDimitry Andric        }
6340b57cec5SDimitry Andric        if (__recalc)
6350b57cec5SDimitry Andric        {
6360b57cec5SDimitry Andric            __x = _Tp(INFINITY) * (__a * __c - __b * __d);
6370b57cec5SDimitry Andric            __y = _Tp(INFINITY) * (__a * __d + __b * __c);
6380b57cec5SDimitry Andric        }
6390b57cec5SDimitry Andric    }
6400b57cec5SDimitry Andric    return complex<_Tp>(__x, __y);
6410b57cec5SDimitry Andric}
6420b57cec5SDimitry Andric
6430b57cec5SDimitry Andrictemplate<class _Tp>
6440b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
6450b57cec5SDimitry Andriccomplex<_Tp>
6460b57cec5SDimitry Andricoperator*(const complex<_Tp>& __x, const _Tp& __y)
6470b57cec5SDimitry Andric{
6480b57cec5SDimitry Andric    complex<_Tp> __t(__x);
6490b57cec5SDimitry Andric    __t *= __y;
6500b57cec5SDimitry Andric    return __t;
6510b57cec5SDimitry Andric}
6520b57cec5SDimitry Andric
6530b57cec5SDimitry Andrictemplate<class _Tp>
6540b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
6550b57cec5SDimitry Andriccomplex<_Tp>
6560b57cec5SDimitry Andricoperator*(const _Tp& __x, const complex<_Tp>& __y)
6570b57cec5SDimitry Andric{
6580b57cec5SDimitry Andric    complex<_Tp> __t(__y);
6590b57cec5SDimitry Andric    __t *= __x;
6600b57cec5SDimitry Andric    return __t;
6610b57cec5SDimitry Andric}
6620b57cec5SDimitry Andric
6630b57cec5SDimitry Andrictemplate<class _Tp>
6640b57cec5SDimitry Andriccomplex<_Tp>
6650b57cec5SDimitry Andricoperator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
6660b57cec5SDimitry Andric{
6670b57cec5SDimitry Andric    int __ilogbw = 0;
6680b57cec5SDimitry Andric    _Tp __a = __z.real();
6690b57cec5SDimitry Andric    _Tp __b = __z.imag();
6700b57cec5SDimitry Andric    _Tp __c = __w.real();
6710b57cec5SDimitry Andric    _Tp __d = __w.imag();
6720b57cec5SDimitry Andric    _Tp __logbw = logb(fmax(fabs(__c), fabs(__d)));
6730b57cec5SDimitry Andric    if (__libcpp_isfinite_or_builtin(__logbw))
6740b57cec5SDimitry Andric    {
6750b57cec5SDimitry Andric        __ilogbw = static_cast<int>(__logbw);
6760b57cec5SDimitry Andric        __c = scalbn(__c, -__ilogbw);
6770b57cec5SDimitry Andric        __d = scalbn(__d, -__ilogbw);
6780b57cec5SDimitry Andric    }
6790b57cec5SDimitry Andric    _Tp __denom = __c * __c + __d * __d;
6800b57cec5SDimitry Andric    _Tp __x = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
6810b57cec5SDimitry Andric    _Tp __y = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
6820b57cec5SDimitry Andric    if (__libcpp_isnan_or_builtin(__x) && __libcpp_isnan_or_builtin(__y))
6830b57cec5SDimitry Andric    {
6840b57cec5SDimitry Andric        if ((__denom == _Tp(0)) && (!__libcpp_isnan_or_builtin(__a) || !__libcpp_isnan_or_builtin(__b)))
6850b57cec5SDimitry Andric        {
6860b57cec5SDimitry Andric            __x = copysign(_Tp(INFINITY), __c) * __a;
6870b57cec5SDimitry Andric            __y = copysign(_Tp(INFINITY), __c) * __b;
6880b57cec5SDimitry Andric        }
6890b57cec5SDimitry Andric        else if ((__libcpp_isinf_or_builtin(__a) || __libcpp_isinf_or_builtin(__b)) && __libcpp_isfinite_or_builtin(__c) && __libcpp_isfinite_or_builtin(__d))
6900b57cec5SDimitry Andric        {
6910b57cec5SDimitry Andric            __a = copysign(__libcpp_isinf_or_builtin(__a) ? _Tp(1) : _Tp(0), __a);
6920b57cec5SDimitry Andric            __b = copysign(__libcpp_isinf_or_builtin(__b) ? _Tp(1) : _Tp(0), __b);
6930b57cec5SDimitry Andric            __x = _Tp(INFINITY) * (__a * __c + __b * __d);
6940b57cec5SDimitry Andric            __y = _Tp(INFINITY) * (__b * __c - __a * __d);
6950b57cec5SDimitry Andric        }
6960b57cec5SDimitry Andric        else if (__libcpp_isinf_or_builtin(__logbw) && __logbw > _Tp(0) && __libcpp_isfinite_or_builtin(__a) && __libcpp_isfinite_or_builtin(__b))
6970b57cec5SDimitry Andric        {
6980b57cec5SDimitry Andric            __c = copysign(__libcpp_isinf_or_builtin(__c) ? _Tp(1) : _Tp(0), __c);
6990b57cec5SDimitry Andric            __d = copysign(__libcpp_isinf_or_builtin(__d) ? _Tp(1) : _Tp(0), __d);
7000b57cec5SDimitry Andric            __x = _Tp(0) * (__a * __c + __b * __d);
7010b57cec5SDimitry Andric            __y = _Tp(0) * (__b * __c - __a * __d);
7020b57cec5SDimitry Andric        }
7030b57cec5SDimitry Andric    }
7040b57cec5SDimitry Andric    return complex<_Tp>(__x, __y);
7050b57cec5SDimitry Andric}
7060b57cec5SDimitry Andric
7070b57cec5SDimitry Andrictemplate<class _Tp>
7080b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
7090b57cec5SDimitry Andriccomplex<_Tp>
7100b57cec5SDimitry Andricoperator/(const complex<_Tp>& __x, const _Tp& __y)
7110b57cec5SDimitry Andric{
7120b57cec5SDimitry Andric    return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
7130b57cec5SDimitry Andric}
7140b57cec5SDimitry Andric
7150b57cec5SDimitry Andrictemplate<class _Tp>
7160b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
7170b57cec5SDimitry Andriccomplex<_Tp>
7180b57cec5SDimitry Andricoperator/(const _Tp& __x, const complex<_Tp>& __y)
7190b57cec5SDimitry Andric{
7200b57cec5SDimitry Andric    complex<_Tp> __t(__x);
7210b57cec5SDimitry Andric    __t /= __y;
7220b57cec5SDimitry Andric    return __t;
7230b57cec5SDimitry Andric}
7240b57cec5SDimitry Andric
7250b57cec5SDimitry Andrictemplate<class _Tp>
7260b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
7270b57cec5SDimitry Andriccomplex<_Tp>
7280b57cec5SDimitry Andricoperator+(const complex<_Tp>& __x)
7290b57cec5SDimitry Andric{
7300b57cec5SDimitry Andric    return __x;
7310b57cec5SDimitry Andric}
7320b57cec5SDimitry Andric
7330b57cec5SDimitry Andrictemplate<class _Tp>
7340b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
7350b57cec5SDimitry Andriccomplex<_Tp>
7360b57cec5SDimitry Andricoperator-(const complex<_Tp>& __x)
7370b57cec5SDimitry Andric{
7380b57cec5SDimitry Andric    return complex<_Tp>(-__x.real(), -__x.imag());
7390b57cec5SDimitry Andric}
7400b57cec5SDimitry Andric
7410b57cec5SDimitry Andrictemplate<class _Tp>
7420b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
7430b57cec5SDimitry Andricbool
7440b57cec5SDimitry Andricoperator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
7450b57cec5SDimitry Andric{
7460b57cec5SDimitry Andric    return __x.real() == __y.real() && __x.imag() == __y.imag();
7470b57cec5SDimitry Andric}
7480b57cec5SDimitry Andric
7490b57cec5SDimitry Andrictemplate<class _Tp>
7500b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
7510b57cec5SDimitry Andricbool
7520b57cec5SDimitry Andricoperator==(const complex<_Tp>& __x, const _Tp& __y)
7530b57cec5SDimitry Andric{
7540b57cec5SDimitry Andric    return __x.real() == __y && __x.imag() == 0;
7550b57cec5SDimitry Andric}
7560b57cec5SDimitry Andric
7570b57cec5SDimitry Andrictemplate<class _Tp>
7580b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
7590b57cec5SDimitry Andricbool
7600b57cec5SDimitry Andricoperator==(const _Tp& __x, const complex<_Tp>& __y)
7610b57cec5SDimitry Andric{
7620b57cec5SDimitry Andric    return __x == __y.real() && 0 == __y.imag();
7630b57cec5SDimitry Andric}
7640b57cec5SDimitry Andric
7650b57cec5SDimitry Andrictemplate<class _Tp>
7660b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
7670b57cec5SDimitry Andricbool
7680b57cec5SDimitry Andricoperator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
7690b57cec5SDimitry Andric{
7700b57cec5SDimitry Andric    return !(__x == __y);
7710b57cec5SDimitry Andric}
7720b57cec5SDimitry Andric
7730b57cec5SDimitry Andrictemplate<class _Tp>
7740b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
7750b57cec5SDimitry Andricbool
7760b57cec5SDimitry Andricoperator!=(const complex<_Tp>& __x, const _Tp& __y)
7770b57cec5SDimitry Andric{
7780b57cec5SDimitry Andric    return !(__x == __y);
7790b57cec5SDimitry Andric}
7800b57cec5SDimitry Andric
7810b57cec5SDimitry Andrictemplate<class _Tp>
7820b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
7830b57cec5SDimitry Andricbool
7840b57cec5SDimitry Andricoperator!=(const _Tp& __x, const complex<_Tp>& __y)
7850b57cec5SDimitry Andric{
7860b57cec5SDimitry Andric    return !(__x == __y);
7870b57cec5SDimitry Andric}
7880b57cec5SDimitry Andric
7890b57cec5SDimitry Andric// 26.3.7 values:
7900b57cec5SDimitry Andric
7910b57cec5SDimitry Andrictemplate <class _Tp, bool = is_integral<_Tp>::value,
7920b57cec5SDimitry Andric                     bool = is_floating_point<_Tp>::value
7930b57cec5SDimitry Andric                     >
7940b57cec5SDimitry Andricstruct __libcpp_complex_overload_traits {};
7950b57cec5SDimitry Andric
7960b57cec5SDimitry Andric// Integral Types
7970b57cec5SDimitry Andrictemplate <class _Tp>
7980b57cec5SDimitry Andricstruct __libcpp_complex_overload_traits<_Tp, true, false>
7990b57cec5SDimitry Andric{
8000b57cec5SDimitry Andric    typedef double _ValueType;
8010b57cec5SDimitry Andric    typedef complex<double> _ComplexType;
8020b57cec5SDimitry Andric};
8030b57cec5SDimitry Andric
8040b57cec5SDimitry Andric// Floating point types
8050b57cec5SDimitry Andrictemplate <class _Tp>
8060b57cec5SDimitry Andricstruct __libcpp_complex_overload_traits<_Tp, false, true>
8070b57cec5SDimitry Andric{
8080b57cec5SDimitry Andric    typedef _Tp _ValueType;
8090b57cec5SDimitry Andric    typedef complex<_Tp> _ComplexType;
8100b57cec5SDimitry Andric};
8110b57cec5SDimitry Andric
8120b57cec5SDimitry Andric// real
8130b57cec5SDimitry Andric
8140b57cec5SDimitry Andrictemplate<class _Tp>
8150b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
8160b57cec5SDimitry Andric_Tp
8170b57cec5SDimitry Andricreal(const complex<_Tp>& __c)
8180b57cec5SDimitry Andric{
8190b57cec5SDimitry Andric    return __c.real();
8200b57cec5SDimitry Andric}
8210b57cec5SDimitry Andric
8220b57cec5SDimitry Andrictemplate <class _Tp>
8230b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
8240b57cec5SDimitry Andrictypename __libcpp_complex_overload_traits<_Tp>::_ValueType
8250b57cec5SDimitry Andricreal(_Tp __re)
8260b57cec5SDimitry Andric{
8270b57cec5SDimitry Andric    return __re;
8280b57cec5SDimitry Andric}
8290b57cec5SDimitry Andric
8300b57cec5SDimitry Andric// imag
8310b57cec5SDimitry Andric
8320b57cec5SDimitry Andrictemplate<class _Tp>
8330b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
8340b57cec5SDimitry Andric_Tp
8350b57cec5SDimitry Andricimag(const complex<_Tp>& __c)
8360b57cec5SDimitry Andric{
8370b57cec5SDimitry Andric    return __c.imag();
8380b57cec5SDimitry Andric}
8390b57cec5SDimitry Andric
8400b57cec5SDimitry Andrictemplate <class _Tp>
8410b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
8420b57cec5SDimitry Andrictypename __libcpp_complex_overload_traits<_Tp>::_ValueType
8430b57cec5SDimitry Andricimag(_Tp)
8440b57cec5SDimitry Andric{
8450b57cec5SDimitry Andric    return 0;
8460b57cec5SDimitry Andric}
8470b57cec5SDimitry Andric
8480b57cec5SDimitry Andric// abs
8490b57cec5SDimitry Andric
8500b57cec5SDimitry Andrictemplate<class _Tp>
8510b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
8520b57cec5SDimitry Andric_Tp
8530b57cec5SDimitry Andricabs(const complex<_Tp>& __c)
8540b57cec5SDimitry Andric{
8550b57cec5SDimitry Andric    return hypot(__c.real(), __c.imag());
8560b57cec5SDimitry Andric}
8570b57cec5SDimitry Andric
8580b57cec5SDimitry Andric// arg
8590b57cec5SDimitry Andric
8600b57cec5SDimitry Andrictemplate<class _Tp>
8610b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
8620b57cec5SDimitry Andric_Tp
8630b57cec5SDimitry Andricarg(const complex<_Tp>& __c)
8640b57cec5SDimitry Andric{
8650b57cec5SDimitry Andric    return atan2(__c.imag(), __c.real());
8660b57cec5SDimitry Andric}
8670b57cec5SDimitry Andric
8680b57cec5SDimitry Andrictemplate <class _Tp>
8690b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
8700b57cec5SDimitry Andrictypename enable_if<
8710b57cec5SDimitry Andric    is_same<_Tp, long double>::value,
8720b57cec5SDimitry Andric    long double
8730b57cec5SDimitry Andric>::type
8740b57cec5SDimitry Andricarg(_Tp __re)
8750b57cec5SDimitry Andric{
8760b57cec5SDimitry Andric    return atan2l(0.L, __re);
8770b57cec5SDimitry Andric}
8780b57cec5SDimitry Andric
8790b57cec5SDimitry Andrictemplate<class _Tp>
8800b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
8810b57cec5SDimitry Andrictypename enable_if
8820b57cec5SDimitry Andric<
8830b57cec5SDimitry Andric    is_integral<_Tp>::value || is_same<_Tp, double>::value,
8840b57cec5SDimitry Andric    double
8850b57cec5SDimitry Andric>::type
8860b57cec5SDimitry Andricarg(_Tp __re)
8870b57cec5SDimitry Andric{
8880b57cec5SDimitry Andric    return atan2(0., __re);
8890b57cec5SDimitry Andric}
8900b57cec5SDimitry Andric
8910b57cec5SDimitry Andrictemplate <class _Tp>
8920b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
8930b57cec5SDimitry Andrictypename enable_if<
8940b57cec5SDimitry Andric    is_same<_Tp, float>::value,
8950b57cec5SDimitry Andric    float
8960b57cec5SDimitry Andric>::type
8970b57cec5SDimitry Andricarg(_Tp __re)
8980b57cec5SDimitry Andric{
8990b57cec5SDimitry Andric    return atan2f(0.F, __re);
9000b57cec5SDimitry Andric}
9010b57cec5SDimitry Andric
9020b57cec5SDimitry Andric// norm
9030b57cec5SDimitry Andric
9040b57cec5SDimitry Andrictemplate<class _Tp>
9050b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
9060b57cec5SDimitry Andric_Tp
9070b57cec5SDimitry Andricnorm(const complex<_Tp>& __c)
9080b57cec5SDimitry Andric{
9090b57cec5SDimitry Andric    if (__libcpp_isinf_or_builtin(__c.real()))
9100b57cec5SDimitry Andric        return abs(__c.real());
9110b57cec5SDimitry Andric    if (__libcpp_isinf_or_builtin(__c.imag()))
9120b57cec5SDimitry Andric        return abs(__c.imag());
9130b57cec5SDimitry Andric    return __c.real() * __c.real() + __c.imag() * __c.imag();
9140b57cec5SDimitry Andric}
9150b57cec5SDimitry Andric
9160b57cec5SDimitry Andrictemplate <class _Tp>
9170b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
9180b57cec5SDimitry Andrictypename __libcpp_complex_overload_traits<_Tp>::_ValueType
9190b57cec5SDimitry Andricnorm(_Tp __re)
9200b57cec5SDimitry Andric{
9210b57cec5SDimitry Andric    typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType;
9220b57cec5SDimitry Andric    return static_cast<_ValueType>(__re) * __re;
9230b57cec5SDimitry Andric}
9240b57cec5SDimitry Andric
9250b57cec5SDimitry Andric// conj
9260b57cec5SDimitry Andric
9270b57cec5SDimitry Andrictemplate<class _Tp>
9280b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
9290b57cec5SDimitry Andriccomplex<_Tp>
9300b57cec5SDimitry Andricconj(const complex<_Tp>& __c)
9310b57cec5SDimitry Andric{
9320b57cec5SDimitry Andric    return complex<_Tp>(__c.real(), -__c.imag());
9330b57cec5SDimitry Andric}
9340b57cec5SDimitry Andric
9350b57cec5SDimitry Andrictemplate <class _Tp>
9360b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
9370b57cec5SDimitry Andrictypename __libcpp_complex_overload_traits<_Tp>::_ComplexType
9380b57cec5SDimitry Andricconj(_Tp __re)
9390b57cec5SDimitry Andric{
9400b57cec5SDimitry Andric    typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
9410b57cec5SDimitry Andric    return _ComplexType(__re);
9420b57cec5SDimitry Andric}
9430b57cec5SDimitry Andric
9440b57cec5SDimitry Andric
9450b57cec5SDimitry Andric
9460b57cec5SDimitry Andric// proj
9470b57cec5SDimitry Andric
9480b57cec5SDimitry Andrictemplate<class _Tp>
9490b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
9500b57cec5SDimitry Andriccomplex<_Tp>
9510b57cec5SDimitry Andricproj(const complex<_Tp>& __c)
9520b57cec5SDimitry Andric{
953*e8d8bef9SDimitry Andric    complex<_Tp> __r = __c;
9540b57cec5SDimitry Andric    if (__libcpp_isinf_or_builtin(__c.real()) || __libcpp_isinf_or_builtin(__c.imag()))
9550b57cec5SDimitry Andric        __r = complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag()));
9560b57cec5SDimitry Andric    return __r;
9570b57cec5SDimitry Andric}
9580b57cec5SDimitry Andric
9590b57cec5SDimitry Andrictemplate <class _Tp>
9600b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
9610b57cec5SDimitry Andrictypename enable_if
9620b57cec5SDimitry Andric<
9630b57cec5SDimitry Andric    is_floating_point<_Tp>::value,
9640b57cec5SDimitry Andric    typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
9650b57cec5SDimitry Andric>::type
9660b57cec5SDimitry Andricproj(_Tp __re)
9670b57cec5SDimitry Andric{
9680b57cec5SDimitry Andric    if (__libcpp_isinf_or_builtin(__re))
9690b57cec5SDimitry Andric        __re = abs(__re);
9700b57cec5SDimitry Andric    return complex<_Tp>(__re);
9710b57cec5SDimitry Andric}
9720b57cec5SDimitry Andric
9730b57cec5SDimitry Andrictemplate <class _Tp>
9740b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
9750b57cec5SDimitry Andrictypename enable_if
9760b57cec5SDimitry Andric<
9770b57cec5SDimitry Andric    is_integral<_Tp>::value,
9780b57cec5SDimitry Andric    typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
9790b57cec5SDimitry Andric>::type
9800b57cec5SDimitry Andricproj(_Tp __re)
9810b57cec5SDimitry Andric{
9820b57cec5SDimitry Andric    typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
9830b57cec5SDimitry Andric    return _ComplexType(__re);
9840b57cec5SDimitry Andric}
9850b57cec5SDimitry Andric
9860b57cec5SDimitry Andric// polar
9870b57cec5SDimitry Andric
9880b57cec5SDimitry Andrictemplate<class _Tp>
9890b57cec5SDimitry Andriccomplex<_Tp>
9900b57cec5SDimitry Andricpolar(const _Tp& __rho, const _Tp& __theta = _Tp())
9910b57cec5SDimitry Andric{
9920b57cec5SDimitry Andric    if (__libcpp_isnan_or_builtin(__rho) || signbit(__rho))
9930b57cec5SDimitry Andric        return complex<_Tp>(_Tp(NAN), _Tp(NAN));
9940b57cec5SDimitry Andric    if (__libcpp_isnan_or_builtin(__theta))
9950b57cec5SDimitry Andric    {
9960b57cec5SDimitry Andric        if (__libcpp_isinf_or_builtin(__rho))
9970b57cec5SDimitry Andric            return complex<_Tp>(__rho, __theta);
9980b57cec5SDimitry Andric        return complex<_Tp>(__theta, __theta);
9990b57cec5SDimitry Andric    }
10000b57cec5SDimitry Andric    if (__libcpp_isinf_or_builtin(__theta))
10010b57cec5SDimitry Andric    {
10020b57cec5SDimitry Andric        if (__libcpp_isinf_or_builtin(__rho))
10030b57cec5SDimitry Andric            return complex<_Tp>(__rho, _Tp(NAN));
10040b57cec5SDimitry Andric        return complex<_Tp>(_Tp(NAN), _Tp(NAN));
10050b57cec5SDimitry Andric    }
10060b57cec5SDimitry Andric    _Tp __x = __rho * cos(__theta);
10070b57cec5SDimitry Andric    if (__libcpp_isnan_or_builtin(__x))
10080b57cec5SDimitry Andric        __x = 0;
10090b57cec5SDimitry Andric    _Tp __y = __rho * sin(__theta);
10100b57cec5SDimitry Andric    if (__libcpp_isnan_or_builtin(__y))
10110b57cec5SDimitry Andric        __y = 0;
10120b57cec5SDimitry Andric    return complex<_Tp>(__x, __y);
10130b57cec5SDimitry Andric}
10140b57cec5SDimitry Andric
10150b57cec5SDimitry Andric// log
10160b57cec5SDimitry Andric
10170b57cec5SDimitry Andrictemplate<class _Tp>
10180b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
10190b57cec5SDimitry Andriccomplex<_Tp>
10200b57cec5SDimitry Andriclog(const complex<_Tp>& __x)
10210b57cec5SDimitry Andric{
10220b57cec5SDimitry Andric    return complex<_Tp>(log(abs(__x)), arg(__x));
10230b57cec5SDimitry Andric}
10240b57cec5SDimitry Andric
10250b57cec5SDimitry Andric// log10
10260b57cec5SDimitry Andric
10270b57cec5SDimitry Andrictemplate<class _Tp>
10280b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
10290b57cec5SDimitry Andriccomplex<_Tp>
10300b57cec5SDimitry Andriclog10(const complex<_Tp>& __x)
10310b57cec5SDimitry Andric{
10320b57cec5SDimitry Andric    return log(__x) / log(_Tp(10));
10330b57cec5SDimitry Andric}
10340b57cec5SDimitry Andric
10350b57cec5SDimitry Andric// sqrt
10360b57cec5SDimitry Andric
10370b57cec5SDimitry Andrictemplate<class _Tp>
10380b57cec5SDimitry Andriccomplex<_Tp>
10390b57cec5SDimitry Andricsqrt(const complex<_Tp>& __x)
10400b57cec5SDimitry Andric{
10410b57cec5SDimitry Andric    if (__libcpp_isinf_or_builtin(__x.imag()))
10420b57cec5SDimitry Andric        return complex<_Tp>(_Tp(INFINITY), __x.imag());
10430b57cec5SDimitry Andric    if (__libcpp_isinf_or_builtin(__x.real()))
10440b57cec5SDimitry Andric    {
10450b57cec5SDimitry Andric        if (__x.real() > _Tp(0))
10460b57cec5SDimitry Andric            return complex<_Tp>(__x.real(), __libcpp_isnan_or_builtin(__x.imag()) ? __x.imag() : copysign(_Tp(0), __x.imag()));
10470b57cec5SDimitry Andric        return complex<_Tp>(__libcpp_isnan_or_builtin(__x.imag()) ? __x.imag() : _Tp(0), copysign(__x.real(), __x.imag()));
10480b57cec5SDimitry Andric    }
10490b57cec5SDimitry Andric    return polar(sqrt(abs(__x)), arg(__x) / _Tp(2));
10500b57cec5SDimitry Andric}
10510b57cec5SDimitry Andric
10520b57cec5SDimitry Andric// exp
10530b57cec5SDimitry Andric
10540b57cec5SDimitry Andrictemplate<class _Tp>
10550b57cec5SDimitry Andriccomplex<_Tp>
10560b57cec5SDimitry Andricexp(const complex<_Tp>& __x)
10570b57cec5SDimitry Andric{
10580b57cec5SDimitry Andric    _Tp __i = __x.imag();
10590b57cec5SDimitry Andric    if (__libcpp_isinf_or_builtin(__x.real()))
10600b57cec5SDimitry Andric    {
10610b57cec5SDimitry Andric        if (__x.real() < _Tp(0))
10620b57cec5SDimitry Andric        {
10630b57cec5SDimitry Andric            if (!__libcpp_isfinite_or_builtin(__i))
10640b57cec5SDimitry Andric                __i = _Tp(1);
10650b57cec5SDimitry Andric        }
10660b57cec5SDimitry Andric        else if (__i == 0 || !__libcpp_isfinite_or_builtin(__i))
10670b57cec5SDimitry Andric        {
10680b57cec5SDimitry Andric            if (__libcpp_isinf_or_builtin(__i))
10690b57cec5SDimitry Andric                __i = _Tp(NAN);
10700b57cec5SDimitry Andric            return complex<_Tp>(__x.real(), __i);
10710b57cec5SDimitry Andric        }
10720b57cec5SDimitry Andric    }
10730b57cec5SDimitry Andric    else if (__libcpp_isnan_or_builtin(__x.real()) && __x.imag() == 0)
10740b57cec5SDimitry Andric        return __x;
10750b57cec5SDimitry Andric    _Tp __e = exp(__x.real());
10760b57cec5SDimitry Andric    return complex<_Tp>(__e * cos(__i), __e * sin(__i));
10770b57cec5SDimitry Andric}
10780b57cec5SDimitry Andric
10790b57cec5SDimitry Andric// pow
10800b57cec5SDimitry Andric
10810b57cec5SDimitry Andrictemplate<class _Tp>
10820b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
10830b57cec5SDimitry Andriccomplex<_Tp>
10840b57cec5SDimitry Andricpow(const complex<_Tp>& __x, const complex<_Tp>& __y)
10850b57cec5SDimitry Andric{
10860b57cec5SDimitry Andric    return exp(__y * log(__x));
10870b57cec5SDimitry Andric}
10880b57cec5SDimitry Andric
10890b57cec5SDimitry Andrictemplate<class _Tp, class _Up>
10900b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
10910b57cec5SDimitry Andriccomplex<typename __promote<_Tp, _Up>::type>
10920b57cec5SDimitry Andricpow(const complex<_Tp>& __x, const complex<_Up>& __y)
10930b57cec5SDimitry Andric{
10940b57cec5SDimitry Andric    typedef complex<typename __promote<_Tp, _Up>::type> result_type;
10950b57cec5SDimitry Andric    return _VSTD::pow(result_type(__x), result_type(__y));
10960b57cec5SDimitry Andric}
10970b57cec5SDimitry Andric
10980b57cec5SDimitry Andrictemplate<class _Tp, class _Up>
10990b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
11000b57cec5SDimitry Andrictypename enable_if
11010b57cec5SDimitry Andric<
11020b57cec5SDimitry Andric    is_arithmetic<_Up>::value,
11030b57cec5SDimitry Andric    complex<typename __promote<_Tp, _Up>::type>
11040b57cec5SDimitry Andric>::type
11050b57cec5SDimitry Andricpow(const complex<_Tp>& __x, const _Up& __y)
11060b57cec5SDimitry Andric{
11070b57cec5SDimitry Andric    typedef complex<typename __promote<_Tp, _Up>::type> result_type;
11080b57cec5SDimitry Andric    return _VSTD::pow(result_type(__x), result_type(__y));
11090b57cec5SDimitry Andric}
11100b57cec5SDimitry Andric
11110b57cec5SDimitry Andrictemplate<class _Tp, class _Up>
11120b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
11130b57cec5SDimitry Andrictypename enable_if
11140b57cec5SDimitry Andric<
11150b57cec5SDimitry Andric    is_arithmetic<_Tp>::value,
11160b57cec5SDimitry Andric    complex<typename __promote<_Tp, _Up>::type>
11170b57cec5SDimitry Andric>::type
11180b57cec5SDimitry Andricpow(const _Tp& __x, const complex<_Up>& __y)
11190b57cec5SDimitry Andric{
11200b57cec5SDimitry Andric    typedef complex<typename __promote<_Tp, _Up>::type> result_type;
11210b57cec5SDimitry Andric    return _VSTD::pow(result_type(__x), result_type(__y));
11220b57cec5SDimitry Andric}
11230b57cec5SDimitry Andric
11240b57cec5SDimitry Andric// __sqr, computes pow(x, 2)
11250b57cec5SDimitry Andric
11260b57cec5SDimitry Andrictemplate<class _Tp>
11270b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
11280b57cec5SDimitry Andriccomplex<_Tp>
11290b57cec5SDimitry Andric__sqr(const complex<_Tp>& __x)
11300b57cec5SDimitry Andric{
11310b57cec5SDimitry Andric    return complex<_Tp>((__x.real() - __x.imag()) * (__x.real() + __x.imag()),
11320b57cec5SDimitry Andric                        _Tp(2) * __x.real() * __x.imag());
11330b57cec5SDimitry Andric}
11340b57cec5SDimitry Andric
11350b57cec5SDimitry Andric// asinh
11360b57cec5SDimitry Andric
11370b57cec5SDimitry Andrictemplate<class _Tp>
11380b57cec5SDimitry Andriccomplex<_Tp>
11390b57cec5SDimitry Andricasinh(const complex<_Tp>& __x)
11400b57cec5SDimitry Andric{
11410b57cec5SDimitry Andric    const _Tp __pi(atan2(+0., -0.));
11420b57cec5SDimitry Andric    if (__libcpp_isinf_or_builtin(__x.real()))
11430b57cec5SDimitry Andric    {
11440b57cec5SDimitry Andric        if (__libcpp_isnan_or_builtin(__x.imag()))
11450b57cec5SDimitry Andric            return __x;
11460b57cec5SDimitry Andric        if (__libcpp_isinf_or_builtin(__x.imag()))
11470b57cec5SDimitry Andric            return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
11480b57cec5SDimitry Andric        return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
11490b57cec5SDimitry Andric    }
11500b57cec5SDimitry Andric    if (__libcpp_isnan_or_builtin(__x.real()))
11510b57cec5SDimitry Andric    {
11520b57cec5SDimitry Andric        if (__libcpp_isinf_or_builtin(__x.imag()))
11530b57cec5SDimitry Andric            return complex<_Tp>(__x.imag(), __x.real());
11540b57cec5SDimitry Andric        if (__x.imag() == 0)
11550b57cec5SDimitry Andric            return __x;
11560b57cec5SDimitry Andric        return complex<_Tp>(__x.real(), __x.real());
11570b57cec5SDimitry Andric    }
11580b57cec5SDimitry Andric    if (__libcpp_isinf_or_builtin(__x.imag()))
11590b57cec5SDimitry Andric        return complex<_Tp>(copysign(__x.imag(), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
11600b57cec5SDimitry Andric    complex<_Tp> __z = log(__x + sqrt(__sqr(__x) + _Tp(1)));
11610b57cec5SDimitry Andric    return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
11620b57cec5SDimitry Andric}
11630b57cec5SDimitry Andric
11640b57cec5SDimitry Andric// acosh
11650b57cec5SDimitry Andric
11660b57cec5SDimitry Andrictemplate<class _Tp>
11670b57cec5SDimitry Andriccomplex<_Tp>
11680b57cec5SDimitry Andricacosh(const complex<_Tp>& __x)
11690b57cec5SDimitry Andric{
11700b57cec5SDimitry Andric    const _Tp __pi(atan2(+0., -0.));
11710b57cec5SDimitry Andric    if (__libcpp_isinf_or_builtin(__x.real()))
11720b57cec5SDimitry Andric    {
11730b57cec5SDimitry Andric        if (__libcpp_isnan_or_builtin(__x.imag()))
11740b57cec5SDimitry Andric            return complex<_Tp>(abs(__x.real()), __x.imag());
11750b57cec5SDimitry Andric        if (__libcpp_isinf_or_builtin(__x.imag()))
11760b57cec5SDimitry Andric        {
11770b57cec5SDimitry Andric            if (__x.real() > 0)
11780b57cec5SDimitry Andric                return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
11790b57cec5SDimitry Andric            else
11800b57cec5SDimitry Andric                return complex<_Tp>(-__x.real(), copysign(__pi * _Tp(0.75), __x.imag()));
11810b57cec5SDimitry Andric        }
11820b57cec5SDimitry Andric        if (__x.real() < 0)
11830b57cec5SDimitry Andric            return complex<_Tp>(-__x.real(), copysign(__pi, __x.imag()));
11840b57cec5SDimitry Andric        return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
11850b57cec5SDimitry Andric    }
11860b57cec5SDimitry Andric    if (__libcpp_isnan_or_builtin(__x.real()))
11870b57cec5SDimitry Andric    {
11880b57cec5SDimitry Andric        if (__libcpp_isinf_or_builtin(__x.imag()))
11890b57cec5SDimitry Andric            return complex<_Tp>(abs(__x.imag()), __x.real());
11900b57cec5SDimitry Andric        return complex<_Tp>(__x.real(), __x.real());
11910b57cec5SDimitry Andric    }
11920b57cec5SDimitry Andric    if (__libcpp_isinf_or_builtin(__x.imag()))
11930b57cec5SDimitry Andric        return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag()));
11940b57cec5SDimitry Andric    complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1)));
11950b57cec5SDimitry Andric    return complex<_Tp>(copysign(__z.real(), _Tp(0)), copysign(__z.imag(), __x.imag()));
11960b57cec5SDimitry Andric}
11970b57cec5SDimitry Andric
11980b57cec5SDimitry Andric// atanh
11990b57cec5SDimitry Andric
12000b57cec5SDimitry Andrictemplate<class _Tp>
12010b57cec5SDimitry Andriccomplex<_Tp>
12020b57cec5SDimitry Andricatanh(const complex<_Tp>& __x)
12030b57cec5SDimitry Andric{
12040b57cec5SDimitry Andric    const _Tp __pi(atan2(+0., -0.));
12050b57cec5SDimitry Andric    if (__libcpp_isinf_or_builtin(__x.imag()))
12060b57cec5SDimitry Andric    {
12070b57cec5SDimitry Andric        return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
12080b57cec5SDimitry Andric    }
12090b57cec5SDimitry Andric    if (__libcpp_isnan_or_builtin(__x.imag()))
12100b57cec5SDimitry Andric    {
12110b57cec5SDimitry Andric        if (__libcpp_isinf_or_builtin(__x.real()) || __x.real() == 0)
12120b57cec5SDimitry Andric            return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag());
12130b57cec5SDimitry Andric        return complex<_Tp>(__x.imag(), __x.imag());
12140b57cec5SDimitry Andric    }
12150b57cec5SDimitry Andric    if (__libcpp_isnan_or_builtin(__x.real()))
12160b57cec5SDimitry Andric    {
12170b57cec5SDimitry Andric        return complex<_Tp>(__x.real(), __x.real());
12180b57cec5SDimitry Andric    }
12190b57cec5SDimitry Andric    if (__libcpp_isinf_or_builtin(__x.real()))
12200b57cec5SDimitry Andric    {
12210b57cec5SDimitry Andric        return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
12220b57cec5SDimitry Andric    }
12230b57cec5SDimitry Andric    if (abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0))
12240b57cec5SDimitry Andric    {
12250b57cec5SDimitry Andric        return complex<_Tp>(copysign(_Tp(INFINITY), __x.real()), copysign(_Tp(0), __x.imag()));
12260b57cec5SDimitry Andric    }
12270b57cec5SDimitry Andric    complex<_Tp> __z = log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
12280b57cec5SDimitry Andric    return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
12290b57cec5SDimitry Andric}
12300b57cec5SDimitry Andric
12310b57cec5SDimitry Andric// sinh
12320b57cec5SDimitry Andric
12330b57cec5SDimitry Andrictemplate<class _Tp>
12340b57cec5SDimitry Andriccomplex<_Tp>
12350b57cec5SDimitry Andricsinh(const complex<_Tp>& __x)
12360b57cec5SDimitry Andric{
12370b57cec5SDimitry Andric    if (__libcpp_isinf_or_builtin(__x.real()) && !__libcpp_isfinite_or_builtin(__x.imag()))
12380b57cec5SDimitry Andric        return complex<_Tp>(__x.real(), _Tp(NAN));
12390b57cec5SDimitry Andric    if (__x.real() == 0 && !__libcpp_isfinite_or_builtin(__x.imag()))
12400b57cec5SDimitry Andric        return complex<_Tp>(__x.real(), _Tp(NAN));
12410b57cec5SDimitry Andric    if (__x.imag() == 0 && !__libcpp_isfinite_or_builtin(__x.real()))
12420b57cec5SDimitry Andric        return __x;
12430b57cec5SDimitry Andric    return complex<_Tp>(sinh(__x.real()) * cos(__x.imag()), cosh(__x.real()) * sin(__x.imag()));
12440b57cec5SDimitry Andric}
12450b57cec5SDimitry Andric
12460b57cec5SDimitry Andric// cosh
12470b57cec5SDimitry Andric
12480b57cec5SDimitry Andrictemplate<class _Tp>
12490b57cec5SDimitry Andriccomplex<_Tp>
12500b57cec5SDimitry Andriccosh(const complex<_Tp>& __x)
12510b57cec5SDimitry Andric{
12520b57cec5SDimitry Andric    if (__libcpp_isinf_or_builtin(__x.real()) && !__libcpp_isfinite_or_builtin(__x.imag()))
12530b57cec5SDimitry Andric        return complex<_Tp>(abs(__x.real()), _Tp(NAN));
12540b57cec5SDimitry Andric    if (__x.real() == 0 && !__libcpp_isfinite_or_builtin(__x.imag()))
12550b57cec5SDimitry Andric        return complex<_Tp>(_Tp(NAN), __x.real());
12560b57cec5SDimitry Andric    if (__x.real() == 0 && __x.imag() == 0)
12570b57cec5SDimitry Andric        return complex<_Tp>(_Tp(1), __x.imag());
12580b57cec5SDimitry Andric    if (__x.imag() == 0 && !__libcpp_isfinite_or_builtin(__x.real()))
12590b57cec5SDimitry Andric        return complex<_Tp>(abs(__x.real()), __x.imag());
12600b57cec5SDimitry Andric    return complex<_Tp>(cosh(__x.real()) * cos(__x.imag()), sinh(__x.real()) * sin(__x.imag()));
12610b57cec5SDimitry Andric}
12620b57cec5SDimitry Andric
12630b57cec5SDimitry Andric// tanh
12640b57cec5SDimitry Andric
12650b57cec5SDimitry Andrictemplate<class _Tp>
12660b57cec5SDimitry Andriccomplex<_Tp>
12670b57cec5SDimitry Andrictanh(const complex<_Tp>& __x)
12680b57cec5SDimitry Andric{
12690b57cec5SDimitry Andric    if (__libcpp_isinf_or_builtin(__x.real()))
12700b57cec5SDimitry Andric    {
12710b57cec5SDimitry Andric        if (!__libcpp_isfinite_or_builtin(__x.imag()))
12720b57cec5SDimitry Andric            return complex<_Tp>(_Tp(1), _Tp(0));
12730b57cec5SDimitry Andric        return complex<_Tp>(_Tp(1), copysign(_Tp(0), sin(_Tp(2) * __x.imag())));
12740b57cec5SDimitry Andric    }
12750b57cec5SDimitry Andric    if (__libcpp_isnan_or_builtin(__x.real()) && __x.imag() == 0)
12760b57cec5SDimitry Andric        return __x;
12770b57cec5SDimitry Andric    _Tp __2r(_Tp(2) * __x.real());
12780b57cec5SDimitry Andric    _Tp __2i(_Tp(2) * __x.imag());
12790b57cec5SDimitry Andric    _Tp __d(cosh(__2r) + cos(__2i));
12800b57cec5SDimitry Andric    _Tp __2rsh(sinh(__2r));
12810b57cec5SDimitry Andric    if (__libcpp_isinf_or_builtin(__2rsh) && __libcpp_isinf_or_builtin(__d))
12820b57cec5SDimitry Andric        return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1),
12830b57cec5SDimitry Andric                            __2i > _Tp(0) ? _Tp(0) : _Tp(-0.));
12840b57cec5SDimitry Andric    return  complex<_Tp>(__2rsh/__d, sin(__2i)/__d);
12850b57cec5SDimitry Andric}
12860b57cec5SDimitry Andric
12870b57cec5SDimitry Andric// asin
12880b57cec5SDimitry Andric
12890b57cec5SDimitry Andrictemplate<class _Tp>
12900b57cec5SDimitry Andriccomplex<_Tp>
12910b57cec5SDimitry Andricasin(const complex<_Tp>& __x)
12920b57cec5SDimitry Andric{
12930b57cec5SDimitry Andric    complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real()));
12940b57cec5SDimitry Andric    return complex<_Tp>(__z.imag(), -__z.real());
12950b57cec5SDimitry Andric}
12960b57cec5SDimitry Andric
12970b57cec5SDimitry Andric// acos
12980b57cec5SDimitry Andric
12990b57cec5SDimitry Andrictemplate<class _Tp>
13000b57cec5SDimitry Andriccomplex<_Tp>
13010b57cec5SDimitry Andricacos(const complex<_Tp>& __x)
13020b57cec5SDimitry Andric{
13030b57cec5SDimitry Andric    const _Tp __pi(atan2(+0., -0.));
13040b57cec5SDimitry Andric    if (__libcpp_isinf_or_builtin(__x.real()))
13050b57cec5SDimitry Andric    {
13060b57cec5SDimitry Andric        if (__libcpp_isnan_or_builtin(__x.imag()))
13070b57cec5SDimitry Andric            return complex<_Tp>(__x.imag(), __x.real());
13080b57cec5SDimitry Andric        if (__libcpp_isinf_or_builtin(__x.imag()))
13090b57cec5SDimitry Andric        {
13100b57cec5SDimitry Andric            if (__x.real() < _Tp(0))
13110b57cec5SDimitry Andric                return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
13120b57cec5SDimitry Andric            return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
13130b57cec5SDimitry Andric        }
13140b57cec5SDimitry Andric        if (__x.real() < _Tp(0))
13150b57cec5SDimitry Andric            return complex<_Tp>(__pi, signbit(__x.imag()) ? -__x.real() : __x.real());
13160b57cec5SDimitry Andric        return complex<_Tp>(_Tp(0), signbit(__x.imag()) ? __x.real() : -__x.real());
13170b57cec5SDimitry Andric    }
13180b57cec5SDimitry Andric    if (__libcpp_isnan_or_builtin(__x.real()))
13190b57cec5SDimitry Andric    {
13200b57cec5SDimitry Andric        if (__libcpp_isinf_or_builtin(__x.imag()))
13210b57cec5SDimitry Andric            return complex<_Tp>(__x.real(), -__x.imag());
13220b57cec5SDimitry Andric        return complex<_Tp>(__x.real(), __x.real());
13230b57cec5SDimitry Andric    }
13240b57cec5SDimitry Andric    if (__libcpp_isinf_or_builtin(__x.imag()))
13250b57cec5SDimitry Andric        return complex<_Tp>(__pi/_Tp(2), -__x.imag());
13260b57cec5SDimitry Andric    if (__x.real() == 0 && (__x.imag() == 0 || isnan(__x.imag())))
13270b57cec5SDimitry Andric        return complex<_Tp>(__pi/_Tp(2), -__x.imag());
13280b57cec5SDimitry Andric    complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1)));
13290b57cec5SDimitry Andric    if (signbit(__x.imag()))
13300b57cec5SDimitry Andric        return complex<_Tp>(abs(__z.imag()), abs(__z.real()));
13310b57cec5SDimitry Andric    return complex<_Tp>(abs(__z.imag()), -abs(__z.real()));
13320b57cec5SDimitry Andric}
13330b57cec5SDimitry Andric
13340b57cec5SDimitry Andric// atan
13350b57cec5SDimitry Andric
13360b57cec5SDimitry Andrictemplate<class _Tp>
13370b57cec5SDimitry Andriccomplex<_Tp>
13380b57cec5SDimitry Andricatan(const complex<_Tp>& __x)
13390b57cec5SDimitry Andric{
13400b57cec5SDimitry Andric    complex<_Tp> __z = atanh(complex<_Tp>(-__x.imag(), __x.real()));
13410b57cec5SDimitry Andric    return complex<_Tp>(__z.imag(), -__z.real());
13420b57cec5SDimitry Andric}
13430b57cec5SDimitry Andric
13440b57cec5SDimitry Andric// sin
13450b57cec5SDimitry Andric
13460b57cec5SDimitry Andrictemplate<class _Tp>
13470b57cec5SDimitry Andriccomplex<_Tp>
13480b57cec5SDimitry Andricsin(const complex<_Tp>& __x)
13490b57cec5SDimitry Andric{
13500b57cec5SDimitry Andric    complex<_Tp> __z = sinh(complex<_Tp>(-__x.imag(), __x.real()));
13510b57cec5SDimitry Andric    return complex<_Tp>(__z.imag(), -__z.real());
13520b57cec5SDimitry Andric}
13530b57cec5SDimitry Andric
13540b57cec5SDimitry Andric// cos
13550b57cec5SDimitry Andric
13560b57cec5SDimitry Andrictemplate<class _Tp>
13570b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
13580b57cec5SDimitry Andriccomplex<_Tp>
13590b57cec5SDimitry Andriccos(const complex<_Tp>& __x)
13600b57cec5SDimitry Andric{
13610b57cec5SDimitry Andric    return cosh(complex<_Tp>(-__x.imag(), __x.real()));
13620b57cec5SDimitry Andric}
13630b57cec5SDimitry Andric
13640b57cec5SDimitry Andric// tan
13650b57cec5SDimitry Andric
13660b57cec5SDimitry Andrictemplate<class _Tp>
13670b57cec5SDimitry Andriccomplex<_Tp>
13680b57cec5SDimitry Andrictan(const complex<_Tp>& __x)
13690b57cec5SDimitry Andric{
13700b57cec5SDimitry Andric    complex<_Tp> __z = tanh(complex<_Tp>(-__x.imag(), __x.real()));
13710b57cec5SDimitry Andric    return complex<_Tp>(__z.imag(), -__z.real());
13720b57cec5SDimitry Andric}
13730b57cec5SDimitry Andric
13740b57cec5SDimitry Andrictemplate<class _Tp, class _CharT, class _Traits>
13750b57cec5SDimitry Andricbasic_istream<_CharT, _Traits>&
13760b57cec5SDimitry Andricoperator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
13770b57cec5SDimitry Andric{
13780b57cec5SDimitry Andric    if (__is.good())
13790b57cec5SDimitry Andric    {
13800b57cec5SDimitry Andric        ws(__is);
13810b57cec5SDimitry Andric        if (__is.peek() == _CharT('('))
13820b57cec5SDimitry Andric        {
13830b57cec5SDimitry Andric            __is.get();
13840b57cec5SDimitry Andric            _Tp __r;
13850b57cec5SDimitry Andric            __is >> __r;
13860b57cec5SDimitry Andric            if (!__is.fail())
13870b57cec5SDimitry Andric            {
13880b57cec5SDimitry Andric                ws(__is);
13890b57cec5SDimitry Andric                _CharT __c = __is.peek();
13900b57cec5SDimitry Andric                if (__c == _CharT(','))
13910b57cec5SDimitry Andric                {
13920b57cec5SDimitry Andric                    __is.get();
13930b57cec5SDimitry Andric                    _Tp __i;
13940b57cec5SDimitry Andric                    __is >> __i;
13950b57cec5SDimitry Andric                    if (!__is.fail())
13960b57cec5SDimitry Andric                    {
13970b57cec5SDimitry Andric                        ws(__is);
13980b57cec5SDimitry Andric                        __c = __is.peek();
13990b57cec5SDimitry Andric                        if (__c == _CharT(')'))
14000b57cec5SDimitry Andric                        {
14010b57cec5SDimitry Andric                            __is.get();
14020b57cec5SDimitry Andric                            __x = complex<_Tp>(__r, __i);
14030b57cec5SDimitry Andric                        }
14040b57cec5SDimitry Andric                        else
14055ffd83dbSDimitry Andric                            __is.setstate(__is.failbit);
14060b57cec5SDimitry Andric                    }
14070b57cec5SDimitry Andric                    else
14085ffd83dbSDimitry Andric                        __is.setstate(__is.failbit);
14090b57cec5SDimitry Andric                }
14100b57cec5SDimitry Andric                else if (__c == _CharT(')'))
14110b57cec5SDimitry Andric                {
14120b57cec5SDimitry Andric                    __is.get();
14130b57cec5SDimitry Andric                    __x = complex<_Tp>(__r, _Tp(0));
14140b57cec5SDimitry Andric                }
14150b57cec5SDimitry Andric                else
14165ffd83dbSDimitry Andric                    __is.setstate(__is.failbit);
14170b57cec5SDimitry Andric            }
14180b57cec5SDimitry Andric            else
14195ffd83dbSDimitry Andric                __is.setstate(__is.failbit);
14200b57cec5SDimitry Andric        }
14210b57cec5SDimitry Andric        else
14220b57cec5SDimitry Andric        {
14230b57cec5SDimitry Andric            _Tp __r;
14240b57cec5SDimitry Andric            __is >> __r;
14250b57cec5SDimitry Andric            if (!__is.fail())
14260b57cec5SDimitry Andric                __x = complex<_Tp>(__r, _Tp(0));
14270b57cec5SDimitry Andric            else
14285ffd83dbSDimitry Andric                __is.setstate(__is.failbit);
14290b57cec5SDimitry Andric        }
14300b57cec5SDimitry Andric    }
14310b57cec5SDimitry Andric    else
14325ffd83dbSDimitry Andric        __is.setstate(__is.failbit);
14330b57cec5SDimitry Andric    return __is;
14340b57cec5SDimitry Andric}
14350b57cec5SDimitry Andric
1436*e8d8bef9SDimitry Andric#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
14370b57cec5SDimitry Andrictemplate<class _Tp, class _CharT, class _Traits>
14380b57cec5SDimitry Andricbasic_ostream<_CharT, _Traits>&
14390b57cec5SDimitry Andricoperator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
14400b57cec5SDimitry Andric{
14410b57cec5SDimitry Andric    basic_ostringstream<_CharT, _Traits> __s;
14420b57cec5SDimitry Andric    __s.flags(__os.flags());
14430b57cec5SDimitry Andric    __s.imbue(__os.getloc());
14440b57cec5SDimitry Andric    __s.precision(__os.precision());
14450b57cec5SDimitry Andric    __s << '(' << __x.real() << ',' << __x.imag() << ')';
14460b57cec5SDimitry Andric    return __os << __s.str();
14470b57cec5SDimitry Andric}
1448*e8d8bef9SDimitry Andric#endif // !_LIBCPP_HAS_NO_LOCALIZATION
14490b57cec5SDimitry Andric
14500b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
14510b57cec5SDimitry Andric// Literal suffix for complex number literals [complex.literals]
14520b57cec5SDimitry Andricinline namespace literals
14530b57cec5SDimitry Andric{
14540b57cec5SDimitry Andric  inline namespace complex_literals
14550b57cec5SDimitry Andric  {
14560b57cec5SDimitry Andric    constexpr complex<long double> operator""il(long double __im)
14570b57cec5SDimitry Andric    {
14580b57cec5SDimitry Andric        return { 0.0l, __im };
14590b57cec5SDimitry Andric    }
14600b57cec5SDimitry Andric
14610b57cec5SDimitry Andric    constexpr complex<long double> operator""il(unsigned long long __im)
14620b57cec5SDimitry Andric    {
14630b57cec5SDimitry Andric        return { 0.0l, static_cast<long double>(__im) };
14640b57cec5SDimitry Andric    }
14650b57cec5SDimitry Andric
14660b57cec5SDimitry Andric
14670b57cec5SDimitry Andric    constexpr complex<double> operator""i(long double __im)
14680b57cec5SDimitry Andric    {
14690b57cec5SDimitry Andric        return { 0.0, static_cast<double>(__im) };
14700b57cec5SDimitry Andric    }
14710b57cec5SDimitry Andric
14720b57cec5SDimitry Andric    constexpr complex<double> operator""i(unsigned long long __im)
14730b57cec5SDimitry Andric    {
14740b57cec5SDimitry Andric        return { 0.0, static_cast<double>(__im) };
14750b57cec5SDimitry Andric    }
14760b57cec5SDimitry Andric
14770b57cec5SDimitry Andric
14780b57cec5SDimitry Andric    constexpr complex<float> operator""if(long double __im)
14790b57cec5SDimitry Andric    {
14800b57cec5SDimitry Andric        return { 0.0f, static_cast<float>(__im) };
14810b57cec5SDimitry Andric    }
14820b57cec5SDimitry Andric
14830b57cec5SDimitry Andric    constexpr complex<float> operator""if(unsigned long long __im)
14840b57cec5SDimitry Andric    {
14850b57cec5SDimitry Andric        return { 0.0f, static_cast<float>(__im) };
14860b57cec5SDimitry Andric    }
14870b57cec5SDimitry Andric  }
14880b57cec5SDimitry Andric}
14890b57cec5SDimitry Andric#endif
14900b57cec5SDimitry Andric
14910b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
14920b57cec5SDimitry Andric
14930b57cec5SDimitry Andric#endif  // _LIBCPP_COMPLEX
1494