OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [bootloaders/] [orpmon/] [coremark/] [core_state.c] - Blame information for rev 578

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 355 julius
/*
2
Author : Shay Gal-On, EEMBC
3
 
4
This file is part of  EEMBC(R) and CoreMark(TM), which are Copyright (C) 2009
5
All rights reserved.
6
 
7
EEMBC CoreMark Software is a product of EEMBC and is provided under the terms of the
8
CoreMark License that is distributed with the official EEMBC COREMARK Software release.
9
If you received this EEMBC CoreMark Software without the accompanying CoreMark License,
10
you must discontinue use and download the official release from www.coremark.org.
11
 
12
Also, if you are publicly displaying scores generated from the EEMBC CoreMark software,
13
make sure that you are in compliance with Run and Reporting rules specified in the accompanying readme.txt file.
14
 
15
EEMBC
16
4354 Town Center Blvd. Suite 114-200
17
El Dorado Hills, CA, 95762
18 406 julius
*/
19 355 julius
#include "coremark.h"
20
/* local functions */
21 406 julius
enum CORE_STATE core_state_transition(ee_u8 ** instr,
22
                                      ee_u32 * transition_count);
23 355 julius
 
24
/*
25
Topic: Description
26
        Simple state machines like this one are used in many embedded products.
27
 
28
        For more complex state machines, sometimes a state transition table implementation is used instead,
29
        trading speed of direct coding for ease of maintenance.
30
 
31
        Since the main goal of using a state machine in CoreMark is to excercise the switch/if behaviour,
32
        we are using a small moore machine.
33
 
34
        In particular, this machine tests type of string input,
35
        trying to determine whether the input is a number or something else.
36
        (see core_state.png).
37
*/
38
 
39
/* Function: core_bench_state
40
        Benchmark function
41
 
42
        Go over the input twice, once direct, and once after introducing some corruption.
43
*/
44 406 julius
ee_u16 core_bench_state(ee_u32 blksize, ee_u8 * memblock,
45
                        ee_s16 seed1, ee_s16 seed2, ee_s16 step, ee_u16 crc)
46 355 julius
{
47
        ee_u32 final_counts[NUM_CORE_STATES];
48
        ee_u32 track_counts[NUM_CORE_STATES];
49 406 julius
        ee_u8 *p = memblock;
50 355 julius
        ee_u32 i;
51
 
52
#if CORE_DEBUG
53 406 julius
        ee_printf("State Bench: %d,%d,%d,%04x\n", seed1, seed2, step, crc);
54 355 julius
#endif
55 406 julius
        for (i = 0; i < NUM_CORE_STATES; i++) {
56
                final_counts[i] = track_counts[i] = 0;
57 355 julius
        }
58
        /* run the state machine over the input */
59 406 julius
        while (*p != 0) {
60
                enum CORE_STATE fstate =
61
                    core_state_transition(&p, track_counts);
62 355 julius
                final_counts[fstate]++;
63
#if CORE_DEBUG
64 406 julius
                ee_printf("%d,", fstate);
65 355 julius
        }
66
        ee_printf("\n");
67
#else
68
        }
69
#endif
70 406 julius
        p = memblock;
71
        while (p < (memblock + blksize)) {      /* insert some corruption */
72
                if (*p != ',')
73
                        *p ^= (ee_u8) seed1;
74
                p += step;
75 355 julius
        }
76 406 julius
        p = memblock;
77 355 julius
        /* run the state machine over the input again */
78 406 julius
        while (*p != 0) {
79
                enum CORE_STATE fstate =
80
                    core_state_transition(&p, track_counts);
81 355 julius
                final_counts[fstate]++;
82
#if CORE_DEBUG
83 406 julius
                ee_printf("%d,", fstate);
84 355 julius
        }
85
        ee_printf("\n");
86
#else
87
        }
88
#endif
89 406 julius
        p = memblock;
90
        while (p < (memblock + blksize)) {      /* undo corruption is seed1 and seed2 are equal */
91
                if (*p != ',')
92
                        *p ^= (ee_u8) seed2;
93
                p += step;
94 355 julius
        }
95
        /* end timing */
