| 1 | // The template and inlines for the -*- C++ -*- complex number classes. |
| 2 | |
| 3 | // Copyright (C) 1997-2025 Free Software Foundation, Inc. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // Under Section 7 of GPL version 3, you are granted additional |
| 17 | // permissions described in the GCC Runtime Library Exception, version |
| 18 | // 3.1, as published by the Free Software Foundation. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License and |
| 21 | // a copy of the GCC Runtime Library Exception along with this program; |
| 22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 | // <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | /** @file include/complex |
| 26 | * This is a Standard C++ Library header. |
| 27 | */ |
| 28 | |
| 29 | // |
| 30 | // ISO C++ 14882: 26.2 Complex Numbers |
| 31 | // Note: this is not a conforming implementation. |
| 32 | // Initially implemented by Ulrich Drepper <drepper@cygnus.com> |
| 33 | // Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr> |
| 34 | // |
| 35 | |
| 36 | #ifndef _GLIBCXX_COMPLEX |
| 37 | #define _GLIBCXX_COMPLEX 1 |
| 38 | |
| 39 | #ifdef _GLIBCXX_SYSHDR |
| 40 | #pragma GCC system_header |
| 41 | #endif |
| 42 | |
| 43 | #pragma GCC diagnostic push |
| 44 | #pragma GCC diagnostic ignored "-Wc++11-extensions" // extern template |
| 45 | |
| 46 | #include <bits/c++config.h> |
| 47 | #include <bits/cpp_type_traits.h> |
| 48 | #include <ext/type_traits.h> |
| 49 | #include <cmath> |
| 50 | #include <sstream> |
| 51 | |
| 52 | // Get rid of a macro possibly defined in <complex.h> |
| 53 | #undef complex |
| 54 | |
| 55 | #ifdef _GLIBCXX_CLANG |
| 56 | #pragma clang diagnostic push |
| 57 | #pragma clang diagnostic ignored "-Wc99-extensions" |
| 58 | #endif |
| 59 | |
| 60 | #define __glibcxx_want_constexpr_complex |
| 61 | #define __glibcxx_want_complex_udls |
| 62 | #define __glibcxx_want_tuple_like |
| 63 | #include <bits/version.h> |
| 64 | |
| 65 | #if __glibcxx_tuple_like >= 202311 // >= C++26 |
| 66 | # include <bits/utility.h> // for tuple_element_t |
| 67 | # include <bits/stl_pair.h> // for __is_tuple_like_v |
| 68 | #endif |
| 69 | |
| 70 | namespace std _GLIBCXX_VISIBILITY(default) |
| 71 | { |
| 72 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 73 | |
| 74 | /** |
| 75 | * @defgroup complex_numbers Complex Numbers |
| 76 | * @ingroup numerics |
| 77 | * |
| 78 | * Classes and functions for complex numbers. |
| 79 | * @{ |
| 80 | */ |
| 81 | |
| 82 | // Forward declarations. |
| 83 | template<typename _Tp> class complex; |
| 84 | template<> class complex<float>; |
| 85 | template<> class complex<double>; |
| 86 | template<> class complex<long double>; |
| 87 | |
| 88 | /// Return magnitude of @a z. |
| 89 | template<typename _Tp> _Tp abs(const complex<_Tp>&); |
| 90 | /// Return phase angle of @a z. |
| 91 | template<typename _Tp> _Tp arg(const complex<_Tp>&); |
| 92 | /// Return @a z magnitude squared. |
| 93 | template<typename _Tp> _Tp _GLIBCXX20_CONSTEXPR norm(const complex<_Tp>&); |
| 94 | |
| 95 | /// Return complex conjugate of @a z. |
| 96 | template<typename _Tp> |
| 97 | _GLIBCXX20_CONSTEXPR complex<_Tp> conj(const complex<_Tp>&); |
| 98 | /// Return complex with magnitude @a rho and angle @a theta. |
| 99 | template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0); |
| 100 | |
| 101 | // Transcendentals: |
| 102 | /// Return complex cosine of @a z. |
| 103 | template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&); |
| 104 | /// Return complex hyperbolic cosine of @a z. |
| 105 | template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&); |
| 106 | /// Return complex base e exponential of @a z. |
| 107 | template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&); |
| 108 | /// Return complex natural logarithm of @a z. |
| 109 | template<typename _Tp> complex<_Tp> log(const complex<_Tp>&); |
| 110 | /// Return complex base 10 logarithm of @a z. |
| 111 | template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&); |
| 112 | /// Return @a x to the @a y'th power. |
| 113 | template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int); |
| 114 | /// Return @a x to the @a y'th power. |
| 115 | template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&); |
| 116 | /// Return @a x to the @a y'th power. |
| 117 | template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, |
| 118 | const complex<_Tp>&); |
| 119 | /// Return @a x to the @a y'th power. |
| 120 | template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&); |
| 121 | /// Return complex sine of @a z. |
| 122 | template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&); |
| 123 | /// Return complex hyperbolic sine of @a z. |
| 124 | template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&); |
| 125 | /// Return complex square root of @a z. |
| 126 | template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&); |
| 127 | /// Return complex tangent of @a z. |
| 128 | template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&); |
| 129 | /// Return complex hyperbolic tangent of @a z. |
| 130 | template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&); |
| 131 | |
| 132 | #if __glibcxx_tuple_like >= 202311L // >= C++26 |
| 133 | template<typename _Tp> |
| 134 | struct tuple_size<complex<_Tp>> |
| 135 | : public integral_constant<size_t, 2> { }; |
| 136 | template<typename _Tp> |
| 137 | struct tuple_element<0, complex<_Tp>> |
| 138 | { using type = _Tp; }; |
| 139 | template<typename _Tp> |
| 140 | struct tuple_element<1, complex<_Tp>> |
| 141 | { using type = _Tp; }; |
| 142 | template<typename _Tp> |
| 143 | inline constexpr bool __is_tuple_like_v<complex<_Tp>> = true; |
| 144 | #endif // __glibcxx_tuple_like >= 202311 |
| 145 | |
| 146 | // 26.2.2 Primary template class complex |
| 147 | /** |
| 148 | * Template to represent complex numbers. |
| 149 | * |
| 150 | * Specializations for float, double, and long double are part of the |
| 151 | * library. Results with any other type are not guaranteed. |
| 152 | * |
| 153 | * @param Tp Type of real and imaginary values. |
| 154 | */ |
| 155 | template<typename _Tp> |
| 156 | class complex |
| 157 | { |
| 158 | public: |
| 159 | /// Value typedef. |
| 160 | typedef _Tp value_type; |
| 161 | |
| 162 | /// Default constructor. First parameter is x, second parameter is y. |
| 163 | /// Unspecified parameters default to 0. |
| 164 | _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp()) |
| 165 | : _M_real(__r), _M_imag(__i) { } |
| 166 | |
| 167 | // Let the compiler synthesize the copy constructor |
| 168 | #if __cplusplus >= 201103L |
| 169 | constexpr complex(const complex&) = default; |
| 170 | #endif |
| 171 | |
| 172 | /// Converting constructor. |
| 173 | template<typename _Up> |
| 174 | #if __cplusplus > 202002L |
| 175 | explicit(!requires(_Up __u) { _Tp{__u}; }) |
| 176 | #endif |
| 177 | _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z) |
| 178 | : _M_real(_Tp(__z.real())), _M_imag(_Tp(__z.imag())) { } |
| 179 | |
| 180 | #if __cplusplus >= 201103L |
| 181 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 182 | // DR 387. std::complex over-encapsulated. |
| 183 | _GLIBCXX_ABI_TAG_CXX11 |
| 184 | constexpr _Tp |
| 185 | real() const { return _M_real; } |
| 186 | |
| 187 | _GLIBCXX_ABI_TAG_CXX11 |
| 188 | constexpr _Tp |
| 189 | imag() const { return _M_imag; } |
| 190 | #else |
| 191 | /// Return real part of complex number. |
| 192 | _Tp& |
| 193 | real() { return _M_real; } |
| 194 | |
| 195 | /// Return real part of complex number. |
| 196 | const _Tp& |
| 197 | real() const { return _M_real; } |
| 198 | |
| 199 | /// Return imaginary part of complex number. |
| 200 | _Tp& |
| 201 | imag() { return _M_imag; } |
| 202 | |
| 203 | /// Return imaginary part of complex number. |
| 204 | const _Tp& |
| 205 | imag() const { return _M_imag; } |
| 206 | #endif |
| 207 | |
| 208 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 209 | // DR 387. std::complex over-encapsulated. |
| 210 | _GLIBCXX20_CONSTEXPR void |
| 211 | real(_Tp __val) { _M_real = __val; } |
| 212 | |
| 213 | _GLIBCXX20_CONSTEXPR void |
| 214 | imag(_Tp __val) { _M_imag = __val; } |
| 215 | |
| 216 | /// Assign a scalar to this complex number. |
| 217 | _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const _Tp&); |
| 218 | |
| 219 | /// Add a scalar to this complex number. |
| 220 | // 26.2.5/1 |
| 221 | _GLIBCXX20_CONSTEXPR complex<_Tp>& |
| 222 | operator+=(const _Tp& __t) |
| 223 | { |
| 224 | _M_real += __t; |
| 225 | return *this; |
| 226 | } |
| 227 | |
| 228 | /// Subtract a scalar from this complex number. |
| 229 | // 26.2.5/3 |
| 230 | _GLIBCXX20_CONSTEXPR complex<_Tp>& |
| 231 | operator-=(const _Tp& __t) |
| 232 | { |
| 233 | _M_real -= __t; |
| 234 | return *this; |
| 235 | } |
| 236 | |
| 237 | /// Multiply this complex number by a scalar. |
| 238 | _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const _Tp&); |
| 239 | /// Divide this complex number by a scalar. |
| 240 | _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const _Tp&); |
| 241 | |
| 242 | // Let the compiler synthesize the copy assignment operator |
| 243 | #if __cplusplus >= 201103L |
| 244 | _GLIBCXX20_CONSTEXPR complex& operator=(const complex&) = default; |
| 245 | #endif |
| 246 | |
| 247 | /// Assign another complex number to this one. |
| 248 | template<typename _Up> |
| 249 | _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const complex<_Up>&); |
| 250 | /// Add another complex number to this one. |
| 251 | template<typename _Up> |
| 252 | _GLIBCXX20_CONSTEXPR complex<_Tp>& operator+=(const complex<_Up>&); |
| 253 | /// Subtract another complex number from this one. |
| 254 | template<typename _Up> |
| 255 | _GLIBCXX20_CONSTEXPR complex<_Tp>& operator-=(const complex<_Up>&); |
| 256 | /// Multiply this complex number by another. |
| 257 | template<typename _Up> |
| 258 | _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const complex<_Up>&); |
| 259 | /// Divide this complex number by another. |
| 260 | template<typename _Up> |
| 261 | _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const complex<_Up>&); |
| 262 | |
| 263 | _GLIBCXX_CONSTEXPR complex __rep() const |
| 264 | { return *this; } |
| 265 | |
| 266 | #if __glibcxx_tuple_like >= 202311L // >= C++26 |
| 267 | template<typename _Cp> |
| 268 | [[__gnu__::__always_inline__]] |
| 269 | constexpr auto& |
| 270 | __get_part(this _Cp& __z, size_t __i) noexcept |
| 271 | { |
| 272 | return __i == 0 ? __z._M_real : __z._M_imag; |
| 273 | } |
| 274 | #endif |
| 275 | |
| 276 | private: |
| 277 | _Tp _M_real; |
| 278 | _Tp _M_imag; |
| 279 | }; |
| 280 | |
| 281 | template<typename _Tp> |
| 282 | _GLIBCXX20_CONSTEXPR complex<_Tp>& |
| 283 | complex<_Tp>::operator=(const _Tp& __t) |
| 284 | { |
| 285 | _M_real = __t; |
| 286 | _M_imag = _Tp(); |
| 287 | return *this; |
| 288 | } |
| 289 | |
| 290 | // 26.2.5/5 |
| 291 | template<typename _Tp> |
| 292 | _GLIBCXX20_CONSTEXPR complex<_Tp>& |
| 293 | complex<_Tp>::operator*=(const _Tp& __t) |
| 294 | { |
| 295 | _M_real *= __t; |
| 296 | _M_imag *= __t; |
| 297 | return *this; |
| 298 | } |
| 299 | |
| 300 | // 26.2.5/7 |
| 301 | template<typename _Tp> |
| 302 | _GLIBCXX20_CONSTEXPR complex<_Tp>& |
| 303 | complex<_Tp>::operator/=(const _Tp& __t) |
| 304 | { |
| 305 | _M_real /= __t; |
| 306 | _M_imag /= __t; |
| 307 | return *this; |
| 308 | } |
| 309 | |
| 310 | template<typename _Tp> |
| 311 | template<typename _Up> |
| 312 | _GLIBCXX20_CONSTEXPR complex<_Tp>& |
| 313 | complex<_Tp>::operator=(const complex<_Up>& __z) |
| 314 | { |
| 315 | _M_real = __z.real(); |
| 316 | _M_imag = __z.imag(); |
| 317 | return *this; |
| 318 | } |
| 319 | |
| 320 | // 26.2.5/9 |
| 321 | template<typename _Tp> |
| 322 | template<typename _Up> |
| 323 | _GLIBCXX20_CONSTEXPR complex<_Tp>& |
| 324 | complex<_Tp>::operator+=(const complex<_Up>& __z) |
| 325 | { |
| 326 | _M_real += __z.real(); |
| 327 | _M_imag += __z.imag(); |
| 328 | return *this; |
| 329 | } |
| 330 | |
| 331 | // 26.2.5/11 |
| 332 | template<typename _Tp> |
| 333 | template<typename _Up> |
| 334 | _GLIBCXX20_CONSTEXPR complex<_Tp>& |
| 335 | complex<_Tp>::operator-=(const complex<_Up>& __z) |
| 336 | { |
| 337 | _M_real -= __z.real(); |
| 338 | _M_imag -= __z.imag(); |
| 339 | return *this; |
| 340 | } |
| 341 | |
| 342 | // 26.2.5/13 |
| 343 | // XXX: This is a grammar school implementation. |
| 344 | template<typename _Tp> |
| 345 | template<typename _Up> |
| 346 | _GLIBCXX20_CONSTEXPR complex<_Tp>& |
| 347 | complex<_Tp>::operator*=(const complex<_Up>& __z) |
| 348 | { |
| 349 | const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag(); |
| 350 | _M_imag = _M_real * __z.imag() + _M_imag * __z.real(); |
| 351 | _M_real = __r; |
| 352 | return *this; |
| 353 | } |
| 354 | |
| 355 | // 26.2.5/15 |
| 356 | // XXX: This is a grammar school implementation. |
| 357 | template<typename _Tp> |
| 358 | template<typename _Up> |
| 359 | _GLIBCXX20_CONSTEXPR complex<_Tp>& |
| 360 | complex<_Tp>::operator/=(const complex<_Up>& __z) |
| 361 | { |
| 362 | const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag(); |
| 363 | const _Tp __n = std::norm(__z); |
| 364 | _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n; |
| 365 | _M_real = __r / __n; |
| 366 | return *this; |
| 367 | } |
| 368 | |
| 369 | // Operators: |
| 370 | ///@{ |
| 371 | /// Return new complex value @a x plus @a y. |
| 372 | template<typename _Tp> |
| 373 | inline _GLIBCXX20_CONSTEXPR complex<_Tp> |
| 374 | operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) |
| 375 | { |
| 376 | complex<_Tp> __r = __x; |
| 377 | __r += __y; |
| 378 | return __r; |
| 379 | } |
| 380 | |
| 381 | template<typename _Tp> |
| 382 | inline _GLIBCXX20_CONSTEXPR complex<_Tp> |
| 383 | operator+(const complex<_Tp>& __x, const _Tp& __y) |
| 384 | { |
| 385 | complex<_Tp> __r = __x; |
| 386 | __r += __y; |
| 387 | return __r; |
| 388 | } |
| 389 | |
| 390 | template<typename _Tp> |
| 391 | inline _GLIBCXX20_CONSTEXPR complex<_Tp> |
| 392 | operator+(const _Tp& __x, const complex<_Tp>& __y) |
| 393 | { |
| 394 | complex<_Tp> __r = __y; |
| 395 | __r += __x; |
| 396 | return __r; |
| 397 | } |
| 398 | ///@} |
| 399 | |
| 400 | ///@{ |
| 401 | /// Return new complex value @a x minus @a y. |
| 402 | template<typename _Tp> |
| 403 | inline _GLIBCXX20_CONSTEXPR complex<_Tp> |
| 404 | operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) |
| 405 | { |
| 406 | complex<_Tp> __r = __x; |
| 407 | __r -= __y; |
| 408 | return __r; |
| 409 | } |
| 410 | |
| 411 | template<typename _Tp> |
| 412 | inline _GLIBCXX20_CONSTEXPR complex<_Tp> |
| 413 | operator-(const complex<_Tp>& __x, const _Tp& __y) |
| 414 | { |
| 415 | complex<_Tp> __r = __x; |
| 416 | __r -= __y; |
| 417 | return __r; |
| 418 | } |
| 419 | |
| 420 | template<typename _Tp> |
| 421 | inline _GLIBCXX20_CONSTEXPR complex<_Tp> |
| 422 | operator-(const _Tp& __x, const complex<_Tp>& __y) |
| 423 | { |
| 424 | complex<_Tp> __r = -__y; |
| 425 | __r += __x; |
| 426 | return __r; |
| 427 | } |
| 428 | ///@} |
| 429 | |
| 430 | ///@{ |
| 431 | /// Return new complex value @a x times @a y. |
| 432 | template<typename _Tp> |
| 433 | inline _GLIBCXX20_CONSTEXPR complex<_Tp> |
| 434 | operator*(const complex<_Tp>& __x, const complex<_Tp>& __y) |
| 435 | { |
| 436 | complex<_Tp> __r = __x; |
| 437 | __r *= __y; |
| 438 | return __r; |
| 439 | } |
| 440 | |
| 441 | template<typename _Tp> |
| 442 | inline _GLIBCXX20_CONSTEXPR complex<_Tp> |
| 443 | operator*(const complex<_Tp>& __x, const _Tp& __y) |
| 444 | { |
| 445 | complex<_Tp> __r = __x; |
| 446 | __r *= __y; |
| 447 | return __r; |
| 448 | } |
| 449 | |
| 450 | template<typename _Tp> |
| 451 | inline _GLIBCXX20_CONSTEXPR complex<_Tp> |
| 452 | operator*(const _Tp& __x, const complex<_Tp>& __y) |
| 453 | { |
| 454 | complex<_Tp> __r = __y; |
| 455 | __r *= __x; |
| 456 | return __r; |
| 457 | } |
| 458 | ///@} |
| 459 | |
| 460 | ///@{ |
| 461 | /// Return new complex value @a x divided by @a y. |
| 462 | template<typename _Tp> |
| 463 | inline _GLIBCXX20_CONSTEXPR complex<_Tp> |
| 464 | operator/(const complex<_Tp>& __x, const complex<_Tp>& __y) |
| 465 | { |
| 466 | complex<_Tp> __r = __x; |
| 467 | __r /= __y; |
| 468 | return __r; |
| 469 | } |
| 470 | |
| 471 | template<typename _Tp> |
| 472 | inline _GLIBCXX20_CONSTEXPR complex<_Tp> |
| 473 | operator/(const complex<_Tp>& __x, const _Tp& __y) |
| 474 | { |
| 475 | complex<_Tp> __r = __x; |
| 476 | __r /= __y; |
| 477 | return __r; |
| 478 | } |
| 479 | |
| 480 | template<typename _Tp> |
| 481 | inline _GLIBCXX20_CONSTEXPR complex<_Tp> |
| 482 | operator/(const _Tp& __x, const complex<_Tp>& __y) |
| 483 | { |
| 484 | complex<_Tp> __r = __x; |
| 485 | __r /= __y; |
| 486 | return __r; |
| 487 | } |
| 488 | ///@} |
| 489 | |
| 490 | /// Return @a x. |
| 491 | template<typename _Tp> |
| 492 | inline _GLIBCXX20_CONSTEXPR complex<_Tp> |
| 493 | operator+(const complex<_Tp>& __x) |
| 494 | { return __x; } |
| 495 | |
| 496 | /// Return complex negation of @a x. |
| 497 | template<typename _Tp> |
| 498 | inline _GLIBCXX20_CONSTEXPR complex<_Tp> |
| 499 | operator-(const complex<_Tp>& __x) |
| 500 | { return complex<_Tp>(-__x.real(), -__x.imag()); } |
| 501 | |
| 502 | ///@{ |
| 503 | /// Return true if @a x is equal to @a y. |
| 504 | template<typename _Tp> |
| 505 | inline _GLIBCXX_CONSTEXPR bool |
| 506 | operator==(const complex<_Tp>& __x, const complex<_Tp>& __y) |
| 507 | { return __x.real() == __y.real() && __x.imag() == __y.imag(); } |
| 508 | |
| 509 | template<typename _Tp> |
| 510 | inline _GLIBCXX_CONSTEXPR bool |
| 511 | operator==(const complex<_Tp>& __x, const _Tp& __y) |
| 512 | { return __x.real() == __y && __x.imag() == _Tp(); } |
| 513 | |
| 514 | #if !(__cpp_impl_three_way_comparison >= 201907L) |
| 515 | template<typename _Tp> |
| 516 | inline _GLIBCXX_CONSTEXPR bool |
| 517 | operator==(const _Tp& __x, const complex<_Tp>& __y) |
| 518 | { return __x == __y.real() && _Tp() == __y.imag(); } |
| 519 | ///@} |
| 520 | |
| 521 | ///@{ |
| 522 | /// Return false if @a x is equal to @a y. |
| 523 | template<typename _Tp> |
| 524 | inline _GLIBCXX_CONSTEXPR bool |
| 525 | operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y) |
| 526 | { return __x.real() != __y.real() || __x.imag() != __y.imag(); } |
| 527 | |
| 528 | template<typename _Tp> |
| 529 | inline _GLIBCXX_CONSTEXPR bool |
| 530 | operator!=(const complex<_Tp>& __x, const _Tp& __y) |
| 531 | { return __x.real() != __y || __x.imag() != _Tp(); } |
| 532 | |
| 533 | template<typename _Tp> |
| 534 | inline _GLIBCXX_CONSTEXPR bool |
| 535 | operator!=(const _Tp& __x, const complex<_Tp>& __y) |
| 536 | { return __x != __y.real() || _Tp() != __y.imag(); } |
| 537 | #endif |
| 538 | ///@} |
| 539 | |
| 540 | /// Extraction operator for complex values. |
| 541 | template<typename _Tp, typename _CharT, class _Traits> |
| 542 | basic_istream<_CharT, _Traits>& |
| 543 | operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) |
| 544 | { |
| 545 | bool __fail = true; |
| 546 | _CharT __ch; |
| 547 | if (__is >> __ch) |
| 548 | { |
| 549 | if (_Traits::eq(__ch, __is.widen('('))) |
| 550 | { |
| 551 | _Tp __u; |
| 552 | if (__is >> __u >> __ch) |
| 553 | { |
| 554 | const _CharT __rparen = __is.widen(')'); |
| 555 | if (_Traits::eq(__ch, __rparen)) |
| 556 | { |
| 557 | __x = __u; |
| 558 | __fail = false; |
| 559 | } |
| 560 | else if (_Traits::eq(__ch, __is.widen(','))) |
| 561 | { |
| 562 | _Tp __v; |
| 563 | if (__is >> __v >> __ch) |
| 564 | { |
| 565 | if (_Traits::eq(__ch, __rparen)) |
| 566 | { |
| 567 | __x = complex<_Tp>(__u, __v); |
| 568 | __fail = false; |
| 569 | } |
| 570 | else |
| 571 | __is.putback(__ch); |
| 572 | } |
| 573 | } |
| 574 | else |
| 575 | __is.putback(__ch); |
| 576 | } |
| 577 | } |
| 578 | else |
| 579 | { |
| 580 | __is.putback(__ch); |
| 581 | _Tp __u; |
| 582 | if (__is >> __u) |
| 583 | { |
| 584 | __x = __u; |
| 585 | __fail = false; |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | if (__fail) |
| 590 | __is.setstate(ios_base::failbit); |
| 591 | return __is; |
| 592 | } |
| 593 | |
| 594 | /// Insertion operator for complex values. |
| 595 | template<typename _Tp, typename _CharT, class _Traits> |
| 596 | basic_ostream<_CharT, _Traits>& |
| 597 | operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) |
| 598 | { |
| 599 | basic_ostringstream<_CharT, _Traits> __s; |
| 600 | __s.flags(__os.flags()); |
| 601 | __s.imbue(__os.getloc()); |
| 602 | __s.precision(__os.precision()); |
| 603 | __s << '(' << __x.real() << ',' << __x.imag() << ')'; |
| 604 | return __os << __s.str(); |
| 605 | } |
| 606 | |
| 607 | // Values |
| 608 | #if __cplusplus >= 201103L |
| 609 | template<typename _Tp> |
| 610 | constexpr _Tp |
| 611 | real(const complex<_Tp>& __z) |
| 612 | { return __z.real(); } |
| 613 | |
| 614 | template<typename _Tp> |
| 615 | constexpr _Tp |
| 616 | imag(const complex<_Tp>& __z) |
| 617 | { return __z.imag(); } |
| 618 | #else |
| 619 | template<typename _Tp> |
| 620 | inline _Tp& |
| 621 | real(complex<_Tp>& __z) |
| 622 | { return __z.real(); } |
| 623 | |
| 624 | template<typename _Tp> |
| 625 | inline const _Tp& |
| 626 | real(const complex<_Tp>& __z) |
| 627 | { return __z.real(); } |
| 628 | |
| 629 | template<typename _Tp> |
| 630 | inline _Tp& |
| 631 | imag(complex<_Tp>& __z) |
| 632 | { return __z.imag(); } |
| 633 | |
| 634 | template<typename _Tp> |
| 635 | inline const _Tp& |
| 636 | imag(const complex<_Tp>& __z) |
| 637 | { return __z.imag(); } |
| 638 | #endif |
| 639 | |
| 640 | #if __glibcxx_tuple_like >= 202311L // >= C++26 |
| 641 | template<size_t _Int, typename _Tp> |
| 642 | [[nodiscard,__gnu__::__always_inline__]] |
| 643 | constexpr _Tp& |
| 644 | get(complex<_Tp>& __z) noexcept |
| 645 | { |
| 646 | static_assert(_Int < 2); |
| 647 | return __z.__get_part(_Int); |
| 648 | } |
| 649 | |
| 650 | template<size_t _Int, typename _Tp> |
| 651 | [[nodiscard,__gnu__::__always_inline__]] |
| 652 | constexpr _Tp&& |
| 653 | get(complex<_Tp>&& __z) noexcept |
| 654 | { |
| 655 | static_assert(_Int < 2); |
| 656 | return std::move(__z.__get_part(_Int)); |
| 657 | } |
| 658 | |
| 659 | template<size_t _Int, typename _Tp> |
| 660 | [[nodiscard,__gnu__::__always_inline__]] |
| 661 | constexpr const _Tp& |
| 662 | get(const complex<_Tp>& __z) noexcept |
| 663 | { |
| 664 | static_assert(_Int < 2); |
| 665 | return __z.__get_part(_Int); |
| 666 | } |
| 667 | |
| 668 | template<size_t _Int, typename _Tp> |
| 669 | [[nodiscard,__gnu__::__always_inline__]] |
| 670 | constexpr const _Tp&& |
| 671 | get(const complex<_Tp>&& __z) noexcept |
| 672 | { |
| 673 | static_assert(_Int < 2); |
| 674 | return std::move(__z.__get_part(_Int)); |
| 675 | } |
| 676 | #endif // __glibcxx_tuple_like >= 202311 |
| 677 | |
| 678 | #if _GLIBCXX_USE_C99_COMPLEX |
| 679 | #if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) |
| 680 | inline _Float16 |
| 681 | __complex_abs(__complex__ _Float16 __z) |
| 682 | { return _Float16(__builtin_cabsf(__z)); } |
| 683 | |
| 684 | inline _Float16 |
| 685 | __complex_arg(__complex__ _Float16 __z) |
| 686 | { return _Float16(__builtin_cargf(__z)); } |
| 687 | |
| 688 | inline __complex__ _Float16 |
| 689 | __complex_cos(__complex__ _Float16 __z) |
| 690 | { return static_cast<__complex__ _Float16>(__builtin_ccosf(__z)); } |
| 691 | |
| 692 | inline __complex__ _Float16 |
| 693 | __complex_cosh(__complex__ _Float16 __z) |
| 694 | { return static_cast<__complex__ _Float16>(__builtin_ccoshf(__z)); } |
| 695 | |
| 696 | inline __complex__ _Float16 |
| 697 | __complex_exp(__complex__ _Float16 __z) |
| 698 | { return static_cast<__complex__ _Float16>(__builtin_cexpf(__z)); } |
| 699 | |
| 700 | inline __complex__ _Float16 |
| 701 | __complex_log(__complex__ _Float16 __z) |
| 702 | { return static_cast<__complex__ _Float16>(__builtin_clogf(__z)); } |
| 703 | |
| 704 | inline __complex__ _Float16 |
| 705 | __complex_sin(__complex__ _Float16 __z) |
| 706 | { return static_cast<__complex__ _Float16>(__builtin_csinf(__z)); } |
| 707 | |
| 708 | inline __complex__ _Float16 |
| 709 | __complex_sinh(__complex__ _Float16 __z) |
| 710 | { return static_cast<__complex__ _Float16>(__builtin_csinhf(__z)); } |
| 711 | |
| 712 | inline __complex__ _Float16 |
| 713 | __complex_sqrt(__complex__ _Float16 __z) |
| 714 | { return static_cast<__complex__ _Float16>(__builtin_csqrtf(__z)); } |
| 715 | |
| 716 | inline __complex__ _Float16 |
| 717 | __complex_tan(__complex__ _Float16 __z) |
| 718 | { return static_cast<__complex__ _Float16>(__builtin_ctanf(__z)); } |
| 719 | |
| 720 | inline __complex__ _Float16 |
| 721 | __complex_tanh(__complex__ _Float16 __z) |
| 722 | { return static_cast<__complex__ _Float16>(__builtin_ctanhf(__z)); } |
| 723 | |
| 724 | inline __complex__ _Float16 |
| 725 | __complex_pow(__complex__ _Float16 __x, __complex__ _Float16 __y) |
| 726 | { return static_cast<__complex__ _Float16>(__builtin_cpowf(__x, __y)); } |
| 727 | #endif |
| 728 | |
| 729 | #if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) |
| 730 | inline _Float32 |
| 731 | __complex_abs(__complex__ _Float32 __z) { return __builtin_cabsf(__z); } |
| 732 | |
| 733 | inline _Float32 |
| 734 | __complex_arg(__complex__ _Float32 __z) { return __builtin_cargf(__z); } |
| 735 | |
| 736 | inline __complex__ _Float32 |
| 737 | __complex_cos(__complex__ _Float32 __z) { return __builtin_ccosf(__z); } |
| 738 | |
| 739 | inline __complex__ _Float32 |
| 740 | __complex_cosh(__complex__ _Float32 __z) { return __builtin_ccoshf(__z); } |
| 741 | |
| 742 | inline __complex__ _Float32 |
| 743 | __complex_exp(__complex__ _Float32 __z) { return __builtin_cexpf(__z); } |
| 744 | |
| 745 | inline __complex__ _Float32 |
| 746 | __complex_log(__complex__ _Float32 __z) { return __builtin_clogf(__z); } |
| 747 | |
| 748 | inline __complex__ _Float32 |
| 749 | __complex_sin(__complex__ _Float32 __z) { return __builtin_csinf(__z); } |
| 750 | |
| 751 | inline __complex__ _Float32 |
| 752 | __complex_sinh(__complex__ _Float32 __z) { return __builtin_csinhf(__z); } |
| 753 | |
| 754 | inline __complex__ _Float32 |
| 755 | __complex_sqrt(__complex__ _Float32 __z) { return __builtin_csqrtf(__z); } |
| 756 | |
| 757 | inline __complex__ _Float32 |
| 758 | __complex_tan(__complex__ _Float32 __z) { return __builtin_ctanf(__z); } |
| 759 | |
| 760 | inline __complex__ _Float32 |
| 761 | __complex_tanh(__complex__ _Float32 __z) { return __builtin_ctanhf(__z); } |
| 762 | |
| 763 | inline __complex__ _Float32 |
| 764 | __complex_pow(__complex__ _Float32 __x, __complex__ _Float32 __y) |
| 765 | { return __builtin_cpowf(__x, __y); } |
| 766 | #endif |
| 767 | |
| 768 | #if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64) |
| 769 | inline _Float64 |
| 770 | __complex_abs(__complex__ _Float64 __z) { return __builtin_cabs(__z); } |
| 771 | |
| 772 | inline _Float64 |
| 773 | __complex_arg(__complex__ _Float64 __z) { return __builtin_carg(__z); } |
| 774 | |
| 775 | inline __complex__ _Float64 |
| 776 | __complex_cos(__complex__ _Float64 __z) { return __builtin_ccos(__z); } |
| 777 | |
| 778 | inline __complex__ _Float64 |
| 779 | __complex_cosh(__complex__ _Float64 __z) { return __builtin_ccosh(__z); } |
| 780 | |
| 781 | inline __complex__ _Float64 |
| 782 | __complex_exp(__complex__ _Float64 __z) { return __builtin_cexp(__z); } |
| 783 | |
| 784 | inline __complex__ _Float64 |
| 785 | __complex_log(__complex__ _Float64 __z) { return __builtin_clog(__z); } |
| 786 | |
| 787 | inline __complex__ _Float64 |
| 788 | __complex_sin(__complex__ _Float64 __z) { return __builtin_csin(__z); } |
| 789 | |
| 790 | inline __complex__ _Float64 |
| 791 | __complex_sinh(__complex__ _Float64 __z) { return __builtin_csinh(__z); } |
| 792 | |
| 793 | inline __complex__ _Float64 |
| 794 | __complex_sqrt(__complex__ _Float64 __z) { return __builtin_csqrt(__z); } |
| 795 | |
| 796 | inline __complex__ _Float64 |
| 797 | __complex_tan(__complex__ _Float64 __z) { return __builtin_ctan(__z); } |
| 798 | |
| 799 | inline __complex__ _Float64 |
| 800 | __complex_tanh(__complex__ _Float64 __z) { return __builtin_ctanh(__z); } |
| 801 | |
| 802 | inline __complex__ _Float64 |
| 803 | __complex_pow(__complex__ _Float64 __x, __complex__ _Float64 __y) |
| 804 | { return __builtin_cpow(__x, __y); } |
| 805 | #endif |
| 806 | |
| 807 | #if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128) |
| 808 | inline _Float128 |
| 809 | __complex_abs(__complex__ _Float128 __z) { return __builtin_cabsl(__z); } |
| 810 | |
| 811 | inline _Float128 |
| 812 | __complex_arg(__complex__ _Float128 __z) { return __builtin_cargl(__z); } |
| 813 | |
| 814 | inline __complex__ _Float128 |
| 815 | __complex_cos(__complex__ _Float128 __z) { return __builtin_ccosl(__z); } |
| 816 | |
| 817 | inline __complex__ _Float128 |
| 818 | __complex_cosh(__complex__ _Float128 __z) { return __builtin_ccoshl(__z); } |
| 819 | |
| 820 | inline __complex__ _Float128 |
| 821 | __complex_exp(__complex__ _Float128 __z) { return __builtin_cexpl(__z); } |
| 822 | |
| 823 | inline __complex__ _Float128 |
| 824 | __complex_log(__complex__ _Float128 __z) { return __builtin_clogl(__z); } |
| 825 | |
| 826 | inline __complex__ _Float128 |
| 827 | __complex_sin(__complex__ _Float128 __z) { return __builtin_csinl(__z); } |
| 828 | |
| 829 | inline __complex__ _Float128 |
| 830 | __complex_sinh(__complex__ _Float128 __z) { return __builtin_csinhl(__z); } |
| 831 | |
| 832 | inline __complex__ _Float128 |
| 833 | __complex_sqrt(__complex__ _Float128 __z) { return __builtin_csqrtl(__z); } |
| 834 | |
| 835 | inline __complex__ _Float128 |
| 836 | __complex_tan(__complex__ _Float128 __z) { return __builtin_ctanl(__z); } |
| 837 | |
| 838 | inline __complex__ _Float128 |
| 839 | __complex_tanh(__complex__ _Float128 __z) { return __builtin_ctanhl(__z); } |
| 840 | |
| 841 | inline __complex__ _Float128 |
| 842 | __complex_pow(__complex__ _Float128 __x, __complex__ _Float128 __y) |
| 843 | { return __builtin_cpowl(__x, __y); } |
| 844 | #elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH) |
| 845 | inline _Float128 |
| 846 | __complex_abs(__complex__ _Float128 __z) { return __builtin_cabsf128(__z); } |
| 847 | |
| 848 | inline _Float128 |
| 849 | __complex_arg(__complex__ _Float128 __z) { return __builtin_cargf128(__z); } |
| 850 | |
| 851 | inline __complex__ _Float128 |
| 852 | __complex_cos(__complex__ _Float128 __z) { return __builtin_ccosf128(__z); } |
| 853 | |
| 854 | inline __complex__ _Float128 |
| 855 | __complex_cosh(__complex__ _Float128 __z) { return __builtin_ccoshf128(__z); } |
| 856 | |
| 857 | inline __complex__ _Float128 |
| 858 | __complex_exp(__complex__ _Float128 __z) { return __builtin_cexpf128(__z); } |
| 859 | |
| 860 | inline __complex__ _Float128 |
| 861 | __complex_log(__complex__ _Float128 __z) { return __builtin_clogf128(__z); } |
| 862 | |
| 863 | inline __complex__ _Float128 |
| 864 | __complex_sin(__complex__ _Float128 __z) { return __builtin_csinf128(__z); } |
| 865 | |
| 866 | inline __complex__ _Float128 |
| 867 | __complex_sinh(__complex__ _Float128 __z) { return __builtin_csinhf128(__z); } |
| 868 | |
| 869 | inline __complex__ _Float128 |
| 870 | __complex_sqrt(__complex__ _Float128 __z) { return __builtin_csqrtf128(__z); } |
| 871 | |
| 872 | inline __complex__ _Float128 |
| 873 | __complex_tan(__complex__ _Float128 __z) { return __builtin_ctanf128(__z); } |
| 874 | |
| 875 | inline __complex__ _Float128 |
| 876 | __complex_tanh(__complex__ _Float128 __z) { return __builtin_ctanhf128(__z); } |
| 877 | |
| 878 | inline __complex__ _Float128 |
| 879 | __complex_pow(__complex__ _Float128 __x, __complex__ _Float128 __y) |
| 880 | { return __builtin_cpowf128(__x, __y); } |
| 881 | #endif |
| 882 | |
| 883 | #if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) |
| 884 | inline __gnu_cxx::__bfloat16_t |
| 885 | __complex_abs(__complex__ decltype(0.0bf16) __z) |
| 886 | { return __gnu_cxx::__bfloat16_t(__builtin_cabsf(__z)); } |
| 887 | |
| 888 | inline __gnu_cxx::__bfloat16_t |
| 889 | __complex_arg(__complex__ decltype(0.0bf16) __z) |
| 890 | { return __gnu_cxx::__bfloat16_t(__builtin_cargf(__z)); } |
| 891 | |
| 892 | inline __complex__ decltype(0.0bf16) |
| 893 | __complex_cos(__complex__ decltype(0.0bf16) __z) |
| 894 | { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ccosf(__z)); } |
| 895 | |
| 896 | inline __complex__ decltype(0.0bf16) |
| 897 | __complex_cosh(__complex__ decltype(0.0bf16) __z) |
| 898 | { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ccoshf(__z)); } |
| 899 | |
| 900 | inline __complex__ decltype(0.0bf16) |
| 901 | __complex_exp(__complex__ decltype(0.0bf16) __z) |
| 902 | { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cexpf(__z)); } |
| 903 | |
| 904 | inline __complex__ decltype(0.0bf16) |
| 905 | __complex_log(__complex__ decltype(0.0bf16) __z) |
| 906 | { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_clogf(__z)); } |
| 907 | |
| 908 | inline __complex__ decltype(0.0bf16) |
| 909 | __complex_sin(__complex__ decltype(0.0bf16) __z) |
| 910 | { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csinf(__z)); } |
| 911 | |
| 912 | inline __complex__ decltype(0.0bf16) |
| 913 | __complex_sinh(__complex__ decltype(0.0bf16) __z) |
| 914 | { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csinhf(__z)); } |
| 915 | |
| 916 | inline __complex__ decltype(0.0bf16) |
| 917 | __complex_sqrt(__complex__ decltype(0.0bf16) __z) |
| 918 | { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csqrtf(__z)); } |
| 919 | |
| 920 | inline __complex__ decltype(0.0bf16) |
| 921 | __complex_tan(__complex__ decltype(0.0bf16) __z) |
| 922 | { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ctanf(__z)); } |
| 923 | |
| 924 | inline __complex__ decltype(0.0bf16) |
| 925 | __complex_tanh(__complex__ decltype(0.0bf16) __z) |
| 926 | { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ctanhf(__z)); } |
| 927 | |
| 928 | inline __complex__ decltype(0.0bf16) |
| 929 | __complex_pow(__complex__ decltype(0.0bf16) __x, |
| 930 | __complex__ decltype(0.0bf16) __y) |
| 931 | { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cpowf(__x, |
| 932 | __y)); } |
| 933 | #endif |
| 934 | #endif |
| 935 | |
| 936 | // 26.2.7/3 abs(__z): Returns the magnitude of __z. |
| 937 | template<typename _Tp> |
| 938 | inline _Tp |
| 939 | __complex_abs(const complex<_Tp>& __z) |
| 940 | { |
| 941 | _Tp __x = __z.real(); |
| 942 | _Tp __y = __z.imag(); |
| 943 | const _Tp __s = std::max(abs(__x), abs(__y)); |
| 944 | if (__s == _Tp()) // well ... |
| 945 | return __s; |
| 946 | __x /= __s; |
| 947 | __y /= __s; |
| 948 | return __s * sqrt(__x * __x + __y * __y); |
| 949 | } |
| 950 | |
| 951 | #if _GLIBCXX_USE_C99_COMPLEX |
| 952 | inline float |
| 953 | __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); } |
| 954 | |
| 955 | inline double |
| 956 | __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); } |
| 957 | |
| 958 | inline long double |
| 959 | __complex_abs(const __complex__ long double& __z) |
| 960 | { return __builtin_cabsl(__z); } |
| 961 | |
| 962 | template<typename _Tp> |
| 963 | inline _Tp |
| 964 | abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); } |
| 965 | #else |
| 966 | template<typename _Tp> |
| 967 | inline _Tp |
| 968 | abs(const complex<_Tp>& __z) { return __complex_abs(__z); } |
| 969 | #endif |
| 970 | |
| 971 | |
| 972 | // 26.2.7/4: arg(__z): Returns the phase angle of __z. |
| 973 | template<typename _Tp> |
| 974 | inline _Tp |
| 975 | __complex_arg(const complex<_Tp>& __z) |
| 976 | { return atan2(__z.imag(), __z.real()); } |
| 977 | |
| 978 | #if _GLIBCXX_USE_C99_COMPLEX |
| 979 | inline float |
| 980 | __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); } |
| 981 | |
| 982 | inline double |
| 983 | __complex_arg(__complex__ double __z) { return __builtin_carg(__z); } |
| 984 | |
| 985 | inline long double |
| 986 | __complex_arg(const __complex__ long double& __z) |
| 987 | { return __builtin_cargl(__z); } |
| 988 | |
| 989 | template<typename _Tp> |
| 990 | inline _Tp |
| 991 | arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); } |
| 992 | #else |
| 993 | template<typename _Tp> |
| 994 | inline _Tp |
| 995 | arg(const complex<_Tp>& __z) { return __complex_arg(__z); } |
| 996 | #endif |
| 997 | |
| 998 | // 26.2.7/5: norm(__z) returns the squared magnitude of __z. |
| 999 | // As defined, norm() is -not- a norm is the common mathematical |
| 1000 | // sense used in numerics. The helper class _Norm_helper<> tries to |
| 1001 | // distinguish between builtin floating point and the rest, so as |
| 1002 | // to deliver an answer as close as possible to the real value. |
| 1003 | template<bool> |
| 1004 | struct _Norm_helper |
| 1005 | { |
| 1006 | template<typename _Tp> |
| 1007 | static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z) |
| 1008 | { |
| 1009 | const _Tp __x = __z.real(); |
| 1010 | const _Tp __y = __z.imag(); |
| 1011 | return __x * __x + __y * __y; |
| 1012 | } |
| 1013 | }; |
| 1014 | |
| 1015 | template<> |
| 1016 | struct _Norm_helper<true> |
| 1017 | { |
| 1018 | template<typename _Tp> |
| 1019 | static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z) |
| 1020 | { |
| 1021 | //_Tp __res = std::abs(__z); |
| 1022 | //return __res * __res; |
| 1023 | const _Tp __x = __z.real(); |
| 1024 | const _Tp __y = __z.imag(); |
| 1025 | return __x * __x + __y * __y; |
| 1026 | } |
| 1027 | }; |
| 1028 | |
| 1029 | template<typename _Tp> |
| 1030 | inline _GLIBCXX20_CONSTEXPR _Tp |
| 1031 | norm(const complex<_Tp>& __z) |
| 1032 | { |
| 1033 | return _Norm_helper<__is_floating<_Tp>::__value |
| 1034 | && !_GLIBCXX_FAST_MATH>::_S_do_it(__z); |
| 1035 | } |
| 1036 | |
| 1037 | template<typename _Tp> |
| 1038 | inline complex<_Tp> |
| 1039 | polar(const _Tp& __rho, const _Tp& __theta) |
| 1040 | { |
| 1041 | __glibcxx_assert( __rho >= 0 ); |
| 1042 | return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); |
| 1043 | } |
| 1044 | |
| 1045 | template<typename _Tp> |
| 1046 | inline _GLIBCXX20_CONSTEXPR complex<_Tp> |
| 1047 | conj(const complex<_Tp>& __z) |
| 1048 | { return complex<_Tp>(__z.real(), -__z.imag()); } |
| 1049 | |
| 1050 | // Transcendentals |
| 1051 | |
| 1052 | // 26.2.8/1 cos(__z): Returns the cosine of __z. |
| 1053 | template<typename _Tp> |
| 1054 | inline complex<_Tp> |
| 1055 | __complex_cos(const complex<_Tp>& __z) |
| 1056 | { |
| 1057 | const _Tp __x = __z.real(); |
| 1058 | const _Tp __y = __z.imag(); |
| 1059 | return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y)); |
| 1060 | } |
| 1061 | |
| 1062 | #if _GLIBCXX_USE_C99_COMPLEX |
| 1063 | inline __complex__ float |
| 1064 | __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); } |
| 1065 | |
| 1066 | inline __complex__ double |
| 1067 | __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); } |
| 1068 | |
| 1069 | inline __complex__ long double |
| 1070 | __complex_cos(const __complex__ long double& __z) |
| 1071 | { return __builtin_ccosl(__z); } |
| 1072 | |
| 1073 | template<typename _Tp> |
| 1074 | inline complex<_Tp> |
| 1075 | cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); } |
| 1076 | #else |
| 1077 | template<typename _Tp> |
| 1078 | inline complex<_Tp> |
| 1079 | cos(const complex<_Tp>& __z) { return __complex_cos(__z); } |
| 1080 | #endif |
| 1081 | |
| 1082 | // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z. |
| 1083 | template<typename _Tp> |
| 1084 | inline complex<_Tp> |
| 1085 | __complex_cosh(const complex<_Tp>& __z) |
| 1086 | { |
| 1087 | const _Tp __x = __z.real(); |
| 1088 | const _Tp __y = __z.imag(); |
| 1089 | return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y)); |
| 1090 | } |
| 1091 | |
| 1092 | #if _GLIBCXX_USE_C99_COMPLEX |
| 1093 | inline __complex__ float |
| 1094 | __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); } |
| 1095 | |
| 1096 | inline __complex__ double |
| 1097 | __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); } |
| 1098 | |
| 1099 | inline __complex__ long double |
| 1100 | __complex_cosh(const __complex__ long double& __z) |
| 1101 | { return __builtin_ccoshl(__z); } |
| 1102 | |
| 1103 | template<typename _Tp> |
| 1104 | inline complex<_Tp> |
| 1105 | cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); } |
| 1106 | #else |
| 1107 | template<typename _Tp> |
| 1108 | inline complex<_Tp> |
| 1109 | cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); } |
| 1110 | #endif |
| 1111 | |
| 1112 | // 26.2.8/3 exp(__z): Returns the complex base e exponential of x |
| 1113 | template<typename _Tp> |
| 1114 | inline complex<_Tp> |
| 1115 | __complex_exp(const complex<_Tp>& __z) |
| 1116 | { return std::polar<_Tp>(exp(__z.real()), __z.imag()); } |
| 1117 | |
| 1118 | #if _GLIBCXX_USE_C99_COMPLEX |
| 1119 | inline __complex__ float |
| 1120 | __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); } |
| 1121 | |
| 1122 | inline __complex__ double |
| 1123 | __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); } |
| 1124 | |
| 1125 | inline __complex__ long double |
| 1126 | __complex_exp(const __complex__ long double& __z) |
| 1127 | { return __builtin_cexpl(__z); } |
| 1128 | |
| 1129 | template<typename _Tp> |
| 1130 | inline complex<_Tp> |
| 1131 | exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); } |
| 1132 | #else |
| 1133 | template<typename _Tp> |
| 1134 | inline complex<_Tp> |
| 1135 | exp(const complex<_Tp>& __z) { return __complex_exp(__z); } |
| 1136 | #endif |
| 1137 | |
| 1138 | // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z. |
| 1139 | // The branch cut is along the negative axis. |
| 1140 | template<typename _Tp> |
| 1141 | inline complex<_Tp> |
| 1142 | __complex_log(const complex<_Tp>& __z) |
| 1143 | { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); } |
| 1144 | |
| 1145 | #if _GLIBCXX_USE_C99_COMPLEX |
| 1146 | inline __complex__ float |
| 1147 | __complex_log(__complex__ float __z) { return __builtin_clogf(__z); } |
| 1148 | |
| 1149 | inline __complex__ double |
| 1150 | __complex_log(__complex__ double __z) { return __builtin_clog(__z); } |
| 1151 | |
| 1152 | inline __complex__ long double |
| 1153 | __complex_log(const __complex__ long double& __z) |
| 1154 | { return __builtin_clogl(__z); } |
| 1155 | |
| 1156 | template<typename _Tp> |
| 1157 | inline complex<_Tp> |
| 1158 | log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); } |
| 1159 | #else |
| 1160 | template<typename _Tp> |
| 1161 | inline complex<_Tp> |
| 1162 | log(const complex<_Tp>& __z) { return __complex_log(__z); } |
| 1163 | #endif |
| 1164 | |
| 1165 | template<typename _Tp> |
| 1166 | inline complex<_Tp> |
| 1167 | log10(const complex<_Tp>& __z) |
| 1168 | { return std::log(__z) / log(_Tp(10.0)); } |
| 1169 | |
| 1170 | // 26.2.8/10 sin(__z): Returns the sine of __z. |
| 1171 | template<typename _Tp> |
| 1172 | inline complex<_Tp> |
| 1173 | __complex_sin(const complex<_Tp>& __z) |
| 1174 | { |
| 1175 | const _Tp __x = __z.real(); |
| 1176 | const _Tp __y = __z.imag(); |
| 1177 | return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y)); |
| 1178 | } |
| 1179 | |
| 1180 | #if _GLIBCXX_USE_C99_COMPLEX |
| 1181 | inline __complex__ float |
| 1182 | __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); } |
| 1183 | |
| 1184 | inline __complex__ double |
| 1185 | __complex_sin(__complex__ double __z) { return __builtin_csin(__z); } |
| 1186 | |
| 1187 | inline __complex__ long double |
| 1188 | __complex_sin(const __complex__ long double& __z) |
| 1189 | { return __builtin_csinl(__z); } |
| 1190 | |
| 1191 | template<typename _Tp> |
| 1192 | inline complex<_Tp> |
| 1193 | sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); } |
| 1194 | #else |
| 1195 | template<typename _Tp> |
| 1196 | inline complex<_Tp> |
| 1197 | sin(const complex<_Tp>& __z) { return __complex_sin(__z); } |
| 1198 | #endif |
| 1199 | |
| 1200 | // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z. |
| 1201 | template<typename _Tp> |
| 1202 | inline complex<_Tp> |
| 1203 | __complex_sinh(const complex<_Tp>& __z) |
| 1204 | { |
| 1205 | const _Tp __x = __z.real(); |
| 1206 | const _Tp __y = __z.imag(); |
| 1207 | return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y)); |
| 1208 | } |
| 1209 | |
| 1210 | #if _GLIBCXX_USE_C99_COMPLEX |
| 1211 | inline __complex__ float |
| 1212 | __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); } |
| 1213 | |
| 1214 | inline __complex__ double |
| 1215 | __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); } |
| 1216 | |
| 1217 | inline __complex__ long double |
| 1218 | __complex_sinh(const __complex__ long double& __z) |
| 1219 | { return __builtin_csinhl(__z); } |
| 1220 | |
| 1221 | template<typename _Tp> |
| 1222 | inline complex<_Tp> |
| 1223 | sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); } |
| 1224 | #else |
| 1225 | template<typename _Tp> |
| 1226 | inline complex<_Tp> |
| 1227 | sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); } |
| 1228 | #endif |
| 1229 | |
| 1230 | // 26.2.8/13 sqrt(__z): Returns the complex square root of __z. |
| 1231 | // The branch cut is on the negative axis. |
| 1232 | template<typename _Tp> |
| 1233 | complex<_Tp> |
| 1234 | __complex_sqrt(const complex<_Tp>& __z) |
| 1235 | { |
| 1236 | _Tp __x = __z.real(); |
| 1237 | _Tp __y = __z.imag(); |
| 1238 | |
| 1239 | if (__x == _Tp()) |
| 1240 | { |
| 1241 | _Tp __t = sqrt(abs(__y) / 2); |
| 1242 | return complex<_Tp>(__t, __y < _Tp() ? -__t : __t); |
| 1243 | } |
| 1244 | else |
| 1245 | { |
| 1246 | _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x))); |
| 1247 | _Tp __u = __t / 2; |
| 1248 | return __x > _Tp() |
| 1249 | ? complex<_Tp>(__u, __y / __t) |
| 1250 | : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u); |
| 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | #if _GLIBCXX_USE_C99_COMPLEX |
| 1255 | inline __complex__ float |
| 1256 | __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); } |
| 1257 | |
| 1258 | inline __complex__ double |
| 1259 | __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); } |
| 1260 | |
| 1261 | inline __complex__ long double |
| 1262 | __complex_sqrt(const __complex__ long double& __z) |
| 1263 | { return __builtin_csqrtl(__z); } |
| 1264 | |
| 1265 | template<typename _Tp> |
| 1266 | inline complex<_Tp> |
| 1267 | sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); } |
| 1268 | #else |
| 1269 | template<typename _Tp> |
| 1270 | inline complex<_Tp> |
| 1271 | sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); } |
| 1272 | #endif |
| 1273 | |
| 1274 | // 26.2.8/14 tan(__z): Return the complex tangent of __z. |
| 1275 | |
| 1276 | template<typename _Tp> |
| 1277 | inline complex<_Tp> |
| 1278 | __complex_tan(const complex<_Tp>& __z) |
| 1279 | { return std::sin(__z) / std::cos(__z); } |
| 1280 | |
| 1281 | #if _GLIBCXX_USE_C99_COMPLEX |
| 1282 | inline __complex__ float |
| 1283 | __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); } |
| 1284 | |
| 1285 | inline __complex__ double |
| 1286 | __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); } |
| 1287 | |
| 1288 | inline __complex__ long double |
| 1289 | __complex_tan(const __complex__ long double& __z) |
| 1290 | { return __builtin_ctanl(__z); } |
| 1291 | |
| 1292 | template<typename _Tp> |
| 1293 | inline complex<_Tp> |
| 1294 | tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); } |
| 1295 | #else |
| 1296 | template<typename _Tp> |
| 1297 | inline complex<_Tp> |
| 1298 | tan(const complex<_Tp>& __z) { return __complex_tan(__z); } |
| 1299 | #endif |
| 1300 | |
| 1301 | |
| 1302 | // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z. |
| 1303 | |
| 1304 | template<typename _Tp> |
| 1305 | inline complex<_Tp> |
| 1306 | __complex_tanh(const complex<_Tp>& __z) |
| 1307 | { return std::sinh(__z) / std::cosh(__z); } |
| 1308 | |
| 1309 | #if _GLIBCXX_USE_C99_COMPLEX |
| 1310 | inline __complex__ float |
| 1311 | __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); } |
| 1312 | |
| 1313 | inline __complex__ double |
| 1314 | __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); } |
| 1315 | |
| 1316 | inline __complex__ long double |
| 1317 | __complex_tanh(const __complex__ long double& __z) |
| 1318 | { return __builtin_ctanhl(__z); } |
| 1319 | |
| 1320 | template<typename _Tp> |
| 1321 | inline complex<_Tp> |
| 1322 | tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); } |
| 1323 | #else |
| 1324 | template<typename _Tp> |
| 1325 | inline complex<_Tp> |
| 1326 | tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); } |
| 1327 | #endif |
| 1328 | |
| 1329 | |
| 1330 | // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x |
| 1331 | // raised to the __y-th power. The branch |
| 1332 | // cut is on the negative axis. |
| 1333 | template<typename _Tp> |
| 1334 | complex<_Tp> |
| 1335 | __complex_pow_unsigned(complex<_Tp> __x, unsigned __n) |
| 1336 | { |
| 1337 | complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1); |
| 1338 | |
| 1339 | while (__n >>= 1) |
| 1340 | { |
| 1341 | __x *= __x; |
| 1342 | if (__n % 2) |
| 1343 | __y *= __x; |
| 1344 | } |
| 1345 | |
| 1346 | return __y; |
| 1347 | } |
| 1348 | |
| 1349 | // In C++11 mode we used to implement the resolution of |
| 1350 | // DR 844. complex pow return type is ambiguous. |
| 1351 | // thus the following overload was disabled in that mode. However, doing |
| 1352 | // that causes all sorts of issues, see, for example: |
| 1353 | // http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html |
| 1354 | // and also PR57974. |
| 1355 | template<typename _Tp> |
| 1356 | inline complex<_Tp> |
| 1357 | pow(const complex<_Tp>& __z, int __n) |
| 1358 | { |
| 1359 | return __n < 0 |
| 1360 | ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n) |
| 1361 | : std::__complex_pow_unsigned(__z, __n); |
| 1362 | } |
| 1363 | |
| 1364 | template<typename _Tp> |
| 1365 | complex<_Tp> |
| 1366 | pow(const complex<_Tp>& __x, const _Tp& __y) |
| 1367 | { |
| 1368 | #if ! _GLIBCXX_USE_C99_COMPLEX |
| 1369 | if (__x == _Tp()) |
| 1370 | return _Tp(); |
| 1371 | #endif |
| 1372 | if (__x.imag() == _Tp() && __x.real() > _Tp()) |
| 1373 | return pow(__x.real(), __y); |
| 1374 | |
| 1375 | complex<_Tp> __t = std::log(__x); |
| 1376 | return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag()); |
| 1377 | } |
| 1378 | |
| 1379 | template<typename _Tp> |
| 1380 | inline complex<_Tp> |
| 1381 | __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y) |
| 1382 | { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); } |
| 1383 | |
| 1384 | #if _GLIBCXX_USE_C99_COMPLEX |
| 1385 | inline __complex__ float |
| 1386 | __complex_pow(__complex__ float __x, __complex__ float __y) |
| 1387 | { return __builtin_cpowf(__x, __y); } |
| 1388 | |
| 1389 | inline __complex__ double |
| 1390 | __complex_pow(__complex__ double __x, __complex__ double __y) |
| 1391 | { return __builtin_cpow(__x, __y); } |
| 1392 | |
| 1393 | inline __complex__ long double |
| 1394 | __complex_pow(const __complex__ long double& __x, |
| 1395 | const __complex__ long double& __y) |
| 1396 | { return __builtin_cpowl(__x, __y); } |
| 1397 | |
| 1398 | template<typename _Tp> |
| 1399 | inline complex<_Tp> |
| 1400 | pow(const complex<_Tp>& __x, const complex<_Tp>& __y) |
| 1401 | { return __complex_pow(__x.__rep(), __y.__rep()); } |
| 1402 | #else |
| 1403 | template<typename _Tp> |
| 1404 | inline complex<_Tp> |
| 1405 | pow(const complex<_Tp>& __x, const complex<_Tp>& __y) |
| 1406 | { return __complex_pow(__x, __y); } |
| 1407 | #endif |
| 1408 | |
| 1409 | template<typename _Tp> |
| 1410 | inline complex<_Tp> |
| 1411 | pow(const _Tp& __x, const complex<_Tp>& __y) |
| 1412 | { |
| 1413 | return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()), |
| 1414 | __y.imag() * log(__x)) |
| 1415 | : std::pow(complex<_Tp>(__x), __y); |
| 1416 | } |
| 1417 | |
| 1418 | #if __glibcxx_tuple_like >= 202311L // >= C++26 |
| 1419 | #define _GLIBCXX26_DECLARE_COMPLEX_TUPLE_HELPER_ACCESSOR \ |
| 1420 | template<typename _Cp> \ |
| 1421 | [[__gnu__::__always_inline__]] \ |
| 1422 | constexpr auto& \ |
| 1423 | __get_part(this _Cp& __z, size_t __i) noexcept \ |
| 1424 | { \ |
| 1425 | return __i == 0 ? __real__ __z._M_value \ |
| 1426 | : __imag__ __z._M_value; \ |
| 1427 | } |
| 1428 | #else |
| 1429 | #define _GLIBCXX26_DECLARE_COMPLEX_TUPLE_HELPER_ACCESSOR |
| 1430 | #endif |
| 1431 | |
| 1432 | /// 26.2.3 complex specializations |
| 1433 | /// complex<float> specialization |
| 1434 | template<> |
| 1435 | class complex<float> |
| 1436 | { |
| 1437 | public: |
| 1438 | typedef float value_type; |
| 1439 | typedef __complex__ float _ComplexT; |
| 1440 | |
| 1441 | _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } |
| 1442 | |
| 1443 | _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f) |
| 1444 | #if __cplusplus >= 201103L |
| 1445 | : _M_value{ __r, __i } { } |
| 1446 | #else |
| 1447 | { |
| 1448 | __real__ _M_value = __r; |
| 1449 | __imag__ _M_value = __i; |
| 1450 | } |
| 1451 | #endif |
| 1452 | |
| 1453 | #if __cplusplus >= 201103L |
| 1454 | _GLIBCXX14_CONSTEXPR complex(const complex&) = default; |
| 1455 | #endif |
| 1456 | |
| 1457 | #if __cplusplus > 202002L |
| 1458 | template<typename _Up> |
| 1459 | explicit(!requires(_Up __u) { value_type{__u}; }) |
| 1460 | constexpr complex(const complex<_Up>& __z) |
| 1461 | : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { } |
| 1462 | #else |
| 1463 | explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&); |
| 1464 | explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); |
| 1465 | #endif |
| 1466 | |
| 1467 | #if __cplusplus >= 201103L |
| 1468 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 1469 | // DR 387. std::complex over-encapsulated. |
| 1470 | __attribute ((__abi_tag__ ("cxx11" ))) |
| 1471 | constexpr float |
| 1472 | real() const { return __real__ _M_value; } |
| 1473 | |
| 1474 | __attribute ((__abi_tag__ ("cxx11" ))) |
| 1475 | constexpr float |
| 1476 | imag() const { return __imag__ _M_value; } |
| 1477 | #else |
| 1478 | float& |
| 1479 | real() { return __real__ _M_value; } |
| 1480 | |
| 1481 | const float& |
| 1482 | real() const { return __real__ _M_value; } |
| 1483 | |
| 1484 | float& |
| 1485 | imag() { return __imag__ _M_value; } |
| 1486 | |
| 1487 | const float& |
| 1488 | imag() const { return __imag__ _M_value; } |
| 1489 | #endif |
| 1490 | |
| 1491 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 1492 | // DR 387. std::complex over-encapsulated. |
| 1493 | _GLIBCXX20_CONSTEXPR void |
| 1494 | real(float __val) { __real__ _M_value = __val; } |
| 1495 | |
| 1496 | _GLIBCXX20_CONSTEXPR void |
| 1497 | imag(float __val) { __imag__ _M_value = __val; } |
| 1498 | |
| 1499 | _GLIBCXX20_CONSTEXPR complex& |
| 1500 | operator=(float __f) |
| 1501 | { |
| 1502 | _M_value = __f; |
| 1503 | return *this; |
| 1504 | } |
| 1505 | |
| 1506 | _GLIBCXX20_CONSTEXPR complex& |
| 1507 | operator+=(float __f) |
| 1508 | { |
| 1509 | _M_value += __f; |
| 1510 | return *this; |
| 1511 | } |
| 1512 | |
| 1513 | _GLIBCXX20_CONSTEXPR complex& |
| 1514 | operator-=(float __f) |
| 1515 | { |
| 1516 | _M_value -= __f; |
| 1517 | return *this; |
| 1518 | } |
| 1519 | |
| 1520 | _GLIBCXX20_CONSTEXPR complex& |
| 1521 | operator*=(float __f) |
| 1522 | { |
| 1523 | _M_value *= __f; |
| 1524 | return *this; |
| 1525 | } |
| 1526 | |
| 1527 | _GLIBCXX20_CONSTEXPR complex& |
| 1528 | operator/=(float __f) |
| 1529 | { |
| 1530 | _M_value /= __f; |
| 1531 | return *this; |
| 1532 | } |
| 1533 | |
| 1534 | // Let the compiler synthesize the copy and assignment |
| 1535 | // operator. It always does a pretty good job. |
| 1536 | #if __cplusplus >= 201103L |
| 1537 | _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default; |
| 1538 | #endif |
| 1539 | |
| 1540 | template<typename _Tp> |
| 1541 | _GLIBCXX20_CONSTEXPR complex& |
| 1542 | operator=(const complex<_Tp>& __z) |
| 1543 | { |
| 1544 | __real__ _M_value = __z.real(); |
| 1545 | __imag__ _M_value = __z.imag(); |
| 1546 | return *this; |
| 1547 | } |
| 1548 | |
| 1549 | template<typename _Tp> |
| 1550 | _GLIBCXX20_CONSTEXPR complex& |
| 1551 | operator+=(const complex<_Tp>& __z) |
| 1552 | { |
| 1553 | _M_value += __z.__rep(); |
| 1554 | return *this; |
| 1555 | } |
| 1556 | |
| 1557 | template<class _Tp> |
| 1558 | _GLIBCXX20_CONSTEXPR complex& |
| 1559 | operator-=(const complex<_Tp>& __z) |
| 1560 | { |
| 1561 | _M_value -= __z.__rep(); |
| 1562 | return *this; |
| 1563 | } |
| 1564 | |
| 1565 | template<class _Tp> |
| 1566 | _GLIBCXX20_CONSTEXPR complex& |
| 1567 | operator*=(const complex<_Tp>& __z) |
| 1568 | { |
| 1569 | const _ComplexT __t = __z.__rep(); |
| 1570 | _M_value *= __t; |
| 1571 | return *this; |
| 1572 | } |
| 1573 | |
| 1574 | template<class _Tp> |
| 1575 | _GLIBCXX20_CONSTEXPR complex& |
| 1576 | operator/=(const complex<_Tp>& __z) |
| 1577 | { |
| 1578 | const _ComplexT __t = __z.__rep(); |
| 1579 | _M_value /= __t; |
| 1580 | return *this; |
| 1581 | } |
| 1582 | |
| 1583 | _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; } |
| 1584 | |
| 1585 | _GLIBCXX26_DECLARE_COMPLEX_TUPLE_HELPER_ACCESSOR |
| 1586 | |
| 1587 | private: |
| 1588 | _ComplexT _M_value; |
| 1589 | }; |
| 1590 | |
| 1591 | /// 26.2.3 complex specializations |
| 1592 | /// complex<double> specialization |
| 1593 | template<> |
| 1594 | class complex<double> |
| 1595 | { |
| 1596 | public: |
| 1597 | typedef double value_type; |
| 1598 | typedef __complex__ double _ComplexT; |
| 1599 | |
| 1600 | _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } |
| 1601 | |
| 1602 | _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0) |
| 1603 | #if __cplusplus >= 201103L |
| 1604 | : _M_value{ __r, __i } { } |
| 1605 | #else |
| 1606 | { |
| 1607 | __real__ _M_value = __r; |
| 1608 | __imag__ _M_value = __i; |
| 1609 | } |
| 1610 | #endif |
| 1611 | |
| 1612 | #if __cplusplus >= 201103L |
| 1613 | _GLIBCXX14_CONSTEXPR complex(const complex&) = default; |
| 1614 | #endif |
| 1615 | |
| 1616 | #if __cplusplus > 202002L |
| 1617 | template<typename _Up> |
| 1618 | explicit(!requires(_Up __u) { value_type{__u}; }) |
| 1619 | constexpr complex(const complex<_Up>& __z) |
| 1620 | : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { } |
| 1621 | #else |
| 1622 | _GLIBCXX_CONSTEXPR complex(const complex<float>& __z) |
| 1623 | : _M_value(__z.__rep()) { } |
| 1624 | |
| 1625 | explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); |
| 1626 | #endif |
| 1627 | |
| 1628 | #if __cplusplus >= 201103L |
| 1629 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 1630 | // DR 387. std::complex over-encapsulated. |
| 1631 | __attribute ((__abi_tag__ ("cxx11" ))) |
| 1632 | constexpr double |
| 1633 | real() const { return __real__ _M_value; } |
| 1634 | |
| 1635 | __attribute ((__abi_tag__ ("cxx11" ))) |
| 1636 | constexpr double |
| 1637 | imag() const { return __imag__ _M_value; } |
| 1638 | #else |
| 1639 | double& |
| 1640 | real() { return __real__ _M_value; } |
| 1641 | |
| 1642 | const double& |
| 1643 | real() const { return __real__ _M_value; } |
| 1644 | |
| 1645 | double& |
| 1646 | imag() { return __imag__ _M_value; } |
| 1647 | |
| 1648 | const double& |
| 1649 | imag() const { return __imag__ _M_value; } |
| 1650 | #endif |
| 1651 | |
| 1652 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 1653 | // DR 387. std::complex over-encapsulated. |
| 1654 | _GLIBCXX20_CONSTEXPR void |
| 1655 | real(double __val) { __real__ _M_value = __val; } |
| 1656 | |
| 1657 | _GLIBCXX20_CONSTEXPR void |
| 1658 | imag(double __val) { __imag__ _M_value = __val; } |
| 1659 | |
| 1660 | _GLIBCXX20_CONSTEXPR complex& |
| 1661 | operator=(double __d) |
| 1662 | { |
| 1663 | _M_value = __d; |
| 1664 | return *this; |
| 1665 | } |
| 1666 | |
| 1667 | _GLIBCXX20_CONSTEXPR complex& |
| 1668 | operator+=(double __d) |
| 1669 | { |
| 1670 | _M_value += __d; |
| 1671 | return *this; |
| 1672 | } |
| 1673 | |
| 1674 | _GLIBCXX20_CONSTEXPR complex& |
| 1675 | operator-=(double __d) |
| 1676 | { |
| 1677 | _M_value -= __d; |
| 1678 | return *this; |
| 1679 | } |
| 1680 | |
| 1681 | _GLIBCXX20_CONSTEXPR complex& |
| 1682 | operator*=(double __d) |
| 1683 | { |
| 1684 | _M_value *= __d; |
| 1685 | return *this; |
| 1686 | } |
| 1687 | |
| 1688 | _GLIBCXX20_CONSTEXPR complex& |
| 1689 | operator/=(double __d) |
| 1690 | { |
| 1691 | _M_value /= __d; |
| 1692 | return *this; |
| 1693 | } |
| 1694 | |
| 1695 | // The compiler will synthesize this, efficiently. |
| 1696 | #if __cplusplus >= 201103L |
| 1697 | _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default; |
| 1698 | #endif |
| 1699 | |
| 1700 | template<typename _Tp> |
| 1701 | _GLIBCXX20_CONSTEXPR complex& |
| 1702 | operator=(const complex<_Tp>& __z) |
| 1703 | { |
| 1704 | _M_value = __z.__rep(); |
| 1705 | return *this; |
| 1706 | } |
| 1707 | |
| 1708 | template<typename _Tp> |
| 1709 | _GLIBCXX20_CONSTEXPR complex& |
| 1710 | operator+=(const complex<_Tp>& __z) |
| 1711 | { |
| 1712 | _M_value += __z.__rep(); |
| 1713 | return *this; |
| 1714 | } |
| 1715 | |
| 1716 | template<typename _Tp> |
| 1717 | _GLIBCXX20_CONSTEXPR complex& |
| 1718 | operator-=(const complex<_Tp>& __z) |
| 1719 | { |
| 1720 | _M_value -= __z.__rep(); |
| 1721 | return *this; |
| 1722 | } |
| 1723 | |
| 1724 | template<typename _Tp> |
| 1725 | _GLIBCXX20_CONSTEXPR complex& |
| 1726 | operator*=(const complex<_Tp>& __z) |
| 1727 | { |
| 1728 | const _ComplexT __t = __z.__rep(); |
| 1729 | _M_value *= __t; |
| 1730 | return *this; |
| 1731 | } |
| 1732 | |
| 1733 | template<typename _Tp> |
| 1734 | _GLIBCXX20_CONSTEXPR complex& |
| 1735 | operator/=(const complex<_Tp>& __z) |
| 1736 | { |
| 1737 | const _ComplexT __t = __z.__rep(); |
| 1738 | _M_value /= __t; |
| 1739 | return *this; |
| 1740 | } |
| 1741 | |
| 1742 | _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; } |
| 1743 | |
| 1744 | _GLIBCXX26_DECLARE_COMPLEX_TUPLE_HELPER_ACCESSOR |
| 1745 | |
| 1746 | private: |
| 1747 | _ComplexT _M_value; |
| 1748 | }; |
| 1749 | |
| 1750 | /// 26.2.3 complex specializations |
| 1751 | /// complex<long double> specialization |
| 1752 | template<> |
| 1753 | class complex<long double> |
| 1754 | { |
| 1755 | public: |
| 1756 | typedef long double value_type; |
| 1757 | typedef __complex__ long double _ComplexT; |
| 1758 | |
| 1759 | _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } |
| 1760 | |
| 1761 | _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L, |
| 1762 | long double __i = 0.0L) |
| 1763 | #if __cplusplus >= 201103L |
| 1764 | : _M_value{ __r, __i } { } |
| 1765 | #else |
| 1766 | { |
| 1767 | __real__ _M_value = __r; |
| 1768 | __imag__ _M_value = __i; |
| 1769 | } |
| 1770 | #endif |
| 1771 | |
| 1772 | #if __cplusplus >= 201103L |
| 1773 | _GLIBCXX14_CONSTEXPR complex(const complex&) = default; |
| 1774 | #endif |
| 1775 | |
| 1776 | #if __cplusplus > 202002L |
| 1777 | template<typename _Up> |
| 1778 | explicit(!requires(_Up __u) { value_type{__u}; }) |
| 1779 | constexpr complex(const complex<_Up>& __z) |
| 1780 | : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { } |
| 1781 | #else |
| 1782 | _GLIBCXX_CONSTEXPR complex(const complex<float>& __z) |
| 1783 | : _M_value(__z.__rep()) { } |
| 1784 | |
| 1785 | _GLIBCXX_CONSTEXPR complex(const complex<double>& __z) |
| 1786 | : _M_value(__z.__rep()) { } |
| 1787 | #endif |
| 1788 | |
| 1789 | #if __cplusplus >= 201103L |
| 1790 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 1791 | // DR 387. std::complex over-encapsulated. |
| 1792 | __attribute ((__abi_tag__ ("cxx11" ))) |
| 1793 | constexpr long double |
| 1794 | real() const { return __real__ _M_value; } |
| 1795 | |
| 1796 | __attribute ((__abi_tag__ ("cxx11" ))) |
| 1797 | constexpr long double |
| 1798 | imag() const { return __imag__ _M_value; } |
| 1799 | #else |
| 1800 | long double& |
| 1801 | real() { return __real__ _M_value; } |
| 1802 | |
| 1803 | const long double& |
| 1804 | real() const { return __real__ _M_value; } |
| 1805 | |
| 1806 | long double& |
| 1807 | imag() { return __imag__ _M_value; } |
| 1808 | |
| 1809 | const long double& |
| 1810 | imag() const { return __imag__ _M_value; } |
| 1811 | #endif |
| 1812 | |
| 1813 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 1814 | // DR 387. std::complex over-encapsulated. |
| 1815 | _GLIBCXX20_CONSTEXPR void |
| 1816 | real(long double __val) { __real__ _M_value = __val; } |
| 1817 | |
| 1818 | _GLIBCXX20_CONSTEXPR void |
| 1819 | imag(long double __val) { __imag__ _M_value = __val; } |
| 1820 | |
| 1821 | _GLIBCXX20_CONSTEXPR complex& |
| 1822 | operator=(long double __r) |
| 1823 | { |
| 1824 | _M_value = __r; |
| 1825 | return *this; |
| 1826 | } |
| 1827 | |
| 1828 | _GLIBCXX20_CONSTEXPR complex& |
| 1829 | operator+=(long double __r) |
| 1830 | { |
| 1831 | _M_value += __r; |
| 1832 | return *this; |
| 1833 | } |
| 1834 | |
| 1835 | _GLIBCXX20_CONSTEXPR complex& |
| 1836 | operator-=(long double __r) |
| 1837 | { |
| 1838 | _M_value -= __r; |
| 1839 | return *this; |
| 1840 | } |
| 1841 | |
| 1842 | _GLIBCXX20_CONSTEXPR complex& |
| 1843 | operator*=(long double __r) |
| 1844 | { |
| 1845 | _M_value *= __r; |
| 1846 | return *this; |
| 1847 | } |
| 1848 | |
| 1849 | _GLIBCXX20_CONSTEXPR complex& |
| 1850 | operator/=(long double __r) |
| 1851 | { |
| 1852 | _M_value /= __r; |
| 1853 | return *this; |
| 1854 | } |
| 1855 | |
| 1856 | // The compiler knows how to do this efficiently |
| 1857 | #if __cplusplus >= 201103L |
| 1858 | _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default; |
| 1859 | #endif |
| 1860 | |
| 1861 | template<typename _Tp> |
| 1862 | _GLIBCXX20_CONSTEXPR complex& |
| 1863 | operator=(const complex<_Tp>& __z) |
| 1864 | { |
| 1865 | _M_value = __z.__rep(); |
| 1866 | return *this; |
| 1867 | } |
| 1868 | |
| 1869 | template<typename _Tp> |
| 1870 | _GLIBCXX20_CONSTEXPR complex& |
| 1871 | operator+=(const complex<_Tp>& __z) |
| 1872 | { |
| 1873 | _M_value += __z.__rep(); |
| 1874 | return *this; |
| 1875 | } |
| 1876 | |
| 1877 | template<typename _Tp> |
| 1878 | _GLIBCXX20_CONSTEXPR complex& |
| 1879 | operator-=(const complex<_Tp>& __z) |
| 1880 | { |
| 1881 | _M_value -= __z.__rep(); |
| 1882 | return *this; |
| 1883 | } |
| 1884 | |
| 1885 | template<typename _Tp> |
| 1886 | _GLIBCXX20_CONSTEXPR complex& |
| 1887 | operator*=(const complex<_Tp>& __z) |
| 1888 | { |
| 1889 | const _ComplexT __t = __z.__rep(); |
| 1890 | _M_value *= __t; |
| 1891 | return *this; |
| 1892 | } |
| 1893 | |
| 1894 | template<typename _Tp> |
| 1895 | _GLIBCXX20_CONSTEXPR complex& |
| 1896 | operator/=(const complex<_Tp>& __z) |
| 1897 | { |
| 1898 | const _ComplexT __t = __z.__rep(); |
| 1899 | _M_value /= __t; |
| 1900 | return *this; |
| 1901 | } |
| 1902 | |
| 1903 | _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; } |
| 1904 | |
| 1905 | _GLIBCXX26_DECLARE_COMPLEX_TUPLE_HELPER_ACCESSOR |
| 1906 | |
| 1907 | private: |
| 1908 | _ComplexT _M_value; |
| 1909 | }; |
| 1910 | |
| 1911 | #if __cplusplus > 202002L |
| 1912 | template<typename _Tp> |
| 1913 | struct __complex_type |
| 1914 | { }; |
| 1915 | |
| 1916 | #ifdef __STDCPP_FLOAT16_T__ |
| 1917 | template<> |
| 1918 | struct __complex_type<_Float16> |
| 1919 | { typedef __complex__ _Float16 type; }; |
| 1920 | #endif |
| 1921 | |
| 1922 | #ifdef __STDCPP_FLOAT32_T__ |
| 1923 | template<> |
| 1924 | struct __complex_type<_Float32> |
| 1925 | { typedef __complex__ _Float32 type; }; |
| 1926 | #endif |
| 1927 | |
| 1928 | #ifdef __STDCPP_FLOAT64_T__ |
| 1929 | template<> |
| 1930 | struct __complex_type<_Float64> |
| 1931 | { typedef __complex__ _Float64 type; }; |
| 1932 | #endif |
| 1933 | |
| 1934 | #ifdef __STDCPP_FLOAT128_T__ |
| 1935 | template<> |
| 1936 | struct __complex_type<_Float128> |
| 1937 | { typedef __complex__ _Float128 type; }; |
| 1938 | #endif |
| 1939 | |
| 1940 | #ifdef __STDCPP_BFLOAT16_T__ |
| 1941 | template<> |
| 1942 | struct __complex_type<__gnu_cxx::__bfloat16_t> |
| 1943 | { typedef __complex__ decltype(0.0bf16) type; }; |
| 1944 | #endif |
| 1945 | |
| 1946 | template<typename _Tp> |
| 1947 | requires requires { typename __complex_type<_Tp>::type; } |
| 1948 | class complex<_Tp> |
| 1949 | { |
| 1950 | public: |
| 1951 | typedef _Tp value_type; |
| 1952 | typedef typename std::__complex_type<_Tp>::type _ComplexT; |
| 1953 | |
| 1954 | constexpr complex(_ComplexT __z) : _M_value(__z) { } |
| 1955 | |
| 1956 | constexpr complex(_Tp __r = _Tp(), _Tp __i = _Tp()) |
| 1957 | : _M_value{ __r, __i } { } |
| 1958 | |
| 1959 | template<typename _Up> |
| 1960 | explicit(!requires(_Up __u) { value_type{__u}; }) |
| 1961 | constexpr complex(const complex<_Up>& __z) |
| 1962 | : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { } |
| 1963 | |
| 1964 | constexpr _Tp |
| 1965 | real() const { return __real__ _M_value; } |
| 1966 | |
| 1967 | constexpr _Tp |
| 1968 | imag() const { return __imag__ _M_value; } |
| 1969 | |
| 1970 | constexpr void |
| 1971 | real(_Tp __val) { __real__ _M_value = __val; } |
| 1972 | |
| 1973 | constexpr void |
| 1974 | imag(_Tp __val) { __imag__ _M_value = __val; } |
| 1975 | |
| 1976 | constexpr complex& |
| 1977 | operator=(_Tp __f) |
| 1978 | { |
| 1979 | _M_value = __f; |
| 1980 | return *this; |
| 1981 | } |
| 1982 | |
| 1983 | constexpr complex& |
| 1984 | operator+=(_Tp __f) |
| 1985 | { |
| 1986 | _M_value += __f; |
| 1987 | return *this; |
| 1988 | } |
| 1989 | |
| 1990 | constexpr complex& |
| 1991 | operator-=(_Tp __f) |
| 1992 | { |
| 1993 | _M_value -= __f; |
| 1994 | return *this; |
| 1995 | } |
| 1996 | |
| 1997 | constexpr complex& |
| 1998 | operator*=(_Tp __f) |
| 1999 | { |
| 2000 | _M_value *= __f; |
| 2001 | return *this; |
| 2002 | } |
| 2003 | |
| 2004 | constexpr complex& |
| 2005 | operator/=(_Tp __f) |
| 2006 | { |
| 2007 | _M_value /= __f; |
| 2008 | return *this; |
| 2009 | } |
| 2010 | |
| 2011 | // Let the compiler synthesize the copy and assignment |
| 2012 | // operator. It always does a pretty good job. |
| 2013 | constexpr complex(const complex&) = default; |
| 2014 | constexpr complex& operator=(const complex&) = default; |
| 2015 | |
| 2016 | template<typename _Up> |
| 2017 | constexpr complex& |
| 2018 | operator=(const complex<_Up>& __z) |
| 2019 | { |
| 2020 | __real__ _M_value = __z.real(); |
| 2021 | __imag__ _M_value = __z.imag(); |
| 2022 | return *this; |
| 2023 | } |
| 2024 | |
| 2025 | template<typename _Up> |
| 2026 | constexpr complex& |
| 2027 | operator+=(const complex<_Up>& __z) |
| 2028 | { |
| 2029 | _M_value += __z.__rep(); |
| 2030 | return *this; |
| 2031 | } |
| 2032 | |
| 2033 | template<class _Up> |
| 2034 | constexpr complex& |
| 2035 | operator-=(const complex<_Up>& __z) |
| 2036 | { |
| 2037 | _M_value -= __z.__rep(); |
| 2038 | return *this; |
| 2039 | } |
| 2040 | |
| 2041 | template<class _Up> |
| 2042 | constexpr complex& |
| 2043 | operator*=(const complex<_Up>& __z) |
| 2044 | { |
| 2045 | const _ComplexT __t = __z.__rep(); |
| 2046 | _M_value *= __t; |
| 2047 | return *this; |
| 2048 | } |
| 2049 | |
| 2050 | template<class _Up> |
| 2051 | constexpr complex& |
| 2052 | operator/=(const complex<_Up>& __z) |
| 2053 | { |
| 2054 | const _ComplexT __t = __z.__rep(); |
| 2055 | _M_value /= __t; |
| 2056 | return *this; |
| 2057 | } |
| 2058 | |
| 2059 | constexpr _ComplexT __rep() const { return _M_value; } |
| 2060 | |
| 2061 | _GLIBCXX26_DECLARE_COMPLEX_TUPLE_HELPER_ACCESSOR |
| 2062 | |
| 2063 | private: |
| 2064 | _ComplexT _M_value; |
| 2065 | }; |
| 2066 | #endif |
| 2067 | |
| 2068 | #undef _GLIBCXX26_DECLARE_COMPLEX_TUPLE_HELPER_ACCESSOR |
| 2069 | |
| 2070 | #if __cplusplus <= 202002L |
| 2071 | // These bits have to be at the end of this file, so that the |
| 2072 | // specializations have all been defined. |
| 2073 | inline _GLIBCXX_CONSTEXPR |
| 2074 | complex<float>::complex(const complex<double>& __z) |
| 2075 | : _M_value(__z.__rep()) { } |
| 2076 | |
| 2077 | inline _GLIBCXX_CONSTEXPR |
| 2078 | complex<float>::complex(const complex<long double>& __z) |
| 2079 | : _M_value(__z.__rep()) { } |
| 2080 | |
| 2081 | inline _GLIBCXX_CONSTEXPR |
| 2082 | complex<double>::complex(const complex<long double>& __z) |
| 2083 | : _M_value(__z.__rep()) { } |
| 2084 | #endif |
| 2085 | |
| 2086 | // Inhibit implicit instantiations for required instantiations, |
| 2087 | // which are defined via explicit instantiations elsewhere. |
| 2088 | // NB: This syntax is a GNU extension. |
| 2089 | #if _GLIBCXX_EXTERN_TEMPLATE |
| 2090 | extern template istream& operator>>(istream&, complex<float>&); |
| 2091 | extern template ostream& operator<<(ostream&, const complex<float>&); |
| 2092 | extern template istream& operator>>(istream&, complex<double>&); |
| 2093 | extern template ostream& operator<<(ostream&, const complex<double>&); |
| 2094 | extern template istream& operator>>(istream&, complex<long double>&); |
| 2095 | extern template ostream& operator<<(ostream&, const complex<long double>&); |
| 2096 | |
| 2097 | #ifdef _GLIBCXX_USE_WCHAR_T |
| 2098 | extern template wistream& operator>>(wistream&, complex<float>&); |
| 2099 | extern template wostream& operator<<(wostream&, const complex<float>&); |
| 2100 | extern template wistream& operator>>(wistream&, complex<double>&); |
| 2101 | extern template wostream& operator<<(wostream&, const complex<double>&); |
| 2102 | extern template wistream& operator>>(wistream&, complex<long double>&); |
| 2103 | extern template wostream& operator<<(wostream&, const complex<long double>&); |
| 2104 | #endif |
| 2105 | #endif |
| 2106 | |
| 2107 | /// @} group complex_numbers |
| 2108 | |
| 2109 | _GLIBCXX_END_NAMESPACE_VERSION |
| 2110 | } // namespace |
| 2111 | |
| 2112 | #if __cplusplus >= 201103L |
| 2113 | |
| 2114 | namespace std _GLIBCXX_VISIBILITY(default) |
| 2115 | { |
| 2116 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 2117 | |
| 2118 | // Forward declarations. |
| 2119 | template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&); |
| 2120 | template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&); |
| 2121 | template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&); |
| 2122 | |
| 2123 | template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&); |
| 2124 | template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&); |
| 2125 | template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&); |
| 2126 | // DR 595. |
| 2127 | template<typename _Tp> _Tp fabs(const std::complex<_Tp>&); |
| 2128 | |
| 2129 | template<typename _Tp> |
| 2130 | inline std::complex<_Tp> |
| 2131 | __complex_acos(const std::complex<_Tp>& __z) |
| 2132 | { |
| 2133 | const std::complex<_Tp> __t = std::asin(__z); |
| 2134 | const _Tp __pi_2 = (_Tp) 1.5707963267948966192313216916397514L; |
| 2135 | return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag()); |
| 2136 | } |
| 2137 | |
| 2138 | #if _GLIBCXX_USE_C99_COMPLEX_ARC |
| 2139 | #if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) |
| 2140 | inline __complex__ _Float16 |
| 2141 | __complex_acos(__complex__ _Float16 __z) |
| 2142 | { return static_cast<__complex__ _Float16>(__builtin_cacosf(__z)); } |
| 2143 | |
| 2144 | inline __complex__ _Float16 |
| 2145 | __complex_asin(__complex__ _Float16 __z) |
| 2146 | { return static_cast<__complex__ _Float16>(__builtin_casinf(__z)); } |
| 2147 | |
| 2148 | inline __complex__ _Float16 |
| 2149 | __complex_atan(__complex__ _Float16 __z) |
| 2150 | { return static_cast<__complex__ _Float16>(__builtin_catanf(__z)); } |
| 2151 | |
| 2152 | inline __complex__ _Float16 |
| 2153 | __complex_acosh(__complex__ _Float16 __z) |
| 2154 | { return static_cast<__complex__ _Float16>(__builtin_cacoshf(__z)); } |
| 2155 | |
| 2156 | inline __complex__ _Float16 |
| 2157 | __complex_asinh(__complex__ _Float16 __z) |
| 2158 | { return static_cast<__complex__ _Float16>(__builtin_casinhf(__z)); } |
| 2159 | |
| 2160 | inline __complex__ _Float16 |
| 2161 | __complex_atanh(__complex__ _Float16 __z) |
| 2162 | { return static_cast<__complex__ _Float16>(__builtin_catanhf(__z)); } |
| 2163 | #endif |
| 2164 | |
| 2165 | #if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) |
| 2166 | inline __complex__ _Float32 |
| 2167 | __complex_acos(__complex__ _Float32 __z) |
| 2168 | { return __builtin_cacosf(__z); } |
| 2169 | |
| 2170 | inline __complex__ _Float32 |
| 2171 | __complex_asin(__complex__ _Float32 __z) |
| 2172 | { return __builtin_casinf(__z); } |
| 2173 | |
| 2174 | inline __complex__ _Float32 |
| 2175 | __complex_atan(__complex__ _Float32 __z) |
| 2176 | { return __builtin_catanf(__z); } |
| 2177 | |
| 2178 | inline __complex__ _Float32 |
| 2179 | __complex_acosh(__complex__ _Float32 __z) |
| 2180 | { return __builtin_cacoshf(__z); } |
| 2181 | |
| 2182 | inline __complex__ _Float32 |
| 2183 | __complex_asinh(__complex__ _Float32 __z) |
| 2184 | { return __builtin_casinhf(__z); } |
| 2185 | |
| 2186 | inline __complex__ _Float32 |
| 2187 | __complex_atanh(__complex__ _Float32 __z) |
| 2188 | { return __builtin_catanhf(__z); } |
| 2189 | #endif |
| 2190 | |
| 2191 | #if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64) |
| 2192 | inline __complex__ _Float64 |
| 2193 | __complex_acos(__complex__ _Float64 __z) |
| 2194 | { return __builtin_cacos(__z); } |
| 2195 | |
| 2196 | inline __complex__ _Float64 |
| 2197 | __complex_asin(__complex__ _Float64 __z) |
| 2198 | { return __builtin_casin(__z); } |
| 2199 | |
| 2200 | inline __complex__ _Float64 |
| 2201 | __complex_atan(__complex__ _Float64 __z) |
| 2202 | { return __builtin_catan(__z); } |
| 2203 | |
| 2204 | inline __complex__ _Float64 |
| 2205 | __complex_acosh(__complex__ _Float64 __z) |
| 2206 | { return __builtin_cacosh(__z); } |
| 2207 | |
| 2208 | inline __complex__ _Float64 |
| 2209 | __complex_asinh(__complex__ _Float64 __z) |
| 2210 | { return __builtin_casinh(__z); } |
| 2211 | |
| 2212 | inline __complex__ _Float64 |
| 2213 | __complex_atanh(__complex__ _Float64 __z) |
| 2214 | { return __builtin_catanh(__z); } |
| 2215 | #endif |
| 2216 | |
| 2217 | #if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128) |
| 2218 | inline __complex__ _Float128 |
| 2219 | __complex_acos(__complex__ _Float128 __z) |
| 2220 | { return __builtin_cacosl(__z); } |
| 2221 | |
| 2222 | inline __complex__ _Float128 |
| 2223 | __complex_asin(__complex__ _Float128 __z) |
| 2224 | { return __builtin_casinl(__z); } |
| 2225 | |
| 2226 | inline __complex__ _Float128 |
| 2227 | __complex_atan(__complex__ _Float128 __z) |
| 2228 | { return __builtin_catanl(__z); } |
| 2229 | |
| 2230 | inline __complex__ _Float128 |
| 2231 | __complex_acosh(__complex__ _Float128 __z) |
| 2232 | { return __builtin_cacoshl(__z); } |
| 2233 | |
| 2234 | inline __complex__ _Float128 |
| 2235 | __complex_asinh(__complex__ _Float128 __z) |
| 2236 | { return __builtin_casinhl(__z); } |
| 2237 | |
| 2238 | inline __complex__ _Float128 |
| 2239 | __complex_atanh(__complex__ _Float128 __z) |
| 2240 | { return __builtin_catanhl(__z); } |
| 2241 | #elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH) |
| 2242 | inline __complex__ _Float128 |
| 2243 | __complex_acos(__complex__ _Float128 __z) |
| 2244 | { return __builtin_cacosf128(__z); } |
| 2245 | |
| 2246 | inline __complex__ _Float128 |
| 2247 | __complex_asin(__complex__ _Float128 __z) |
| 2248 | { return __builtin_casinf128(__z); } |
| 2249 | |
| 2250 | inline __complex__ _Float128 |
| 2251 | __complex_atan(__complex__ _Float128 __z) |
| 2252 | { return __builtin_catanf128(__z); } |
| 2253 | |
| 2254 | inline __complex__ _Float128 |
| 2255 | __complex_acosh(__complex__ _Float128 __z) |
| 2256 | { return __builtin_cacoshf128(__z); } |
| 2257 | |
| 2258 | inline __complex__ _Float128 |
| 2259 | __complex_asinh(__complex__ _Float128 __z) |
| 2260 | { return __builtin_casinhf128(__z); } |
| 2261 | |
| 2262 | inline __complex__ _Float128 |
| 2263 | __complex_atanh(__complex__ _Float128 __z) |
| 2264 | { return __builtin_catanhf128(__z); } |
| 2265 | #endif |
| 2266 | |
| 2267 | #if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) |
| 2268 | inline __complex__ decltype(0.0bf16) |
| 2269 | __complex_acos(__complex__ decltype(0.0bf16) __z) |
| 2270 | { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cacosf(__z)); } |
| 2271 | |
| 2272 | inline __complex__ decltype(0.0bf16) |
| 2273 | __complex_asin(__complex__ decltype(0.0bf16) __z) |
| 2274 | { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_casinf(__z)); } |
| 2275 | |
| 2276 | inline __complex__ decltype(0.0bf16) |
| 2277 | __complex_atan(__complex__ decltype(0.0bf16) __z) |
| 2278 | { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_catanf(__z)); } |
| 2279 | |
| 2280 | inline __complex__ decltype(0.0bf16) |
| 2281 | __complex_acosh(__complex__ decltype(0.0bf16) __z) |
| 2282 | { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cacoshf(__z)); } |
| 2283 | |
| 2284 | inline __complex__ decltype(0.0bf16) |
| 2285 | __complex_asinh(__complex__ decltype(0.0bf16) __z) |
| 2286 | { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_casinhf(__z)); } |
| 2287 | |
| 2288 | inline __complex__ decltype(0.0bf16) |
| 2289 | __complex_atanh(__complex__ decltype(0.0bf16) __z) |
| 2290 | { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_catanhf(__z)); } |
| 2291 | #endif |
| 2292 | #endif |
| 2293 | |
| 2294 | #if _GLIBCXX_USE_C99_COMPLEX_ARC |
| 2295 | inline __complex__ float |
| 2296 | __complex_acos(__complex__ float __z) |
| 2297 | { return __builtin_cacosf(__z); } |
| 2298 | |
| 2299 | inline __complex__ double |
| 2300 | __complex_acos(__complex__ double __z) |
| 2301 | { return __builtin_cacos(__z); } |
| 2302 | |
| 2303 | inline __complex__ long double |
| 2304 | __complex_acos(const __complex__ long double& __z) |
| 2305 | { return __builtin_cacosl(__z); } |
| 2306 | |
| 2307 | template<typename _Tp> |
| 2308 | inline std::complex<_Tp> |
| 2309 | acos(const std::complex<_Tp>& __z) |
| 2310 | { return __complex_acos(__z.__rep()); } |
| 2311 | #else |
| 2312 | /// acos(__z) [8.1.2]. |
| 2313 | // Effects: Behaves the same as C99 function cacos, defined |
| 2314 | // in subclause 7.3.5.1. |
| 2315 | template<typename _Tp> |
| 2316 | inline std::complex<_Tp> |
| 2317 | acos(const std::complex<_Tp>& __z) |
| 2318 | { return __complex_acos(__z); } |
| 2319 | #endif |
| 2320 | |
| 2321 | template<typename _Tp> |
| 2322 | inline std::complex<_Tp> |
| 2323 | __complex_asin(const std::complex<_Tp>& __z) |
| 2324 | { |
| 2325 | std::complex<_Tp> __t(-__z.imag(), __z.real()); |
| 2326 | __t = std::asinh(__t); |
| 2327 | return std::complex<_Tp>(__t.imag(), -__t.real()); |
| 2328 | } |
| 2329 | |
| 2330 | #if _GLIBCXX_USE_C99_COMPLEX_ARC |
| 2331 | inline __complex__ float |
| 2332 | __complex_asin(__complex__ float __z) |
| 2333 | { return __builtin_casinf(__z); } |
| 2334 | |
| 2335 | inline __complex__ double |
| 2336 | __complex_asin(__complex__ double __z) |
| 2337 | { return __builtin_casin(__z); } |
| 2338 | |
| 2339 | inline __complex__ long double |
| 2340 | __complex_asin(const __complex__ long double& __z) |
| 2341 | { return __builtin_casinl(__z); } |
| 2342 | |
| 2343 | template<typename _Tp> |
| 2344 | inline std::complex<_Tp> |
| 2345 | asin(const std::complex<_Tp>& __z) |
| 2346 | { return __complex_asin(__z.__rep()); } |
| 2347 | #else |
| 2348 | /// asin(__z) [8.1.3]. |
| 2349 | // Effects: Behaves the same as C99 function casin, defined |
| 2350 | // in subclause 7.3.5.2. |
| 2351 | template<typename _Tp> |
| 2352 | inline std::complex<_Tp> |
| 2353 | asin(const std::complex<_Tp>& __z) |
| 2354 | { return __complex_asin(__z); } |
| 2355 | #endif |
| 2356 | |
| 2357 | template<typename _Tp> |
| 2358 | std::complex<_Tp> |
| 2359 | __complex_atan(const std::complex<_Tp>& __z) |
| 2360 | { |
| 2361 | const _Tp __r2 = __z.real() * __z.real(); |
| 2362 | const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag(); |
| 2363 | |
| 2364 | _Tp __num = __z.imag() + _Tp(1.0); |
| 2365 | _Tp __den = __z.imag() - _Tp(1.0); |
| 2366 | |
| 2367 | __num = __r2 + __num * __num; |
| 2368 | __den = __r2 + __den * __den; |
| 2369 | |
| 2370 | return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x), |
| 2371 | _Tp(0.25) * log(__num / __den)); |
| 2372 | } |
| 2373 | |
| 2374 | #if _GLIBCXX_USE_C99_COMPLEX_ARC |
| 2375 | inline __complex__ float |
| 2376 | __complex_atan(__complex__ float __z) |
| 2377 | { return __builtin_catanf(__z); } |
| 2378 | |
| 2379 | inline __complex__ double |
| 2380 | __complex_atan(__complex__ double __z) |
| 2381 | { return __builtin_catan(__z); } |
| 2382 | |
| 2383 | inline __complex__ long double |
| 2384 | __complex_atan(const __complex__ long double& __z) |
| 2385 | { return __builtin_catanl(__z); } |
| 2386 | |
| 2387 | template<typename _Tp> |
| 2388 | inline std::complex<_Tp> |
| 2389 | atan(const std::complex<_Tp>& __z) |
| 2390 | { return __complex_atan(__z.__rep()); } |
| 2391 | #else |
| 2392 | /// atan(__z) [8.1.4]. |
| 2393 | // Effects: Behaves the same as C99 function catan, defined |
| 2394 | // in subclause 7.3.5.3. |
| 2395 | template<typename _Tp> |
| 2396 | inline std::complex<_Tp> |
| 2397 | atan(const std::complex<_Tp>& __z) |
| 2398 | { return __complex_atan(__z); } |
| 2399 | #endif |
| 2400 | |
| 2401 | template<typename _Tp> |
| 2402 | std::complex<_Tp> |
| 2403 | __complex_acosh(const std::complex<_Tp>& __z) |
| 2404 | { |
| 2405 | // Kahan's formula. |
| 2406 | return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0))) |
| 2407 | + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0)))); |
| 2408 | } |
| 2409 | |
| 2410 | #if _GLIBCXX_USE_C99_COMPLEX_ARC |
| 2411 | inline __complex__ float |
| 2412 | __complex_acosh(__complex__ float __z) |
| 2413 | { return __builtin_cacoshf(__z); } |
| 2414 | |
| 2415 | inline __complex__ double |
| 2416 | __complex_acosh(__complex__ double __z) |
| 2417 | { return __builtin_cacosh(__z); } |
| 2418 | |
| 2419 | inline __complex__ long double |
| 2420 | __complex_acosh(const __complex__ long double& __z) |
| 2421 | { return __builtin_cacoshl(__z); } |
| 2422 | |
| 2423 | template<typename _Tp> |
| 2424 | inline std::complex<_Tp> |
| 2425 | acosh(const std::complex<_Tp>& __z) |
| 2426 | { return __complex_acosh(__z.__rep()); } |
| 2427 | #else |
| 2428 | /// acosh(__z) [8.1.5]. |
| 2429 | // Effects: Behaves the same as C99 function cacosh, defined |
| 2430 | // in subclause 7.3.6.1. |
| 2431 | template<typename _Tp> |
| 2432 | inline std::complex<_Tp> |
| 2433 | acosh(const std::complex<_Tp>& __z) |
| 2434 | { return __complex_acosh(__z); } |
| 2435 | #endif |
| 2436 | |
| 2437 | template<typename _Tp> |
| 2438 | std::complex<_Tp> |
| 2439 | __complex_asinh(const std::complex<_Tp>& __z) |
| 2440 | { |
| 2441 | std::complex<_Tp> __t((__z.real() - __z.imag()) |
| 2442 | * (__z.real() + __z.imag()) + _Tp(1.0), |
| 2443 | _Tp(2.0) * __z.real() * __z.imag()); |
| 2444 | __t = std::sqrt(__t); |
| 2445 | |
| 2446 | return std::log(__t + __z); |
| 2447 | } |
| 2448 | |
| 2449 | #if _GLIBCXX_USE_C99_COMPLEX_ARC |
| 2450 | inline __complex__ float |
| 2451 | __complex_asinh(__complex__ float __z) |
| 2452 | { return __builtin_casinhf(__z); } |
| 2453 | |
| 2454 | inline __complex__ double |
| 2455 | __complex_asinh(__complex__ double __z) |
| 2456 | { return __builtin_casinh(__z); } |
| 2457 | |
| 2458 | inline __complex__ long double |
| 2459 | __complex_asinh(const __complex__ long double& __z) |
| 2460 | { return __builtin_casinhl(__z); } |
| 2461 | |
| 2462 | template<typename _Tp> |
| 2463 | inline std::complex<_Tp> |
| 2464 | asinh(const std::complex<_Tp>& __z) |
| 2465 | { return __complex_asinh(__z.__rep()); } |
| 2466 | #else |
| 2467 | /// asinh(__z) [8.1.6]. |
| 2468 | // Effects: Behaves the same as C99 function casin, defined |
| 2469 | // in subclause 7.3.6.2. |
| 2470 | template<typename _Tp> |
| 2471 | inline std::complex<_Tp> |
| 2472 | asinh(const std::complex<_Tp>& __z) |
| 2473 | { return __complex_asinh(__z); } |
| 2474 | #endif |
| 2475 | |
| 2476 | template<typename _Tp> |
| 2477 | std::complex<_Tp> |
| 2478 | __complex_atanh(const std::complex<_Tp>& __z) |
| 2479 | { |
| 2480 | const _Tp __i2 = __z.imag() * __z.imag(); |
| 2481 | const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real(); |
| 2482 | |
| 2483 | _Tp __num = _Tp(1.0) + __z.real(); |
| 2484 | _Tp __den = _Tp(1.0) - __z.real(); |
| 2485 | |
| 2486 | __num = __i2 + __num * __num; |
| 2487 | __den = __i2 + __den * __den; |
| 2488 | |
| 2489 | return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)), |
| 2490 | _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x)); |
| 2491 | } |
| 2492 | |
| 2493 | #if _GLIBCXX_USE_C99_COMPLEX_ARC |
| 2494 | inline __complex__ float |
| 2495 | __complex_atanh(__complex__ float __z) |
| 2496 | { return __builtin_catanhf(__z); } |
| 2497 | |
| 2498 | inline __complex__ double |
| 2499 | __complex_atanh(__complex__ double __z) |
| 2500 | { return __builtin_catanh(__z); } |
| 2501 | |
| 2502 | inline __complex__ long double |
| 2503 | __complex_atanh(const __complex__ long double& __z) |
| 2504 | { return __builtin_catanhl(__z); } |
| 2505 | |
| 2506 | template<typename _Tp> |
| 2507 | inline std::complex<_Tp> |
| 2508 | atanh(const std::complex<_Tp>& __z) |
| 2509 | { return __complex_atanh(__z.__rep()); } |
| 2510 | #else |
| 2511 | /// atanh(__z) [8.1.7]. |
| 2512 | // Effects: Behaves the same as C99 function catanh, defined |
| 2513 | // in subclause 7.3.6.3. |
| 2514 | template<typename _Tp> |
| 2515 | inline std::complex<_Tp> |
| 2516 | atanh(const std::complex<_Tp>& __z) |
| 2517 | { return __complex_atanh(__z); } |
| 2518 | #endif |
| 2519 | |
| 2520 | template<typename _Tp> |
| 2521 | inline _Tp |
| 2522 | /// fabs(__z) [8.1.8]. |
| 2523 | // Effects: Behaves the same as C99 function cabs, defined |
| 2524 | // in subclause 7.3.8.1. |
| 2525 | fabs(const std::complex<_Tp>& __z) |
| 2526 | { return std::abs(__z); } |
| 2527 | |
| 2528 | /// Additional overloads [8.1.9]. |
| 2529 | template<typename _Tp> |
| 2530 | inline typename __gnu_cxx::__promote<_Tp>::__type |
| 2531 | arg(_Tp __x) |
| 2532 | { |
| 2533 | typedef typename __gnu_cxx::__promote<_Tp>::__type __type; |
| 2534 | #if (_GLIBCXX11_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC) |
| 2535 | return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L) |
| 2536 | : __type(); |
| 2537 | #else |
| 2538 | return std::arg(std::complex<__type>(__x)); |
| 2539 | #endif |
| 2540 | } |
| 2541 | |
| 2542 | template<typename _Tp> |
| 2543 | _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type |
| 2544 | imag(_Tp) |
| 2545 | { return _Tp(); } |
| 2546 | |
| 2547 | template<typename _Tp> |
| 2548 | _GLIBCXX20_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type |
| 2549 | norm(_Tp __x) |
| 2550 | { |
| 2551 | typedef typename __gnu_cxx::__promote<_Tp>::__type __type; |
| 2552 | return __type(__x) * __type(__x); |
| 2553 | } |
| 2554 | |
| 2555 | template<typename _Tp> |
| 2556 | _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type |
| 2557 | real(_Tp __x) |
| 2558 | { return __x; } |
| 2559 | |
| 2560 | template<typename _Tp, typename _Up> |
| 2561 | inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> |
| 2562 | pow(const std::complex<_Tp>& __x, const _Up& __y) |
| 2563 | { |
| 2564 | typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; |
| 2565 | return std::pow(std::complex<__type>(__x), __type(__y)); |
| 2566 | } |
| 2567 | |
| 2568 | template<typename _Tp, typename _Up> |
| 2569 | inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> |
| 2570 | pow(const _Tp& __x, const std::complex<_Up>& __y) |
| 2571 | { |
| 2572 | typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; |
| 2573 | return std::pow(__type(__x), std::complex<__type>(__y)); |
| 2574 | } |
| 2575 | |
| 2576 | template<typename _Tp, typename _Up> |
| 2577 | inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> |
| 2578 | pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y) |
| 2579 | { |
| 2580 | typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; |
| 2581 | return std::pow(std::complex<__type>(__x), |
| 2582 | std::complex<__type>(__y)); |
| 2583 | } |
| 2584 | |
| 2585 | // Forward declarations. |
| 2586 | // DR 781. |
| 2587 | template<typename _Tp> |
| 2588 | std::complex<_Tp> proj(const std::complex<_Tp>&); |
| 2589 | |
| 2590 | // Generic implementation of std::proj, does not work for infinities. |
| 2591 | template<typename _Tp> |
| 2592 | inline std::complex<_Tp> |
| 2593 | __complex_proj(const std::complex<_Tp>& __z) |
| 2594 | { return __z; } |
| 2595 | |
| 2596 | #if _GLIBCXX_USE_C99_COMPLEX |
| 2597 | inline complex<float> |
| 2598 | __complex_proj(const complex<float>& __z) |
| 2599 | { return __builtin_cprojf(__z.__rep()); } |
| 2600 | |
| 2601 | inline complex<double> |
| 2602 | __complex_proj(const complex<double>& __z) |
| 2603 | { return __builtin_cproj(__z.__rep()); } |
| 2604 | |
| 2605 | inline complex<long double> |
| 2606 | __complex_proj(const complex<long double>& __z) |
| 2607 | { return __builtin_cprojl(__z.__rep()); } |
| 2608 | |
| 2609 | #if __cplusplus > 202002L |
| 2610 | #if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) |
| 2611 | inline __complex__ _Float16 |
| 2612 | __complex_proj(__complex__ _Float16 __z) |
| 2613 | { return static_cast<__complex__ _Float16>(__builtin_cprojf(__z)); } |
| 2614 | #endif |
| 2615 | |
| 2616 | #if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) |
| 2617 | inline __complex__ _Float32 |
| 2618 | __complex_proj(__complex__ _Float32 __z) |
| 2619 | { return __builtin_cprojf(__z); } |
| 2620 | #endif |
| 2621 | |
| 2622 | #if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64) |
| 2623 | inline __complex__ _Float64 |
| 2624 | __complex_proj(__complex__ _Float64 __z) |
| 2625 | { return __builtin_cproj(__z); } |
| 2626 | #endif |
| 2627 | |
| 2628 | #if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128) |
| 2629 | inline __complex__ _Float128 |
| 2630 | __complex_proj(__complex__ _Float128 __z) |
| 2631 | { return __builtin_cprojl(__z); } |
| 2632 | #elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH) |
| 2633 | inline __complex__ _Float128 |
| 2634 | __complex_proj(__complex__ _Float128 __z) |
| 2635 | { return __builtin_cprojf128(__z); } |
| 2636 | #endif |
| 2637 | |
| 2638 | #if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) |
| 2639 | inline __complex__ decltype(0.0bf16) |
| 2640 | __complex_proj(__complex__ decltype(0.0bf16) __z) |
| 2641 | { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cprojf(__z)); } |
| 2642 | #endif |
| 2643 | |
| 2644 | template<typename _Tp> |
| 2645 | requires requires { typename __complex_type<_Tp>::type; } |
| 2646 | inline complex<_Tp> |
| 2647 | __complex_proj(const complex<_Tp>& __z) |
| 2648 | { return __complex_proj(__z.__rep()); } |
| 2649 | #endif |
| 2650 | |
| 2651 | #elif defined _GLIBCXX_USE_C99_MATH_FUNCS |
| 2652 | inline complex<float> |
| 2653 | __complex_proj(const complex<float>& __z) |
| 2654 | { |
| 2655 | if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag())) |
| 2656 | return complex<float>(__builtin_inff(), |
| 2657 | __builtin_copysignf(0.0f, __z.imag())); |
| 2658 | return __z; |
| 2659 | } |
| 2660 | |
| 2661 | inline complex<double> |
| 2662 | __complex_proj(const complex<double>& __z) |
| 2663 | { |
| 2664 | if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag())) |
| 2665 | return complex<double>(__builtin_inf(), |
| 2666 | __builtin_copysign(0.0, __z.imag())); |
| 2667 | return __z; |
| 2668 | } |
| 2669 | |
| 2670 | inline complex<long double> |
| 2671 | __complex_proj(const complex<long double>& __z) |
| 2672 | { |
| 2673 | if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag())) |
| 2674 | return complex<long double>(__builtin_infl(), |
| 2675 | __builtin_copysignl(0.0l, __z.imag())); |
| 2676 | return __z; |
| 2677 | } |
| 2678 | #endif |
| 2679 | |
| 2680 | template<typename _Tp> |
| 2681 | inline std::complex<_Tp> |
| 2682 | proj(const std::complex<_Tp>& __z) |
| 2683 | { return __complex_proj(__z); } |
| 2684 | |
| 2685 | // Overload for scalars |
| 2686 | template<typename _Tp> |
| 2687 | inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type> |
| 2688 | proj(_Tp __x) |
| 2689 | { |
| 2690 | typedef typename __gnu_cxx::__promote<_Tp>::__type __type; |
| 2691 | return std::proj(std::complex<__type>(__x)); |
| 2692 | } |
| 2693 | |
| 2694 | template<typename _Tp> |
| 2695 | inline _GLIBCXX20_CONSTEXPR |
| 2696 | std::complex<typename __gnu_cxx::__promote<_Tp>::__type> |
| 2697 | conj(_Tp __x) |
| 2698 | { |
| 2699 | typedef typename __gnu_cxx::__promote<_Tp>::__type __type; |
| 2700 | return std::complex<__type>(__x, -__type()); |
| 2701 | } |
| 2702 | |
| 2703 | #ifdef __cpp_lib_complex_udls // C++ >= 14 |
| 2704 | |
| 2705 | inline namespace literals { |
| 2706 | inline namespace complex_literals { |
| 2707 | #pragma GCC diagnostic push |
| 2708 | #pragma GCC diagnostic ignored "-Wliteral-suffix" |
| 2709 | |
| 2710 | constexpr std::complex<float> |
| 2711 | operator""if (long double __num) |
| 2712 | { return std::complex<float>{0.0F, static_cast<float>(__num)}; } |
| 2713 | |
| 2714 | constexpr std::complex<float> |
| 2715 | operator""if (unsigned long long __num) |
| 2716 | { return std::complex<float>{0.0F, static_cast<float>(__num)}; } |
| 2717 | |
| 2718 | constexpr std::complex<double> |
| 2719 | operator""i (long double __num) |
| 2720 | { return std::complex<double>{0.0, static_cast<double>(__num)}; } |
| 2721 | |
| 2722 | constexpr std::complex<double> |
| 2723 | operator""i (unsigned long long __num) |
| 2724 | { return std::complex<double>{0.0, static_cast<double>(__num)}; } |
| 2725 | |
| 2726 | constexpr std::complex<long double> |
| 2727 | operator""il (long double __num) |
| 2728 | { return std::complex<long double>{0.0L, __num}; } |
| 2729 | |
| 2730 | constexpr std::complex<long double> |
| 2731 | operator""il (unsigned long long __num) |
| 2732 | { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; } |
| 2733 | |
| 2734 | #pragma GCC diagnostic pop |
| 2735 | } // inline namespace complex_literals |
| 2736 | } // inline namespace literals |
| 2737 | |
| 2738 | #endif // __cpp_lib_complex_udls |
| 2739 | |
| 2740 | _GLIBCXX_END_NAMESPACE_VERSION |
| 2741 | } // namespace |
| 2742 | |
| 2743 | #endif // C++11 |
| 2744 | |
| 2745 | #ifdef _GLIBCXX_CLANG |
| 2746 | #pragma clang diagnostic pop |
| 2747 | #endif |
| 2748 | |
| 2749 | #pragma GCC diagnostic pop |
| 2750 | #endif /* _GLIBCXX_COMPLEX */ |
| 2751 | |