-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFixedPointNumberDemo.cpp
More file actions
127 lines (116 loc) · 3.31 KB
/
FixedPointNumberDemo.cpp
File metadata and controls
127 lines (116 loc) · 3.31 KB
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
#include <iostream>
#include <vector>
#include <cstdint>
#include <cassert>
#include <cmath>
#include "FixedPointNumber.hpp"
using namespace std;
extern uint32_t H[32];
extern uint32_t X[1024];
template <typename T1, typename T2>
vector<T1> convolution(vector<T1> x, vector<T2> h) {
vector<T1> y;
for (int i = h.size()-1; i < x.size()+h.size()-1; ++i) {
T1 yi = 0;
for (int k = 0; k < h.size(); ++k) {
if (i-k < x.size()) {
yi = yi + x[i-k] * h[k];
}
}
y.emplace_back(yi);
}
return y;
}
template <typename T1, typename T2>
vector<T1> convolution_high_precision(vector<T1> x, vector<T2> h) {
vector<T1> y;
for (int i = h.size()-1; i < x.size()+h.size()-1; ++i) {
FixedPointNumber<7, 24> yi = 0;
for (int k = 0; k < h.size(); ++k) {
if (i-k < x.size()) {
yi = yi + (FixedPointNumber<7, 24>)x[i-k] * (FixedPointNumber<7, 24>)h[k];
}
}
y.emplace_back(yi);
}
return y;
}
int main(int argc, char** argv)
{
string dir = argv[1];
string path;
FILE* fp = NULL;
vector<FixedPointNumber<7, 8>> x_vec;
path = dir + "/X_FixedPoint.dat";
fp = fopen(path.c_str(), "r");
assert(fp);
for (int i = 0; i < 1024; ++i) {
uint32_t x_in;
fscanf(fp, "%x", &x_in);
x_vec.emplace_back(x_in);
}
fclose(fp);
vector<FixedPointNumber<3, 16>> h_vec;
path = dir + "/H_FixedPoint.dat";
fp = fopen(path.c_str(), "r");
assert(fp);
for (int i = 0; i < 32; ++i) {
uint32_t h_in;
fscanf(fp, "%x", &h_in);
h_vec.emplace_back(h_in);
}
fclose(fp);
int errcnt;
vector<FixedPointNumber<7, 8>> y_vec;
cout << "==================== Low Precision ====================" << endl;
y_vec = convolution(x_vec, h_vec);
path = dir + "/Y_FixedPoint.dat";
fp = fopen(path.c_str(), "r");
assert(fp);
errcnt = 0;
y_vec.clear();
for (int i = 0; i < 1024; ++i) {
uint32_t y_in;
fscanf(fp, "%x", &y_in);
if (abs((int)y_in - (int)y_vec[i].get_value()) > 1) {
cout << i << "-th:" << endl;
cout << '\t' << "Result = " << y_vec[i] << endl;
cout << '\t' << "Expect = " << FixedPointNumber<7, 8>(y_in) << endl;
errcnt += 1;
}
}
fclose(fp);
if (errcnt) {
cout << errcnt << ' ' << "errors" << endl;
}
else {
cout << "All are correct" << endl;
}
cout << endl << endl;
cout << "==================== High Precision ===================" << endl;
y_vec = convolution_high_precision(x_vec, h_vec);
path = dir + "/Y_FixedPoint.dat";
fp = fopen(path.c_str(), "r");
assert(fp);
errcnt = 0;
y_vec.clear();
for (int i = 0; i < 1024; ++i) {
uint32_t y_in;
fscanf(fp, "%x", &y_in);
if (abs((int)y_in - (int)y_vec[i].get_value()) > 1) {
cout << i << "-th:" << endl;
cout << '\t' << "Result = " << y_vec[i] << endl;
cout << '\t' << "Expect = " << FixedPointNumber<7, 8>(y_in) << endl;
errcnt += 1;
}
}
fclose(fp);
if (errcnt) {
cout << errcnt << ' ' << "errors" << endl;
}
else {
cout << "All are correct" << endl;
}
cout << endl << endl;
return 0;
}