Skip to content

Commit d5d5402

Browse files
committed
benchmark fix cuz compiler is a liar
1 parent cf51d61 commit d5d5402

1 file changed

Lines changed: 13 additions & 16 deletions

File tree

pybricks/experimental/pb_module_experimental.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,31 +77,28 @@ static mp_obj_t experimental_atan2(mp_obj_t y_in, mp_obj_t x_in) {
7777
}
7878
static MP_DEFINE_CONST_FUN_OBJ_2(experimental_atan2_obj, experimental_atan2);
7979

80-
static mp_obj_t experimental_benchmark_hardware(void) {
80+
static mp_obj_t experimental_benchmark_hardware(mp_obj_t seed_in) {
81+
float seed = mp_obj_get_float(seed_in);
82+
8183
DEMCR |= 0x01000000; DWT_CONTROL |= 1;
82-
83-
float test_val = 1.1f;
84-
uint32_t start, cyc_sin, cyc_atan;
84+
uint32_t start, cyc_sin;
8585
volatile float res;
8686

87-
// Measure Sin
87+
// We use a small loop to 'prime' the pipeline
8888
DWT_CYCCNT = 0;
8989
start = DWT_CYCCNT;
90-
res = fast_sin_internal(test_val);
91-
__asm volatile ("dsb"); // Barrier: Force math completion
90+
91+
// Changing the input slightly inside C to ensure the FPU actually works
92+
res = fast_sin_internal(seed);
93+
res = fast_sin_internal(res + 0.01f); // Chain them so the CPU must wait for the first to finish
94+
95+
__asm volatile ("dsb");
9296
cyc_sin = DWT_CYCCNT - start;
9397

94-
// Measure Atan2
95-
DWT_CYCCNT = 0;
96-
start = DWT_CYCCNT;
97-
res = fast_atan2_internal(test_val, 0.5f);
98-
__asm volatile ("dsb"); // Barrier: Force math completion
99-
cyc_atan = DWT_CYCCNT - start;
100-
10198
(void)res;
10299

103-
mp_obj_t tuple[2] = { mp_obj_new_int(cyc_sin), mp_obj_new_int(cyc_atan) };
104-
return mp_obj_new_tuple(2, tuple);
100+
// We divide by 2 because we ran the function twice to chain the results
101+
return mp_obj_new_int(cyc_sin / 2);
105102
}
106103
static MP_DEFINE_CONST_FUN_OBJ_0(experimental_benchmark_hardware_obj, experimental_benchmark_hardware);
107104

0 commit comments

Comments
 (0)