1 /* Generated by re2c */
2 #line 1 "main.re"
3 // re2c $INPUT -o $OUTPUT -b
4 /* re2c lesson 001_upn_calculator, main.re, (c) M. Boerger, L. Allan 2006 */
5 #line 43 "main.re"
6 
7 
8 #define VC_EXTRALEAN		// Exclude rarely-used stuff from Windows headers
9 
10 #if _MSC_VER > 1200
11 #define WINVER 0x0400   // Change this to the appropriate value to target Windows 98 and Windows 2000 or later.
12 #endif                  // Prevents warning from vc7.1 complaining about redefinition
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <assert.h>
17 #include <string.h>
18 #include <windows.h>
19 #include "HiResTimer.h"
20 
21 static char gTestBuf[1000] = "";
22 
23 /**
24  * @brief Setup HiResolution timer and confirm it is working ok
25  */
InitHiResTimerAndVerifyWorking(void)26 void InitHiResTimerAndVerifyWorking(void)
27 {
28    double elapsed;
29    HrtInit();
30    HrtSetPriority(ABOVE_NORMAL_PRIORITY_CLASS);
31    HrtStart();
32    Sleep(100);
33    elapsed = HrtElapsedMillis();
34    if ((elapsed < 90) || (elapsed > 110)) {
35       printf("HiResTimer misbehaving: %f\n", elapsed);
36       exit(2);
37    }
38 }
39 
40 /**
41  * @brief Scan for numbers in different formats
42  */
ScanFullSpeed(char * pzStrToScan,size_t lenStrToScan)43 int ScanFullSpeed(char *pzStrToScan, size_t lenStrToScan)
44 {
45 	unsigned char *pzCurScanPos = (unsigned char*)pzStrToScan;
46 	unsigned char *pzBacktrackInfo = 0;
47 #define YYCTYPE         unsigned char
48 #define YYCURSOR        pzCurScanPos
49 #define YYLIMIT         (pzStrToScan+lenStrToScan)
50 #define YYMARKER        pzBacktrackInfo
51 #define YYFILL(n)
52 
53 	for(;;)
54 	{
55 
56 #line 57 "main.c"
57 		{
58 			YYCTYPE yych;
59 			static const unsigned char yybm[] = {
60 				  0,   0,   0,   0,   0,   0,   0,   0,
61 				  0,   0,   0,   0,   0,   0,   0,   0,
62 				  0,   0,   0,   0,   0,   0,   0,   0,
63 				  0,   0,   0,   0,   0,   0,   0,   0,
64 				  0,   0,   0,   0,   0,   0,   0,   0,
65 				  0,   0,   0,   0,   0,   0,   0,   0,
66 				128, 128, 128, 128, 128, 128, 128, 128,
67 				128, 128,   0,   0,   0,   0,   0,   0,
68 				  0,   0,   0,   0,   0,   0,   0,   0,
69 				  0,   0,   0,   0,   0,   0,   0,   0,
70 				  0,   0,   0,   0,   0,   0,   0,   0,
71 				  0,   0,   0,   0,   0,   0,   0,   0,
72 				  0,   0,   0,   0,   0,   0,   0,   0,
73 				  0,   0,   0,   0,   0,   0,   0,   0,
74 				  0,   0,   0,   0,   0,   0,   0,   0,
75 				  0,   0,   0,   0,   0,   0,   0,   0,
76 				  0,   0,   0,   0,   0,   0,   0,   0,
77 				  0,   0,   0,   0,   0,   0,   0,   0,
78 				  0,   0,   0,   0,   0,   0,   0,   0,
79 				  0,   0,   0,   0,   0,   0,   0,   0,
80 				  0,   0,   0,   0,   0,   0,   0,   0,
81 				  0,   0,   0,   0,   0,   0,   0,   0,
82 				  0,   0,   0,   0,   0,   0,   0,   0,
83 				  0,   0,   0,   0,   0,   0,   0,   0,
84 				  0,   0,   0,   0,   0,   0,   0,   0,
85 				  0,   0,   0,   0,   0,   0,   0,   0,
86 				  0,   0,   0,   0,   0,   0,   0,   0,
87 				  0,   0,   0,   0,   0,   0,   0,   0,
88 				  0,   0,   0,   0,   0,   0,   0,   0,
89 				  0,   0,   0,   0,   0,   0,   0,   0,
90 				  0,   0,   0,   0,   0,   0,   0,   0,
91 				  0,   0,   0,   0,   0,   0,   0,   0,
92 			};
93 			if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
94 			yych = *YYCURSOR;
95 			if (yych <= ',') {
96 				if (yych <= 0x00) goto yy2;
97 				if (yych == '+') goto yy6;
98 				goto yy4;
99 			} else {
100 				if (yych <= '/') {
101 					if (yych <= '-') goto yy8;
102 					goto yy4;
103 				} else {
104 					if (yych <= '0') goto yy10;
105 					if (yych <= '9') goto yy11;
106 					goto yy4;
107 				}
108 			}
109 yy2:
110 			++YYCURSOR;
111 #line 98 "main.re"
112 			{ return 0; }
113 #line 114 "main.c"
114 yy4:
115 			++YYCURSOR;
116 yy5:
117 #line 99 "main.re"
118 			{ return 1; }
119 #line 120 "main.c"
120 yy6:
121 			++YYCURSOR;
122 #line 96 "main.re"
123 			{ continue; }
124 #line 125 "main.c"
125 yy8:
126 			++YYCURSOR;
127 #line 97 "main.re"
128 			{ continue; }
129 #line 130 "main.c"
130 yy10:
131 			yych = *++YYCURSOR;
132 			if (yych <= '/') goto yy5;
133 			if (yych <= '9') goto yy14;
134 			goto yy5;
135 yy11:
136 			++YYCURSOR;
137 			if (YYLIMIT <= YYCURSOR) YYFILL(1);
138 			yych = *YYCURSOR;
139 			if (yybm[0+yych] & 128) {
140 				goto yy11;
141 			}
142 #line 94 "main.re"
143 			{ continue; }
144 #line 145 "main.c"
145 yy14:
146 			++YYCURSOR;
147 			if (YYLIMIT <= YYCURSOR) YYFILL(1);
148 			yych = *YYCURSOR;
149 			if (yych <= '/') goto yy16;
150 			if (yych <= '9') goto yy14;
151 yy16:
152 #line 95 "main.re"
153 			{ continue; }
154 #line 155 "main.c"
155 		}
156 #line 100 "main.re"
157 
158 	}
159 }
160 
161 /**
162  * @brief Scan for numbers in different formats
163  */
scan(char * pzStrToScan,size_t lenStrToScan)164 int scan(char *pzStrToScan, size_t lenStrToScan)
165 {
166 	unsigned char *pzCurScanPos = (unsigned char*)pzStrToScan;
167 	unsigned char *pzBacktrackInfo = 0;
168 #define YYCTYPE         unsigned char
169 #define YYCURSOR        pzCurScanPos
170 #define YYLIMIT         (pzStrToScan+lenStrToScan)
171 #define YYMARKER        pzBacktrackInfo
172 #define YYFILL(n)
173 
174 	for(;;)
175 	{
176 
177 #line 178 "main.c"
178 		{
179 			YYCTYPE yych;
180 			static const unsigned char yybm[] = {
181 				  0,   0,   0,   0,   0,   0,   0,   0,
182 				  0,   0,   0,   0,   0,   0,   0,   0,
183 				  0,   0,   0,   0,   0,   0,   0,   0,
184 				  0,   0,   0,   0,   0,   0,   0,   0,
185 				  0,   0,   0,   0,   0,   0,   0,   0,
186 				  0,   0,   0,   0,   0,   0,   0,   0,
187 				128, 128, 128, 128, 128, 128, 128, 128,
188 				128, 128,   0,   0,   0,   0,   0,   0,
189 				  0,   0,   0,   0,   0,   0,   0,   0,
190 				  0,   0,   0,   0,   0,   0,   0,   0,
191 				  0,   0,   0,   0,   0,   0,   0,   0,
192 				  0,   0,   0,   0,   0,   0,   0,   0,
193 				  0,   0,   0,   0,   0,   0,   0,   0,
194 				  0,   0,   0,   0,   0,   0,   0,   0,
195 				  0,   0,   0,   0,   0,   0,   0,   0,
196 				  0,   0,   0,   0,   0,   0,   0,   0,
197 				  0,   0,   0,   0,   0,   0,   0,   0,
198 				  0,   0,   0,   0,   0,   0,   0,   0,
199 				  0,   0,   0,   0,   0,   0,   0,   0,
200 				  0,   0,   0,   0,   0,   0,   0,   0,
201 				  0,   0,   0,   0,   0,   0,   0,   0,
202 				  0,   0,   0,   0,   0,   0,   0,   0,
203 				  0,   0,   0,   0,   0,   0,   0,   0,
204 				  0,   0,   0,   0,   0,   0,   0,   0,
205 				  0,   0,   0,   0,   0,   0,   0,   0,
206 				  0,   0,   0,   0,   0,   0,   0,   0,
207 				  0,   0,   0,   0,   0,   0,   0,   0,
208 				  0,   0,   0,   0,   0,   0,   0,   0,
209 				  0,   0,   0,   0,   0,   0,   0,   0,
210 				  0,   0,   0,   0,   0,   0,   0,   0,
211 				  0,   0,   0,   0,   0,   0,   0,   0,
212 				  0,   0,   0,   0,   0,   0,   0,   0,
213 			};
214 			if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
215 			yych = *YYCURSOR;
216 			if (yych <= ',') {
217 				if (yych <= 0x00) goto yy19;
218 				if (yych == '+') goto yy23;
219 				goto yy21;
220 			} else {
221 				if (yych <= '/') {
222 					if (yych <= '-') goto yy25;
223 					goto yy21;
224 				} else {
225 					if (yych <= '0') goto yy27;
226 					if (yych <= '9') goto yy28;
227 					goto yy21;
228 				}
229 			}
230 yy19:
231 			++YYCURSOR;
232 #line 125 "main.re"
233 			{ printf("EOF\n");                           return 0; }
234 #line 235 "main.c"
235 yy21:
236 			++YYCURSOR;
237 yy22:
238 #line 126 "main.re"
239 			{ printf("ERR\n"); strcat(gTestBuf, "ERR "); return 1; }
240 #line 241 "main.c"
241 yy23:
242 			++YYCURSOR;
243 #line 123 "main.re"
244 			{ printf("+\n");   strcat(gTestBuf, "+ ");   continue; }
245 #line 246 "main.c"
246 yy25:
247 			++YYCURSOR;
248 #line 124 "main.re"
249 			{ printf("-\n");   strcat(gTestBuf, "- ");   continue; }
250 #line 251 "main.c"
251 yy27:
252 			yych = *++YYCURSOR;
253 			if (yych <= '/') goto yy22;
254 			if (yych <= '9') goto yy31;
255 			goto yy22;
256 yy28:
257 			++YYCURSOR;
258 			if (YYLIMIT <= YYCURSOR) YYFILL(1);
259 			yych = *YYCURSOR;
260 			if (yybm[0+yych] & 128) {
261 				goto yy28;
262 			}
263 #line 121 "main.re"
264 			{ printf("Num\n"); strcat(gTestBuf, "Num "); continue; }
265 #line 266 "main.c"
266 yy31:
267 			++YYCURSOR;
268 			if (YYLIMIT <= YYCURSOR) YYFILL(1);
269 			yych = *YYCURSOR;
270 			if (yych <= '/') goto yy33;
271 			if (yych <= '9') goto yy31;
272 yy33:
273 #line 122 "main.re"
274 			{ printf("Oct\n"); strcat(gTestBuf, "Oct "); continue; }
275 #line 276 "main.c"
276 		}
277 #line 127 "main.re"
278 
279 	}
280 }
281 
282 /**
283  * @brief Show high resolution elapsed time for 10,000 and 100,000 loops
284  */
DoTimingsOfStrnCmp(void)285 void DoTimingsOfStrnCmp(void)
286 {
287    char testStr[] = "Hello, world";
288    int totLoops = 10000;
289    int totFoundCount = 0;
290    int foundCount = 0;
291    int loop;
292    int rc;
293    const int progressAnd = 0xFFFFF000;
294    double elapsed;
295 
296    printf("\n\n%d loops with * every %d loops to confirm\n", totLoops, ((~progressAnd) + 1));
297 
298    HrtStart();
299    for (loop = 0; loop < totLoops; ++loop) {
300       foundCount = 0;
301       rc = strncmp(testStr, "Hello", 5);
302       if (rc == 0) {
303          foundCount++;
304          totFoundCount++;
305          if ((totFoundCount & progressAnd) == totFoundCount) {
306             printf("*");
307          }
308       }
309    }
310    elapsed = HrtElapsedMillis();
311    printf("\nstrncmp Elapsed for %7d loops milliseconds: %7.3f\n", totLoops, elapsed);
312    printf("FoundCount each loop: %d\n", foundCount);
313    printf("TotalFoundCount for all loops: %d\n", totFoundCount);
314 
315    totLoops = 100000;
316    HrtStart();
317    for (loop = 0; loop < totLoops; ++loop) {
318       foundCount = 0;
319       rc = strncmp(testStr, "Hello", 5);
320       if (rc == 0) {
321          foundCount++;
322          totFoundCount++;
323          if ((totFoundCount & progressAnd) == totFoundCount) {
324             printf("*");
325          }
326       }
327    }
328    elapsed = HrtElapsedMillis();
329    printf("\nstrncmp Elapsed for %7d loops milliseconds: %7.3f\n", totLoops, elapsed);
330    printf("FoundCount each loop: %d\n", foundCount);
331    printf("TotalFoundCount for all loops: %d\n", totFoundCount);
332 }
333 
334 /**
335  * @brief Show high resolution elapsed time for 10,000 and 100,000 loops
336  */
DoTimingsOfRe2c(void)337 void DoTimingsOfRe2c(void)
338 {
339    char* testStrings[] = { "123", "1234", "+123", "01234", "-04321", "abc", "123abc" };
340    const int testCount = sizeof(testStrings) / sizeof(testStrings[0]);
341    int i;
342    int totLoops = 10000 / testCount;  // Doing more than one per loop
343    int totFoundCount = 0;
344    int foundCount = 0;
345    int loop;
346    int rc;
347    const int progressAnd = 0xFFFFF000;
348    double elapsed;
349 
350    printf("\n\n%d loops with * every %d loops to confirm\n", totLoops, ((~progressAnd) + 1));
351 
352    HrtStart();
353    for (loop = 0; loop < totLoops; ++loop) {
354       foundCount = 0;
355       strcpy(gTestBuf, "");
356       for (i = 0; i < testCount; ++i) {
357          char* pzCurStr = testStrings[i];
358          size_t len = strlen(pzCurStr);  // Calc of strlen slows things down ... std::string?
359          rc = ScanFullSpeed(pzCurStr, len);
360          if (rc == 0) {
361             foundCount++;
362             totFoundCount++;
363             if ((totFoundCount & progressAnd) == totFoundCount) {
364                printf("*");
365             }
366          }
367       }
368    }
369    elapsed = HrtElapsedMillis();
370    printf("\nRe2c Elapsed for %7d loops milliseconds: %7.3f\n", totLoops, elapsed);
371    printf("FoundCount each loop: %d\n", foundCount);
372    printf("TotalFoundCount for all loops: %d\n", totFoundCount);
373 
374    totLoops = 100000 / testCount;
375    printf("\n\n%d loops with * every %d loops to confirm\n", totLoops, ((~progressAnd) + 1));
376 
377    HrtStart();
378    for (loop = 0; loop < totLoops; ++loop) {
379       foundCount = 0;
380       strcpy(gTestBuf, "");
381       for (i = 0; i < testCount; ++i) {
382          char* pzCurStr = testStrings[i];
383          size_t len = strlen(pzCurStr);  // Calc of strlen slows things down ... std::string?
384          rc = ScanFullSpeed(pzCurStr, len);
385          if (rc == 0) {
386             foundCount++;
387             totFoundCount++;
388             if ((totFoundCount & progressAnd) == totFoundCount) {
389                printf("*");
390             }
391          }
392       }
393    }
394    elapsed = HrtElapsedMillis();
395    printf("\nRe2c Elapsed for %7d loops milliseconds: %7.3f\n", totLoops, elapsed);
396    printf("FoundCount each loop: %d\n", foundCount);
397    printf("TotalFoundCount for all loops: %d\n", totFoundCount);
398 }
399 
400 /**
401  * @brief Entry point for console app
402  */
main(int argc,char ** argv)403 int main(int argc, char **argv)
404 {
405    char  testStr_A[] = "123";
406    char* testStr_B   = "456";
407    char* testStrings[] = { "123", "1234", "+123", "01234", "-04321", "abc", "123abc" };
408    const int testCount = sizeof(testStrings) / sizeof(testStrings[0]);
409    int i;
410 
411    int rc = scan(testStr_A, 3);
412    printf("rc: %d\n", rc);
413 
414    rc = scan(testStr_B, 3);
415    printf("rc: %d\n", rc);
416 
417    rc = scan("789", 3);
418    printf("rc: %d\n", rc);
419 
420    strcpy(gTestBuf, "");
421    for (i = 0; i < testCount; ++i) {
422       char* pzCurStr = testStrings[i];
423       size_t len = strlen(pzCurStr);
424       scan(pzCurStr, len);
425    }
426    printf("%s\n", gTestBuf);
427    rc = strcmp(gTestBuf, "Num Num + Num Oct - Oct ERR Num ERR ");
428    if (rc == 0) {
429       printf("Success\n");
430    }
431    else {
432       printf("Failure\n");
433    }
434    assert(0 == rc); // Doesn't work with Release build
435 
436    InitHiResTimerAndVerifyWorking();
437 
438    DoTimingsOfStrnCmp();
439 
440    DoTimingsOfRe2c();
441 
442    return 0;
443 }
444