core_state.c 6.9 KB

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