Skip to content

Commit 4789cad

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

1 file changed

Lines changed: 6 additions & 8 deletions

File tree

pybricks/experimental/pb_module_experimental.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,30 +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+
// Benchmark with seed to prevent constant folding
8081
static mp_obj_t experimental_benchmark_hardware(mp_obj_t seed_in) {
8182
float seed = mp_obj_get_float(seed_in);
82-
8383
DEMCR |= 0x01000000; DWT_CONTROL |= 1;
84+
8485
uint32_t start, cyc_sin;
8586
volatile float res;
8687

87-
// We use a small loop to 'prime' the pipeline
8888
DWT_CYCCNT = 0;
8989
start = DWT_CYCCNT;
90-
91-
// Changing the input slightly inside C to ensure the FPU actually works
90+
// Chain operations to force the FPU to wait for the previous result
9291
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-
92+
res = fast_sin_internal(res + 0.01f);
9593
__asm volatile ("dsb");
9694
cyc_sin = DWT_CYCCNT - start;
9795

9896
(void)res;
9997

100-
// We divide by 2 because we ran the function twice to chain the results
98+
// Return avg cycles for one operation
10199
return mp_obj_new_int(cyc_sin / 2);
102100
}
103-
static MP_DEFINE_CONST_FUN_OBJ_0(experimental_benchmark_hardware_obj, experimental_benchmark_hardware);
101+
static MP_DEFINE_CONST_FUN_OBJ_1(experimental_benchmark_hardware_obj, experimental_benchmark_hardware);
104102

105103
static const mp_rom_map_elem_t experimental_globals_table[] = {
106104
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_experimental) },

0 commit comments

Comments
 (0)