Skip to content

Commit 83555cd

Browse files
authored
[SYCL] Adds complex Literals support to sycl::experimental::complex (#9819)
Adds support for complex literals to `sycl::ext::oneapi::experimental::complex` Fixes #8363
1 parent f9d285e commit 83555cd

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

sycl/doc/extensions/proposed/sycl_ext_oneapi_complex.asciidoc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,33 @@ mathematical operation.
438438
|Compute the hyperbolic tangent of complex number x.
439439
|===
440440

441+
442+
=== Suffixes for complex number literals
443+
444+
This proposal describes literal suffixes for constructing complex number literals.
445+
The suffixes `i` and `if` create complex numbers of the types `complex<double>` and `complex<float>` respectively, with their imaginary part denoted by the given literal number and the real part being zero.
446+
447+
```C++
448+
namespace sycl {
449+
namespace ext {
450+
namespace oneapi {
451+
452+
namespace literals {
453+
namespace complex_literals {
454+
constexpr complex<double> operator""i(long double x);
455+
constexpr complex<double> operator""i(unsigned long long x);
456+
457+
constexpr complex<float> operator""if(long double x);
458+
constexpr complex<float> operator""if(unsigned long long x);
459+
} // namespace complex_literals
460+
} // namespace literals
461+
462+
} // namespace oneapi
463+
} // namespace ext
464+
} // namespace sycl
465+
```
466+
467+
441468
== Implementation notes
442469

443470
The complex mathematical operations can all be defined using SYCL built-ins.

sycl/include/sycl/ext/oneapi/experimental/sycl_complex.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,27 @@ tan(const complex<_Tp> &__x) {
979979
return complex<_Tp>(__z.imag(), -__z.real());
980980
}
981981

982+
// Literal suffix for complex number literals [complex.literals]
983+
inline namespace literals {
984+
inline namespace complex_literals {
985+
constexpr complex<double> operator""i(long double __im) {
986+
return {0.0, static_cast<double>(__im)};
987+
}
988+
989+
constexpr complex<double> operator""i(unsigned long long __im) {
990+
return {0.0, static_cast<double>(__im)};
991+
}
992+
993+
constexpr complex<float> operator""if(long double __im) {
994+
return {0.0f, static_cast<float>(__im)};
995+
}
996+
997+
constexpr complex<float> operator""if(unsigned long long __im) {
998+
return {0.0f, static_cast<float>(__im)};
999+
}
1000+
} // namespace complex_literals
1001+
} // namespace literals
1002+
9821003
} // namespace experimental
9831004
} // namespace oneapi
9841005
} // namespace ext

sycl/test/extensions/test_complex.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,16 @@ void check_sycl_constructor_from_std() {
186186
}
187187
}
188188

189+
// Check types for sycl complex constructed from literals
190+
void check_sycl_complex_literals() {
191+
static_assert(
192+
std::is_same_v<decltype(0.3if),
193+
sycl::ext::oneapi::experimental::complex<float>>);
194+
static_assert(
195+
std::is_same_v<decltype(0.3i),
196+
sycl::ext::oneapi::experimental::complex<double>>);
197+
}
198+
189199
int main() {
190200
check_math_function_types();
191201
check_math_operator_types();
@@ -195,5 +205,7 @@ int main() {
195205
check_std_to_sycl_conversion();
196206
check_sycl_constructor_from_std();
197207

208+
check_sycl_complex_literals();
209+
198210
return 0;
199211
}

0 commit comments

Comments
 (0)