Skip to content

Commit 4532491

Browse files
committed
benchmark fix cuz compiler is a liar
1 parent 0ceeae6 commit 4532491

1 file changed

Lines changed: 13 additions & 19 deletions

File tree

pybricks/experimental/pb_module_experimental.c

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,37 +88,31 @@ static MP_DEFINE_CONST_FUN_OBJ_2(experimental_atan2_obj, experimental_atan2);
8888
// -----------------------------------------------------------------------------
8989

9090
static mp_obj_t experimental_benchmark_hardware(void) {
91-
// Enable DWT Cycle Counter
92-
DEMCR |= 0x01000000;
93-
DWT_CYCCNT = 0;
94-
DWT_CONTROL |= 1;
91+
DEMCR |= 0x01000000; DWT_CYCCNT = 0; DWT_CONTROL |= 1;
9592

9693
float test_val = 1.1f;
9794
uint32_t start, cyc_sin, cyc_cos, cyc_atan;
95+
volatile float res;
9896

99-
// Measure Sin
97+
// Measure Sin with Barrier
10098
start = DWT_CYCCNT;
101-
volatile float s = fast_sin_internal(test_val);
99+
res = fast_sin_internal(test_val);
100+
__asm volatile ("dsb"); // Hardware Barrier: Wait for math to finish
102101
cyc_sin = DWT_CYCCNT - start;
103102

104-
// Measure Cos
103+
// Measure Atan2 with Barrier
104+
DWT_CYCCNT = 0;
105105
start = DWT_CYCCNT;
106-
volatile float c = fast_sin_internal(test_val + HALF_PI_F);
107-
cyc_cos = DWT_CYCCNT - start;
108-
109-
// Measure Atan2
110-
start = DWT_CYCCNT;
111-
volatile float a = fast_atan2_internal(test_val, 0.5f);
106+
res = fast_atan2_internal(test_val, 0.5f);
107+
__asm volatile ("dsb"); // Hardware Barrier
112108
cyc_atan = DWT_CYCCNT - start;
113109

114-
(void)s; (void)c; (void)a; // Prevent optimization cleanup
110+
(void)res;
115111

116-
mp_obj_t tuple[3] = {
117-
mp_obj_new_int(cyc_sin),
118-
mp_obj_new_int(cyc_cos),
112+
return mp_obj_new_tuple(2, (mp_obj_t[]){
113+
mp_obj_new_int(cyc_sin),
119114
mp_obj_new_int(cyc_atan)
120-
};
121-
return mp_obj_new_tuple(3, tuple);
115+
});
122116
}
123117
static MP_DEFINE_CONST_FUN_OBJ_0(experimental_benchmark_hardware_obj, experimental_benchmark_hardware);
124118

0 commit comments

Comments
 (0)