Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===--------------------------- complex ----------------------------------===// |
| 3 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_COMPLEX |
| 11 | #define _LIBCPP_COMPLEX |
| 12 | |
| 13 | /* |
| 14 | complex synopsis |
| 15 | |
| 16 | namespace std |
| 17 | { |
| 18 | |
| 19 | template<class T> |
| 20 | class complex |
| 21 | { |
| 22 | public: |
| 23 | typedef T value_type; |
| 24 | |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 25 | complex(const T& re = T(), const T& im = T()); // constexpr in C++14 |
| 26 | complex(const complex&); // constexpr in C++14 |
| 27 | template<class X> complex(const complex<X>&); // constexpr in C++14 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 28 | |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 29 | T real() const; // constexpr in C++14 |
| 30 | T imag() const; // constexpr in C++14 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 31 | |
| 32 | void real(T); |
| 33 | void imag(T); |
| 34 | |
| 35 | complex<T>& operator= (const T&); |
| 36 | complex<T>& operator+=(const T&); |
| 37 | complex<T>& operator-=(const T&); |
| 38 | complex<T>& operator*=(const T&); |
| 39 | complex<T>& operator/=(const T&); |
| 40 | |
| 41 | complex& operator=(const complex&); |
| 42 | template<class X> complex<T>& operator= (const complex<X>&); |
| 43 | template<class X> complex<T>& operator+=(const complex<X>&); |
| 44 | template<class X> complex<T>& operator-=(const complex<X>&); |
| 45 | template<class X> complex<T>& operator*=(const complex<X>&); |
| 46 | template<class X> complex<T>& operator/=(const complex<X>&); |
| 47 | }; |
| 48 | |
| 49 | template<> |
| 50 | class complex<float> |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 51 | { |
| 52 | public: |
| 53 | typedef float value_type; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 54 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 55 | constexpr complex(float re = 0.0f, float im = 0.0f); |
| 56 | explicit constexpr complex(const complex<double>&); |
| 57 | explicit constexpr complex(const complex<long double>&); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 58 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 59 | constexpr float real() const; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 60 | void real(float); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 61 | constexpr float imag() const; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 62 | void imag(float); |
| 63 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 64 | complex<float>& operator= (float); |
| 65 | complex<float>& operator+=(float); |
| 66 | complex<float>& operator-=(float); |
| 67 | complex<float>& operator*=(float); |
| 68 | complex<float>& operator/=(float); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 69 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 70 | complex<float>& operator=(const complex<float>&); |
| 71 | template<class X> complex<float>& operator= (const complex<X>&); |
| 72 | template<class X> complex<float>& operator+=(const complex<X>&); |
| 73 | template<class X> complex<float>& operator-=(const complex<X>&); |
| 74 | template<class X> complex<float>& operator*=(const complex<X>&); |
| 75 | template<class X> complex<float>& operator/=(const complex<X>&); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | template<> |
| 79 | class complex<double> |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 80 | { |
| 81 | public: |
| 82 | typedef double value_type; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 83 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 84 | constexpr complex(double re = 0.0, double im = 0.0); |
| 85 | constexpr complex(const complex<float>&); |
| 86 | explicit constexpr complex(const complex<long double>&); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 87 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 88 | constexpr double real() const; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 89 | void real(double); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 90 | constexpr double imag() const; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 91 | void imag(double); |
| 92 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 93 | complex<double>& operator= (double); |
| 94 | complex<double>& operator+=(double); |
| 95 | complex<double>& operator-=(double); |
| 96 | complex<double>& operator*=(double); |
| 97 | complex<double>& operator/=(double); |
| 98 | complex<double>& operator=(const complex<double>&); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 99 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 100 | template<class X> complex<double>& operator= (const complex<X>&); |
| 101 | template<class X> complex<double>& operator+=(const complex<X>&); |
| 102 | template<class X> complex<double>& operator-=(const complex<X>&); |
| 103 | template<class X> complex<double>& operator*=(const complex<X>&); |
| 104 | template<class X> complex<double>& operator/=(const complex<X>&); |
| 105 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 106 | |
| 107 | template<> |
| 108 | class complex<long double> |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 109 | { |
| 110 | public: |
| 111 | typedef long double value_type; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 112 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 113 | constexpr complex(long double re = 0.0L, long double im = 0.0L); |
| 114 | constexpr complex(const complex<float>&); |
| 115 | constexpr complex(const complex<double>&); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 116 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 117 | constexpr long double real() const; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 118 | void real(long double); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 119 | constexpr long double imag() const; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 120 | void imag(long double); |
| 121 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 122 | complex<long double>& operator=(const complex<long double>&); |
| 123 | complex<long double>& operator= (long double); |
| 124 | complex<long double>& operator+=(long double); |
| 125 | complex<long double>& operator-=(long double); |
| 126 | complex<long double>& operator*=(long double); |
| 127 | complex<long double>& operator/=(long double); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 128 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 129 | template<class X> complex<long double>& operator= (const complex<X>&); |
| 130 | template<class X> complex<long double>& operator+=(const complex<X>&); |
| 131 | template<class X> complex<long double>& operator-=(const complex<X>&); |
| 132 | template<class X> complex<long double>& operator*=(const complex<X>&); |
| 133 | template<class X> complex<long double>& operator/=(const complex<X>&); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | // 26.3.6 operators: |
| 137 | template<class T> complex<T> operator+(const complex<T>&, const complex<T>&); |
| 138 | template<class T> complex<T> operator+(const complex<T>&, const T&); |
| 139 | template<class T> complex<T> operator+(const T&, const complex<T>&); |
| 140 | template<class T> complex<T> operator-(const complex<T>&, const complex<T>&); |
| 141 | template<class T> complex<T> operator-(const complex<T>&, const T&); |
| 142 | template<class T> complex<T> operator-(const T&, const complex<T>&); |
| 143 | template<class T> complex<T> operator*(const complex<T>&, const complex<T>&); |
| 144 | template<class T> complex<T> operator*(const complex<T>&, const T&); |
| 145 | template<class T> complex<T> operator*(const T&, const complex<T>&); |
| 146 | template<class T> complex<T> operator/(const complex<T>&, const complex<T>&); |
| 147 | template<class T> complex<T> operator/(const complex<T>&, const T&); |
| 148 | template<class T> complex<T> operator/(const T&, const complex<T>&); |
| 149 | template<class T> complex<T> operator+(const complex<T>&); |
| 150 | template<class T> complex<T> operator-(const complex<T>&); |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 151 | template<class T> bool operator==(const complex<T>&, const complex<T>&); // constexpr in C++14 |
| 152 | template<class T> bool operator==(const complex<T>&, const T&); // constexpr in C++14 |
| 153 | template<class T> bool operator==(const T&, const complex<T>&); // constexpr in C++14 |
| 154 | template<class T> bool operator!=(const complex<T>&, const complex<T>&); // constexpr in C++14 |
| 155 | template<class T> bool operator!=(const complex<T>&, const T&); // constexpr in C++14 |
| 156 | template<class T> bool operator!=(const T&, const complex<T>&); // constexpr in C++14 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 157 | |
| 158 | template<class T, class charT, class traits> |
| 159 | basic_istream<charT, traits>& |
| 160 | operator>>(basic_istream<charT, traits>&, complex<T>&); |
| 161 | template<class T, class charT, class traits> |
| 162 | basic_ostream<charT, traits>& |
| 163 | operator<<(basic_ostream<charT, traits>&, const complex<T>&); |
| 164 | |
| 165 | // 26.3.7 values: |
| 166 | |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 167 | template<class T> T real(const complex<T>&); // constexpr in C++14 |
| 168 | long double real(long double); // constexpr in C++14 |
| 169 | double real(double); // constexpr in C++14 |
| 170 | template<Integral T> double real(T); // constexpr in C++14 |
| 171 | float real(float); // constexpr in C++14 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 172 | |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 173 | template<class T> T imag(const complex<T>&); // constexpr in C++14 |
| 174 | long double imag(long double); // constexpr in C++14 |
| 175 | double imag(double); // constexpr in C++14 |
| 176 | template<Integral T> double imag(T); // constexpr in C++14 |
| 177 | float imag(float); // constexpr in C++14 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 178 | |
| 179 | template<class T> T abs(const complex<T>&); |
| 180 | |
| 181 | template<class T> T arg(const complex<T>&); |
| 182 | long double arg(long double); |
| 183 | double arg(double); |
| 184 | template<Integral T> double arg(T); |
| 185 | float arg(float); |
| 186 | |
| 187 | template<class T> T norm(const complex<T>&); |
| 188 | long double norm(long double); |
| 189 | double norm(double); |
| 190 | template<Integral T> double norm(T); |
| 191 | float norm(float); |
| 192 | |
Howard Hinnant | a753356 | 2010-11-18 17:34:48 +0000 | [diff] [blame] | 193 | template<class T> complex<T> conj(const complex<T>&); |
| 194 | complex<long double> conj(long double); |
| 195 | complex<double> conj(double); |
| 196 | template<Integral T> complex<double> conj(T); |
| 197 | complex<float> conj(float); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 198 | |
Howard Hinnant | a753356 | 2010-11-18 17:34:48 +0000 | [diff] [blame] | 199 | template<class T> complex<T> proj(const complex<T>&); |
| 200 | complex<long double> proj(long double); |
| 201 | complex<double> proj(double); |
| 202 | template<Integral T> complex<double> proj(T); |
| 203 | complex<float> proj(float); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 204 | |
Marshall Clow | 2f28373 | 2018-01-31 21:42:39 +0000 | [diff] [blame] | 205 | template<class T> complex<T> polar(const T&, const T& = T()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 206 | |
| 207 | // 26.3.8 transcendentals: |
| 208 | template<class T> complex<T> acos(const complex<T>&); |
| 209 | template<class T> complex<T> asin(const complex<T>&); |
| 210 | template<class T> complex<T> atan(const complex<T>&); |
| 211 | template<class T> complex<T> acosh(const complex<T>&); |
| 212 | template<class T> complex<T> asinh(const complex<T>&); |
| 213 | template<class T> complex<T> atanh(const complex<T>&); |
| 214 | template<class T> complex<T> cos (const complex<T>&); |
| 215 | template<class T> complex<T> cosh (const complex<T>&); |
| 216 | template<class T> complex<T> exp (const complex<T>&); |
| 217 | template<class T> complex<T> log (const complex<T>&); |
| 218 | template<class T> complex<T> log10(const complex<T>&); |
| 219 | |
| 220 | template<class T> complex<T> pow(const complex<T>&, const T&); |
| 221 | template<class T> complex<T> pow(const complex<T>&, const complex<T>&); |
| 222 | template<class T> complex<T> pow(const T&, const complex<T>&); |
| 223 | |
| 224 | template<class T> complex<T> sin (const complex<T>&); |
| 225 | template<class T> complex<T> sinh (const complex<T>&); |
| 226 | template<class T> complex<T> sqrt (const complex<T>&); |
| 227 | template<class T> complex<T> tan (const complex<T>&); |
| 228 | template<class T> complex<T> tanh (const complex<T>&); |
| 229 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 230 | } // std |
| 231 | |
| 232 | */ |
| 233 | |
| 234 | #include <__config> |
| 235 | #include <type_traits> |
| 236 | #include <stdexcept> |
| 237 | #include <cmath> |
Louis Dionne | 2d93782 | 2020-02-19 16:09:41 -0500 | [diff] [blame] | 238 | #include <iosfwd> |
Antonio Sanchez | d4fb740 | 2020-05-07 13:49:12 -0400 | [diff] [blame] | 239 | #include <sstream> |
Marshall Clow | 0a1e750 | 2018-09-12 19:41:40 +0000 | [diff] [blame] | 240 | #include <version> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 241 | |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 242 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 243 | #pragma GCC system_header |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 244 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 245 | |
| 246 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 247 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 248 | template<class _Tp> class _LIBCPP_TEMPLATE_VIS complex; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 249 | |
| 250 | template<class _Tp> complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w); |
| 251 | template<class _Tp> complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y); |
| 252 | |
| 253 | template<class _Tp> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 254 | class _LIBCPP_TEMPLATE_VIS complex |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 255 | { |
| 256 | public: |
| 257 | typedef _Tp value_type; |
| 258 | private: |
| 259 | value_type __re_; |
| 260 | value_type __im_; |
| 261 | public: |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 262 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 263 | complex(const value_type& __re = value_type(), const value_type& __im = value_type()) |
| 264 | : __re_(__re), __im_(__im) {} |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 265 | template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 266 | complex(const complex<_Xp>& __c) |
| 267 | : __re_(__c.real()), __im_(__c.imag()) {} |
| 268 | |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 269 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type real() const {return __re_;} |
| 270 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type imag() const {return __im_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 271 | |
| 272 | _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;} |
| 273 | _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;} |
| 274 | |
Howard Hinnant | 233b73f | 2012-01-10 15:15:47 +0000 | [diff] [blame] | 275 | _LIBCPP_INLINE_VISIBILITY complex& operator= (const value_type& __re) |
| 276 | {__re_ = __re; __im_ = value_type(); return *this;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 277 | _LIBCPP_INLINE_VISIBILITY complex& operator+=(const value_type& __re) {__re_ += __re; return *this;} |
| 278 | _LIBCPP_INLINE_VISIBILITY complex& operator-=(const value_type& __re) {__re_ -= __re; return *this;} |
| 279 | _LIBCPP_INLINE_VISIBILITY complex& operator*=(const value_type& __re) {__re_ *= __re; __im_ *= __re; return *this;} |
| 280 | _LIBCPP_INLINE_VISIBILITY complex& operator/=(const value_type& __re) {__re_ /= __re; __im_ /= __re; return *this;} |
| 281 | |
| 282 | template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c) |
| 283 | { |
| 284 | __re_ = __c.real(); |
| 285 | __im_ = __c.imag(); |
| 286 | return *this; |
| 287 | } |
| 288 | template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c) |
| 289 | { |
| 290 | __re_ += __c.real(); |
| 291 | __im_ += __c.imag(); |
| 292 | return *this; |
| 293 | } |
| 294 | template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c) |
| 295 | { |
| 296 | __re_ -= __c.real(); |
| 297 | __im_ -= __c.imag(); |
| 298 | return *this; |
| 299 | } |
| 300 | template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c) |
| 301 | { |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 302 | *this = *this * complex(__c.real(), __c.imag()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 303 | return *this; |
| 304 | } |
| 305 | template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c) |
| 306 | { |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 307 | *this = *this / complex(__c.real(), __c.imag()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 308 | return *this; |
| 309 | } |
| 310 | }; |
| 311 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 312 | template<> class _LIBCPP_TEMPLATE_VIS complex<double>; |
| 313 | template<> class _LIBCPP_TEMPLATE_VIS complex<long double>; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 314 | |
| 315 | template<> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 316 | class _LIBCPP_TEMPLATE_VIS complex<float> |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 317 | { |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 318 | float __re_; |
| 319 | float __im_; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 320 | public: |
| 321 | typedef float value_type; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 322 | |
Howard Hinnant | 8e556b6 | 2012-07-20 22:18:27 +0000 | [diff] [blame] | 323 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(float __re = 0.0f, float __im = 0.0f) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 324 | : __re_(__re), __im_(__im) {} |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 325 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e556b6 | 2012-07-20 22:18:27 +0000 | [diff] [blame] | 326 | explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c); |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 327 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e556b6 | 2012-07-20 22:18:27 +0000 | [diff] [blame] | 328 | explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 329 | |
Howard Hinnant | 8e556b6 | 2012-07-20 22:18:27 +0000 | [diff] [blame] | 330 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float real() const {return __re_;} |
| 331 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float imag() const {return __im_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 332 | |
| 333 | _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;} |
| 334 | _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;} |
| 335 | |
Howard Hinnant | 233b73f | 2012-01-10 15:15:47 +0000 | [diff] [blame] | 336 | _LIBCPP_INLINE_VISIBILITY complex& operator= (float __re) |
| 337 | {__re_ = __re; __im_ = value_type(); return *this;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 338 | _LIBCPP_INLINE_VISIBILITY complex& operator+=(float __re) {__re_ += __re; return *this;} |
| 339 | _LIBCPP_INLINE_VISIBILITY complex& operator-=(float __re) {__re_ -= __re; return *this;} |
| 340 | _LIBCPP_INLINE_VISIBILITY complex& operator*=(float __re) {__re_ *= __re; __im_ *= __re; return *this;} |
| 341 | _LIBCPP_INLINE_VISIBILITY complex& operator/=(float __re) {__re_ /= __re; __im_ /= __re; return *this;} |
| 342 | |
| 343 | template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c) |
| 344 | { |
| 345 | __re_ = __c.real(); |
| 346 | __im_ = __c.imag(); |
| 347 | return *this; |
| 348 | } |
| 349 | template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c) |
| 350 | { |
| 351 | __re_ += __c.real(); |
| 352 | __im_ += __c.imag(); |
| 353 | return *this; |
| 354 | } |
| 355 | template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c) |
| 356 | { |
| 357 | __re_ -= __c.real(); |
| 358 | __im_ -= __c.imag(); |
| 359 | return *this; |
| 360 | } |
| 361 | template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c) |
| 362 | { |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 363 | *this = *this * complex(__c.real(), __c.imag()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 364 | return *this; |
| 365 | } |
| 366 | template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c) |
| 367 | { |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 368 | *this = *this / complex(__c.real(), __c.imag()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 369 | return *this; |
| 370 | } |
| 371 | }; |
| 372 | |
| 373 | template<> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 374 | class _LIBCPP_TEMPLATE_VIS complex<double> |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 375 | { |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 376 | double __re_; |
| 377 | double __im_; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 378 | public: |
| 379 | typedef double value_type; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 380 | |
Howard Hinnant | 8e556b6 | 2012-07-20 22:18:27 +0000 | [diff] [blame] | 381 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 382 | : __re_(__re), __im_(__im) {} |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 383 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e556b6 | 2012-07-20 22:18:27 +0000 | [diff] [blame] | 384 | _LIBCPP_CONSTEXPR complex(const complex<float>& __c); |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 385 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e556b6 | 2012-07-20 22:18:27 +0000 | [diff] [blame] | 386 | explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 387 | |
Howard Hinnant | 8e556b6 | 2012-07-20 22:18:27 +0000 | [diff] [blame] | 388 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double real() const {return __re_;} |
| 389 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double imag() const {return __im_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 390 | |
| 391 | _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;} |
| 392 | _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;} |
| 393 | |
Howard Hinnant | 233b73f | 2012-01-10 15:15:47 +0000 | [diff] [blame] | 394 | _LIBCPP_INLINE_VISIBILITY complex& operator= (double __re) |
| 395 | {__re_ = __re; __im_ = value_type(); return *this;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 396 | _LIBCPP_INLINE_VISIBILITY complex& operator+=(double __re) {__re_ += __re; return *this;} |
| 397 | _LIBCPP_INLINE_VISIBILITY complex& operator-=(double __re) {__re_ -= __re; return *this;} |
| 398 | _LIBCPP_INLINE_VISIBILITY complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;} |
| 399 | _LIBCPP_INLINE_VISIBILITY complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;} |
| 400 | |
| 401 | template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c) |
| 402 | { |
| 403 | __re_ = __c.real(); |
| 404 | __im_ = __c.imag(); |
| 405 | return *this; |
| 406 | } |
| 407 | template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c) |
| 408 | { |
| 409 | __re_ += __c.real(); |
| 410 | __im_ += __c.imag(); |
| 411 | return *this; |
| 412 | } |
| 413 | template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c) |
| 414 | { |
| 415 | __re_ -= __c.real(); |
| 416 | __im_ -= __c.imag(); |
| 417 | return *this; |
| 418 | } |
| 419 | template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c) |
| 420 | { |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 421 | *this = *this * complex(__c.real(), __c.imag()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 422 | return *this; |
| 423 | } |
| 424 | template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c) |
| 425 | { |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 426 | *this = *this / complex(__c.real(), __c.imag()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 427 | return *this; |
| 428 | } |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 429 | }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 430 | |
| 431 | template<> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 432 | class _LIBCPP_TEMPLATE_VIS complex<long double> |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 433 | { |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 434 | long double __re_; |
| 435 | long double __im_; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 436 | public: |
| 437 | typedef long double value_type; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 438 | |
Howard Hinnant | 8e556b6 | 2012-07-20 22:18:27 +0000 | [diff] [blame] | 439 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 440 | : __re_(__re), __im_(__im) {} |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 441 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e556b6 | 2012-07-20 22:18:27 +0000 | [diff] [blame] | 442 | _LIBCPP_CONSTEXPR complex(const complex<float>& __c); |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 443 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 8e556b6 | 2012-07-20 22:18:27 +0000 | [diff] [blame] | 444 | _LIBCPP_CONSTEXPR complex(const complex<double>& __c); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 445 | |
Howard Hinnant | 8e556b6 | 2012-07-20 22:18:27 +0000 | [diff] [blame] | 446 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double real() const {return __re_;} |
| 447 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double imag() const {return __im_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 448 | |
| 449 | _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;} |
| 450 | _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;} |
| 451 | |
Howard Hinnant | 233b73f | 2012-01-10 15:15:47 +0000 | [diff] [blame] | 452 | _LIBCPP_INLINE_VISIBILITY complex& operator= (long double __re) |
| 453 | {__re_ = __re; __im_ = value_type(); return *this;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 454 | _LIBCPP_INLINE_VISIBILITY complex& operator+=(long double __re) {__re_ += __re; return *this;} |
| 455 | _LIBCPP_INLINE_VISIBILITY complex& operator-=(long double __re) {__re_ -= __re; return *this;} |
| 456 | _LIBCPP_INLINE_VISIBILITY complex& operator*=(long double __re) {__re_ *= __re; __im_ *= __re; return *this;} |
| 457 | _LIBCPP_INLINE_VISIBILITY complex& operator/=(long double __re) {__re_ /= __re; __im_ /= __re; return *this;} |
| 458 | |
| 459 | template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c) |
| 460 | { |
| 461 | __re_ = __c.real(); |
| 462 | __im_ = __c.imag(); |
| 463 | return *this; |
| 464 | } |
| 465 | template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c) |
| 466 | { |
| 467 | __re_ += __c.real(); |
| 468 | __im_ += __c.imag(); |
| 469 | return *this; |
| 470 | } |
| 471 | template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c) |
| 472 | { |
| 473 | __re_ -= __c.real(); |
| 474 | __im_ -= __c.imag(); |
| 475 | return *this; |
| 476 | } |
| 477 | template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c) |
| 478 | { |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 479 | *this = *this * complex(__c.real(), __c.imag()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 480 | return *this; |
| 481 | } |
| 482 | template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c) |
| 483 | { |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 484 | *this = *this / complex(__c.real(), __c.imag()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 485 | return *this; |
| 486 | } |
| 487 | }; |
| 488 | |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 489 | inline |
Howard Hinnant | 8e556b6 | 2012-07-20 22:18:27 +0000 | [diff] [blame] | 490 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 491 | complex<float>::complex(const complex<double>& __c) |
| 492 | : __re_(__c.real()), __im_(__c.imag()) {} |
| 493 | |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 494 | inline |
Howard Hinnant | 8e556b6 | 2012-07-20 22:18:27 +0000 | [diff] [blame] | 495 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 496 | complex<float>::complex(const complex<long double>& __c) |
| 497 | : __re_(__c.real()), __im_(__c.imag()) {} |
| 498 | |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 499 | inline |
Howard Hinnant | 8e556b6 | 2012-07-20 22:18:27 +0000 | [diff] [blame] | 500 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 501 | complex<double>::complex(const complex<float>& __c) |
| 502 | : __re_(__c.real()), __im_(__c.imag()) {} |
| 503 | |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 504 | inline |
Howard Hinnant | 8e556b6 | 2012-07-20 22:18:27 +0000 | [diff] [blame] | 505 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 506 | complex<double>::complex(const complex<long double>& __c) |
| 507 | : __re_(__c.real()), __im_(__c.imag()) {} |
| 508 | |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 509 | inline |
Howard Hinnant | 8e556b6 | 2012-07-20 22:18:27 +0000 | [diff] [blame] | 510 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 511 | complex<long double>::complex(const complex<float>& __c) |
| 512 | : __re_(__c.real()), __im_(__c.imag()) {} |
| 513 | |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 514 | inline |
Howard Hinnant | 8e556b6 | 2012-07-20 22:18:27 +0000 | [diff] [blame] | 515 | _LIBCPP_CONSTEXPR |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 516 | complex<long double>::complex(const complex<double>& __c) |
| 517 | : __re_(__c.real()), __im_(__c.imag()) {} |
| 518 | |
| 519 | // 26.3.6 operators: |
| 520 | |
| 521 | template<class _Tp> |
| 522 | inline _LIBCPP_INLINE_VISIBILITY |
| 523 | complex<_Tp> |
| 524 | operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) |
| 525 | { |
| 526 | complex<_Tp> __t(__x); |
| 527 | __t += __y; |
| 528 | return __t; |
| 529 | } |
| 530 | |
| 531 | template<class _Tp> |
| 532 | inline _LIBCPP_INLINE_VISIBILITY |
| 533 | complex<_Tp> |
| 534 | operator+(const complex<_Tp>& __x, const _Tp& __y) |
| 535 | { |
| 536 | complex<_Tp> __t(__x); |
| 537 | __t += __y; |
| 538 | return __t; |
| 539 | } |
| 540 | |
| 541 | template<class _Tp> |
| 542 | inline _LIBCPP_INLINE_VISIBILITY |
| 543 | complex<_Tp> |
| 544 | operator+(const _Tp& __x, const complex<_Tp>& __y) |
| 545 | { |
| 546 | complex<_Tp> __t(__y); |
| 547 | __t += __x; |
| 548 | return __t; |
| 549 | } |
| 550 | |
| 551 | template<class _Tp> |
| 552 | inline _LIBCPP_INLINE_VISIBILITY |
| 553 | complex<_Tp> |
| 554 | operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) |
| 555 | { |
| 556 | complex<_Tp> __t(__x); |
| 557 | __t -= __y; |
| 558 | return __t; |
| 559 | } |
| 560 | |
| 561 | template<class _Tp> |
| 562 | inline _LIBCPP_INLINE_VISIBILITY |
| 563 | complex<_Tp> |
| 564 | operator-(const complex<_Tp>& __x, const _Tp& __y) |
| 565 | { |
| 566 | complex<_Tp> __t(__x); |
| 567 | __t -= __y; |
| 568 | return __t; |
| 569 | } |
| 570 | |
| 571 | template<class _Tp> |
| 572 | inline _LIBCPP_INLINE_VISIBILITY |
| 573 | complex<_Tp> |
| 574 | operator-(const _Tp& __x, const complex<_Tp>& __y) |
| 575 | { |
| 576 | complex<_Tp> __t(-__y); |
| 577 | __t += __x; |
| 578 | return __t; |
| 579 | } |
| 580 | |
| 581 | template<class _Tp> |
| 582 | complex<_Tp> |
| 583 | operator*(const complex<_Tp>& __z, const complex<_Tp>& __w) |
| 584 | { |
| 585 | _Tp __a = __z.real(); |
| 586 | _Tp __b = __z.imag(); |
| 587 | _Tp __c = __w.real(); |
| 588 | _Tp __d = __w.imag(); |
| 589 | _Tp __ac = __a * __c; |
| 590 | _Tp __bd = __b * __d; |
| 591 | _Tp __ad = __a * __d; |
| 592 | _Tp __bc = __b * __c; |
| 593 | _Tp __x = __ac - __bd; |
| 594 | _Tp __y = __ad + __bc; |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 595 | if (__libcpp_isnan_or_builtin(__x) && __libcpp_isnan_or_builtin(__y)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 596 | { |
| 597 | bool __recalc = false; |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 598 | if (__libcpp_isinf_or_builtin(__a) || __libcpp_isinf_or_builtin(__b)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 599 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 600 | __a = copysign(__libcpp_isinf_or_builtin(__a) ? _Tp(1) : _Tp(0), __a); |
| 601 | __b = copysign(__libcpp_isinf_or_builtin(__b) ? _Tp(1) : _Tp(0), __b); |
| 602 | if (__libcpp_isnan_or_builtin(__c)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 603 | __c = copysign(_Tp(0), __c); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 604 | if (__libcpp_isnan_or_builtin(__d)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 605 | __d = copysign(_Tp(0), __d); |
| 606 | __recalc = true; |
| 607 | } |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 608 | if (__libcpp_isinf_or_builtin(__c) || __libcpp_isinf_or_builtin(__d)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 609 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 610 | __c = copysign(__libcpp_isinf_or_builtin(__c) ? _Tp(1) : _Tp(0), __c); |
| 611 | __d = copysign(__libcpp_isinf_or_builtin(__d) ? _Tp(1) : _Tp(0), __d); |
| 612 | if (__libcpp_isnan_or_builtin(__a)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 613 | __a = copysign(_Tp(0), __a); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 614 | if (__libcpp_isnan_or_builtin(__b)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 615 | __b = copysign(_Tp(0), __b); |
| 616 | __recalc = true; |
| 617 | } |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 618 | if (!__recalc && (__libcpp_isinf_or_builtin(__ac) || __libcpp_isinf_or_builtin(__bd) || |
| 619 | __libcpp_isinf_or_builtin(__ad) || __libcpp_isinf_or_builtin(__bc))) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 620 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 621 | if (__libcpp_isnan_or_builtin(__a)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 622 | __a = copysign(_Tp(0), __a); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 623 | if (__libcpp_isnan_or_builtin(__b)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 624 | __b = copysign(_Tp(0), __b); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 625 | if (__libcpp_isnan_or_builtin(__c)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 626 | __c = copysign(_Tp(0), __c); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 627 | if (__libcpp_isnan_or_builtin(__d)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 628 | __d = copysign(_Tp(0), __d); |
| 629 | __recalc = true; |
| 630 | } |
| 631 | if (__recalc) |
| 632 | { |
| 633 | __x = _Tp(INFINITY) * (__a * __c - __b * __d); |
| 634 | __y = _Tp(INFINITY) * (__a * __d + __b * __c); |
| 635 | } |
| 636 | } |
| 637 | return complex<_Tp>(__x, __y); |
| 638 | } |
| 639 | |
| 640 | template<class _Tp> |
| 641 | inline _LIBCPP_INLINE_VISIBILITY |
| 642 | complex<_Tp> |
| 643 | operator*(const complex<_Tp>& __x, const _Tp& __y) |
| 644 | { |
| 645 | complex<_Tp> __t(__x); |
| 646 | __t *= __y; |
| 647 | return __t; |
| 648 | } |
| 649 | |
| 650 | template<class _Tp> |
| 651 | inline _LIBCPP_INLINE_VISIBILITY |
| 652 | complex<_Tp> |
| 653 | operator*(const _Tp& __x, const complex<_Tp>& __y) |
| 654 | { |
| 655 | complex<_Tp> __t(__y); |
| 656 | __t *= __x; |
| 657 | return __t; |
| 658 | } |
| 659 | |
| 660 | template<class _Tp> |
| 661 | complex<_Tp> |
| 662 | operator/(const complex<_Tp>& __z, const complex<_Tp>& __w) |
| 663 | { |
| 664 | int __ilogbw = 0; |
| 665 | _Tp __a = __z.real(); |
| 666 | _Tp __b = __z.imag(); |
| 667 | _Tp __c = __w.real(); |
| 668 | _Tp __d = __w.imag(); |
| 669 | _Tp __logbw = logb(fmax(fabs(__c), fabs(__d))); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 670 | if (__libcpp_isfinite_or_builtin(__logbw)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 671 | { |
| 672 | __ilogbw = static_cast<int>(__logbw); |
| 673 | __c = scalbn(__c, -__ilogbw); |
| 674 | __d = scalbn(__d, -__ilogbw); |
| 675 | } |
| 676 | _Tp __denom = __c * __c + __d * __d; |
| 677 | _Tp __x = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw); |
| 678 | _Tp __y = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 679 | if (__libcpp_isnan_or_builtin(__x) && __libcpp_isnan_or_builtin(__y)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 680 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 681 | if ((__denom == _Tp(0)) && (!__libcpp_isnan_or_builtin(__a) || !__libcpp_isnan_or_builtin(__b))) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 682 | { |
| 683 | __x = copysign(_Tp(INFINITY), __c) * __a; |
| 684 | __y = copysign(_Tp(INFINITY), __c) * __b; |
| 685 | } |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 686 | else if ((__libcpp_isinf_or_builtin(__a) || __libcpp_isinf_or_builtin(__b)) && __libcpp_isfinite_or_builtin(__c) && __libcpp_isfinite_or_builtin(__d)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 687 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 688 | __a = copysign(__libcpp_isinf_or_builtin(__a) ? _Tp(1) : _Tp(0), __a); |
| 689 | __b = copysign(__libcpp_isinf_or_builtin(__b) ? _Tp(1) : _Tp(0), __b); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 690 | __x = _Tp(INFINITY) * (__a * __c + __b * __d); |
| 691 | __y = _Tp(INFINITY) * (__b * __c - __a * __d); |
| 692 | } |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 693 | else if (__libcpp_isinf_or_builtin(__logbw) && __logbw > _Tp(0) && __libcpp_isfinite_or_builtin(__a) && __libcpp_isfinite_or_builtin(__b)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 694 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 695 | __c = copysign(__libcpp_isinf_or_builtin(__c) ? _Tp(1) : _Tp(0), __c); |
| 696 | __d = copysign(__libcpp_isinf_or_builtin(__d) ? _Tp(1) : _Tp(0), __d); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 697 | __x = _Tp(0) * (__a * __c + __b * __d); |
| 698 | __y = _Tp(0) * (__b * __c - __a * __d); |
| 699 | } |
| 700 | } |
| 701 | return complex<_Tp>(__x, __y); |
| 702 | } |
| 703 | |
| 704 | template<class _Tp> |
| 705 | inline _LIBCPP_INLINE_VISIBILITY |
| 706 | complex<_Tp> |
| 707 | operator/(const complex<_Tp>& __x, const _Tp& __y) |
| 708 | { |
| 709 | return complex<_Tp>(__x.real() / __y, __x.imag() / __y); |
| 710 | } |
| 711 | |
| 712 | template<class _Tp> |
| 713 | inline _LIBCPP_INLINE_VISIBILITY |
| 714 | complex<_Tp> |
| 715 | operator/(const _Tp& __x, const complex<_Tp>& __y) |
| 716 | { |
| 717 | complex<_Tp> __t(__x); |
| 718 | __t /= __y; |
| 719 | return __t; |
| 720 | } |
| 721 | |
| 722 | template<class _Tp> |
| 723 | inline _LIBCPP_INLINE_VISIBILITY |
| 724 | complex<_Tp> |
| 725 | operator+(const complex<_Tp>& __x) |
| 726 | { |
| 727 | return __x; |
| 728 | } |
| 729 | |
| 730 | template<class _Tp> |
| 731 | inline _LIBCPP_INLINE_VISIBILITY |
| 732 | complex<_Tp> |
| 733 | operator-(const complex<_Tp>& __x) |
| 734 | { |
| 735 | return complex<_Tp>(-__x.real(), -__x.imag()); |
| 736 | } |
| 737 | |
| 738 | template<class _Tp> |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 739 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 740 | bool |
| 741 | operator==(const complex<_Tp>& __x, const complex<_Tp>& __y) |
| 742 | { |
| 743 | return __x.real() == __y.real() && __x.imag() == __y.imag(); |
| 744 | } |
| 745 | |
| 746 | template<class _Tp> |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 747 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 748 | bool |
| 749 | operator==(const complex<_Tp>& __x, const _Tp& __y) |
| 750 | { |
| 751 | return __x.real() == __y && __x.imag() == 0; |
| 752 | } |
| 753 | |
| 754 | template<class _Tp> |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 755 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 756 | bool |
| 757 | operator==(const _Tp& __x, const complex<_Tp>& __y) |
| 758 | { |
| 759 | return __x == __y.real() && 0 == __y.imag(); |
| 760 | } |
| 761 | |
| 762 | template<class _Tp> |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 763 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 764 | bool |
| 765 | operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y) |
| 766 | { |
| 767 | return !(__x == __y); |
| 768 | } |
| 769 | |
| 770 | template<class _Tp> |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 771 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 772 | bool |
| 773 | operator!=(const complex<_Tp>& __x, const _Tp& __y) |
| 774 | { |
| 775 | return !(__x == __y); |
| 776 | } |
| 777 | |
| 778 | template<class _Tp> |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 779 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 780 | bool |
| 781 | operator!=(const _Tp& __x, const complex<_Tp>& __y) |
| 782 | { |
| 783 | return !(__x == __y); |
| 784 | } |
| 785 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 786 | // 26.3.7 values: |
| 787 | |
Eric Fiselier | f8b772b | 2016-07-20 00:14:10 +0000 | [diff] [blame] | 788 | template <class _Tp, bool = is_integral<_Tp>::value, |
| 789 | bool = is_floating_point<_Tp>::value |
| 790 | > |
| 791 | struct __libcpp_complex_overload_traits {}; |
| 792 | |
| 793 | // Integral Types |
| 794 | template <class _Tp> |
| 795 | struct __libcpp_complex_overload_traits<_Tp, true, false> |
| 796 | { |
| 797 | typedef double _ValueType; |
| 798 | typedef complex<double> _ComplexType; |
| 799 | }; |
| 800 | |
| 801 | // Floating point types |
| 802 | template <class _Tp> |
| 803 | struct __libcpp_complex_overload_traits<_Tp, false, true> |
| 804 | { |
| 805 | typedef _Tp _ValueType; |
| 806 | typedef complex<_Tp> _ComplexType; |
| 807 | }; |
| 808 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 809 | // real |
| 810 | |
| 811 | template<class _Tp> |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 812 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 813 | _Tp |
| 814 | real(const complex<_Tp>& __c) |
| 815 | { |
| 816 | return __c.real(); |
| 817 | } |
| 818 | |
Eric Fiselier | f8b772b | 2016-07-20 00:14:10 +0000 | [diff] [blame] | 819 | template <class _Tp> |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 820 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Eric Fiselier | f8b772b | 2016-07-20 00:14:10 +0000 | [diff] [blame] | 821 | typename __libcpp_complex_overload_traits<_Tp>::_ValueType |
| 822 | real(_Tp __re) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 823 | { |
| 824 | return __re; |
| 825 | } |
| 826 | |
| 827 | // imag |
| 828 | |
| 829 | template<class _Tp> |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 830 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 831 | _Tp |
| 832 | imag(const complex<_Tp>& __c) |
| 833 | { |
| 834 | return __c.imag(); |
| 835 | } |
| 836 | |
Eric Fiselier | f8b772b | 2016-07-20 00:14:10 +0000 | [diff] [blame] | 837 | template <class _Tp> |
Marshall Clow | df7b694 | 2013-07-31 21:02:34 +0000 | [diff] [blame] | 838 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Eric Fiselier | f8b772b | 2016-07-20 00:14:10 +0000 | [diff] [blame] | 839 | typename __libcpp_complex_overload_traits<_Tp>::_ValueType |
Eric Fiselier | 6003c77 | 2016-12-23 23:37:52 +0000 | [diff] [blame] | 840 | imag(_Tp) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 841 | { |
| 842 | return 0; |
| 843 | } |
| 844 | |
| 845 | // abs |
| 846 | |
| 847 | template<class _Tp> |
| 848 | inline _LIBCPP_INLINE_VISIBILITY |
| 849 | _Tp |
| 850 | abs(const complex<_Tp>& __c) |
| 851 | { |
| 852 | return hypot(__c.real(), __c.imag()); |
| 853 | } |
| 854 | |
| 855 | // arg |
| 856 | |
| 857 | template<class _Tp> |
| 858 | inline _LIBCPP_INLINE_VISIBILITY |
| 859 | _Tp |
| 860 | arg(const complex<_Tp>& __c) |
| 861 | { |
| 862 | return atan2(__c.imag(), __c.real()); |
| 863 | } |
| 864 | |
Eric Fiselier | f8b772b | 2016-07-20 00:14:10 +0000 | [diff] [blame] | 865 | template <class _Tp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 866 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | f8b772b | 2016-07-20 00:14:10 +0000 | [diff] [blame] | 867 | typename enable_if< |
| 868 | is_same<_Tp, long double>::value, |
| 869 | long double |
| 870 | >::type |
| 871 | arg(_Tp __re) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 872 | { |
| 873 | return atan2l(0.L, __re); |
| 874 | } |
| 875 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 876 | template<class _Tp> |
| 877 | inline _LIBCPP_INLINE_VISIBILITY |
| 878 | typename enable_if |
| 879 | < |
Eric Fiselier | f8b772b | 2016-07-20 00:14:10 +0000 | [diff] [blame] | 880 | is_integral<_Tp>::value || is_same<_Tp, double>::value, |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 881 | double |
| 882 | >::type |
| 883 | arg(_Tp __re) |
| 884 | { |
| 885 | return atan2(0., __re); |
| 886 | } |
| 887 | |
Eric Fiselier | f8b772b | 2016-07-20 00:14:10 +0000 | [diff] [blame] | 888 | template <class _Tp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 889 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | f8b772b | 2016-07-20 00:14:10 +0000 | [diff] [blame] | 890 | typename enable_if< |
| 891 | is_same<_Tp, float>::value, |
| 892 | float |
| 893 | >::type |
| 894 | arg(_Tp __re) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 895 | { |
| 896 | return atan2f(0.F, __re); |
| 897 | } |
| 898 | |
| 899 | // norm |
| 900 | |
| 901 | template<class _Tp> |
| 902 | inline _LIBCPP_INLINE_VISIBILITY |
| 903 | _Tp |
| 904 | norm(const complex<_Tp>& __c) |
| 905 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 906 | if (__libcpp_isinf_or_builtin(__c.real())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 907 | return abs(__c.real()); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 908 | if (__libcpp_isinf_or_builtin(__c.imag())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 909 | return abs(__c.imag()); |
| 910 | return __c.real() * __c.real() + __c.imag() * __c.imag(); |
| 911 | } |
| 912 | |
Eric Fiselier | f8b772b | 2016-07-20 00:14:10 +0000 | [diff] [blame] | 913 | template <class _Tp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 914 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | f8b772b | 2016-07-20 00:14:10 +0000 | [diff] [blame] | 915 | typename __libcpp_complex_overload_traits<_Tp>::_ValueType |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 916 | norm(_Tp __re) |
| 917 | { |
Eric Fiselier | f8b772b | 2016-07-20 00:14:10 +0000 | [diff] [blame] | 918 | typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType; |
| 919 | return static_cast<_ValueType>(__re) * __re; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | // conj |
| 923 | |
| 924 | template<class _Tp> |
| 925 | inline _LIBCPP_INLINE_VISIBILITY |
| 926 | complex<_Tp> |
| 927 | conj(const complex<_Tp>& __c) |
| 928 | { |
| 929 | return complex<_Tp>(__c.real(), -__c.imag()); |
| 930 | } |
| 931 | |
Eric Fiselier | f8b772b | 2016-07-20 00:14:10 +0000 | [diff] [blame] | 932 | template <class _Tp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 933 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | f8b772b | 2016-07-20 00:14:10 +0000 | [diff] [blame] | 934 | typename __libcpp_complex_overload_traits<_Tp>::_ComplexType |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 935 | conj(_Tp __re) |
| 936 | { |
Eric Fiselier | f8b772b | 2016-07-20 00:14:10 +0000 | [diff] [blame] | 937 | typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType; |
| 938 | return _ComplexType(__re); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Eric Fiselier | f8b772b | 2016-07-20 00:14:10 +0000 | [diff] [blame] | 941 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 942 | |
| 943 | // proj |
| 944 | |
| 945 | template<class _Tp> |
| 946 | inline _LIBCPP_INLINE_VISIBILITY |
| 947 | complex<_Tp> |
| 948 | proj(const complex<_Tp>& __c) |
| 949 | { |
| 950 | std::complex<_Tp> __r = __c; |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 951 | if (__libcpp_isinf_or_builtin(__c.real()) || __libcpp_isinf_or_builtin(__c.imag())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 952 | __r = complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag())); |
| 953 | return __r; |
| 954 | } |
| 955 | |
Eric Fiselier | f8b772b | 2016-07-20 00:14:10 +0000 | [diff] [blame] | 956 | template <class _Tp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 957 | inline _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | f8b772b | 2016-07-20 00:14:10 +0000 | [diff] [blame] | 958 | typename enable_if |
| 959 | < |
| 960 | is_floating_point<_Tp>::value, |
| 961 | typename __libcpp_complex_overload_traits<_Tp>::_ComplexType |
| 962 | >::type |
| 963 | proj(_Tp __re) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 964 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 965 | if (__libcpp_isinf_or_builtin(__re)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 966 | __re = abs(__re); |
Eric Fiselier | f8b772b | 2016-07-20 00:14:10 +0000 | [diff] [blame] | 967 | return complex<_Tp>(__re); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Eric Fiselier | f8b772b | 2016-07-20 00:14:10 +0000 | [diff] [blame] | 970 | template <class _Tp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 971 | inline _LIBCPP_INLINE_VISIBILITY |
| 972 | typename enable_if |
| 973 | < |
| 974 | is_integral<_Tp>::value, |
Eric Fiselier | f8b772b | 2016-07-20 00:14:10 +0000 | [diff] [blame] | 975 | typename __libcpp_complex_overload_traits<_Tp>::_ComplexType |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 976 | >::type |
| 977 | proj(_Tp __re) |
| 978 | { |
Eric Fiselier | f8b772b | 2016-07-20 00:14:10 +0000 | [diff] [blame] | 979 | typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType; |
| 980 | return _ComplexType(__re); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 983 | // polar |
| 984 | |
| 985 | template<class _Tp> |
| 986 | complex<_Tp> |
Marshall Clow | 2f28373 | 2018-01-31 21:42:39 +0000 | [diff] [blame] | 987 | polar(const _Tp& __rho, const _Tp& __theta = _Tp()) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 988 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 989 | if (__libcpp_isnan_or_builtin(__rho) || signbit(__rho)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 990 | return complex<_Tp>(_Tp(NAN), _Tp(NAN)); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 991 | if (__libcpp_isnan_or_builtin(__theta)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 992 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 993 | if (__libcpp_isinf_or_builtin(__rho)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 994 | return complex<_Tp>(__rho, __theta); |
| 995 | return complex<_Tp>(__theta, __theta); |
| 996 | } |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 997 | if (__libcpp_isinf_or_builtin(__theta)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 998 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 999 | if (__libcpp_isinf_or_builtin(__rho)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1000 | return complex<_Tp>(__rho, _Tp(NAN)); |
| 1001 | return complex<_Tp>(_Tp(NAN), _Tp(NAN)); |
| 1002 | } |
| 1003 | _Tp __x = __rho * cos(__theta); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1004 | if (__libcpp_isnan_or_builtin(__x)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1005 | __x = 0; |
| 1006 | _Tp __y = __rho * sin(__theta); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1007 | if (__libcpp_isnan_or_builtin(__y)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1008 | __y = 0; |
| 1009 | return complex<_Tp>(__x, __y); |
| 1010 | } |
| 1011 | |
| 1012 | // log |
| 1013 | |
| 1014 | template<class _Tp> |
| 1015 | inline _LIBCPP_INLINE_VISIBILITY |
| 1016 | complex<_Tp> |
| 1017 | log(const complex<_Tp>& __x) |
| 1018 | { |
| 1019 | return complex<_Tp>(log(abs(__x)), arg(__x)); |
| 1020 | } |
| 1021 | |
| 1022 | // log10 |
| 1023 | |
| 1024 | template<class _Tp> |
| 1025 | inline _LIBCPP_INLINE_VISIBILITY |
| 1026 | complex<_Tp> |
| 1027 | log10(const complex<_Tp>& __x) |
| 1028 | { |
| 1029 | return log(__x) / log(_Tp(10)); |
| 1030 | } |
| 1031 | |
| 1032 | // sqrt |
| 1033 | |
| 1034 | template<class _Tp> |
| 1035 | complex<_Tp> |
| 1036 | sqrt(const complex<_Tp>& __x) |
| 1037 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1038 | if (__libcpp_isinf_or_builtin(__x.imag())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1039 | return complex<_Tp>(_Tp(INFINITY), __x.imag()); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1040 | if (__libcpp_isinf_or_builtin(__x.real())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1041 | { |
| 1042 | if (__x.real() > _Tp(0)) |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1043 | return complex<_Tp>(__x.real(), __libcpp_isnan_or_builtin(__x.imag()) ? __x.imag() : copysign(_Tp(0), __x.imag())); |
| 1044 | return complex<_Tp>(__libcpp_isnan_or_builtin(__x.imag()) ? __x.imag() : _Tp(0), copysign(__x.real(), __x.imag())); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1045 | } |
| 1046 | return polar(sqrt(abs(__x)), arg(__x) / _Tp(2)); |
| 1047 | } |
| 1048 | |
| 1049 | // exp |
| 1050 | |
| 1051 | template<class _Tp> |
| 1052 | complex<_Tp> |
| 1053 | exp(const complex<_Tp>& __x) |
| 1054 | { |
| 1055 | _Tp __i = __x.imag(); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1056 | if (__libcpp_isinf_or_builtin(__x.real())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1057 | { |
| 1058 | if (__x.real() < _Tp(0)) |
| 1059 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1060 | if (!__libcpp_isfinite_or_builtin(__i)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1061 | __i = _Tp(1); |
| 1062 | } |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1063 | else if (__i == 0 || !__libcpp_isfinite_or_builtin(__i)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1064 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1065 | if (__libcpp_isinf_or_builtin(__i)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1066 | __i = _Tp(NAN); |
| 1067 | return complex<_Tp>(__x.real(), __i); |
| 1068 | } |
| 1069 | } |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1070 | else if (__libcpp_isnan_or_builtin(__x.real()) && __x.imag() == 0) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1071 | return __x; |
| 1072 | _Tp __e = exp(__x.real()); |
| 1073 | return complex<_Tp>(__e * cos(__i), __e * sin(__i)); |
| 1074 | } |
| 1075 | |
| 1076 | // pow |
| 1077 | |
| 1078 | template<class _Tp> |
| 1079 | inline _LIBCPP_INLINE_VISIBILITY |
| 1080 | complex<_Tp> |
| 1081 | pow(const complex<_Tp>& __x, const complex<_Tp>& __y) |
| 1082 | { |
| 1083 | return exp(__y * log(__x)); |
| 1084 | } |
| 1085 | |
| 1086 | template<class _Tp, class _Up> |
| 1087 | inline _LIBCPP_INLINE_VISIBILITY |
| 1088 | complex<typename __promote<_Tp, _Up>::type> |
| 1089 | pow(const complex<_Tp>& __x, const complex<_Up>& __y) |
| 1090 | { |
| 1091 | typedef complex<typename __promote<_Tp, _Up>::type> result_type; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1092 | return _VSTD::pow(result_type(__x), result_type(__y)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | template<class _Tp, class _Up> |
| 1096 | inline _LIBCPP_INLINE_VISIBILITY |
| 1097 | typename enable_if |
| 1098 | < |
| 1099 | is_arithmetic<_Up>::value, |
| 1100 | complex<typename __promote<_Tp, _Up>::type> |
| 1101 | >::type |
| 1102 | pow(const complex<_Tp>& __x, const _Up& __y) |
| 1103 | { |
| 1104 | typedef complex<typename __promote<_Tp, _Up>::type> result_type; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1105 | return _VSTD::pow(result_type(__x), result_type(__y)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | template<class _Tp, class _Up> |
| 1109 | inline _LIBCPP_INLINE_VISIBILITY |
| 1110 | typename enable_if |
| 1111 | < |
| 1112 | is_arithmetic<_Tp>::value, |
| 1113 | complex<typename __promote<_Tp, _Up>::type> |
| 1114 | >::type |
| 1115 | pow(const _Tp& __x, const complex<_Up>& __y) |
| 1116 | { |
| 1117 | typedef complex<typename __promote<_Tp, _Up>::type> result_type; |
Howard Hinnant | b1ad5a8 | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 1118 | return _VSTD::pow(result_type(__x), result_type(__y)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
Mikhail Maltsev | fa9a85e | 2018-02-19 15:41:36 +0000 | [diff] [blame] | 1121 | // __sqr, computes pow(x, 2) |
| 1122 | |
| 1123 | template<class _Tp> |
| 1124 | inline _LIBCPP_INLINE_VISIBILITY |
| 1125 | complex<_Tp> |
| 1126 | __sqr(const complex<_Tp>& __x) |
| 1127 | { |
| 1128 | return complex<_Tp>((__x.real() - __x.imag()) * (__x.real() + __x.imag()), |
| 1129 | _Tp(2) * __x.real() * __x.imag()); |
| 1130 | } |
| 1131 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1132 | // asinh |
| 1133 | |
| 1134 | template<class _Tp> |
| 1135 | complex<_Tp> |
| 1136 | asinh(const complex<_Tp>& __x) |
| 1137 | { |
| 1138 | const _Tp __pi(atan2(+0., -0.)); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1139 | if (__libcpp_isinf_or_builtin(__x.real())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1140 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1141 | if (__libcpp_isnan_or_builtin(__x.imag())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1142 | return __x; |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1143 | if (__libcpp_isinf_or_builtin(__x.imag())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1144 | return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag())); |
| 1145 | return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag())); |
| 1146 | } |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1147 | if (__libcpp_isnan_or_builtin(__x.real())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1148 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1149 | if (__libcpp_isinf_or_builtin(__x.imag())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1150 | return complex<_Tp>(__x.imag(), __x.real()); |
| 1151 | if (__x.imag() == 0) |
| 1152 | return __x; |
| 1153 | return complex<_Tp>(__x.real(), __x.real()); |
| 1154 | } |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1155 | if (__libcpp_isinf_or_builtin(__x.imag())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1156 | return complex<_Tp>(copysign(__x.imag(), __x.real()), copysign(__pi/_Tp(2), __x.imag())); |
Mikhail Maltsev | fa9a85e | 2018-02-19 15:41:36 +0000 | [diff] [blame] | 1157 | complex<_Tp> __z = log(__x + sqrt(__sqr(__x) + _Tp(1))); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1158 | return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag())); |
| 1159 | } |
| 1160 | |
| 1161 | // acosh |
| 1162 | |
| 1163 | template<class _Tp> |
| 1164 | complex<_Tp> |
| 1165 | acosh(const complex<_Tp>& __x) |
| 1166 | { |
| 1167 | const _Tp __pi(atan2(+0., -0.)); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1168 | if (__libcpp_isinf_or_builtin(__x.real())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1169 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1170 | if (__libcpp_isnan_or_builtin(__x.imag())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1171 | return complex<_Tp>(abs(__x.real()), __x.imag()); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1172 | if (__libcpp_isinf_or_builtin(__x.imag())) |
Howard Hinnant | 3edce76 | 2012-11-06 21:55:44 +0000 | [diff] [blame] | 1173 | { |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1174 | if (__x.real() > 0) |
| 1175 | return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag())); |
| 1176 | else |
| 1177 | return complex<_Tp>(-__x.real(), copysign(__pi * _Tp(0.75), __x.imag())); |
Howard Hinnant | 3edce76 | 2012-11-06 21:55:44 +0000 | [diff] [blame] | 1178 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1179 | if (__x.real() < 0) |
| 1180 | return complex<_Tp>(-__x.real(), copysign(__pi, __x.imag())); |
| 1181 | return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag())); |
| 1182 | } |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1183 | if (__libcpp_isnan_or_builtin(__x.real())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1184 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1185 | if (__libcpp_isinf_or_builtin(__x.imag())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1186 | return complex<_Tp>(abs(__x.imag()), __x.real()); |
| 1187 | return complex<_Tp>(__x.real(), __x.real()); |
| 1188 | } |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1189 | if (__libcpp_isinf_or_builtin(__x.imag())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1190 | return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag())); |
Mikhail Maltsev | fa9a85e | 2018-02-19 15:41:36 +0000 | [diff] [blame] | 1191 | complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1))); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1192 | return complex<_Tp>(copysign(__z.real(), _Tp(0)), copysign(__z.imag(), __x.imag())); |
| 1193 | } |
| 1194 | |
| 1195 | // atanh |
| 1196 | |
| 1197 | template<class _Tp> |
| 1198 | complex<_Tp> |
| 1199 | atanh(const complex<_Tp>& __x) |
| 1200 | { |
| 1201 | const _Tp __pi(atan2(+0., -0.)); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1202 | if (__libcpp_isinf_or_builtin(__x.imag())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1203 | { |
| 1204 | return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag())); |
| 1205 | } |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1206 | if (__libcpp_isnan_or_builtin(__x.imag())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1207 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1208 | if (__libcpp_isinf_or_builtin(__x.real()) || __x.real() == 0) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1209 | return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag()); |
| 1210 | return complex<_Tp>(__x.imag(), __x.imag()); |
| 1211 | } |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1212 | if (__libcpp_isnan_or_builtin(__x.real())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1213 | { |
| 1214 | return complex<_Tp>(__x.real(), __x.real()); |
| 1215 | } |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1216 | if (__libcpp_isinf_or_builtin(__x.real())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1217 | { |
| 1218 | return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag())); |
| 1219 | } |
| 1220 | if (abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0)) |
| 1221 | { |
| 1222 | return complex<_Tp>(copysign(_Tp(INFINITY), __x.real()), copysign(_Tp(0), __x.imag())); |
| 1223 | } |
| 1224 | complex<_Tp> __z = log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2); |
| 1225 | return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag())); |
| 1226 | } |
| 1227 | |
| 1228 | // sinh |
| 1229 | |
| 1230 | template<class _Tp> |
| 1231 | complex<_Tp> |
| 1232 | sinh(const complex<_Tp>& __x) |
| 1233 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1234 | if (__libcpp_isinf_or_builtin(__x.real()) && !__libcpp_isfinite_or_builtin(__x.imag())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1235 | return complex<_Tp>(__x.real(), _Tp(NAN)); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1236 | if (__x.real() == 0 && !__libcpp_isfinite_or_builtin(__x.imag())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1237 | return complex<_Tp>(__x.real(), _Tp(NAN)); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1238 | if (__x.imag() == 0 && !__libcpp_isfinite_or_builtin(__x.real())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1239 | return __x; |
| 1240 | return complex<_Tp>(sinh(__x.real()) * cos(__x.imag()), cosh(__x.real()) * sin(__x.imag())); |
| 1241 | } |
| 1242 | |
| 1243 | // cosh |
| 1244 | |
| 1245 | template<class _Tp> |
| 1246 | complex<_Tp> |
| 1247 | cosh(const complex<_Tp>& __x) |
| 1248 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1249 | if (__libcpp_isinf_or_builtin(__x.real()) && !__libcpp_isfinite_or_builtin(__x.imag())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1250 | return complex<_Tp>(abs(__x.real()), _Tp(NAN)); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1251 | if (__x.real() == 0 && !__libcpp_isfinite_or_builtin(__x.imag())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1252 | return complex<_Tp>(_Tp(NAN), __x.real()); |
| 1253 | if (__x.real() == 0 && __x.imag() == 0) |
| 1254 | return complex<_Tp>(_Tp(1), __x.imag()); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1255 | if (__x.imag() == 0 && !__libcpp_isfinite_or_builtin(__x.real())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1256 | return complex<_Tp>(abs(__x.real()), __x.imag()); |
| 1257 | return complex<_Tp>(cosh(__x.real()) * cos(__x.imag()), sinh(__x.real()) * sin(__x.imag())); |
| 1258 | } |
| 1259 | |
| 1260 | // tanh |
| 1261 | |
| 1262 | template<class _Tp> |
| 1263 | complex<_Tp> |
| 1264 | tanh(const complex<_Tp>& __x) |
| 1265 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1266 | if (__libcpp_isinf_or_builtin(__x.real())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1267 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1268 | if (!__libcpp_isfinite_or_builtin(__x.imag())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1269 | return complex<_Tp>(_Tp(1), _Tp(0)); |
| 1270 | return complex<_Tp>(_Tp(1), copysign(_Tp(0), sin(_Tp(2) * __x.imag()))); |
| 1271 | } |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1272 | if (__libcpp_isnan_or_builtin(__x.real()) && __x.imag() == 0) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1273 | return __x; |
| 1274 | _Tp __2r(_Tp(2) * __x.real()); |
| 1275 | _Tp __2i(_Tp(2) * __x.imag()); |
| 1276 | _Tp __d(cosh(__2r) + cos(__2i)); |
Howard Hinnant | 3e7b3b5 | 2012-09-19 23:51:47 +0000 | [diff] [blame] | 1277 | _Tp __2rsh(sinh(__2r)); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1278 | if (__libcpp_isinf_or_builtin(__2rsh) && __libcpp_isinf_or_builtin(__d)) |
Howard Hinnant | 3e7b3b5 | 2012-09-19 23:51:47 +0000 | [diff] [blame] | 1279 | return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1), |
| 1280 | __2i > _Tp(0) ? _Tp(0) : _Tp(-0.)); |
| 1281 | return complex<_Tp>(__2rsh/__d, sin(__2i)/__d); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | // asin |
| 1285 | |
| 1286 | template<class _Tp> |
| 1287 | complex<_Tp> |
| 1288 | asin(const complex<_Tp>& __x) |
| 1289 | { |
| 1290 | complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real())); |
| 1291 | return complex<_Tp>(__z.imag(), -__z.real()); |
| 1292 | } |
| 1293 | |
| 1294 | // acos |
| 1295 | |
| 1296 | template<class _Tp> |
| 1297 | complex<_Tp> |
| 1298 | acos(const complex<_Tp>& __x) |
| 1299 | { |
| 1300 | const _Tp __pi(atan2(+0., -0.)); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1301 | if (__libcpp_isinf_or_builtin(__x.real())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1302 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1303 | if (__libcpp_isnan_or_builtin(__x.imag())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1304 | return complex<_Tp>(__x.imag(), __x.real()); |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1305 | if (__libcpp_isinf_or_builtin(__x.imag())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1306 | { |
| 1307 | if (__x.real() < _Tp(0)) |
| 1308 | return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag()); |
| 1309 | return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag()); |
| 1310 | } |
| 1311 | if (__x.real() < _Tp(0)) |
| 1312 | return complex<_Tp>(__pi, signbit(__x.imag()) ? -__x.real() : __x.real()); |
| 1313 | return complex<_Tp>(_Tp(0), signbit(__x.imag()) ? __x.real() : -__x.real()); |
| 1314 | } |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1315 | if (__libcpp_isnan_or_builtin(__x.real())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1316 | { |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1317 | if (__libcpp_isinf_or_builtin(__x.imag())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1318 | return complex<_Tp>(__x.real(), -__x.imag()); |
| 1319 | return complex<_Tp>(__x.real(), __x.real()); |
| 1320 | } |
Duncan P. N. Exon Smith | 4a29720 | 2017-07-07 05:13:36 +0000 | [diff] [blame] | 1321 | if (__libcpp_isinf_or_builtin(__x.imag())) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1322 | return complex<_Tp>(__pi/_Tp(2), -__x.imag()); |
Marshall Clow | 17af7e4 | 2016-04-04 16:08:54 +0000 | [diff] [blame] | 1323 | if (__x.real() == 0 && (__x.imag() == 0 || isnan(__x.imag()))) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1324 | return complex<_Tp>(__pi/_Tp(2), -__x.imag()); |
Mikhail Maltsev | fa9a85e | 2018-02-19 15:41:36 +0000 | [diff] [blame] | 1325 | complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1))); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1326 | if (signbit(__x.imag())) |
| 1327 | return complex<_Tp>(abs(__z.imag()), abs(__z.real())); |
| 1328 | return complex<_Tp>(abs(__z.imag()), -abs(__z.real())); |
| 1329 | } |
| 1330 | |
| 1331 | // atan |
| 1332 | |
| 1333 | template<class _Tp> |
| 1334 | complex<_Tp> |
| 1335 | atan(const complex<_Tp>& __x) |
| 1336 | { |
| 1337 | complex<_Tp> __z = atanh(complex<_Tp>(-__x.imag(), __x.real())); |
| 1338 | return complex<_Tp>(__z.imag(), -__z.real()); |
| 1339 | } |
| 1340 | |
| 1341 | // sin |
| 1342 | |
| 1343 | template<class _Tp> |
| 1344 | complex<_Tp> |
| 1345 | sin(const complex<_Tp>& __x) |
| 1346 | { |
| 1347 | complex<_Tp> __z = sinh(complex<_Tp>(-__x.imag(), __x.real())); |
| 1348 | return complex<_Tp>(__z.imag(), -__z.real()); |
| 1349 | } |
| 1350 | |
| 1351 | // cos |
| 1352 | |
| 1353 | template<class _Tp> |
| 1354 | inline _LIBCPP_INLINE_VISIBILITY |
| 1355 | complex<_Tp> |
| 1356 | cos(const complex<_Tp>& __x) |
| 1357 | { |
| 1358 | return cosh(complex<_Tp>(-__x.imag(), __x.real())); |
| 1359 | } |
| 1360 | |
| 1361 | // tan |
| 1362 | |
| 1363 | template<class _Tp> |
| 1364 | complex<_Tp> |
| 1365 | tan(const complex<_Tp>& __x) |
| 1366 | { |
| 1367 | complex<_Tp> __z = tanh(complex<_Tp>(-__x.imag(), __x.real())); |
| 1368 | return complex<_Tp>(__z.imag(), -__z.real()); |
| 1369 | } |
| 1370 | |
| 1371 | template<class _Tp, class _CharT, class _Traits> |
| 1372 | basic_istream<_CharT, _Traits>& |
| 1373 | operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) |
| 1374 | { |
| 1375 | if (__is.good()) |
| 1376 | { |
| 1377 | ws(__is); |
| 1378 | if (__is.peek() == _CharT('(')) |
| 1379 | { |
| 1380 | __is.get(); |
| 1381 | _Tp __r; |
| 1382 | __is >> __r; |
| 1383 | if (!__is.fail()) |
| 1384 | { |
| 1385 | ws(__is); |
| 1386 | _CharT __c = __is.peek(); |
| 1387 | if (__c == _CharT(',')) |
| 1388 | { |
| 1389 | __is.get(); |
| 1390 | _Tp __i; |
| 1391 | __is >> __i; |
| 1392 | if (!__is.fail()) |
| 1393 | { |
| 1394 | ws(__is); |
| 1395 | __c = __is.peek(); |
| 1396 | if (__c == _CharT(')')) |
| 1397 | { |
| 1398 | __is.get(); |
| 1399 | __x = complex<_Tp>(__r, __i); |
| 1400 | } |
| 1401 | else |
Louis Dionne | 2d93782 | 2020-02-19 16:09:41 -0500 | [diff] [blame] | 1402 | __is.setstate(__is.failbit); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1403 | } |
| 1404 | else |
Louis Dionne | 2d93782 | 2020-02-19 16:09:41 -0500 | [diff] [blame] | 1405 | __is.setstate(__is.failbit); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1406 | } |
| 1407 | else if (__c == _CharT(')')) |
| 1408 | { |
| 1409 | __is.get(); |
| 1410 | __x = complex<_Tp>(__r, _Tp(0)); |
| 1411 | } |
| 1412 | else |
Louis Dionne | 2d93782 | 2020-02-19 16:09:41 -0500 | [diff] [blame] | 1413 | __is.setstate(__is.failbit); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1414 | } |
| 1415 | else |
Louis Dionne | 2d93782 | 2020-02-19 16:09:41 -0500 | [diff] [blame] | 1416 | __is.setstate(__is.failbit); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1417 | } |
| 1418 | else |
| 1419 | { |
| 1420 | _Tp __r; |
| 1421 | __is >> __r; |
| 1422 | if (!__is.fail()) |
| 1423 | __x = complex<_Tp>(__r, _Tp(0)); |
| 1424 | else |
Louis Dionne | 2d93782 | 2020-02-19 16:09:41 -0500 | [diff] [blame] | 1425 | __is.setstate(__is.failbit); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1426 | } |
| 1427 | } |
| 1428 | else |
Louis Dionne | 2d93782 | 2020-02-19 16:09:41 -0500 | [diff] [blame] | 1429 | __is.setstate(__is.failbit); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1430 | return __is; |
| 1431 | } |
| 1432 | |
| 1433 | template<class _Tp, class _CharT, class _Traits> |
| 1434 | basic_ostream<_CharT, _Traits>& |
| 1435 | operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) |
| 1436 | { |
Antonio Sanchez | d4fb740 | 2020-05-07 13:49:12 -0400 | [diff] [blame] | 1437 | basic_ostringstream<_CharT, _Traits> __s; |
| 1438 | __s.flags(__os.flags()); |
| 1439 | __s.imbue(__os.getloc()); |
| 1440 | __s.precision(__os.precision()); |
| 1441 | __s << '(' << __x.real() << ',' << __x.imag() << ')'; |
| 1442 | return __os << __s.str(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 1445 | #if _LIBCPP_STD_VER > 11 |
Marshall Clow | 35f9114 | 2013-10-05 21:19:49 +0000 | [diff] [blame] | 1446 | // Literal suffix for complex number literals [complex.literals] |
| 1447 | inline namespace literals |
Louis Dionne | 173f29e | 2019-05-29 16:01:36 +0000 | [diff] [blame] | 1448 | { |
Marshall Clow | 35f9114 | 2013-10-05 21:19:49 +0000 | [diff] [blame] | 1449 | inline namespace complex_literals |
| 1450 | { |
| 1451 | constexpr complex<long double> operator""il(long double __im) |
| 1452 | { |
| 1453 | return { 0.0l, __im }; |
| 1454 | } |
| 1455 | |
| 1456 | constexpr complex<long double> operator""il(unsigned long long __im) |
| 1457 | { |
| 1458 | return { 0.0l, static_cast<long double>(__im) }; |
| 1459 | } |
| 1460 | |
| 1461 | |
| 1462 | constexpr complex<double> operator""i(long double __im) |
| 1463 | { |
| 1464 | return { 0.0, static_cast<double>(__im) }; |
| 1465 | } |
| 1466 | |
| 1467 | constexpr complex<double> operator""i(unsigned long long __im) |
| 1468 | { |
| 1469 | return { 0.0, static_cast<double>(__im) }; |
| 1470 | } |
| 1471 | |
| 1472 | |
| 1473 | constexpr complex<float> operator""if(long double __im) |
| 1474 | { |
| 1475 | return { 0.0f, static_cast<float>(__im) }; |
| 1476 | } |
| 1477 | |
| 1478 | constexpr complex<float> operator""if(unsigned long long __im) |
| 1479 | { |
| 1480 | return { 0.0f, static_cast<float>(__im) }; |
| 1481 | } |
| 1482 | } |
| 1483 | } |
| 1484 | #endif |
| 1485 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1486 | _LIBCPP_END_NAMESPACE_STD |
| 1487 | |
| 1488 | #endif // _LIBCPP_COMPLEX |