File: std_complex_arithmetic.cpp

package info (click to toggle)
llvm-toolchain-16 1%3A16.0.6-15~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,634,792 kB
  • sloc: cpp: 6,179,261; ansic: 1,216,205; asm: 741,319; python: 196,614; objc: 75,325; f90: 49,640; lisp: 32,396; pascal: 12,286; sh: 9,394; perl: 7,442; ml: 5,494; awk: 3,523; makefile: 2,723; javascript: 1,206; xml: 886; fortran: 581; cs: 573
file content (84 lines) | stat: -rw-r--r-- 2,232 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// RUN: %libomptarget-compilexx-generic && %libomptarget-run-generic
// RUN: %libomptarget-compilexx-generic -O3 && %libomptarget-run-generic
// RUN: %libomptarget-compilexx-generic -O3 -ffast-math && \
// RUN:   %libomptarget-run-generic

#include <cassert>
#include <complex>
#include <iostream>

template <typename T> void test_map() {
  std::complex<T> a(0.2, 1), a_check;
#pragma omp target map(from : a_check)
  { a_check = a; }

  assert(std::abs(a - a_check) < 1e-6);
}

template <typename RT, typename AT, typename BT> void test_plus(AT a, BT b) {
  std::complex<RT> c, c_host;

  c_host = a + b;
#pragma omp target map(from : c)
  { c = a + b; }

  assert(std::abs(c - c_host) < 1e-6);
}

template <typename RT, typename AT, typename BT> void test_minus(AT a, BT b) {
  std::complex<RT> c, c_host;

  c_host = a - b;
#pragma omp target map(from : c)
  { c = a - b; }

  assert(std::abs(c - c_host) < 1e-6);
}

template <typename RT, typename AT, typename BT> void test_mul(AT a, BT b) {
  std::complex<RT> c, c_host;

  c_host = a * b;
#pragma omp target map(from : c)
  { c = a * b; }

  assert(std::abs(c - c_host) < 1e-6);
}

template <typename RT, typename AT, typename BT> void test_div(AT a, BT b) {
  std::complex<RT> c, c_host;

  c_host = a / b;
#pragma omp target map(from : c)
  { c = a / b; }

  assert(std::abs(c - c_host) < 1e-6);
}

template <typename T> void test_complex() {
  test_map<T>();

  test_plus<T>(std::complex<T>(0, 1), std::complex<T>(0.5, 0.3));
  test_plus<T>(std::complex<T>(0, 1), T(0.5));
  test_plus<T>(T(0.5), std::complex<T>(0, 1));

  test_minus<T>(std::complex<T>(0, 1), std::complex<T>(0.5, 0.3));
  test_minus<T>(std::complex<T>(0, 1), T(0.5));
  test_minus<T>(T(0.5), std::complex<T>(0, 1));

  test_mul<T>(std::complex<T>(0, 1), std::complex<T>(0.5, 0.3));
  test_mul<T>(std::complex<T>(0, 1), T(0.5));
  test_mul<T>(T(0.5), std::complex<T>(0, 1));

  test_div<T>(std::complex<T>(0, 1), std::complex<T>(0.5, 0.3));
  test_div<T>(std::complex<T>(0, 1), T(0.5));
  test_div<T>(T(0.5), std::complex<T>(0, 1));
}

int main() {
  std::cout << "Testing float" << std::endl;
  test_complex<float>();
  std::cout << "Testing double" << std::endl;
  test_complex<double>();
  return 0;
}