96 406 julius
        for (i = 0; i < NUM_CORE_STATES; i++) {
97
                crc = crcu32(final_counts[i], crc);
98
                crc = crcu32(track_counts[i], crc);
99 355 julius
        }
100
        return crc;
101
}
102
 
103
/* Default initialization patterns */
104 406 julius
static ee_u8 *intpat[4] =
105
    { (ee_u8 *) "5012", (ee_u8 *) "1234", (ee_u8 *) "-874", (ee_u8 *) "+122" };
106
static ee_u8 *floatpat[4] =
107
    { (ee_u8 *) "35.54400", (ee_u8 *) ".1234500", (ee_u8 *) "-110.700",
108
(ee_u8 *) "+0.64400" };
109
static ee_u8 *scipat[4] =
110
    { (ee_u8 *) "5.500e+3", (ee_u8 *) "-.123e-2", (ee_u8 *) "-87e+832",
111
(ee_u8 *) "+0.6e-12" };
112
static ee_u8 *errpat[4] =
113
    { (ee_u8 *) "T0.3e-1F", (ee_u8 *) "-T.T++Tq", (ee_u8 *) "1T3.4e4z",
114
(ee_u8 *) "34.0e-T^" };
115 355 julius
 
116
/* Function: core_init_state
117
        Initialize the input data for the state machine.
118
 
119
        Populate the input with several predetermined strings, interspersed.
120
        Actual patterns chosen depend on the seed parameter.
121
 
122
        Note:
123
        The seed parameter MUST be supplied from a source that cannot be determined at compile time
124
*/
125 406 julius
void core_init_state(ee_u32 size, ee_s16 seed, ee_u8 * p)
126
{
127
        ee_u32 total = 0, next = 0, i;
128
        ee_u8 *buf = 0;
129 355 julius
#if CORE_DEBUG
130 406 julius
        ee_u8 *start = p;
131
        ee_printf("State: %d,%d\n", size, seed);
132 355 julius
#endif
133
        size--;
134 406 julius
        next = 0;
135
        while ((total + next + 1) < size) {
136
                if (next > 0) {
137
                        for (i = 0; i < next; i++)
138
                                *(p + total + i) = buf[i];
139
                        *(p + total + i) = ',';
140
                        total += next + 1;
141 355 julius
                }
142
                seed++;
143
                switch (seed & 0x7) {
144 406 julius
                case 0:  /* int */
145
                case 1: /* int */
146
                case 2: /* int */
147
                        buf = intpat[(seed >> 3) & 0x3];
148
                        next = 4;
149 355 julius
                        break;
150 406 julius
                case 3: /* float */
151
                case 4: /* float */
152
                        buf = floatpat[(seed >> 3) & 0x3];
153
                        next = 8;
154 355 julius
                        break;
155 406 julius
                case 5: /* scientific */
156
                case 6: /* scientific */
157
                        buf = scipat[(seed >> 3) & 0x3];
158
                        next = 8;
159 355 julius
                        break;
160 406 julius
                case 7: /* invalid */
161
                        buf = errpat[(seed >> 3) & 0x3];
162
                        next = 8;
163 355 julius
                        break;
164 406 julius
                default:        /* Never happen, just to make some compilers happy */
165 355 julius
                        break;
166
                }
167
        }
168
        size++;
169 406 julius
        while (total < size) {  /* fill the rest with 0 */
170
                *(p + total) = 0;
171 355 julius
                total++;
172
        }
173
#if CORE_DEBUG
174 406 julius
        ee_printf("State Input: %s\n", start);
175 355 julius
#endif
176
}
177
 
178 406 julius
static ee_u8 ee_isdigit(ee_u8 c)
179
{
180 355 julius
        ee_u8 retval;
181 406 julius
        retval = ((c >= '0') & (c <= '9')) ? 1 : 0;
182 355 julius
        return retval;
183
}
184
 
185
/* Function: core_state_transition
186
        Actual state machine.
187
 
188
        The state machine will continue scanning until either:
189
        1 - an invalid input is detcted.
190
        2 - a valid number has been detected.
191
 
192
        The input pointer is updated to point to the end of the token, and the end state is returned (either specific format determined or invalid).
193
*/
194
 
