[flang] Accept polymorphic component element in storage_size
[llvm-project.git] / flang / runtime / complex-powi.cpp
blob18723bb93cbf17228e5e667fb7faef176fff936f
1 /*===-- flang/runtime/complex-powi.cpp ----------------------------*- C++ -*-===
3 * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 * See https://llvm.org/LICENSE.txt for license information.
5 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 * ===-----------------------------------------------------------------------===
8 */
9 #include "flang/Runtime/entry-names.h"
10 #include <cstdint>
11 #include <cstdio>
12 #include <limits>
14 #ifdef __clang_major__
15 #pragma clang diagnostic ignored "-Wc99-extensions"
16 #endif
18 template <typename C, typename I> C tgpowi(C base, I exp) {
19 if (exp == 0) {
20 return C{1};
23 bool invertResult{exp < 0};
24 bool isMin{exp == std::numeric_limits<I>::min()};
26 if (isMin) {
27 exp = std::numeric_limits<I>::max();
30 if (exp < 0) {
31 exp = exp * -1;
34 C origBase{base};
36 while ((exp & 1) == 0) {
37 base *= base;
38 exp >>= 1;
41 C acc{base};
43 while (exp > 1) {
44 exp >>= 1;
45 base *= base;
46 if ((exp & 1) == 1) {
47 acc *= base;
51 if (isMin) {
52 acc *= origBase;
55 if (invertResult) {
56 acc = C{1} / acc;
59 return acc;
62 #ifndef _MSC_VER
63 // With most compilers, C complex is implemented as a builtin type that may have
64 // specific ABI requirements
65 extern "C" float _Complex RTNAME(cpowi)(float _Complex base, std::int32_t exp) {
66 return tgpowi(base, exp);
69 extern "C" double _Complex RTNAME(zpowi)(
70 double _Complex base, std::int32_t exp) {
71 return tgpowi(base, exp);
74 extern "C" float _Complex RTNAME(cpowk)(float _Complex base, std::int64_t exp) {
75 return tgpowi(base, exp);
78 extern "C" double _Complex RTNAME(zpowk)(
79 double _Complex base, std::int64_t exp) {
80 return tgpowi(base, exp);
82 #else
83 // on MSVC, C complex is always just a struct of two members as it is not
84 // supported as a builtin type. So we use C++ complex here as that has the
85 // same ABI and layout. See:
86 // https://learn.microsoft.com/en-us/cpp/c-runtime-library/complex-math-support
87 #include <complex>
89 // MSVC doesn't allow including <ccomplex> or <complex.h> in C++17 mode to get
90 // the Windows definitions of these structs so just redefine here.
91 struct Fcomplex {
92 float re;
93 float im;
96 struct Dcomplex {
97 double re;
98 double im;
101 extern "C" Fcomplex RTNAME(cpowi)(Fcomplex base, std::int32_t exp) {
102 auto cppbase = *(std::complex<float> *)(&base);
103 auto cppres = tgpowi(cppbase, exp);
104 return *(Fcomplex *)(&cppres);
107 extern "C" Dcomplex RTNAME(zpowi)(Dcomplex base, std::int32_t exp) {
108 auto cppbase = *(std::complex<double> *)(&base);
109 auto cppres = tgpowi(cppbase, exp);
110 return *(Dcomplex *)(&cppres);
113 extern "C" Fcomplex RTNAME(cpowk)(Fcomplex base, std::int64_t exp) {
114 auto cppbase = *(std::complex<float> *)(&base);
115 auto cppres = tgpowi(cppbase, exp);
116 return *(Fcomplex *)(&cppres);
119 extern "C" Dcomplex RTNAME(zpowk)(Dcomplex base, std::int32_t exp) {
120 auto cppbase = *(std::complex<double> *)(&base);
121 auto cppres = tgpowi(cppbase, exp);
122 return *(Dcomplex *)(&cppres);
125 #endif