Skip to content

Commit cf51d61

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

1 file changed

Lines changed: 12 additions & 27 deletions

File tree

pybricks/experimental/pb_module_experimental.c

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static const float HALF_PI_F = 1.570796326794896f;
2121
static const float INV_TWO_PI_F = 0.159154943091895f;
2222

2323
// -----------------------------------------------------------------------------
24-
// Core Math Engines (Optimized for Auto-VMLA generation)
24+
// Core Math Engines (High-Precision VMLA)
2525
// -----------------------------------------------------------------------------
2626

2727
static inline float fast_sin_internal(float theta) {
@@ -32,8 +32,6 @@ static inline float fast_sin_internal(float theta) {
3232
else if (x < -HALF_PI_F) { x = -PI_F - x; }
3333

3434
float x2 = x * x;
35-
36-
// GCC Pattern for VMLA (Multiply-Accumulate)
3735
float res = -0.000195152f;
3836
res = 0.008332152f + (x2 * res);
3937
res = -0.166666567f + (x2 * res);
@@ -44,18 +42,14 @@ static inline float fast_sin_internal(float theta) {
4442

4543
static inline float fast_atan2_internal(float y, float x) {
4644
if (x == 0.0f && y == 0.0f) return 0.0f;
47-
4845
float abs_y = fabsf(y) + 1e-10f;
4946
float abs_x = fabsf(x);
5047
float angle;
51-
5248
if (abs_x >= abs_y) {
5349
float r = y / x;
5450
float den = 1.0f + (r * r * 0.28086f);
5551
angle = r * (1.0f / den);
56-
if (x < 0.0f) {
57-
angle += (y >= 0.0f) ? PI_F : -PI_F;
58-
}
52+
if (x < 0.0f) { angle += (y >= 0.0f) ? PI_F : -PI_F; }
5953
} else {
6054
float r = x / y;
6155
float den = 1.0f + (r * r * 0.28086f);
@@ -65,7 +59,7 @@ static inline float fast_atan2_internal(float y, float x) {
6559
}
6660

6761
// -----------------------------------------------------------------------------
68-
// MicroPython Wrappers
62+
// Wrappers & Hardware Benchmarks
6963
// -----------------------------------------------------------------------------
7064

7165
static mp_obj_t experimental_sin(mp_obj_t theta_in) {
@@ -83,43 +77,34 @@ static mp_obj_t experimental_atan2(mp_obj_t y_in, mp_obj_t x_in) {
8377
}
8478
static MP_DEFINE_CONST_FUN_OBJ_2(experimental_atan2_obj, experimental_atan2);
8579

86-
// -----------------------------------------------------------------------------
87-
// The "Truth" Hardware Benchmark
88-
// -----------------------------------------------------------------------------
89-
9080
static mp_obj_t experimental_benchmark_hardware(void) {
91-
DEMCR |= 0x01000000; DWT_CYCCNT = 0; DWT_CONTROL |= 1;
81+
DEMCR |= 0x01000000; DWT_CONTROL |= 1;
9282

9383
float test_val = 1.1f;
94-
uint32_t start, cyc_sin, cyc_cos, cyc_atan;
84+
uint32_t start, cyc_sin, cyc_atan;
9585
volatile float res;
9686

97-
// Measure Sin with Barrier
87+
// Measure Sin
88+
DWT_CYCCNT = 0;
9889
start = DWT_CYCCNT;
9990
res = fast_sin_internal(test_val);
100-
__asm volatile ("dsb"); // Hardware Barrier: Wait for math to finish
91+
__asm volatile ("dsb"); // Barrier: Force math completion
10192
cyc_sin = DWT_CYCCNT - start;
10293

103-
// Measure Atan2 with Barrier
94+
// Measure Atan2
10495
DWT_CYCCNT = 0;
10596
start = DWT_CYCCNT;
10697
res = fast_atan2_internal(test_val, 0.5f);
107-
__asm volatile ("dsb"); // Hardware Barrier
98+
__asm volatile ("dsb"); // Barrier: Force math completion
10899
cyc_atan = DWT_CYCCNT - start;
109100

110101
(void)res;
111102

112-
return mp_obj_new_tuple(2, (mp_obj_t[]){
113-
mp_obj_new_int(cyc_sin),
114-
mp_obj_new_int(cyc_atan)
115-
});
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);
116105
}
117106
static MP_DEFINE_CONST_FUN_OBJ_0(experimental_benchmark_hardware_obj, experimental_benchmark_hardware);
118107

119-
// -----------------------------------------------------------------------------
120-
// Registry
121-
// -----------------------------------------------------------------------------
122-
123108
static const mp_rom_map_elem_t experimental_globals_table[] = {
124109
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_experimental) },
125110
{ MP_ROM_QSTR(MP_QSTR_sin), MP_ROM_PTR(&experimental_sin_obj) },

0 commit comments

Comments
 (0)