195 406 julius
enum CORE_STATE core_state_transition(ee_u8 ** instr, ee_u32 * transition_count)
196
{
197
        ee_u8 *str = *instr;
198 355 julius
        ee_u8 NEXT_SYMBOL;
199 406 julius
        enum CORE_STATE state = CORE_START;
200
        for (; *str && state != CORE_INVALID; str++) {
201 355 julius
                NEXT_SYMBOL = *str;
202 406 julius
                if (NEXT_SYMBOL == ',') {       /* end of this input */
203 355 julius
                        str++;
204
                        break;
205
                }
206 406 julius
                switch (state) {
207 355 julius
                case CORE_START:
208 406 julius
                        if (ee_isdigit(NEXT_SYMBOL)) {
209 355 julius
                                state = CORE_INT;
210 406 julius
                        } else if (NEXT_SYMBOL == '+' || NEXT_SYMBOL == '-') {
211 355 julius
                                state = CORE_S1;
212 406 julius
                        } else if (NEXT_SYMBOL == '.') {
213 355 julius
                                state = CORE_FLOAT;
214 406 julius
                        } else {
215 355 julius
                                state = CORE_INVALID;
216
                                transition_count[CORE_INVALID]++;
217
                        }
218
                        transition_count[CORE_START]++;
219
                        break;
220
                case CORE_S1:
221 406 julius
                        if (ee_isdigit(NEXT_SYMBOL)) {
222 355 julius
                                state = CORE_INT;
223
                                transition_count[CORE_S1]++;
224 406 julius
                        } else if (NEXT_SYMBOL == '.') {
225 355 julius
                                state = CORE_FLOAT;
226
                                transition_count[CORE_S1]++;
227 406 julius
                        } else {
228 355 julius
                                state = CORE_INVALID;
229
                                transition_count[CORE_S1]++;
230
                        }
231
                        break;
232
                case CORE_INT:
233 406 julius
                        if (NEXT_SYMBOL == '.') {
234 355 julius
                                state = CORE_FLOAT;
235
                                transition_count[CORE_INT]++;
236 406 julius
                        } else if (!ee_isdigit(NEXT_SYMBOL)) {
237 355 julius
                                state = CORE_INVALID;
238
                                transition_count[CORE_INT]++;
239
                        }
240
                        break;
241
                case CORE_FLOAT:
242 406 julius
                        if (NEXT_SYMBOL == 'E' || NEXT_SYMBOL == 'e') {
243 355 julius
                                state = CORE_S2;
244
                                transition_count[CORE_FLOAT]++;
245 406 julius
                        } else if (!ee_isdigit(NEXT_SYMBOL)) {
246 355 julius
                                state = CORE_INVALID;
247
                                transition_count[CORE_FLOAT]++;
248
                        }
249
                        break;
250
                case CORE_S2:
251 406 julius
                        if (NEXT_SYMBOL == '+' || NEXT_SYMBOL == '-') {
252 355 julius
                                state = CORE_EXPONENT;
253
                                transition_count[CORE_S2]++;
254 406 julius
                        } else {
255 355 julius
                                state = CORE_INVALID;
256
                                transition_count[CORE_S2]++;
257
                        }
258
                        break;
259
                case CORE_EXPONENT:
260 406 julius
                        if (ee_isdigit(NEXT_SYMBOL)) {
261 355 julius
                                state = CORE_SCIENTIFIC;
262
                                transition_count[CORE_EXPONENT]++;
263 406 julius
                        } else {
264 355 julius
                                state = CORE_INVALID;
265
                                transition_count[CORE_EXPONENT]++;
266
                        }
267
                        break;
268
                case CORE_SCIENTIFIC:
269 406 julius
                        if (!ee_isdigit(NEXT_SYMBOL)) {
270 355 julius
                                state = CORE_INVALID;
271
                                transition_count[CORE_INVALID]++;
272
                        }
273
                        break;
274
                default:
275
                        break;
276
                }
277
        }
278 406 julius
        *instr = str;
279 355 julius
        return state;
280
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.