Force noinline for this function
| 28 | |
| 29 | // Force noinline for this function |
| 30 | __attribute__((noinline)) |
| 31 | void benchmark_sincos32_simd(int calls) { |
| 32 | // Pre-generate test angles (16 different angle sets to prevent pattern optimization) |
| 33 | FL_ALIGNAS(16) u32 angle_sets[16][4]; |
| 34 | for (int set = 0; set < 16; set++) { |
| 35 | angle_sets[set][0] = set * 1048576; // 0, 22.5, 45, ... degrees |
| 36 | angle_sets[set][1] = set * 1048576 + 4194304; // +90 degrees |
| 37 | angle_sets[set][2] = set * 1048576 + 8388608; // +180 degrees |
| 38 | angle_sets[set][3] = set * 1048576 + 12582912; // +270 degrees |
| 39 | } |
| 40 | |
| 41 | i32 local_accumulator = 0; |
| 42 | |
| 43 | for (int i = 0; i < calls; i++) { |
| 44 | // Vary angles using volatile offset to defeat constant propagation |
| 45 | int set_idx = (i + g_angle_offset) & 15; // Cycle through 16 sets |
| 46 | |
| 47 | // Force load from memory (prevent compiler from caching angles) |
| 48 | asm volatile("" : : : "memory"); |
| 49 | |
| 50 | simd::simd_u32x4 angles = simd::load_u32_4(angle_sets[set_idx]); |
| 51 | |
| 52 | // Force call through volatile function pointer to prevent inlining |
| 53 | SinCos32_simd result; |
| 54 | asm volatile("" : "=m"(angles) : "m"(angles)); |
| 55 | result = g_sincos_func(angles); // Cannot be inlined |
| 56 | asm volatile("" : "=m"(result) : "m"(result)); |
| 57 | |
| 58 | // Extract scalar values to force computation |
| 59 | FL_ALIGNAS(16) u32 sin_vals[4]; |
| 60 | FL_ALIGNAS(16) u32 cos_vals[4]; |
| 61 | simd::store_u32_4(sin_vals, result.sin_vals); |
| 62 | simd::store_u32_4(cos_vals, result.cos_vals); |
| 63 | |
| 64 | // Accumulate results to prevent dead code elimination |
| 65 | // Use XOR to prevent overflow and keep operation cheap |
| 66 | local_accumulator ^= static_cast<i32>(sin_vals[0]); |
| 67 | local_accumulator ^= static_cast<i32>(cos_vals[0]); |
| 68 | |
| 69 | // Memory barrier to prevent hoisting/sinking |
| 70 | asm volatile("" : "+r"(local_accumulator) : : "memory"); |
| 71 | } |
| 72 | |
| 73 | // Write to volatile global to ensure compiler doesn't optimize away the entire loop |
| 74 | g_accumulator = local_accumulator; |
| 75 | } |
| 76 | |
| 77 | int main(int argc, char *argv[]) { |
| 78 | bool json_output = (argc > 1 && fl::strcmp(argv[1], "baseline") == 0); |