-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
174 lines (164 loc) · 3.5 KB
/
main.cpp
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#ifdef ARDUINO_TARGET
#include "Arduino.h"
#include <avr/pgmspace.h>
#define F_CPU 16000000
#define ARDUINO 100
#else
#include <string>
#include <ctime>
#include <iostream>
#include <sys/time.h>
#include <unistd.h>
#endif
#include "ecamatrix.h"
#include "ansmatrix.h"
using namespace::std;
const unsigned char rows = 20;
const unsigned char columns = 20;
void printMatrix(int result[rows][columns], unsigned char row, unsigned char column)
{
row = 0;
while (row < rows)
{
column = 0;
while (column < columns)
{
char message[5];
snprintf(message, 5, "%5d", result[row][column]);
#ifdef ARDUINO_TARGET
Serial.print(message);
Serial.print(F(", "));
#else
cout << message << ", ";
#endif
column++;
}
#ifdef ARDUINO_TARGET
Serial.println();
#else
count << endl;
#endif
row++;
}
#ifdef ARDUINO_TARGET
Serial.println();
#else
count << endl;
#endif
}
bool checkMatrix(int result[rows][columns], unsigned char i, unsigned char j)
{
i = 0;
j = 0;
while (i < rows)
{
j = 0;
while (j < columns)
{
#ifdef ARDUINO_TARGET
if (result[i][j] != pgm_read_word(&C[i][j]))
{
Serial.print(result[i][j]);
Serial.print(F(" != "));
Serial.print(pgm_read_word(&C[i][j]));
Serial.print(F(" ("));
Serial.print(i);
Serial.print(F(","));
Serial.print(j);
Serial.println(F(")"));
return false;
}
#else
int correct = C[i][j];
if (result[i][j] != correct)
{
cout << result[i][j] << " != " << correct << " (" << i << "," << j << ")" << endl;
return false;
}
#endif
j++;
}
i++;
}
return true;
}
void setup()
{
#ifdef ARDUINO_TARGET
Serial.begin(115200);
#endif
}
void loop()
{
int rounds = 1000;
#ifdef ARDUINO_TARGET
Serial.println(F("START"));
#else
cout << "START" << endl;
#endif
char cache[rows][columns];
int B[rows][columns];
int round = 0;
unsigned char row = 0;
unsigned char column;
while (row < rows)
{
column = 0;
while (column < columns)
{
#ifdef ARDUINO_TARGET
cache[row][column] = (char)pgm_read_word(&A[row][column]);
#else
cache[row][column] = (char)A[row][column];
#endif
column++;
}
row++;
}
int temp[rows];
#ifdef ARDUINO_TARGET
unsigned long timePoint = micros();
#else
clock_t timePoint = clock();
#endif
while (round <= rounds)
{
row = 0;
while (row < rows)
{
#include "calc.h"
row++;
}
round++;
}
#ifdef ARDUINO_TARGET
timePoint = micros() - timePoint;
if (checkMatrix(B, row, column) == true)
{
printMatrix(B, row, column);
}
Serial.print(timePoint/rounds);
Serial.println(" us");
Serial.print(timePoint / rounds * 16);
Serial.println(" ticks");
Serial.print(timePoint / rounds * 16 / (20 * 20 * 20));
Serial.println(" ticks per multiplication");
Serial.println(F("DONE"));
while(1);
#else
clock_t end = clock();
double diff = double(clock() - timePoint);
cout << diff / rounds / CLOCKS_PER_SEC << " us" << endl;
cout << diff / rounds << " clicks" << endl;
cout << diff / rounds / (20 * 20 * 20) << " ticks per multiplication" << endl;
cout << checkMatrix(B, row, column) << endl;
cout << "DONE" << endl;
#endif
}
#ifndef ARDUINO_TARGET
int main(int argc, char **argv) {
setup();
loop();
return 0;
}
#endif