Blame view

validate/validate-complex.cpp 4.37 KB
f1402849   dmayerich   renewed commit
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  #include <complex>

  #include <iostream>

  #include "rts/complex.h"

  

  #include "compare.h"

  

  #define N		5

  #define epsilon	0.000001
  
  template <typename T>
  void validateOperators()
  {
      int precision = sizeof(T) * 8;
      std::stringstream ss;
      ss<<" ("<<precision<<"-bit)";
      std::string bitString = ss.str();
      //validate complex binary functions

  	T x0, x1, y0, y1;
      for(int i = 0; i<N; i++)

  	{

  		//generate a random complex number

  		x0 = (double)rand()/(double)RAND_MAX * 2 - 1;

  		y0 = (double)rand()/(double)RAND_MAX * 2 - 1;

  		x1 = (double)rand()/(double)RAND_MAX * 2 - 1;

  		y1 = (double)rand()/(double)RAND_MAX * 2 - 1;

  

  		//create an STD and RTS instance of the complex class

  		std::complex<T> stdComplex0(x0, y0);

  		rts::complex<T> rtsComplex0(x0, y0);

  

  		std::complex<T> stdComplex1(x1, y1);

  		rts::complex<T> rtsComplex1(x1, y1);

  

  		std::complex<T> stdResult;

  		rts::complex<T> rtsResult;

  

  		//test addition

  		stdResult = stdComplex0 + stdComplex1;

  		rtsResult = rtsComplex0 + rtsComplex1;

  		compare(stdResult, rtsResult, std::string("Binary Addition") + bitString);
  		//test addition with real value

  		stdResult = stdComplex1.real() + stdComplex0;

  		rtsResult = rtsComplex1.r + rtsComplex0;

  		compare(stdResult, rtsResult, std::string("Addition with Real Value") + bitString);

  

  		//test subtraction

  		stdResult = stdComplex0 - stdComplex1;

  		rtsResult = rtsComplex0 - rtsComplex1;

  		compare(stdResult, rtsResult, std::string("Binary Subtraction") + bitString);
  		//test subtraction with real value

  		stdResult = stdComplex1.real() - stdComplex0;

  		rtsResult = rtsComplex1.r - rtsComplex0;

  		compare(stdResult, rtsResult, std::string("Subtraction with Real Value") + bitString);

  

  		//test multiplication

  		stdResult = stdComplex0 * stdComplex1;

  		rtsResult = rtsComplex0 * rtsComplex1;

  		compare(stdResult, rtsResult, std::string("Binary Multiplication") + bitString);

  		//test multiplication with real value

  		stdResult = stdComplex1.real() * stdComplex0;

  		rtsResult = rtsComplex1.r * rtsComplex0;

  		compare(stdResult, rtsResult, std::string("Multiplication with Real Value") + bitString);

  

  		//test division

  		stdResult = stdComplex0 / stdComplex1;

  		rtsResult = rtsComplex0 / rtsComplex1;

  		compare(stdResult, rtsResult, std::string("Binary Division") + bitString);
  		//test division with real value

  		stdResult = stdComplex1.real() / stdComplex0;

  		rtsResult = rtsComplex1.r / rtsComplex0;

  		compare(stdResult, rtsResult, std::string("Division with Real Value") + bitString);

  

  		//test abs()

  		stdResult = abs(stdComplex0);

  		rtsResult = abs(rtsComplex0);

  		compare(stdResult, rtsResult, std::string("abs()") + bitString);

  

  		//test log()

  		stdResult = log(stdComplex0);

  		rtsResult = log(rtsComplex0);

  		compare(stdResult, rtsResult, std::string("log()") + bitString);

  

  		//test exp()

  		stdResult = exp(stdComplex0);

  		rtsResult = exp(rtsComplex0);

  		compare(stdResult, rtsResult, std::string("exp()") + bitString);

  

  		//test pow()

  		stdResult = pow(stdComplex0, (T)2.0);

  		rtsResult = pow(rtsComplex0, (T)2.0);

  		compare(stdResult, rtsResult, std::string("pow()") + bitString);

  

  		//test sqrt()

  		stdResult = sqrt(stdComplex0);

  		rtsResult = rts::sqrt(rtsComplex0);

  		compare(stdResult, rtsResult, std::string("sqrt()") + bitString);

  

  		//trigonometric functions

  		stdResult = sin(stdComplex0);

  		rtsResult = rts::sin(rtsComplex0);

  		compare(stdResult, rtsResult, std::string("sin()") + bitString);

  

  		//trigonometric functions

  		stdResult = cos(stdComplex0);

  		rtsResult = rts::cos(rtsComplex0);

  		compare(stdResult, rtsResult, std::string("cos()") + bitString);

  

  		//ASSIGNMENT OPERATORS

  

  		//     +=

  		stdResult = stdComplex0;

  		stdResult += stdComplex1;

  		rtsResult = rtsComplex0;

  		rtsResult += rtsComplex1;

  		compare(stdResult, rtsResult, std::string("operator +=") + bitString);

  

  		//     *=

  		stdResult = stdComplex0;

  		stdResult *= stdComplex1;

  		rtsResult = rtsComplex0;

  		rtsResult *= rtsComplex1;

  		compare(stdResult, rtsResult, std::string("operator *=") + bitString);

  

  		//     /=

  		stdResult = stdComplex0;

  		stdResult /= stdComplex1;

  		rtsResult = rtsComplex0;

  		rtsResult /= rtsComplex1;

  		compare(stdResult, rtsResult, std::string("operator /=") + bitString);

  

  	}
  }

  

  void cpuValidateComplex()

  {
      //validate both floating point and double precision
      validateOperators<float>();

      validateOperators<double>();

  

  }