Benchmark of DP, SP and Int. Arithmetic operations for ARM Cortex M7 MCU, Nucleo-144 Stm32F746 and Stm32F767 (modified from ddown post at "Arduino for STM32") by Jovan Ivković (JovanEps)

Dependencies:   mbed

Committer:
JovanEps
Date:
Wed Jan 04 14:28:09 2017 +0000
Revision:
3:12d9e9070739
Parent:
2:03cf226a5ba3
Final ver. 1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JovanEps 0:43b96e9650ef 1 //********************************************************
JovanEps 3:12d9e9070739 2 //** Nucleo-144 Stm32F746 and Stm32F767 benchmark ******
JovanEps 3:12d9e9070739 3 //** DP, SP and Int Arithemtic ops. ******
JovanEps 3:12d9e9070739 4 //** modified from ddown post at Arduino for STM32 ******
JovanEps 3:12d9e9070739 5 //** github.com/ddrown/benchmark.c (Arduino IDE) ******
JovanEps 3:12d9e9070739 6 //** by ******
JovanEps 3:12d9e9070739 7 //** Jovan Ivković - 2016. ******
JovanEps 3:12d9e9070739 8 //** JovanEps (jovan.eps@gmail.com). ******
JovanEps 0:43b96e9650ef 9 //********************************************************
JovanEps 0:43b96e9650ef 10 #include "mbed.h"
JovanEps 0:43b96e9650ef 11 DigitalOut myled(LED1);
JovanEps 0:43b96e9650ef 12 Serial pc(USBTX, USBRX);
JovanEps 0:43b96e9650ef 13 Timer timer;
JovanEps 0:43b96e9650ef 14
JovanEps 0:43b96e9650ef 15 #include <stdlib.h>
JovanEps 0:43b96e9650ef 16 #include <stdio.h>
JovanEps 0:43b96e9650ef 17 #include <string.h>
JovanEps 0:43b96e9650ef 18 #include <math.h>
JovanEps 0:43b96e9650ef 19 /* the following is optional depending on the timing function used */
JovanEps 0:43b96e9650ef 20 #include <time.h>
JovanEps 0:43b96e9650ef 21
JovanEps 0:43b96e9650ef 22
JovanEps 2:03cf226a5ba3 23 #define REDO_COMPUTATIONS 10
JovanEps 2:03cf226a5ba3 24 struct results {
JovanEps 2:03cf226a5ba3 25 uint32_t doubletime;
JovanEps 2:03cf226a5ba3 26 uint32_t floattime;
JovanEps 2:03cf226a5ba3 27 uint32_t inttime;
JovanEps 2:03cf226a5ba3 28 };
JovanEps 0:43b96e9650ef 29
JovanEps 2:03cf226a5ba3 30 #define MAX_LOOPS 512
JovanEps 2:03cf226a5ba3 31 double MyDoubles[MAX_LOOPS];
JovanEps 2:03cf226a5ba3 32 double a_d = 12345.67, b_d = 54321.11;
JovanEps 2:03cf226a5ba3 33 float MyFloats[MAX_LOOPS];
JovanEps 2:03cf226a5ba3 34 float a_f = 67890.12, b_f = 8756451.17;
JovanEps 2:03cf226a5ba3 35 int Myints[MAX_LOOPS];
JovanEps 2:03cf226a5ba3 36 int a_i = 581674411, b_i = 18714;
JovanEps 0:43b96e9650ef 37
JovanEps 2:03cf226a5ba3 38 //****************************************
JovanEps 2:03cf226a5ba3 39 uint32_t micros ()
JovanEps 2:03cf226a5ba3 40 {
JovanEps 2:03cf226a5ba3 41 //****************************************
JovanEps 2:03cf226a5ba3 42 // uint32_t usec = timer.read_us();
JovanEps 2:03cf226a5ba3 43 //return usec;
JovanEps 2:03cf226a5ba3 44 return timer.read_us();
JovanEps 2:03cf226a5ba3 45 }
JovanEps 0:43b96e9650ef 46
JovanEps 2:03cf226a5ba3 47 //****************************************
JovanEps 2:03cf226a5ba3 48 void math_add (struct results *r) {
JovanEps 2:03cf226a5ba3 49 //****************************************
JovanEps 2:03cf226a5ba3 50 uint32_t t, c, l;
JovanEps 2:03cf226a5ba3 51
JovanEps 2:03cf226a5ba3 52 t = micros ();
JovanEps 2:03cf226a5ba3 53 for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
JovanEps 2:03cf226a5ba3 54 {
JovanEps 2:03cf226a5ba3 55 for ( l = 0 ; l < MAX_LOOPS ; l ++ )
JovanEps 2:03cf226a5ba3 56 {
JovanEps 2:03cf226a5ba3 57 MyDoubles [ l ] = double ( a_d + b_d * double ( l ) );
JovanEps 1:be78b18b8347 58 }
JovanEps 2:03cf226a5ba3 59 }
JovanEps 2:03cf226a5ba3 60 r->doubletime = micros () - t;
JovanEps 0:43b96e9650ef 61
JovanEps 2:03cf226a5ba3 62 t = micros ();
JovanEps 2:03cf226a5ba3 63 for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
JovanEps 2:03cf226a5ba3 64 {
JovanEps 2:03cf226a5ba3 65 for ( l = 0 ; l < MAX_LOOPS ; l ++ )
JovanEps 2:03cf226a5ba3 66 {
JovanEps 2:03cf226a5ba3 67 MyFloats [ l ] = float ( a_f + b_f * float ( l ) );
JovanEps 0:43b96e9650ef 68 }
JovanEps 2:03cf226a5ba3 69 }
JovanEps 2:03cf226a5ba3 70 r->floattime = micros () - t;
JovanEps 0:43b96e9650ef 71
JovanEps 2:03cf226a5ba3 72 t = micros ();
JovanEps 2:03cf226a5ba3 73 for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
JovanEps 2:03cf226a5ba3 74 {
JovanEps 2:03cf226a5ba3 75 for ( l = 0 ; l < MAX_LOOPS ; l ++ )
JovanEps 2:03cf226a5ba3 76 {
JovanEps 2:03cf226a5ba3 77 Myints [ l ] = a_i + b_i * l;
JovanEps 1:be78b18b8347 78 }
JovanEps 2:03cf226a5ba3 79 }
JovanEps 2:03cf226a5ba3 80 r->inttime = micros () - t;
JovanEps 2:03cf226a5ba3 81 }
JovanEps 0:43b96e9650ef 82
JovanEps 2:03cf226a5ba3 83 //****************************************
JovanEps 2:03cf226a5ba3 84 void math_sub (struct results *r) {
JovanEps 2:03cf226a5ba3 85 //****************************************
JovanEps 2:03cf226a5ba3 86 uint32_t t, c, l;
JovanEps 2:03cf226a5ba3 87
JovanEps 2:03cf226a5ba3 88 t = micros ();
JovanEps 2:03cf226a5ba3 89 for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
JovanEps 2:03cf226a5ba3 90 {
JovanEps 2:03cf226a5ba3 91 for ( l = 0 ; l < MAX_LOOPS ; l ++ )
JovanEps 2:03cf226a5ba3 92 {
JovanEps 2:03cf226a5ba3 93 MyDoubles [ l ] = double ( a_d - b_d * double ( l ) );
JovanEps 0:43b96e9650ef 94 }
JovanEps 2:03cf226a5ba3 95 }
JovanEps 2:03cf226a5ba3 96 r->doubletime = micros () - t;
JovanEps 0:43b96e9650ef 97
JovanEps 2:03cf226a5ba3 98 t = micros ();
JovanEps 2:03cf226a5ba3 99 for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
JovanEps 2:03cf226a5ba3 100 {
JovanEps 2:03cf226a5ba3 101 for ( l = 0 ; l < MAX_LOOPS ; l ++ )
JovanEps 2:03cf226a5ba3 102 {
JovanEps 2:03cf226a5ba3 103 MyFloats [ l ] = float ( a_f - b_f * float ( l ) );
JovanEps 2:03cf226a5ba3 104 }
JovanEps 2:03cf226a5ba3 105 }
JovanEps 2:03cf226a5ba3 106 r->floattime = micros () - t;
JovanEps 0:43b96e9650ef 107
JovanEps 2:03cf226a5ba3 108 t = micros ();
JovanEps 2:03cf226a5ba3 109 for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
JovanEps 2:03cf226a5ba3 110 {
JovanEps 2:03cf226a5ba3 111 for ( l = 0 ; l < MAX_LOOPS ; l ++ )
JovanEps 2:03cf226a5ba3 112 {
JovanEps 2:03cf226a5ba3 113 Myints [ l ] = a_i - b_i * l;
JovanEps 2:03cf226a5ba3 114 }
JovanEps 2:03cf226a5ba3 115 }
JovanEps 2:03cf226a5ba3 116 r->inttime = micros () - t;
JovanEps 0:43b96e9650ef 117 }
JovanEps 0:43b96e9650ef 118
JovanEps 2:03cf226a5ba3 119 //****************************************
JovanEps 2:03cf226a5ba3 120 void math_mul (struct results *r) {
JovanEps 2:03cf226a5ba3 121 //****************************************
JovanEps 2:03cf226a5ba3 122 uint32_t t, c, l;
JovanEps 2:03cf226a5ba3 123
JovanEps 2:03cf226a5ba3 124 t = micros ();
JovanEps 2:03cf226a5ba3 125 for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
JovanEps 2:03cf226a5ba3 126 {
JovanEps 2:03cf226a5ba3 127 for ( l = 0 ; l < MAX_LOOPS ; l ++ )
JovanEps 2:03cf226a5ba3 128 {
JovanEps 2:03cf226a5ba3 129 MyDoubles [ l ] = double ( a_d * b_d * double ( l ) );
JovanEps 2:03cf226a5ba3 130 }
JovanEps 2:03cf226a5ba3 131 }
JovanEps 2:03cf226a5ba3 132 r->doubletime = micros () - t;
JovanEps 0:43b96e9650ef 133
JovanEps 2:03cf226a5ba3 134 t = micros ();
JovanEps 2:03cf226a5ba3 135 for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
JovanEps 2:03cf226a5ba3 136 {
JovanEps 2:03cf226a5ba3 137 for ( l = 0 ; l < MAX_LOOPS ; l ++ )
JovanEps 2:03cf226a5ba3 138 {
JovanEps 2:03cf226a5ba3 139 MyFloats [ l ] = float ( a_f * b_f * float ( l ) );
JovanEps 2:03cf226a5ba3 140 }
JovanEps 2:03cf226a5ba3 141 }
JovanEps 2:03cf226a5ba3 142 r->floattime = micros () - t;
JovanEps 0:43b96e9650ef 143
JovanEps 2:03cf226a5ba3 144 t = micros ();
JovanEps 2:03cf226a5ba3 145 for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
JovanEps 2:03cf226a5ba3 146 {
JovanEps 2:03cf226a5ba3 147 for ( l = 0 ; l < MAX_LOOPS ; l ++ )
JovanEps 2:03cf226a5ba3 148 {
JovanEps 2:03cf226a5ba3 149 Myints [ l ] = a_i * b_i * l;
JovanEps 2:03cf226a5ba3 150 }
JovanEps 2:03cf226a5ba3 151 }
JovanEps 2:03cf226a5ba3 152 r->inttime = micros () - t;
JovanEps 0:43b96e9650ef 153 }
JovanEps 0:43b96e9650ef 154
JovanEps 2:03cf226a5ba3 155 //****************************************
JovanEps 2:03cf226a5ba3 156 void math_div (struct results *r) {
JovanEps 2:03cf226a5ba3 157 //****************************************
JovanEps 2:03cf226a5ba3 158 uint32_t t, c, l;
JovanEps 2:03cf226a5ba3 159
JovanEps 2:03cf226a5ba3 160 t = micros ();
JovanEps 2:03cf226a5ba3 161 for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
JovanEps 2:03cf226a5ba3 162 {
JovanEps 2:03cf226a5ba3 163 for ( l = 0 ; l < MAX_LOOPS ; l ++ )
JovanEps 2:03cf226a5ba3 164 {
JovanEps 2:03cf226a5ba3 165 MyDoubles [ l ] = double ( a_d / b_d * double ( l ) );
JovanEps 2:03cf226a5ba3 166 }
JovanEps 2:03cf226a5ba3 167 }
JovanEps 2:03cf226a5ba3 168 r->doubletime = micros () - t;
JovanEps 2:03cf226a5ba3 169
JovanEps 2:03cf226a5ba3 170 t = micros ();
JovanEps 2:03cf226a5ba3 171 for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
JovanEps 2:03cf226a5ba3 172 {
JovanEps 2:03cf226a5ba3 173 for ( l = 0 ; l < MAX_LOOPS ; l ++ )
JovanEps 2:03cf226a5ba3 174 {
JovanEps 2:03cf226a5ba3 175 MyFloats [ l ] = float ( a_f / b_f * float ( l ) );
JovanEps 2:03cf226a5ba3 176 }
JovanEps 2:03cf226a5ba3 177 }
JovanEps 2:03cf226a5ba3 178 r->floattime = micros () - t;
JovanEps 2:03cf226a5ba3 179
JovanEps 2:03cf226a5ba3 180 t = micros ();
JovanEps 2:03cf226a5ba3 181 for ( c = 0 ; c < REDO_COMPUTATIONS ; c ++ )
JovanEps 2:03cf226a5ba3 182 {
JovanEps 2:03cf226a5ba3 183 for ( l = 0 ; l < MAX_LOOPS ; l ++ )
JovanEps 2:03cf226a5ba3 184 {
JovanEps 2:03cf226a5ba3 185 Myints [ l ] = a_i / b_i * l;
JovanEps 2:03cf226a5ba3 186 }
JovanEps 2:03cf226a5ba3 187 }
JovanEps 2:03cf226a5ba3 188 r->inttime = micros () - t;
JovanEps 0:43b96e9650ef 189 }
JovanEps 0:43b96e9650ef 190
JovanEps 2:03cf226a5ba3 191 //****************************************
JovanEps 2:03cf226a5ba3 192 void bench_loop() {
JovanEps 2:03cf226a5ba3 193 //****************************************
JovanEps 2:03cf226a5ba3 194
JovanEps 2:03cf226a5ba3 195 struct results add_ops, sub_ops, mul_ops, div_ops;
JovanEps 2:03cf226a5ba3 196
JovanEps 2:03cf226a5ba3 197 math_add(&add_ops);
JovanEps 2:03cf226a5ba3 198 math_sub(&sub_ops);
JovanEps 2:03cf226a5ba3 199 math_mul(&mul_ops);
JovanEps 2:03cf226a5ba3 200 math_div(&div_ops);
JovanEps 0:43b96e9650ef 201
JovanEps 2:03cf226a5ba3 202 pc.printf("\n\n");
JovanEps 2:03cf226a5ba3 203 pc.printf("\n FUNCTION DOUBLE SINGLE INT");
JovanEps 2:03cf226a5ba3 204
JovanEps 2:03cf226a5ba3 205 pc.printf("\n Time - ADD (us/512) :\t ");
JovanEps 2:03cf226a5ba3 206 pc.printf( "%0.1f", ( float ) add_ops.doubletime / REDO_COMPUTATIONS );
JovanEps 2:03cf226a5ba3 207 pc.printf("\t\t");
JovanEps 2:03cf226a5ba3 208 pc.printf( "%0.1f", ( float ) add_ops.floattime / REDO_COMPUTATIONS );
JovanEps 2:03cf226a5ba3 209 pc.printf("\t\t");
JovanEps 2:03cf226a5ba3 210 pc.printf("%0.1f", ( float ) add_ops.inttime / REDO_COMPUTATIONS );
JovanEps 2:03cf226a5ba3 211 pc.printf("\n");
JovanEps 2:03cf226a5ba3 212
JovanEps 2:03cf226a5ba3 213 pc.printf("\n Time - SUB (us/512) :\t ");
JovanEps 2:03cf226a5ba3 214 pc.printf( "%0.1f", ( float ) sub_ops.doubletime / REDO_COMPUTATIONS );
JovanEps 2:03cf226a5ba3 215 pc.printf("\t\t");
JovanEps 2:03cf226a5ba3 216 pc.printf( "%0.1f", ( float ) sub_ops.floattime / REDO_COMPUTATIONS );
JovanEps 2:03cf226a5ba3 217 pc.printf("\t\t");
JovanEps 2:03cf226a5ba3 218 pc.printf( "%0.1f", ( float ) sub_ops.inttime / REDO_COMPUTATIONS );
JovanEps 2:03cf226a5ba3 219 pc.printf("\n");
JovanEps 0:43b96e9650ef 220
JovanEps 2:03cf226a5ba3 221 pc.printf("\n Time - MUL (us/512) :\t ");
JovanEps 2:03cf226a5ba3 222 pc.printf( "%0.1f", ( float ) mul_ops.doubletime / REDO_COMPUTATIONS );
JovanEps 2:03cf226a5ba3 223 pc.printf("\t\t");
JovanEps 2:03cf226a5ba3 224 pc.printf( "%0.1f", ( float ) mul_ops.floattime / REDO_COMPUTATIONS );
JovanEps 2:03cf226a5ba3 225 pc.printf("\t\t");
JovanEps 2:03cf226a5ba3 226 pc.printf( "%0.1f", ( float ) mul_ops.inttime / REDO_COMPUTATIONS );
JovanEps 2:03cf226a5ba3 227 pc.printf("\n");
JovanEps 2:03cf226a5ba3 228
JovanEps 2:03cf226a5ba3 229 pc.printf("\n Time - DIV (us/512) :\t ");
JovanEps 2:03cf226a5ba3 230 pc.printf( "%0.1f", ( float ) div_ops.doubletime / REDO_COMPUTATIONS );
JovanEps 2:03cf226a5ba3 231 pc.printf("\t\t");
JovanEps 2:03cf226a5ba3 232 pc.printf( "%0.1f", ( float ) div_ops.floattime / REDO_COMPUTATIONS );
JovanEps 2:03cf226a5ba3 233 pc.printf("\t\t");
JovanEps 2:03cf226a5ba3 234 pc.printf( "%0.1f", ( float ) div_ops.inttime / REDO_COMPUTATIONS );
JovanEps 2:03cf226a5ba3 235 pc.printf("\n");
JovanEps 2:03cf226a5ba3 236
JovanEps 2:03cf226a5ba3 237 wait(1);
JovanEps 0:43b96e9650ef 238 }
JovanEps 0:43b96e9650ef 239
JovanEps 1:be78b18b8347 240 //*********************************
JovanEps 1:be78b18b8347 241 //** MAIN block **
JovanEps 1:be78b18b8347 242 //*********************************
JovanEps 1:be78b18b8347 243 int main()
JovanEps 1:be78b18b8347 244 {
JovanEps 2:03cf226a5ba3 245 pc.baud(57600);
JovanEps 0:43b96e9650ef 246
JovanEps 2:03cf226a5ba3 247 pc.printf("\n My Benchamrk ...");
JovanEps 2:03cf226a5ba3 248 pc.printf("Beginningbenchmark at ");
JovanEps 2:03cf226a5ba3 249 pc.printf("default 216 MHz ...\n");
JovanEps 2:03cf226a5ba3 250 pc.printf("\n\n");
JovanEps 2:03cf226a5ba3 251
JovanEps 1:be78b18b8347 252 while(1)
JovanEps 1:be78b18b8347 253 {
JovanEps 2:03cf226a5ba3 254 myled=1;
JovanEps 2:03cf226a5ba3 255 timer.start();
JovanEps 2:03cf226a5ba3 256
JovanEps 2:03cf226a5ba3 257 bench_loop(); //Call of banch method
JovanEps 2:03cf226a5ba3 258
JovanEps 2:03cf226a5ba3 259 pc.printf(" kraj \n");
JovanEps 2:03cf226a5ba3 260 myled=0;
JovanEps 2:03cf226a5ba3 261 timer.stop();
JovanEps 2:03cf226a5ba3 262
JovanEps 1:be78b18b8347 263 }
JovanEps 0:43b96e9650ef 264 }