xref: /llvm-project/offload/test/offloading/std_complex_arithmetic.cpp (revision 330d8983d25d08580fc1642fea48b2473f47a9da)
1 // RUN: %libomptarget-compilexx-generic && %libomptarget-run-generic
2 // RUN: %libomptarget-compilexx-generic -O3 && %libomptarget-run-generic
3 // RUN: %libomptarget-compilexx-generic -O3 -ffast-math && \
4 // RUN:   %libomptarget-run-generic
5 
6 // FIXME: This fails to link due to missing math symbols. We should provide the
7 //        needed math functions in the GPU `libm` and require the GPU C library.
8 // UNSUPPORTED: amdgcn-amd-amdhsa
9 // UNSUPPORTED: nvptx64-nvidia-cuda-LTO
10 
11 #include <cassert>
12 #include <complex>
13 #include <iostream>
14 
test_map()15 template <typename T> void test_map() {
16   std::complex<T> a(0.2, 1), a_check;
17 #pragma omp target map(from : a_check)
18   { a_check = a; }
19 
20   assert(std::abs(a - a_check) < 1e-6);
21 }
22 
test_plus(AT a,BT b)23 template <typename RT, typename AT, typename BT> void test_plus(AT a, BT b) {
24   std::complex<RT> c, c_host;
25 
26   c_host = a + b;
27 #pragma omp target map(from : c)
28   { c = a + b; }
29 
30   assert(std::abs(c - c_host) < 1e-6);
31 }
32 
test_minus(AT a,BT b)33 template <typename RT, typename AT, typename BT> void test_minus(AT a, BT b) {
34   std::complex<RT> c, c_host;
35 
36   c_host = a - b;
37 #pragma omp target map(from : c)
38   { c = a - b; }
39 
40   assert(std::abs(c - c_host) < 1e-6);
41 }
42 
test_mul(AT a,BT b)43 template <typename RT, typename AT, typename BT> void test_mul(AT a, BT b) {
44   std::complex<RT> c, c_host;
45 
46   c_host = a * b;
47 #pragma omp target map(from : c)
48   { c = a * b; }
49 
50   assert(std::abs(c - c_host) < 1e-6);
51 }
52 
test_div(AT a,BT b)53 template <typename RT, typename AT, typename BT> void test_div(AT a, BT b) {
54   std::complex<RT> c, c_host;
55 
56   c_host = a / b;
57 #pragma omp target map(from : c)
58   { c = a / b; }
59 
60   assert(std::abs(c - c_host) < 1e-6);
61 }
62 
test_complex()63 template <typename T> void test_complex() {
64   test_map<T>();
65 
66   test_plus<T>(std::complex<T>(0, 1), std::complex<T>(0.5, 0.3));
67   test_plus<T>(std::complex<T>(0, 1), T(0.5));
68   test_plus<T>(T(0.5), std::complex<T>(0, 1));
69 
70   test_minus<T>(std::complex<T>(0, 1), std::complex<T>(0.5, 0.3));
71   test_minus<T>(std::complex<T>(0, 1), T(0.5));
72   test_minus<T>(T(0.5), std::complex<T>(0, 1));
73 
74   test_mul<T>(std::complex<T>(0, 1), std::complex<T>(0.5, 0.3));
75   test_mul<T>(std::complex<T>(0, 1), T(0.5));
76   test_mul<T>(T(0.5), std::complex<T>(0, 1));
77 
78   test_div<T>(std::complex<T>(0, 1), std::complex<T>(0.5, 0.3));
79   test_div<T>(std::complex<T>(0, 1), T(0.5));
80   test_div<T>(T(0.5), std::complex<T>(0, 1));
81 }
82 
main()83 int main() {
84   std::cout << "Testing float" << std::endl;
85   test_complex<float>();
86   std::cout << "Testing double" << std::endl;
87   test_complex<double>();
88   return 0;
89 }
90