-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcore_state.f
More file actions
283 lines (268 loc) · 8.45 KB
/
core_state.f
File metadata and controls
283 lines (268 loc) · 8.45 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
\ Parse ccc delimited by " (double-quote),
\ reserve data space and store ccc
: ," ( "ccc<quote>" -- )
[char] " parse ( c-addr u )
here over allot
swap cmove ;
: create_pat ( u "<spaces>name" -- )
create
, \ fixed strings size
does> ( u1 -- c-addr u2 )
swap over @ swap over *
rot cell+ + swap ;
#4 create_pat intpat
," 50121234-874+122" \ allot and store strings
#8 create_pat floatpat
," 35.54400.1234500-110.700+0.64400" \ allot and store strings
#8 create_pat scipat
," 5.500e+3-.123e-2-87e+832+0.6e-12" \ allot and store strings
#8 create_pat errpat
," T0.3e-1F-T.T++Tq1T3.4e4z34.0e-T^" \ allot and store strings
\ Initialize the input data for the state machine: size &p seed --
\ void core_init_state(ee_u32 size, ee_s16 seed, ee_u8 *p)
: core_init_state ( u1 a-addr u2 -- )
>r 2dup ! \ store size
cell+
swap 1- ( c-addr u1 )
begin ( c-addr u1 :R seed )
r> 1+ dup >r
dup #3 rshift $0003 and
swap $0007 and
dup #3 < if
drop intpat
else
dup #5 < if
drop floatpat
else
#7 < if
scipat
else
errpat
then
then
then ( c-addr u1 c-addr1 u2 )
rot 1- 2dup <
while
over - >r ( c-addr c-addr1 u2 )
rot 2dup + >r
swap cmove
r> [char] , over c!
1+
r>
repeat
nip nip 2 +
0 fill \ fill the rest with 0
r> drop ;
: ee_isdigit ( char -- flag )
[char] 0 -
#9 swap u<
invert ;
0 constant CORE_START
1 constant CORE_INVALID
2 constant CORE_S1
3 constant CORE_S2
4 constant CORE_INT
5 constant CORE_FLOAT
6 constant CORE_EXPONENT
7 constant CORE_SCIENTIFIC
8 constant NUM_CORE_STATES
\ The state machine will continue scanning until either:
\ 1 - an invalid input is detcted.
\ 2 - a valid number has been detected.
\ 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).
\ enum CORE_STATE core_state_transition( ee_u8 **instr , ee_u32 *transition_count)
: core_state_transition ( c-addr1 a-addr -- c-addr2 u )
>r CORE_START
begin \ &str state R: &transition_count
over c@
2dup swap CORE_INVALID <> and
while \ &str state char R: &transition_count
dup [char] ,
<> if
over CORE_START = if
dup ee_isdigit if
nip CORE_INT swap
else
dup [char] + =
over [char] - =
or if
nip CORE_S1 swap
else
dup [char] . = if
nip CORE_FLOAT swap
else
nip CORE_INVALID swap
CORE_INVALID cells r@ +
dup @ 1+ swap !
then
then
then
CORE_START cells r@ +
dup @ 1+ swap !
else
over CORE_S1 = if
dup ee_isdigit if
nip CORE_INT swap
else
dup [char] . = if
nip CORE_FLOAT swap
else
nip CORE_INVALID swap
then
then
CORE_S1 cells r@ +
dup @ 1+ swap !
else
over CORE_INT = if
dup [char] . = if
nip CORE_FLOAT swap
CORE_INT cells r@ +
dup @ 1+ swap !
else
dup ee_isdigit if
else
nip CORE_INVALID swap
CORE_INT cells r@ +
dup @ 1+ swap !
then
then
else
over CORE_FLOAT = if
dup [char] E =
over [char] e =
or if
nip CORE_S2 swap
CORE_FLOAT cells r@ +
dup @ 1+ swap !
else
dup ee_isdigit if
else
nip CORE_INVALID swap
CORE_FLOAT cells r@ +
dup @ 1+ swap !
then
then
else
over CORE_S2 = if
dup [char] + =
over [char] - =
or if
nip CORE_EXPONENT swap
else
nip CORE_INVALID swap
then
CORE_S2 cells r@ +
dup @ 1+ swap !
else
over CORE_EXPONENT = if
dup ee_isdigit if
nip CORE_SCIENTIFIC swap
else
nip CORE_INVALID swap
then
CORE_EXPONENT cells r@ +
dup @ 1+ swap !
else
over CORE_SCIENTIFIC = if
dup ee_isdigit if
else
nip CORE_INVALID swap
CORE_INVALID cells r@ +
dup @ 1+ swap !
then
then
then
then
then
then
then
then
else
r> 2drop
swap 1+ swap
exit
then
drop swap 1+ swap
repeat
r> 2drop ;
\ Simple state machines like this one are used in many embedded products.
\ For more complex state machines, sometimes a state transition table implementation is used instead,
\ trading speed of direct coding for ease of maintenance.
\ Since the main goal of using a state machine in CoreMark is to excercise the switch/if behaviour,
\ we are using a small moore machine.
\ In particular, this machine tests type of string input,
\ trying to determine whether the input is a number or something else.
\ (see core_state.png).
create final_counts NUM_CORE_STATES cells allot \ no need for ee_u32, ee_u16 is enough
create track_counts NUM_CORE_STATES cells allot \ no need for ee_u32, ee_u16 is enough
: reset_counts ( a-addr1 a-addr2 u -- )
0 do
0 over !
cell+ swap
0 over !
cell+
loop
2drop ;
\ Benchmark function: crc seed2 seed1 step &state_data
\ Go over the input twice, once direct, and once after introducing some corruption.
\ ee_u16 core_bench_state(ee_u32 blksize, ee_u8 *memblock,
\ ee_s16 seed1, ee_s16 seed2, ee_s16 step, ee_u16 crc)
: core_bench_state ( u1 n1 n2 n3 a-addr -- u2 )
>r \ R: &state_data
final_counts track_counts NUM_CORE_STATES reset_counts
track_counts r@ cell+
begin \ track_counts &str
dup c@
while
over core_state_transition
cells final_counts +
dup @ 1+ swap !
repeat
2drop
swap $FF and r@ swap >r
dup cell+ swap @ over + \ step &str &str+blksize
>r
begin \ step &str R: seed1 &str+blksize
dup r@ u<
while
dup c@ [char] ,
<> if
dup c@
r> r@ swap >r
xor over c!
then
over +
repeat
r> drop r> 2drop \ crc seed2 step
track_counts r@ cell+
begin \ track_counts &str
dup c@
while
over core_state_transition
cells final_counts +
dup @ 1+ swap !
repeat
2drop
swap $FF and r@ swap >r
dup cell+ swap @ over + \ step &str &str+blksize
>r
begin \ step &str R: seed2 &str+blksize
dup r@ u<
while
dup c@ [char] ,
<> if
dup c@
r> r@ swap >r
xor over c!
then
over +
repeat
r> 2drop r> 2drop \ crc
track_counts final_counts rot NUM_CORE_STATES
0 do \ &track_counts &final_counts crc
>r dup @ 0 r> crcu32
>r cell+ swap
dup @ 0 r> crcu32
>r cell+ swap r>
loop
nip nip r> drop ;